Basis.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #pragma once
20 
21 #include <gtsam/base/Matrix.h>
23 
24 #include <iostream>
25 
67 namespace gtsam {
68 
69 using Weights = Eigen::Matrix<double, 1, -1>; /* 1xN vector */
70 
83 Matrix GTSAM_EXPORT kroneckerProductIdentity(size_t M, const Weights& w);
84 
89 template <typename DERIVED>
90 class Basis {
91  public:
97  static Matrix WeightMatrix(size_t N, const Vector& X) {
98  Matrix W(X.size(), N);
99  for (int i = 0; i < X.size(); i++)
100  W.row(i) = DERIVED::CalculateWeights(N, X(i));
101  return W;
102  }
103 
113  static Matrix WeightMatrix(size_t N, const Vector& X, double a, double b) {
114  Matrix W(X.size(), N);
115  for (int i = 0; i < X.size(); i++)
116  W.row(i) = DERIVED::CalculateWeights(N, X(i), a, b);
117  return W;
118  }
119 
128  protected:
130 
131  public:
134 
136  EvaluationFunctor(size_t N, double x)
137  : weights_(DERIVED::CalculateWeights(N, x)) {}
138 
140  EvaluationFunctor(size_t N, double x, double a, double b)
141  : weights_(DERIVED::CalculateWeights(N, x, a, b)) {}
142 
144  double apply(const typename DERIVED::Parameters& p,
145  OptionalJacobian<-1, -1> H = {}) const {
146  if (H) *H = weights_;
147  return (weights_ * p)(0);
148  }
149 
151  double operator()(const typename DERIVED::Parameters& p,
152  OptionalJacobian<-1, -1> H = {}) const {
153  return apply(p, H); // might call apply in derived
154  }
155 
156  void print(const std::string& s = "") const {
157  std::cout << s << (s != "" ? " " : "") << weights_ << std::endl;
158  }
159  };
160 
168  protected:
169  using Jacobian = Eigen::Matrix<double, /*MxMN*/ -1, -1>;
171 
172  size_t M_;
173 
183  H_ = kroneckerProductIdentity(M_, this->weights_);
184  }
185 
186  public:
188 
191 
193  VectorEvaluationFunctor(size_t M, size_t N, double x)
194  : EvaluationFunctor(N, x), M_(M) {
196  }
197 
199  VectorEvaluationFunctor(size_t M, size_t N, double x, double a, double b)
200  : EvaluationFunctor(N, x, a, b), M_(M) {
202  }
203 
206  OptionalJacobian</*MxN*/ -1, -1> H = {}) const {
207  if (H) *H = H_;
208  return P.matrix() * this->weights_.transpose();
209  }
210 
213  OptionalJacobian</*MxN*/ -1, -1> H = {}) const {
214  return apply(P, H);
215  }
216  };
217 
226  protected:
227  using Jacobian = Eigen::Matrix<double, /*1xMN*/ 1, -1>;
229 
230  size_t M_;
231  size_t rowIndex_;
232 
233  /*
234  * Calculate the `1*(M*N)` Jacobian of this functor with respect to
235  * the M*N parameter matrix `P`.
236  * We flatten assuming column-major order, e.g., if N=3 and M=2, we have
237  * H=[w(0) 0 w(1) 0 w(2) 0] for rowIndex==0
238  * H=[0 w(0) 0 w(1) 0 w(2)] for rowIndex==1
239  * i.e., one row of the Kronecker product of weights_ with the
240  * MxM identity matrix. See also VectorEvaluationFunctor.
241  */
244  for (int j = 0; j < EvaluationFunctor::weights_.size(); j++)
246  }
247 
248  public:
251 
253  VectorComponentFunctor(size_t M, size_t N, size_t i, double x)
254  : EvaluationFunctor(N, x), M_(M), rowIndex_(i) {
256  }
257 
259  VectorComponentFunctor(size_t M, size_t N, size_t i, double x, double a,
260  double b)
261  : EvaluationFunctor(N, x, a, b), M_(M), rowIndex_(i) {
263  }
264 
266  double apply(const Matrix& P,
268  if (H) *H = H_;
269  return P.row(rowIndex_) * EvaluationFunctor::weights_.transpose();
270  }
271 
273  double operator()(const Matrix& P,
275  return apply(P, H);
276  }
277  };
278 
292  template <class T>
296 
297  public:
300 
302  ManifoldEvaluationFunctor(size_t N, double x) : Base(M, N, x) {}
303 
305  ManifoldEvaluationFunctor(size_t N, double x, double a, double b)
306  : Base(M, N, x, a, b) {}
307 
310  // Interpolate the M-dimensional vector to yield a vector in tangent space
312 
313  // Now call retract with this M-vector, possibly with derivatives
314  Eigen::Matrix<double, M, M> D_result_xi;
315  T result = T::ChartAtOrigin::Retract(xi, H ? &D_result_xi : 0);
316 
317  // Finally, if derivatives are asked, apply chain rule where H is Mx(M*N)
318  // derivative of interpolation and D_result_xi is MxM derivative of
319  // retract.
320  if (H) *H = D_result_xi * (*H);
321 
322  // and return a T
323  return result;
324  }
325 
328  OptionalJacobian</*MxN*/ -1, -1> H = {}) const {
329  return apply(P, H); // might call apply in derived
330  }
331  };
332 
335  protected:
337 
338  public:
341 
342  DerivativeFunctorBase(size_t N, double x)
343  : weights_(DERIVED::DerivativeWeights(N, x)) {}
344 
345  DerivativeFunctorBase(size_t N, double x, double a, double b)
346  : weights_(DERIVED::DerivativeWeights(N, x, a, b)) {}
347 
348  void print(const std::string& s = "") const {
349  std::cout << s << (s != "" ? " " : "") << weights_ << std::endl;
350  }
351  };
352 
361  public:
364 
365  DerivativeFunctor(size_t N, double x) : DerivativeFunctorBase(N, x) {}
366 
367  DerivativeFunctor(size_t N, double x, double a, double b)
368  : DerivativeFunctorBase(N, x, a, b) {}
369 
370  double apply(const typename DERIVED::Parameters& p,
371  OptionalJacobian</*1xN*/ -1, -1> H = {}) const {
372  if (H) *H = this->weights_;
373  return (this->weights_ * p)(0);
374  }
376  double operator()(const typename DERIVED::Parameters& p,
377  OptionalJacobian</*1xN*/ -1, -1> H = {}) const {
378  return apply(p, H); // might call apply in derived
379  }
380  };
381 
391  protected:
392  using Jacobian = Eigen::Matrix<double, /*MxMN*/ -1, -1>;
394 
395  size_t M_;
396 
406  H_ = kroneckerProductIdentity(M_, this->weights_);
407  }
408 
409  public:
411 
414 
416  VectorDerivativeFunctor(size_t M, size_t N, double x)
417  : DerivativeFunctorBase(N, x), M_(M) {
419  }
420 
422  VectorDerivativeFunctor(size_t M, size_t N, double x, double a, double b)
423  : DerivativeFunctorBase(N, x, a, b), M_(M) {
425  }
426 
429  if (H) *H = H_;
430  return P.matrix() * this->weights_.transpose();
431  }
435  return apply(P, H);
436  }
437  };
438 
447  protected:
448  using Jacobian = Eigen::Matrix<double, /*1xMN*/ 1, -1>;
450 
451  size_t M_;
452  size_t rowIndex_;
453 
454  /*
455  * Calculate the `1*(M*N)` Jacobian of this functor with respect to
456  * the M*N parameter matrix `P`.
457  * We flatten assuming column-major order, e.g., if N=3 and M=2, we have
458  * H=[w(0) 0 w(1) 0 w(2) 0] for rowIndex==0
459  * H=[0 w(0) 0 w(1) 0 w(2)] for rowIndex==1
460  * i.e., one row of the Kronecker product of weights_ with the
461  * MxM identity matrix. See also VectorDerivativeFunctor.
462  */
464  H_.setZero(1, M_ * this->weights_.size());
465  for (int j = 0; j < this->weights_.size(); j++)
466  H_(0, rowIndex_ + j * M_) = this->weights_(j);
467  }
468 
469  public:
472 
474  ComponentDerivativeFunctor(size_t M, size_t N, size_t i, double x)
477  }
478 
480  ComponentDerivativeFunctor(size_t M, size_t N, size_t i, double x, double a,
481  double b)
482  : DerivativeFunctorBase(N, x, a, b), M_(M), rowIndex_(i) {
484  }
486  double apply(const Matrix& P,
488  if (H) *H = H_;
489  return P.row(rowIndex_) * this->weights_.transpose();
490  }
492  double operator()(const Matrix& P,
494  return apply(P, H);
495  }
496  };
497 };
498 
499 } // namespace gtsam
gtsam::Basis::ComponentDerivativeFunctor::apply
double apply(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
Calculate derivative of component rowIndex_ of F.
Definition: Basis.h:486
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
gtsam::Basis::DerivativeFunctorBase::print
void print(const std::string &s="") const
Definition: Basis.h:348
gtsam::Basis::DerivativeFunctor::DerivativeFunctor
DerivativeFunctor(size_t N, double x)
Definition: Basis.h:365
gtsam::Basis::WeightMatrix
static Matrix WeightMatrix(size_t N, const Vector &X, double a, double b)
Calculate weights for all x in vector X, with interval [a,b].
Definition: Basis.h:113
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::Basis::VectorEvaluationFunctor::VectorEvaluationFunctor
VectorEvaluationFunctor(size_t M, size_t N, double x, double a, double b)
Constructor, with interval [a,b].
Definition: Basis.h:199
gtsam::Basis::DerivativeFunctorBase
Base class for functors below that calculate derivative weights.
Definition: Basis.h:334
screwPose2::xi
Vector xi
Definition: testPose2.cpp:148
gtsam::Basis::DerivativeFunctorBase::weights_
Weights weights_
Definition: Basis.h:336
Matrix.h
typedef and functions to augment Eigen's MatrixXd
gtsam::Basis::VectorDerivativeFunctor::H_
Jacobian H_
Definition: Basis.h:393
gtsam::Basis::EvaluationFunctor::operator()
double operator()(const typename DERIVED::Parameters &p, OptionalJacobian<-1, -1 > H={}) const
c++ sugar
Definition: Basis.h:151
gtsam::Basis::EvaluationFunctor
Definition: Basis.h:127
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::Basis
Definition: Basis.h:90
gtsam::Basis::VectorDerivativeFunctor::M_
size_t M_
Definition: Basis.h:395
gtsam::Basis::EvaluationFunctor::EvaluationFunctor
EvaluationFunctor(size_t N, double x, double a, double b)
Constructor with interval [a,b].
Definition: Basis.h:140
gtsam::Basis::ManifoldEvaluationFunctor::apply
T apply(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
Manifold evaluation.
Definition: Basis.h:309
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
X
#define X
Definition: icosphere.cpp:20
gtsam::Basis::ComponentDerivativeFunctor::rowIndex_
size_t rowIndex_
Definition: Basis.h:452
gtsam::Basis::VectorComponentFunctor::VectorComponentFunctor
VectorComponentFunctor(size_t M, size_t N, size_t i, double x)
Construct with row index.
Definition: Basis.h:253
gtsam::Basis::VectorEvaluationFunctor::M_
size_t M_
Definition: Basis.h:172
gtsam::Basis::ComponentDerivativeFunctor
Definition: Basis.h:446
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::Basis::DerivativeFunctorBase::DerivativeFunctorBase
DerivativeFunctorBase(size_t N, double x, double a, double b)
Definition: Basis.h:345
gtsam::Basis::DerivativeFunctor
Definition: Basis.h:360
gtsam::Basis::ManifoldEvaluationFunctor::ManifoldEvaluationFunctor
ManifoldEvaluationFunctor(size_t N, double x)
Default Constructor.
Definition: Basis.h:302
gtsam::Basis::VectorDerivativeFunctor::apply
Vector apply(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
Definition: Basis.h:427
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
gtsam::Basis::EvaluationFunctor::EvaluationFunctor
EvaluationFunctor(size_t N, double x)
Constructor with interval [a,b].
Definition: Basis.h:136
gtsam::Basis::VectorComponentFunctor::rowIndex_
size_t rowIndex_
Definition: Basis.h:231
gtsam::Basis::VectorDerivativeFunctor::calculateJacobian
void calculateJacobian()
Definition: Basis.h:405
gtsam::Basis::ComponentDerivativeFunctor::operator()
double operator()(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
c++ sugar
Definition: Basis.h:492
gtsam::Basis::VectorEvaluationFunctor::VectorEvaluationFunctor
VectorEvaluationFunctor(size_t M, size_t N, double x)
Default Constructor.
Definition: Basis.h:193
gtsam::Basis::DerivativeFunctorBase::DerivativeFunctorBase
DerivativeFunctorBase(size_t N, double x)
Definition: Basis.h:342
gtsam::Basis::VectorComponentFunctor
Definition: Basis.h:225
gtsam::kroneckerProductIdentity
Matrix kroneckerProductIdentity(size_t M, const Weights &w)
Function for computing the kronecker product of the 1*N Weight vector w with the MxM identity matrix ...
Definition: Basis.cpp:23
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::Basis::VectorComponentFunctor::apply
double apply(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
Calculate component of component rowIndex_ of P.
Definition: Basis.h:266
gtsam::Basis::WeightMatrix
static Matrix WeightMatrix(size_t N, const Vector &X)
Definition: Basis.h:97
gtsam::Basis::VectorDerivativeFunctor::VectorDerivativeFunctor
VectorDerivativeFunctor(size_t M, size_t N, double x)
Default Constructor.
Definition: Basis.h:416
gtsam::Basis::VectorDerivativeFunctor::operator()
Vector operator()(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
c++ sugar
Definition: Basis.h:433
OptionalJacobian.h
Special class for optional Jacobian arguments.
Eigen::Triplet< double >
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:841
gtsam::Basis::VectorComponentFunctor::VectorComponentFunctor
VectorComponentFunctor()
For serialization.
Definition: Basis.h:250
gtsam::Basis::ManifoldEvaluationFunctor::ManifoldEvaluationFunctor
ManifoldEvaluationFunctor(size_t N, double x, double a, double b)
Constructor, with interval [a,b].
Definition: Basis.h:305
gtsam::Basis::DerivativeFunctor::apply
double apply(const typename DERIVED::Parameters &p, OptionalJacobian< -1, -1 > H={}) const
Definition: Basis.h:370
gtsam::Basis::ComponentDerivativeFunctor::ComponentDerivativeFunctor
ComponentDerivativeFunctor()
For serialization.
Definition: Basis.h:471
gtsam::Basis::VectorDerivativeFunctor::VectorDerivativeFunctor
VectorDerivativeFunctor(size_t M, size_t N, double x, double a, double b)
Constructor, with optional interval [a,b].
Definition: Basis.h:422
gtsam::Basis::VectorDerivativeFunctor::VectorDerivativeFunctor
EIGEN_MAKE_ALIGNED_OPERATOR_NEW VectorDerivativeFunctor()
For serialization.
Definition: Basis.h:413
gtsam::Basis::EvaluationFunctor::apply
double apply(const typename DERIVED::Parameters &p, OptionalJacobian<-1, -1 > H={}) const
Regular 1D evaluation.
Definition: Basis.h:144
gtsam::Basis::VectorComponentFunctor::VectorComponentFunctor
VectorComponentFunctor(size_t M, size_t N, size_t i, double x, double a, double b)
Construct with row index and interval.
Definition: Basis.h:259
gtsam::b
const G & b
Definition: Group.h:79
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam::Basis::ManifoldEvaluationFunctor::operator()
T operator()(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
c++ sugar
Definition: Basis.h:327
gtsam
traits
Definition: SFMdata.h:40
gtsam::traits
Definition: Group.h:36
gtsam::Basis::ManifoldEvaluationFunctor::M
@ M
Definition: Basis.h:294
gtsam::Basis::ComponentDerivativeFunctor::M_
size_t M_
Definition: Basis.h:451
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
gtsam::Basis::VectorEvaluationFunctor::apply
Vector apply(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
M-dimensional evaluation.
Definition: Basis.h:205
gtsam::symbol_shorthand::W
Key W(std::uint64_t j)
Definition: inference/Symbol.h:170
gtsam::Basis::VectorEvaluationFunctor::operator()
Vector operator()(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
c++ sugar
Definition: Basis.h:212
gtsam::Basis::ManifoldEvaluationFunctor::ManifoldEvaluationFunctor
ManifoldEvaluationFunctor()
For serialization.
Definition: Basis.h:299
p
float * p
Definition: Tutorial_Map_using.cpp:9
gtsam::Basis::VectorEvaluationFunctor::H_
Jacobian H_
Definition: Basis.h:170
gtsam::Basis::EvaluationFunctor::print
void print(const std::string &s="") const
Definition: Basis.h:156
gtsam::Basis::VectorDerivativeFunctor
Definition: Basis.h:390
gtsam::Basis::ComponentDerivativeFunctor::calculateJacobian
void calculateJacobian()
Definition: Basis.h:463
P
static double P[]
Definition: ellpe.c:68
gtsam::Basis::VectorEvaluationFunctor::calculateJacobian
void calculateJacobian()
Definition: Basis.h:182
gtsam::Basis::DerivativeFunctor::DerivativeFunctor
DerivativeFunctor(size_t N, double x, double a, double b)
Definition: Basis.h:367
Eigen::Matrix< double, 1, -1 >
gtsam::Basis::VectorComponentFunctor::operator()
double operator()(const Matrix &P, OptionalJacobian< -1, -1 > H={}) const
c++ sugar
Definition: Basis.h:273
gtsam::Basis::VectorComponentFunctor::H_
Jacobian H_
Definition: Basis.h:228
N
#define N
Definition: igam.h:9
Eigen::PlainObjectBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:562
gtsam::Basis::ManifoldEvaluationFunctor
Definition: Basis.h:293
gtsam::Basis::ComponentDerivativeFunctor::H_
Jacobian H_
Definition: Basis.h:449
gtsam::Basis::DerivativeFunctor::DerivativeFunctor
DerivativeFunctor()
For serialization.
Definition: Basis.h:363
gtsam::Basis::EvaluationFunctor::EvaluationFunctor
EvaluationFunctor()
For serialization.
Definition: Basis.h:133
gtsam::Basis::EvaluationFunctor::weights_
Weights weights_
Definition: Basis.h:129
gtsam::Basis::VectorEvaluationFunctor::VectorEvaluationFunctor
EIGEN_MAKE_ALIGNED_OPERATOR_NEW VectorEvaluationFunctor()
For serialization.
Definition: Basis.h:190
gtsam::Basis::DerivativeFunctor::operator()
double operator()(const typename DERIVED::Parameters &p, OptionalJacobian< -1, -1 > H={}) const
c++ sugar
Definition: Basis.h:376
gtsam::Basis::ComponentDerivativeFunctor::ComponentDerivativeFunctor
ComponentDerivativeFunctor(size_t M, size_t N, size_t i, double x, double a, double b)
Construct with row index and interval.
Definition: Basis.h:480
gtsam::Basis::ComponentDerivativeFunctor::ComponentDerivativeFunctor
ComponentDerivativeFunctor(size_t M, size_t N, size_t i, double x)
Construct with row index.
Definition: Basis.h:474
gtsam::Basis::VectorComponentFunctor::calculateJacobian
void calculateJacobian()
Definition: Basis.h:242
gtsam::Basis::VectorComponentFunctor::M_
size_t M_
Definition: Basis.h:230
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::Basis::DerivativeFunctorBase::DerivativeFunctorBase
DerivativeFunctorBase()
For serialization.
Definition: Basis.h:340
gtsam::Basis::VectorEvaluationFunctor
Definition: Basis.h:167
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:54