13 int *i=INTEGER(R_do_slot(M,Rf_install(
"i")));
14 int *j=INTEGER(R_do_slot(M,Rf_install(
"j")));
15 double *x=REAL(R_do_slot(M,Rf_install(
"x")));
16 int n=LENGTH(R_do_slot(M,Rf_install(
"x")));
17 int *dim=INTEGER(R_do_slot(M,Rf_install(
"Dim")));
18 typedef Eigen::Triplet<Type> T;
19 std::vector<T> tripletList;
21 tripletList.push_back(T(i[k],j[k],x[k]));
23 Eigen::SparseMatrix<Type> mat(dim[0],dim[1]);
24 mat.setFromTriplets(tripletList.begin(), tripletList.end());
40 return (x == Type(0)) && (! CppAD::Variable(x));
54 typedef Eigen::Triplet<Type> T;
55 std::vector<T> tripletList;
56 for(
int i=0;i<x.rows();i++)
57 for(
int j=0;j<x.cols();j++)
59 tripletList.push_back(T(i,j,x(i,j)));
60 Eigen::SparseMatrix<Type> mat(x.rows(),x.cols());
61 mat.setFromTriplets(tripletList.begin(), tripletList.end());
77 typedef Eigen::Triplet<Type> T;
78 std::vector<T> tripletList;
79 Eigen::SparseVector<Type> mat(x.rows());
80 for(
int i=0;i<x.rows();i++)
88 Eigen::SparseMatrix<Type>
kronecker(Eigen::SparseMatrix<Type> x,
89 Eigen::SparseMatrix<Type> y){
90 typedef Eigen::Triplet<Type> T;
91 typedef typename Eigen::SparseMatrix<Type>::InnerIterator Iterator;
92 std::vector<T> tripletList;
93 int n1=x.rows(),n2=x.cols(),n3=y.rows(),n4=y.cols();
96 for (
int cx=0; cx<x.outerSize(); cx++)
97 for (Iterator itx(x,cx); itx; ++itx)
99 for (
int cy=0; cy<y.outerSize(); cy++)
100 for (Iterator ity(y,cy); ity; ++ity)
106 tripletList.push_back(T(i*n3+k,j*n4+l, itx.value()*ity.value() ));
108 Eigen::SparseMatrix<Type> mat(n1*n3,n2*n4);
109 mat.setFromTriplets(tripletList.begin(), tripletList.end());
114 template <
class Type>
122 Eigen::SparseMatrix<Type> AxA=
kronecker(A,A);
123 Eigen::SparseMatrix<Type> IxI=
kronecker(I,I);
125 Eigen::SparseMatrix<Type> C=IxI-AxA;
131 Eigen::SimplicialLDLT< Eigen::SparseMatrix<Type> > ldlt(C.transpose()*C);
134 for(
int i=0;i<z.size();i++)V(i)=z(i);
139 template <
class Type>
143 Eigen::SimplicialLDLT< Eigen::SparseMatrix<Type> > ldlt(A);
Eigen::SparseVector< Type > asSparseVector(vector< Type > x)
Create sparse vector from dense vector.
matrix< Type > discrLyap(matrix< Type > A_)
bool isStructuralZero(Type x)
Test if a scalar is a structural zero.
Eigen::SparseMatrix< Type > asSparseMatrix(SEXP M)
Matrix class used by TMB.
matrix< Type > invertSparseMatrix(Eigen::SparseMatrix< Type > A)
Vector class used by TMB.
Matrix< scalartype, n1 *n3, n2 *n4 > kronecker(Matrix< scalartype, n1, n2 > x, Matrix< scalartype, n3, n4 > y)
Kronecker product of two matrices.
Utility functions for TMB (automatically included)