19 Eigen::SparseMatrix<Type>::StorageIndex StorageIndex;
21 StorageIndex nrow, ncol, nnz;
23 std::vector<StorageIndex> i;
24 std::vector<StorageIndex> p;
25 SparseATx(
const Eigen::SparseMatrix<Type> &x) :
29 i(x.innerIndexPtr(), x.innerIndexPtr() + nnz),
30 p(x.outerIndexPtr(), x.outerIndexPtr() + ncol + 1) {}
31 StorageIndex rows()
const {
return nrow; }
32 StorageIndex cols()
const {
return ncol; }
35 void f(
const T *A,
const T *x, T *y) {
36 for (StorageIndex j = 0; j < ncol; j++) {
38 for (StorageIndex k = p[j]; k < p[j+1]; k++) {
39 y[j] += A[k] * x[i[k]];
45 void df(
const T *A,
const T *x,
const T *y,
46 T *dA, T *dx,
const T *dy) {
47 for (StorageIndex j = 0; j < ncol; j++) {
48 for (StorageIndex k = p[j]; k < p[j+1]; k++) {
49 dA[k] += x[i[k]] * dy[j];
50 dx[i[k]] += A[k] * dy[j];
57 template<
class Type,
bool transpose=false>
60 Eigen::SparseMatrix<Type>::StorageIndex StorageIndex;
61 std::shared_ptr<SparseATx<Type> > P;
63 SpAxOp(
const Eigen::SparseMatrix<Type> &x) :
64 P(std::make_shared<SparseATx<Type> >( x )) { }
65 static const bool have_input_size_output_size =
true;
66 TMBad::Index input_size()
const {
69 TMBad::Index output_size()
const {
76 const T* A = args.
x_ptr(0);
77 const T* x = args.
x_ptr(1);
79 (*P).template f(A, x, y);
83 const T* A = args.
x_ptr(0);
84 const T* x = args.
x_ptr(1);
85 const T* y = args.
y_ptr(0);
88 const T* dy = args.
dy_ptr(0);
89 (*P).template
df(A, x, y, dA, dx, dy);
93 void dependencies(
TMBad::Args<> &args, TMBad::Dependencies &dep)
const {
94 dep.add_segment(args.
input(0), (*P).nnz );
95 dep.add_segment(args.
input(1), (*P).rows() );
97 static const bool have_dependencies =
true;
99 static const bool implicit_dependencies =
true;
101 static const bool allow_remap =
false;
105 const char* op_name() {
return "SpAxOp"; }
121 config() : normalize(false), trace(true), warn(true), Nmax(100) {}
150 A_values = vec(A.valuePtr(), A.nonZeros());
154 N(N), A_values(A.valuePtr(), A.nonZeros()), multiply(A), cfg(cfg)
159 std::vector<TMBad::ad_segment> args = {A_values, x, vec(N, 1)};
160 if (! F.initialized() ) {
164 bool operator() (
const std::vector<TMBad::Scalar*> &x) {
165 using TMBad::operator<<;
166 TMBad::Scalar N = x[2][0];
167 if ( (
int) N == cfg.
Nmax) {
169 Rf_warning(
"expm: N terms reduced to Nmax (%i)", (
int) cfg.
Nmax);
171 bool change = (Nold != N);
172 if (cfg.
trace && change) {
173 Rcout <<
"Retaping:" <<
" Nold=" << Nold <<
" Nnew=" << N <<
"\n";
179 Test N_changed = {cfg, N.Value()};
192 int N = (int) N_[0].Value();
195 for (
int n=1; n<N; n++) {
196 term = multiply(A, term) / n;
207 Eigen::SparseMatrix<double> A;
209 void update(Eigen::SparseMatrix<double> &A) {
215 if ((
int) N > cfg.
Nmax) {
217 Rf_warning(
"expm: N terms reduced to Nmax (%i)", (
int) cfg.
Nmax);
221 vec operator()(vec x) {
224 for (
int n=1; n<N; n++) {
250 return ceil(rho + 4*sqrt(rho) + 5);
255 Eigen::SparseMatrix<T> A(Q);
259 for (
int i=1; i<d.size(); i++) {
264 A.diagonal().array() += rho;
271 if (ExpA.
cfg.normalize) {
bool trace
Trace retaping?
Vector class used by TMB.
Matrix exponential of generator matrix multiplied by vector.
bool warn
Warn if Nmax exceeded?
Type * y_ptr(Index j)
pointer version - use with caution.
Type df(Type x, Type df1, Type df2, int give_log)
Probability density function of the Fisher distribution.
Access input/output values and derivatives during a reverse pass. Write access granted for the input ...
Shared configuration parameters for expm_series and expm_generator
Contiguous set of variables on the current tape.
Type * y_ptr(Index j)
pointer version - use with caution.
int Nmax
Use no more than this number of terms in series.
Access input/output values during a forward pass. Write access granted for the output value only...
Transform a functor to have packed input/output.
Type * x_ptr(Index j)
pointer version - use with caution.
vec operator()(vec x)
Evaluate x^T*exp(A)
Vector of consequtive values on the AD tape.
Type min(const vector< Type > &x)
Type * x_ptr(Index j)
pointer version - use with caution.
bool normalize
Normalize result? (expm_generator only)
Type * dx_ptr(Index j)
pointer version - use with caution.
Sparse matrix exponentiation.
void update(Eigen::SparseMatrix< T > &A)
vec operator()(vec x)
Evaluate x^T*exp(Q)
Operator auto-completion.
Operator that requires dynamic allocation. Compile time known input/output size.
std::vector< T > operator()(const std::vector< T > &xp)
Transformed functor assuming original maps std::vector<ad_segment> to ad_segment
Type sum(Vector< Type > x)
Matrix exponential multiplied by vector.
ADFun ADFun_retaping(Functor &F, const std::vector< ad_aug > &x, Test test=Test())
Construct ADFun that automatically retapes.
Index input(Index j) const
Get variable index of j'th input to current operator.
TMBad::global::Complete< SpAxOp< T > > multiply
Type * dy_ptr(Index j)
pointer version - use with caution.
T getN(T rho)
Uniformization Grassmann (1977) eq. 10. => error < 1e-4 rho=abs(max(diag(Q)))
Container of ADFun object with packed input and output.