25#ifndef LIBMV_NUMERIC_NUMERIC_H
26#define LIBMV_NUMERIC_NUMERIC_H
28#include <Eigen/Cholesky>
30#include <Eigen/Eigenvalues>
31#include <Eigen/Geometry>
36#if !defined(__MINGW64__)
37# if defined(_WIN32) || defined(__APPLE__) || defined(__NetBSD__) || \
39inline void sincos(
double x,
double* sinx,
double* cosx) {
46#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
47inline long lround(
double d) {
48 return (
long)(d > 0 ? d + 0.5 :
ceil(d - 0.5));
51inline int round(
double d) {
52 return (d > 0) ?
int(d + 0.5) :
int(d - 0.5);
55typedef unsigned int uint;
60typedef Eigen::MatrixXd
Mat;
61typedef Eigen::VectorXd
Vec;
63typedef Eigen::MatrixXf
Matf;
64typedef Eigen::VectorXf
Vecf;
66typedef Eigen::Matrix<unsigned int, Eigen::Dynamic, Eigen::Dynamic>
Matu;
67typedef Eigen::Matrix<unsigned int, Eigen::Dynamic, 1>
Vecu;
68typedef Eigen::Matrix<unsigned int, 2, 1>
Vec2u;
70typedef Eigen::Matrix<double, 2, 2>
Mat2;
71typedef Eigen::Matrix<double, 2, 3>
Mat23;
72typedef Eigen::Matrix<double, 3, 3>
Mat3;
73typedef Eigen::Matrix<double, 3, 4>
Mat34;
74typedef Eigen::Matrix<double, 3, 5>
Mat35;
75typedef Eigen::Matrix<double, 4, 1>
Mat41;
76typedef Eigen::Matrix<double, 4, 3>
Mat43;
77typedef Eigen::Matrix<double, 4, 4>
Mat4;
78typedef Eigen::Matrix<double, 4, 6>
Mat46;
79typedef Eigen::Matrix<float, 2, 2>
Mat2f;
80typedef Eigen::Matrix<float, 2, 3>
Mat23f;
81typedef Eigen::Matrix<float, 3, 3>
Mat3f;
82typedef Eigen::Matrix<float, 3, 4>
Mat34f;
83typedef Eigen::Matrix<float, 3, 5>
Mat35f;
84typedef Eigen::Matrix<float, 4, 3>
Mat43f;
85typedef Eigen::Matrix<float, 4, 4>
Mat4f;
86typedef Eigen::Matrix<float, 4, 6>
Mat46f;
88typedef Eigen::Matrix<double, 3, 3, Eigen::RowMajor>
RMat3;
89typedef Eigen::Matrix<double, 4, 4, Eigen::RowMajor>
RMat4;
91typedef Eigen::Matrix<double, 2, Eigen::Dynamic>
Mat2X;
92typedef Eigen::Matrix<double, 3, Eigen::Dynamic>
Mat3X;
93typedef Eigen::Matrix<double, 4, Eigen::Dynamic>
Mat4X;
94typedef Eigen::Matrix<double, Eigen::Dynamic, 2>
MatX2;
95typedef Eigen::Matrix<double, Eigen::Dynamic, 3>
MatX3;
96typedef Eigen::Matrix<double, Eigen::Dynamic, 4>
MatX4;
97typedef Eigen::Matrix<double, Eigen::Dynamic, 5>
MatX5;
98typedef Eigen::Matrix<double, Eigen::Dynamic, 6>
MatX6;
99typedef Eigen::Matrix<double, Eigen::Dynamic, 7>
MatX7;
100typedef Eigen::Matrix<double, Eigen::Dynamic, 8>
MatX8;
101typedef Eigen::Matrix<double, Eigen::Dynamic, 9>
MatX9;
102typedef Eigen::Matrix<double, Eigen::Dynamic, 15>
MatX15;
103typedef Eigen::Matrix<double, Eigen::Dynamic, 16>
MatX16;
108typedef Eigen::Matrix<double, 5, 1>
Vec5;
109typedef Eigen::Matrix<double, 6, 1>
Vec6;
110typedef Eigen::Matrix<double, 7, 1>
Vec7;
111typedef Eigen::Matrix<double, 8, 1>
Vec8;
112typedef Eigen::Matrix<double, 9, 1>
Vec9;
113typedef Eigen::Matrix<double, 10, 1>
Vec10;
114typedef Eigen::Matrix<double, 11, 1>
Vec11;
115typedef Eigen::Matrix<double, 12, 1>
Vec12;
116typedef Eigen::Matrix<double, 13, 1>
Vec13;
117typedef Eigen::Matrix<double, 14, 1>
Vec14;
118typedef Eigen::Matrix<double, 15, 1>
Vec15;
119typedef Eigen::Matrix<double, 16, 1>
Vec16;
120typedef Eigen::Matrix<double, 17, 1>
Vec17;
121typedef Eigen::Matrix<double, 18, 1>
Vec18;
122typedef Eigen::Matrix<double, 19, 1>
Vec19;
123typedef Eigen::Matrix<double, 20, 1>
Vec20;
135typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
148template <
typename TMat,
typename TVec>
157template <
typename TMat,
typename TVec>
159 Eigen::JacobiSVD<TMat> svd(*A, Eigen::ComputeFullV);
160 (*nullspace) = svd.matrixV().col(A->cols() - 1);
161 if (A->rows() >= A->cols()) {
162 return svd.singularValues()(A->cols() - 1);
173template <
typename TMat,
typename TVec1,
typename TVec2>
175 Eigen::JacobiSVD<TMat> svd(*A, Eigen::ComputeFullV);
176 *x1 = svd.matrixV().col(A->cols() - 1);
177 *x2 = svd.matrixV().col(A->cols() - 2);
178 if (A->rows() >= A->cols()) {
179 return svd.singularValues()(A->cols() - 1);
188 *A = A->transpose().eval();
191template <
typename TVec>
193 return x.array().abs().sum();
196template <
typename TVec>
201template <
typename TVec>
203 return x.array().abs().maxCoeff();
206template <
typename TVec>
208 return (x - y).array().abs().sum();
211template <
typename TVec>
213 return (x - y).norm();
215template <
typename TVec>
217 return (x - y).array().abs().maxCoeff();
222template <
typename TVec>
231template <
typename TVec>
240template <
typename TVec>
267template <
typename TVec>
269 return x.asDiagonal();
272template <
typename TMat>
274 return sqrt(A.array().abs2().sum());
277template <
typename TMat>
290 Vec* variance_pointer);
296# define SUM_OR_DYNAMIC(x, y) \
297 (x == Eigen::Dynamic || y == Eigen::Dynamic) ? Eigen::Dynamic : (x + y)
299template <
typename Derived1,
typename Derived2>
300struct hstack_return {
301 typedef typename Derived1::Scalar
Scalar;
303 RowsAtCompileTime = Derived1::RowsAtCompileTime,
304 ColsAtCompileTime = SUM_OR_DYNAMIC(Derived1::ColsAtCompileTime,
305 Derived2::ColsAtCompileTime),
306 Options = Derived1::Flags & Eigen::RowMajorBit ? Eigen::RowMajor : 0,
307 MaxRowsAtCompileTime = Derived1::MaxRowsAtCompileTime,
308 MaxColsAtCompileTime = SUM_OR_DYNAMIC(Derived1::MaxColsAtCompileTime,
309 Derived2::MaxColsAtCompileTime)
311 typedef Eigen::Matrix<
Scalar,
315 MaxRowsAtCompileTime,
316 MaxColsAtCompileTime>
320template <
typename Derived1,
typename Derived2>
321typename hstack_return<Derived1, Derived2>::type
HStack(
322 const Eigen::MatrixBase<Derived1>& lhs,
323 const Eigen::MatrixBase<Derived2>&
rhs) {
324 typename hstack_return<Derived1, Derived2>::type res;
325 res.resize(lhs.rows(), lhs.cols() +
rhs.cols());
330template <
typename Derived1,
typename Derived2>
331struct vstack_return {
332 typedef typename Derived1::Scalar
Scalar;
334 RowsAtCompileTime = SUM_OR_DYNAMIC(Derived1::RowsAtCompileTime,
335 Derived2::RowsAtCompileTime),
336 ColsAtCompileTime = Derived1::ColsAtCompileTime,
337 Options = Derived1::Flags & Eigen::RowMajorBit ? Eigen::RowMajor : 0,
338 MaxRowsAtCompileTime = SUM_OR_DYNAMIC(Derived1::MaxRowsAtCompileTime,
339 Derived2::MaxRowsAtCompileTime),
340 MaxColsAtCompileTime = Derived1::MaxColsAtCompileTime
342 typedef Eigen::Matrix<
Scalar,
346 MaxRowsAtCompileTime,
347 MaxColsAtCompileTime>
351template <
typename Derived1,
typename Derived2>
352typename vstack_return<Derived1, Derived2>::type
VStack(
353 const Eigen::MatrixBase<Derived1>& lhs,
354 const Eigen::MatrixBase<Derived2>&
rhs) {
355 typename vstack_return<Derived1, Derived2>::type res;
356 res.resize(lhs.rows() +
rhs.rows(), lhs.cols());
366 ((ColsLeft == Eigen::Dynamic || ColsRight == Eigen::Dynamic) \
368 : (ColsLeft + ColsRight))
372 ((RowsLeft == Eigen::Dynamic && RowsRight == Eigen::Dynamic) \
374 : ((RowsLeft == Eigen::Dynamic) ? RowsRight : RowsLeft))
377template <
typename T,
int RowsLeft,
int RowsRight,
int ColsLeft,
int ColsRight>
379 const Eigen::Matrix<T, RowsLeft, ColsLeft>& left,
380 const Eigen::Matrix<T, RowsRight, ColsRight>& right) {
381 assert(left.rows() == right.rows());
383 int m1 = left.cols();
384 int m2 = right.cols();
386 Eigen::Matrix<T, ROWS, COLS> stacked(n, m1 + m2);
387 stacked.block(0, 0, n, m1) =
left;
388 stacked.block(0, m1, n, m2) = right;
397template <
typename T,
int RowsLeft,
int RowsRight,
int ColsLeft,
int ColsRight>
399 const Eigen::Matrix<T, ColsLeft, RowsLeft>& top,
400 const Eigen::Matrix<T, ColsRight, RowsRight>& bottom) {
401 assert(top.cols() == bottom.cols());
403 int n2 = bottom.rows();
406 Eigen::Matrix<T, COLS, ROWS> stacked(n1 + n2, m);
407 stacked.block(0, 0, n1, m) =
top;
408 stacked.block(n1, 0, n2, m) = bottom;
417template <
typename TTop,
typename TBot,
typename TStacked>
419 assert(top.cols() == bottom.cols());
421 int n2 = bottom.rows();
424 stacked->resize(n1 + n2, m);
425 stacked->block(0, 0, n1, m) =
top;
426 stacked->block(n1, 0, n2, m) = bottom;
433template <
typename TMat,
typename TCols>
435 TMat compressed(A.rows(), columns.size());
436 for (
int i = 0; i < columns.size(); ++i) {
437 compressed.col(i) = A.col(columns[i]);
442template <
typename TMat,
typename TDest>
443void reshape(
const TMat& a,
int rows,
int cols, TDest*
b) {
444 assert(a.rows() * a.cols() == rows * cols);
445 b->resize(rows, cols);
446 for (
int i = 0; i < rows; i++) {
447 for (
int j = 0; j < cols; j++) {
448 (*b)(i, j) = a[cols * i + j];
455 return _isnan(i) > 0;
457 return std::isnan(i);
463template <
typename FloatType>
464FloatType
ceil0(
const FloatType& value) {
465 FloatType result = std::ceil(std::fabs(value));
472 skew << 0, -
x(2),
x(1),
x(2), 0, -
x(0), -
x(1),
x(0), 0;
479 skew << 0, -1,
x(1), 1, 0, -
x(0);
485 double theta = euler_vector.norm();
487 return Mat3::Identity();
489 Vec3 w = euler_vector / theta;
491 return Mat3::Identity() + w_hat * sin(theta) +
492 w_hat * w_hat * (1 -
cos(theta));
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
local_group_size(16, 16) .push_constant(Type b
local_group_size(16, 16) .push_constant(Type rhs
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
ccl_device_inline float3 ceil(const float3 a)
ccl_device_inline float3 cos(float3 v)
T sin(const AngleRadianBase< T > &a)
double DistanceLInfinity(const TVec &x, const TVec &y)
Eigen::Matrix< double, 19, 1 > Vec19
Eigen::Matrix< float, 3, 5 > Mat35f
Eigen::Matrix< double, 11, 1 > Vec11
double Nullspace(TMat *A, TVec *nullspace)
Eigen::Matrix< double, 6, 1 > Vec6
double NormL2(const TVec &x)
Eigen::Matrix< double, 3, 3, Eigen::RowMajor > RMat3
Eigen::Matrix< double, 13, 1 > Vec13
Mat3 CrossProductMatrix(const Vec3 &x)
double DistanceL1(const TVec &x, const TVec &y)
Eigen::Matrix< double, 10, 1 > Vec10
Mat3 RotationFromEulerVector(Vec3 euler_vector)
Returns the rotaiton matrix built from given vector of euler angles.
void TransposeInPlace(TA *A)
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatX2
Eigen::Matrix< double, 4, 4 > Mat4
void VerticalStack(const TTop &top, const TBot &bottom, TStacked *stacked)
Eigen::Matrix< double, 4, 1 > Mat41
Eigen::Matrix< double, Eigen::Dynamic, 3 > MatX3
Eigen::Matrix< double, Eigen::Dynamic, 15 > MatX15
double DistanceL2(const TVec &x, const TVec &y)
Eigen::Matrix< double, 3, 3 > Mat3
void SVD(TMat *, Vec *, Mat *, Mat *)
Eigen::Matrix< double, Eigen::Dynamic, 4 > MatX4
Eigen::Matrix< double, Eigen::Dynamic, 5 > MatX5
double NormalizeL1(TVec *x)
Mat3 RotationAroundX(double angle)
Eigen::Matrix< float, 2, 2 > Mat2f
double NormLInfinity(const TVec &x)
Eigen::Matrix< float, 4, 3 > Mat43f
double FrobeniusDistance(const TMat &A, const TMat &B)
Eigen::Matrix< double, 4, 4, Eigen::RowMajor > RMat4
Eigen::Matrix< double, 14, 1 > Vec14
Eigen::Matrix< double, 4, 6 > Mat46
void MatrixColumn(const Mat &A, int i, Vec2 *v)
void MeanAndVarianceAlongRows(const Mat &A, Vec *mean_pointer, Vec *variance_pointer)
Mat23 SkewMatMinimal(const Vec2 &x)
Eigen::Matrix< T, ROWS, COLS > HStack(const Eigen::Matrix< T, RowsLeft, ColsLeft > &left, const Eigen::Matrix< T, RowsRight, ColsRight > &right)
Eigen::Matrix< double, 7, 1 > Vec7
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > RMatf
Eigen::Matrix< double, Eigen::Dynamic, 9 > MatX9
double NormalizeLInfinity(TVec *x)
Eigen::Matrix< double, Eigen::Dynamic, 8 > MatX8
Eigen::NumTraits< double > EigenDouble
Eigen::Matrix< float, 3, 3 > Mat3f
Mat3 RotationRodrigues(const Vec3 &axis)
Eigen::Matrix< double, 2, 3 > Mat23
Eigen::Matrix< double, 3, 4 > Mat34
Eigen::Matrix< double, Eigen::Dynamic, 7 > MatX7
Eigen::Matrix< double, 8, 1 > Vec8
Eigen::Matrix< unsigned int, Eigen::Dynamic, 1 > Vecu
Mat3 SkewMat(const Vec3 &x)
Returns the skew anti-symmetric matrix of a vector.
Eigen::Matrix< double, 18, 1 > Vec18
Eigen::Matrix< double, Eigen::Dynamic, 16 > MatX16
Eigen::Matrix< double, 5, 1 > Vec5
Eigen::Matrix< double, 15, 1 > Vec15
FloatType ceil0(const FloatType &value)
Eigen::Matrix< double, 4, Eigen::Dynamic > Mat4X
Eigen::Matrix< double, 3, Eigen::Dynamic > Mat3X
Eigen::Matrix< float, 2, 3 > Mat23f
Eigen::Matrix< float, 3, 4 > Mat34f
Eigen::Matrix< double, 3, 5 > Mat35
Eigen::Matrix< unsigned int, Eigen::Dynamic, Eigen::Dynamic > Matu
double FrobeniusNorm(const TMat &A)
Eigen::Matrix< double, 12, 1 > Vec12
Eigen::Matrix< double, 16, 1 > Vec16
Eigen::Matrix< T, COLS, ROWS > VStack(const Eigen::Matrix< T, ColsLeft, RowsLeft > &top, const Eigen::Matrix< T, ColsRight, RowsRight > &bottom)
Eigen::Matrix< double, 20, 1 > Vec20
void HorizontalStack(const Mat &left, const Mat &right, Mat *stacked)
Mat3 RotationAroundZ(double angle)
void reshape(const TMat &a, int rows, int cols, TDest *b)
Eigen::Matrix< double, 9, 1 > Vec9
Eigen::Matrix< double, 2, Eigen::Dynamic > Mat2X
Mat3 RotationAroundY(double angle)
Eigen::Matrix< float, 4, 6 > Mat46f
double NormalizeL2(TVec *x)
double Nullspace2(TMat *A, TVec1 *x1, TVec2 *x2)
Eigen::Matrix< double, 17, 1 > Vec17
Eigen::Matrix< double, 2, 2 > Mat2
TMat ExtractColumns(const TMat &A, const TCols &columns)
Eigen::Matrix< float, 4, 4 > Mat4f
Eigen::Matrix< unsigned int, 2, 1 > Vec2u
Vec3 CrossProduct(const Vec3 &x, const Vec3 &y)
double NormL1(const TVec &x)
Eigen::Matrix< double, Eigen::Dynamic, 6 > MatX6
Eigen::Matrix< double, 4, 3 > Mat43