37#ifndef VIGRA_MATRIX_HXX
38#define VIGRA_MATRIX_HXX
43#include "multi_array.hxx"
44#include "mathutil.hxx"
45#include "numerictraits.hxx"
46#include "multi_pointoperators.hxx"
67template <
class T,
class C>
69rowCount(
const MultiArrayView<2, T, C> &x);
71template <
class T,
class C>
75template <
class T,
class C>
76inline MultiArrayView <2, T, C>
79template <
class T,
class C>
80inline MultiArrayView <2, T, C>
83template <
class T,
class ALLOC = std::allocator<T> >
86template <
class T,
class C1,
class C2>
87void transpose(
const MultiArrayView<2, T, C1> &v, MultiArrayView<2, T, C2> &r);
89template <
class T,
class C>
92enum RawArrayMemoryLayout { RowMajor, ColumnMajor };
122template <
class T,
class ALLOC = std::allocator<T> >
262 template<
class U,
class C>
286 if(this->
shape() == rhs.shape())
299 template <
class U,
class C>
399 destMultiArrayRange(result),
411 destMultiArrayRange(result),
422 destMultiArrayRange(result),
434 destMultiArrayRange(result),
453 value_type
operator()(difference_type_1
row, difference_type_1 column)
const;
461 typename NormTraits<Matrix>::NormType
norm()
const;
476 template <
class U,
class C>
485 template <
class U,
class C>
494 template <
class U,
class C>
503 template <
class U,
class C>
547template <
class T,
class ALLOC>
556 typedef typename BaseType::value_type value_type;
557 typedef typename BaseType::pointer pointer;
558 typedef typename BaseType::const_pointer const_pointer;
559 typedef typename BaseType::reference reference;
560 typedef typename BaseType::const_reference const_reference;
561 typedef typename BaseType::difference_type difference_type;
562 typedef typename BaseType::difference_type_1 difference_type_1;
563 typedef ALLOC allocator_type;
565 TemporaryMatrix(difference_type
const &
shape)
569 TemporaryMatrix(difference_type
const &
shape, const_reference init)
570 : BaseType(
shape, init, ALLOC())
573 TemporaryMatrix(difference_type_1 rows, difference_type_1 columns)
574 : BaseType(rows, columns, ALLOC())
577 TemporaryMatrix(difference_type_1 rows, difference_type_1 columns, const_reference init)
578 : BaseType(rows, columns, init, ALLOC())
581 template<
class U,
class C>
582 TemporaryMatrix(
const MultiArrayView<2, U, C> &rhs)
586 TemporaryMatrix(
const TemporaryMatrix &rhs)
589 this->
swap(
const_cast<TemporaryMatrix &
>(rhs));
593 TemporaryMatrix & init(
const U & init)
599 template <
class U,
class C>
600 TemporaryMatrix &
operator+=(MultiArrayView<2, U, C>
const & other)
606 template <
class U,
class C>
607 TemporaryMatrix &
operator-=(MultiArrayView<2, U, C>
const & other)
613 template <
class U,
class C>
614 TemporaryMatrix &
operator*=(MultiArrayView<2, U, C>
const & other)
620 template <
class U,
class C>
621 TemporaryMatrix &
operator/=(MultiArrayView<2, U, C>
const & other)
652 TemporaryMatrix &
operator=(
const TemporaryMatrix &rhs);
669template <
class T,
class C>
682template <
class T,
class C>
695template <
class T,
class C>
711template <
class T,
class C>
716 return m.
subarray(first, Shape(first[0]+1, end));
725template <
class T,
class C>
740template <
class T,
class C>
745 return m.
subarray(first, Shape(end, first[1]+1));
759template <
class T,
class C>
765 return m.
subarray(Shape(first, 0), Shape(end, 1));
767 "linalg::subVector(): Input must be a vector (1xN or Nx1).");
768 return m.
subarray(Shape(0, first), Shape(1, end));
777template <
class T,
class C>
799template <
class T,
class C>
800typename NumericTraits<T>::Promote
803 typedef typename NumericTraits<T>::Promote SumType;
806 vigra_precondition(size ==
columnCount(
m),
"linalg::trace(): Matrix must be square.");
808 SumType
sum = NumericTraits<SumType>::zero();
822template <
class T,
class ALLOC>
832template <
class T,
class ALLOC>
844template <
class T,
class C>
849 "identityMatrix(): Matrix must be square.");
852 r(
j,
i) = NumericTraits<T>::zero();
853 r(
i,
i) = NumericTraits<T>::one();
873 ret(
i,
i) = NumericTraits<T>::one();
896template <
class T,
class C1,
class C2>
900 vigra_precondition(rowCount(r) == size && columnCount(r) == size,
901 "diagonalMatrix(): result must be a square matrix.");
914template <
class T,
class C1,
class C2>
918 "diagonalMatrix(): input must be a vector.");
919 r.
init(NumericTraits<T>::zero());
921 diagonalMatrixImpl(v.bindInner(0), r);
923 diagonalMatrixImpl(v.bindOuter(0), r);
942template <
class T,
class C>
946 "diagonalMatrix(): input must be a vector.");
950 diagonalMatrixImpl(v.bindInner(0),
ret);
952 diagonalMatrixImpl(v.bindOuter(0),
ret);
964template <
class T,
class C1,
class C2>
970 "transpose(): arrays must have transposed shapes.");
993template <
class T,
class C>
997 return v.transpose();
1008template <
class T,
class C1,
class C2>
1016 "joinVertically(): shape mismatch.");
1021 t.
subarray(Shape(0,0), Shape(ma, n)) = a;
1022 t.
subarray(Shape(ma,0), Shape(ma+
mb, n)) = b;
1034template <
class T,
class C1,
class C2>
1042 "joinHorizontally(): shape mismatch.");
1062template <
class T,
class C1,
class C2>
1070 "repeatMatrix(): Shape mismatch.");
1092template <
class T,
class C>
1109template <
class T,
class C1,
class C2,
class C3>
1117 "add(): Matrix shapes must agree.");
1121 r(
j,
i) = a(
j,
i) + b(
j,
i);
1134template <
class T,
class C1,
class C2>
1141template <
class T,
class C>
1148template <
class T,
class C>
1149inline TemporaryMatrix<T>
1150operator+(
const MultiArrayView<2, T, C> &a,
const TemporaryMatrix<T> &b)
1152 return const_cast<TemporaryMatrix<T> &
>(b) += a;
1156inline TemporaryMatrix<T>
1157operator+(
const TemporaryMatrix<T> &a,
const TemporaryMatrix<T> &b)
1159 return const_cast<TemporaryMatrix<T> &
>(a) += b;
1169template <
class T,
class C>
1170inline TemporaryMatrix<T>
1190template <
class T,
class C>
1191inline TemporaryMatrix<T>
1211template <
class T,
class C1,
class C2,
class C3>
1219 "subtract(): Matrix shapes must agree.");
1223 r(
j,
i) = a(
j,
i) - b(
j,
i);
1236template <
class T,
class C1,
class C2>
1243template <
class T,
class C>
1250template <
class T,
class C>
1252operator-(
const MultiArrayView<2, T, C> &a,
const TemporaryMatrix<T> &b)
1256 vigra_precondition(rows == b.rowCount() && cols == b.columnCount(),
1257 "Matrix::operator-(): Shape mismatch.");
1261 const_cast<TemporaryMatrix<T> &
>(b)(j, i) = a(j, i) - b(j, i);
1266inline TemporaryMatrix<T>
1267operator-(
const TemporaryMatrix<T> &a,
const TemporaryMatrix<T> &b)
1269 return const_cast<TemporaryMatrix<T> &
>(a) -= b;
1279template <
class T,
class C>
1280inline TemporaryMatrix<T>
1300template <
class T,
class C>
1301inline TemporaryMatrix<T>
1321template <
class T,
class C>
1322inline TemporaryMatrix<T>
1340template <
class T,
class C1,
class C2>
1341typename NormTraits<T>::SquaredNormType
1344 typename NormTraits<T>::SquaredNormType
ret =
1348 std::ptrdiff_t size = y.shape(0);
1349 if(x.shape(0) == 1 && x.shape(1) == size)
1350 for(std::ptrdiff_t
i = 0;
i < size; ++
i)
1351 ret += x(0,
i) * y(
i, 0);
1352 else if(x.shape(1) == 1u && x.shape(0) == size)
1353 for(std::ptrdiff_t
i = 0;
i < size; ++
i)
1354 ret += x(
i, 0) * y(
i, 0);
1356 vigra_precondition(
false,
"dot(): wrong matrix shapes.");
1358 else if(y.shape(0) == 1)
1360 std::ptrdiff_t size = y.shape(1);
1361 if(x.shape(0) == 1u && x.shape(1) == size)
1362 for(std::ptrdiff_t
i = 0;
i < size; ++
i)
1363 ret += x(0,
i) * y(0,
i);
1364 else if(x.shape(1) == 1u && x.shape(0) == size)
1365 for(std::ptrdiff_t
i = 0;
i < size; ++
i)
1366 ret += x(
i, 0) * y(0,
i);
1368 vigra_precondition(
false,
"dot(): wrong matrix shapes.");
1371 vigra_precondition(
false,
"dot(): wrong matrix shapes.");
1382template <
class T,
class C1,
class C2>
1383typename NormTraits<T>::SquaredNormType
1387 vigra_precondition(n == y.elementCount(),
1388 "dot(): shape mismatch.");
1389 typename NormTraits<T>::SquaredNormType
ret =
1403template <
class T,
class C1,
class C2,
class C3>
1407 vigra_precondition(3 == x.elementCount() && 3 == y.elementCount() && 3 == r.elementCount(),
1408 "cross(): vectors must have length 3.");
1409 r(0) = x(1)*y(2) - x(2)*y(1);
1410 r(1) = x(2)*y(0) - x(0)*y(2);
1411 r(2) = x(0)*y(1) - x(1)*y(0);
1422template <
class T,
class C1,
class C2,
class C3>
1427 "cross(): vectors must have length 3.");
1428 r(0,0) = x(1,0)*y(2,0) - x(2,0)*y(1,0);
1429 r(1,0) = x(2,0)*y(0,0) - x(0,0)*y(2,0);
1430 r(2,0) = x(0,0)*y(1,0) - x(1,0)*y(0,0);
1441template <
class T,
class C1,
class C2>
1458template <
class T,
class C1,
class C2,
class C3>
1466 "outer(): shape mismatch.");
1469 r(
j,
i) = x(
j, 0) * y(0,
i);
1481template <
class T,
class C1,
class C2>
1488 "outer(): shape mismatch.");
1501template <
class T,
class C>
1507 vigra_precondition(
rows == 1 ||
cols == 1,
1508 "outer(): matrix does not represent a vector.");
1516 ret(
j,
i) = x(0,
j) * x(0,
i);
1522 ret(
j,
i) = x(
j, 0) * x(
i, 0);
1534template <
class T,
int N>
1552 PointWise(T
const & it)
1558PointWise<T> pointWise(T
const & t)
1560 return PointWise<T>(t);
1571template <
class T,
class C1,
class C2>
1577 "smul(): Matrix sizes must agree.");
1581 r(
j,
i) = a(
j,
i) * b;
1591template <
class T,
class C2,
class C3>
1604template <
class T,
class C1,
class C2,
class C3>
1612 "mmul(): Matrix shapes must agree.");
1618 r(
j,
i) = a(
j, 0) * b(0,
i);
1621 r(
j,
i) += a(
j,
k) * b(
k,
i);
1633template <
class T,
class C1,
class C2>
1649template <
class T,
class C1,
class C2,
class C3>
1657 "pmul(): Matrix shapes must agree.");
1661 r(
j,
i) = a(
j,
i) * b(
j,
i);
1674template <
class T,
class C1,
class C2>
1701template <
class T,
class C,
class U>
1705 return pmul(a, b.t);
1715template <
class T,
class C>
1736template <
class T,
class C>
1737inline TemporaryMatrix<T>
1758template <
class T,
class A,
int N,
class DATA,
class DERIVED>
1763 "operator*(Matrix, TinyVector): Shape mismatch.");
1779template <
class T,
int N,
class DATA,
class DERIVED,
class A>
1784 "operator*(TinyVector, Matrix): Shape mismatch.");
1800template <
class T,
class C1,
class C2>
1816template <
class T,
class C1,
class C2>
1822 "sdiv(): Matrix sizes must agree.");
1826 r(
j,
i) = a(
j,
i) / b;
1836template <
class T,
class C1,
class C2,
class C3>
1844 "pdiv(): Matrix shapes must agree.");
1848 r(
j,
i) = a(
j,
i) / b(
j,
i);
1861template <
class T,
class C1,
class C2>
1888template <
class T,
class C,
class U>
1892 return pdiv(a, b.t);
1902template <
class T,
class C>
1923template <
class T,
class C>
1924inline TemporaryMatrix<T>
1952template <
class T,
class C>
1955 T
vopt = NumericTraits<T>::max();
1957 for(
int k=0;
k < a.
size(); ++
k)
1985template <
class T,
class C>
1988 T
vopt = NumericTraits<T>::min();
1990 for(
int k=0;
k < a.
size(); ++
k)
2020template <
class T,
class C,
class UnaryFunctor>
2023 T
vopt = NumericTraits<T>::max();
2025 for(
int k=0;
k < a.
size(); ++
k)
2055template <
class T,
class C,
class UnaryFunctor>
2058 T
vopt = NumericTraits<T>::min();
2060 for(
int k=0;
k < a.
size(); ++
k)
2073template <
class T,
class C>
2076 linalg::TemporaryMatrix<T> t(v.shape());
2086linalg::TemporaryMatrix<T> pow(linalg::TemporaryMatrix<T>
const & v, T
exponent)
2088 linalg::TemporaryMatrix<T> & t =
const_cast<linalg::TemporaryMatrix<T> &
>(v);
2097template <
class T,
class C>
2098linalg::TemporaryMatrix<T> pow(MultiArrayView<2, T, C>
const & v,
int exponent)
2100 linalg::TemporaryMatrix<T> t(v.shape());
2105 t(j, i) = vigra::pow(v(j, i), exponent);
2110linalg::TemporaryMatrix<T> pow(linalg::TemporaryMatrix<T>
const & v,
int exponent)
2112 linalg::TemporaryMatrix<T> & t =
const_cast<linalg::TemporaryMatrix<T> &
>(v);
2117 t(j, i) = vigra::pow(t(j, i), exponent);
2122linalg::TemporaryMatrix<int> pow(MultiArrayView<2, int, C>
const & v,
int exponent)
2124 linalg::TemporaryMatrix<int> t(v.shape());
2129 t(j, i) =
static_cast<int>(vigra::pow(
static_cast<double>(v(j, i)), exponent));
2134linalg::TemporaryMatrix<int> pow(linalg::TemporaryMatrix<int>
const & v,
int exponent)
2136 linalg::TemporaryMatrix<int> & t =
const_cast<linalg::TemporaryMatrix<int> &
>(v);
2141 t(j, i) =
static_cast<int>(vigra::pow(
static_cast<double>(t(j, i)), exponent));
2146template <
class T,
class C>
2149template <
class T,
class C>
2152template <
class T,
class C>
2155template <
class T,
class C>
2158template <
class T,
class C>
2161template <
class T,
class C>
2164template <
class T,
class C>
2167template <
class T,
class C>
2170template <
class T,
class C>
2173template <
class T,
class C>
2176template <
class T,
class C>
2179template <
class T,
class C>
2182template <
class T,
class C>
2185template <
class T,
class C>
2188template <
class T,
class C>
2191template <
class T,
class C>
2194#define VIGRA_MATRIX_UNARY_FUNCTION(FUNCTION, NAMESPACE) \
2195using NAMESPACE::FUNCTION; \
2196template <class T, class C> \
2197linalg::TemporaryMatrix<T> FUNCTION(MultiArrayView<2, T, C> const & v) \
2199 linalg::TemporaryMatrix<T> t(v.shape()); \
2200 MultiArrayIndex m = rowCount(v), n = columnCount(v); \
2202 for(MultiArrayIndex i = 0; i < n; ++i) \
2203 for(MultiArrayIndex j = 0; j < m; ++j) \
2204 t(j, i) = NAMESPACE::FUNCTION(v(j, i)); \
2209linalg::TemporaryMatrix<T> FUNCTION(linalg::Matrix<T> const & v) \
2211 linalg::TemporaryMatrix<T> t(v.shape()); \
2212 MultiArrayIndex m = rowCount(v), n = columnCount(v); \
2214 for(MultiArrayIndex i = 0; i < n; ++i) \
2215 for(MultiArrayIndex j = 0; j < m; ++j) \
2216 t(j, i) = NAMESPACE::FUNCTION(v(j, i)); \
2221linalg::TemporaryMatrix<T> FUNCTION(linalg::TemporaryMatrix<T> const & v) \
2223 linalg::TemporaryMatrix<T> & t = const_cast<linalg::TemporaryMatrix<T> &>(v); \
2224 MultiArrayIndex m = rowCount(t), n = columnCount(t); \
2226 for(MultiArrayIndex i = 0; i < n; ++i) \
2227 for(MultiArrayIndex j = 0; j < m; ++j) \
2228 t(j, i) = NAMESPACE::FUNCTION(t(j, i)); \
2232using linalg::FUNCTION;\
2235VIGRA_MATRIX_UNARY_FUNCTION(sqrt, std)
2236VIGRA_MATRIX_UNARY_FUNCTION(exp, std)
2237VIGRA_MATRIX_UNARY_FUNCTION(log, std)
2238VIGRA_MATRIX_UNARY_FUNCTION(
log10, std)
2239VIGRA_MATRIX_UNARY_FUNCTION(sin, std)
2240VIGRA_MATRIX_UNARY_FUNCTION(asin, std)
2241VIGRA_MATRIX_UNARY_FUNCTION(cos, std)
2242VIGRA_MATRIX_UNARY_FUNCTION(acos, std)
2243VIGRA_MATRIX_UNARY_FUNCTION(tan, std)
2244VIGRA_MATRIX_UNARY_FUNCTION(atan, std)
2246VIGRA_MATRIX_UNARY_FUNCTION(floor,
vigra)
2247VIGRA_MATRIX_UNARY_FUNCTION(ceil,
vigra)
2248VIGRA_MATRIX_UNARY_FUNCTION(abs,
vigra)
2249VIGRA_MATRIX_UNARY_FUNCTION(sq,
vigra)
2252#undef VIGRA_MATRIX_UNARY_FUNCTION
2258using linalg::RowMajor;
2259using linalg::ColumnMajor;
2264using linalg::pointWise;
2277using linalg::argMin;
2278using linalg::argMinIf;
2279using linalg::argMax;
2280using linalg::argMaxIf;
2288template <
class T,
class ALLOC>
2290:
public NormTraits<MultiArray<2, T, ALLOC> >
2294 typedef typename BaseType::SquaredNormType SquaredNormType;
2295 typedef typename BaseType::NormType NormType;
2298template <
class T,
class ALLOC>
2299struct NormTraits<linalg::TemporaryMatrix<T, ALLOC> >
2300:
public NormTraits<Matrix<T, ALLOC> >
2302 typedef NormTraits<Matrix<T, ALLOC> > BaseType;
2303 typedef linalg::TemporaryMatrix<T, ALLOC> Type;
2304 typedef typename BaseType::SquaredNormType SquaredNormType;
2305 typedef typename BaseType::NormType NormType;
2322template <
class T,
class C>
2328 ios::fmtflags flags = s.setf(ios::right | ios::fixed, ios::adjustfield | ios::floatfield);
2333 s << m(j, i) <<
" ";
2351template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3>
2353columnStatisticsImpl(MultiArrayView<2, T1, C1>
const & A,
2354 MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & sumOfSquaredDifferences)
2360 "columnStatistics(): Shape mismatch between input and output.");
2363 mean.
init(NumericTraits<T2>::zero());
2364 sumOfSquaredDifferences.
init(NumericTraits<T3>::zero());
2368 typedef typename NumericTraits<T2>::RealPromote TmpType;
2370 TmpType f = TmpType(1.0 / (k + 1.0)),
2371 f1 = TmpType(1.0 - f);
2373 sumOfSquaredDifferences += f1*sq(t);
2377template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3>
2379columnStatistics2PassImpl(MultiArrayView<2, T1, C1>
const & A,
2380 MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & sumOfSquaredDifferences)
2386 "columnStatistics(): Shape mismatch between input and output.");
2389 mean.
init(NumericTraits<T2>::zero());
2394 mean /=
static_cast<double>(m);
2396 sumOfSquaredDifferences.
init(NumericTraits<T3>::zero());
2399 sumOfSquaredDifferences += sq(
rowVector(A, k) - mean);
2464template <
class T1,
class C1,
class T2,
class C2>
2472 "columnStatistics(): Shape mismatch between input and output.");
2474 mean.
init(NumericTraits<T2>::zero());
2483template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3>
2488 detail::columnStatisticsImpl(A, mean, stdDev);
2491 stdDev = sqrt(stdDev /
T3(rowCount(A) - 1.0));
2494template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3,
class T4,
class C4>
2496columnStatistics(MultiArrayView<2, T1, C1>
const & A,
2497 MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & stdDev, MultiArrayView<2, T4, C4> &
norm)
2501 vigra_precondition(1 == rowCount(mean) && n == columnCount(mean) &&
2502 1 == rowCount(stdDev) && n == columnCount(stdDev) &&
2503 1 == rowCount(
norm) && n == columnCount(
norm),
2504 "columnStatistics(): Shape mismatch between input and output.");
2506 detail::columnStatisticsImpl(A, mean, stdDev);
2507 norm = sqrt(stdDev + T2(m) * sq(mean));
2508 stdDev = sqrt(stdDev / T3(m - 1.0));
2567template <
class T1,
class C1,
class T2,
class C2>
2573 "rowStatistics(): Shape mismatch between input and output.");
2578template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3>
2583 vigra_precondition(1 == columnCount(mean) && rowCount(A) == rowCount(mean) &&
2584 1 == columnCount(stdDev) && rowCount(A) == rowCount(stdDev),
2585 "rowStatistics(): Shape mismatch between input and output.");
2588 columnStatistics(transpose(A),
tm,
ts);
2591template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3,
class T4,
class C4>
2593rowStatistics(MultiArrayView<2, T1, C1>
const & A,
2594 MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & stdDev, MultiArrayView<2, T4, C4> &
norm)
2596 vigra_precondition(1 == columnCount(mean) && rowCount(A) == rowCount(mean) &&
2597 1 == columnCount(stdDev) && rowCount(A) == rowCount(stdDev) &&
2598 1 == columnCount(
norm) && rowCount(A) == rowCount(
norm),
2599 "rowStatistics(): Shape mismatch between input and output.");
2600 MultiArrayView<2, T2, StridedArrayTag> tm = transpose(mean);
2601 MultiArrayView<2, T3, StridedArrayTag> ts = transpose(stdDev);
2602 MultiArrayView<2, T4, StridedArrayTag> tn = transpose(
norm);
2603 columnStatistics(transpose(A), tm, ts, tn);
2608template <
class T1,
class C1,
class U,
class T2,
class C2,
class T3,
class C3>
2609void updateCovarianceMatrix(MultiArrayView<2, T1, C1>
const & features,
2610 U & count, MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & covariance)
2614 "updateCovarianceMatrix(): Features must be a row or column vector.");
2615 vigra_precondition(mean.shape() == features.shape(),
2616 "updateCovarianceMatrix(): Shape mismatch between feature vector and mean vector.");
2618 "updateCovarianceMatrix(): Shape mismatch between feature vector and covariance matrix.");
2621 Matrix<T2> t = features - mean;
2623 T2 f = T2(1.0) / count,
2631 covariance(k, k) += f1*sq(t(0, k));
2634 covariance(k, l) += f1*t(0, k)*t(0, l);
2635 covariance(l, k) = covariance(k, l);
2643 covariance(k, k) += f1*sq(t(k, 0));
2646 covariance(k, l) += f1*t(k, 0)*t(l, 0);
2647 covariance(l, k) = covariance(k, l);
2663template <
class T1,
class C1,
class T2,
class C2>
2669 "covarianceMatrixOfColumns(): Shape mismatch between feature matrix and covariance matrix.");
2686template <
class T,
class C>
2703template <
class T1,
class C1,
class T2,
class C2>
2709 "covarianceMatrixOfRows(): Shape mismatch between feature matrix and covariance matrix.");
2726template <
class T,
class C>
2735enum DataPreparationGoals { ZeroMean = 1, UnitVariance = 2, UnitNorm = 4, UnitSum = 8 };
2737inline DataPreparationGoals operator|(DataPreparationGoals l, DataPreparationGoals r)
2739 return DataPreparationGoals(
int(l) |
int(r));
2744template <
class T,
class C1,
class C2,
class C3,
class C4>
2746prepareDataImpl(
const MultiArrayView<2, T, C1> & A,
2747 MultiArrayView<2, T, C2> & res, MultiArrayView<2, T, C3> & offset, MultiArrayView<2, T, C4> & scaling,
2748 DataPreparationGoals goals)
2752 vigra_precondition(A.shape() == res.shape() &&
2755 "prepareDataImpl(): Shape mismatch between input and output.");
2760 offset.init(NumericTraits<T>::zero());
2761 scaling.init(NumericTraits<T>::one());
2765 bool zeroMean = (goals & ZeroMean) != 0;
2766 bool unitVariance = (goals & UnitVariance) != 0;
2767 bool unitNorm = (goals & UnitNorm) != 0;
2768 bool unitSum = (goals & UnitSum) != 0;
2772 vigra_precondition(goals == UnitSum,
2773 "prepareData(): Unit sum is not compatible with any other data preparation goal.");
2777 offset.init(NumericTraits<T>::zero());
2781 if(scaling(0, k) != NumericTraits<T>::zero())
2783 scaling(0, k) = NumericTraits<T>::one() / scaling(0, k);
2788 scaling(0, k) = NumericTraits<T>::one();
2795 vigra_precondition(!(unitVariance && unitNorm),
2796 "prepareData(): Unit variance and unit norm cannot be achieved at the same time.");
2798 Matrix<T> mean(1, n), sumOfSquaredDifferences(1, n);
2799 detail::columnStatisticsImpl(A, mean, sumOfSquaredDifferences);
2803 T stdDev = std::sqrt(sumOfSquaredDifferences(0, k) / T(m-1));
2805 stdDev = NumericTraits<T>::zero();
2806 if(zeroMean && stdDev > NumericTraits<T>::zero())
2809 offset(0, k) = mean(0, k);
2810 mean(0, k) = NumericTraits<T>::zero();
2815 offset(0, k) = NumericTraits<T>::zero();
2818 T
norm = mean(0,k) == NumericTraits<T>::zero()
2819 ? std::sqrt(sumOfSquaredDifferences(0, k))
2820 : std::sqrt(sumOfSquaredDifferences(0, k) + T(m) * sq(mean(0,k)));
2821 if(unitNorm &&
norm > NumericTraits<T>::zero())
2824 scaling(0, k) = NumericTraits<T>::one() /
norm;
2826 else if(unitVariance && stdDev > NumericTraits<T>::zero())
2829 scaling(0, k) = NumericTraits<T>::one() / stdDev;
2833 scaling(0, k) = NumericTraits<T>::one();
2915template <
class T,
class C1,
class C2,
class C3,
class C4>
2919 DataPreparationGoals
goals = ZeroMean | UnitVariance)
2924template <
class T,
class C1,
class C2>
2927 DataPreparationGoals
goals = ZeroMean | UnitVariance)
2991template <
class T,
class C1,
class C2,
class C3,
class C4>
2995 DataPreparationGoals
goals = ZeroMean | UnitVariance)
3001template <
class T,
class C1,
class C2>
3004 DataPreparationGoals
goals = ZeroMean | UnitVariance)
3019using linalg::ZeroMean;
3020using linalg::UnitVariance;
3021using linalg::UnitNorm;
3022using linalg::UnitSum;
Base class for, and view to, vigra::MultiArray.
Definition multi_array.hxx:705
const difference_type & shape() const
Definition multi_array.hxx:1650
difference_type m_shape
Definition multi_array.hxx:795
void copy(const MultiArrayView &rhs)
Definition multi_array.hxx:1218
pointer data() const
Definition multi_array.hxx:1900
Main MultiArray class containing the memory management.
Definition multi_array.hxx:2479
view_type::const_reference const_reference
Definition multi_array.hxx:2516
void swap(MultiArray &other)
Definition multi_array.hxx:3072
void reshape(const difference_type &shape)
Definition multi_array.hxx:2863
MultiArray & operator+=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2705
allocator_type const & allocator() const
Definition multi_array.hxx:2912
view_type::difference_type_1 difference_type_1
Definition multi_array.hxx:2528
view_type::const_pointer const_pointer
Definition multi_array.hxx:2508
view_type::difference_type difference_type
Definition multi_array.hxx:2524
view_type::pointer pointer
Definition multi_array.hxx:2504
view_type::reference reference
Definition multi_array.hxx:2512
MultiArray & operator-=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2721
MultiArray & init(const U &init)
Definition multi_array.hxx:2853
MultiArray & operator=(const MultiArray &rhs)
Definition multi_array.hxx:2671
MultiArray & operator/=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2752
view_type::value_type value_type
Definition multi_array.hxx:2500
MultiArray & operator*=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2736
Class for a single RGB value.
Definition rgbvalue.hxx:128
void init(Iterator i, Iterator end)
Definition tinyvector.hxx:708
size_type size() const
Definition tinyvector.hxx:913
TinyVectorView< VALUETYPE, TO-FROM > subarray() const
Definition tinyvector.hxx:887
Class for fixed size vectors.
Definition tinyvector.hxx:1008
Definition matrix.hxx:125
Matrix & operator*=(T other)
Definition matrix.hxx:528
void reshape(difference_type_1 rows, difference_type_1 columns, const_reference init)
Definition matrix.hxx:332
Matrix(const TemporaryMatrix< T, ALLOC > &rhs)
Definition matrix.hxx:253
NormTraits< Matrix >::SquaredNormType squaredNorm() const
Matrix & operator/=(MultiArrayView< 2, U, C > const &other)
Definition matrix.hxx:504
Matrix(ALLOC const &alloc)
Definition matrix.hxx:148
Matrix(difference_type_1 rows, difference_type_1 columns, ALLOC const &alloc=allocator_type())
Definition matrix.hxx:167
TemporaryMatrix< T > sum() const
Definition matrix.hxx:395
Matrix & operator*=(MultiArrayView< 2, U, C > const &other)
Definition matrix.hxx:495
Matrix & operator-=(T other)
Definition matrix.hxx:520
Matrix(const MultiArrayView< 2, U, C > &rhs)
Definition matrix.hxx:263
Matrix & operator=(const TemporaryMatrix< T, ALLOC > &rhs)
Definition matrix.hxx:284
Matrix & operator+=(T other)
Definition matrix.hxx:512
difference_type_1 columnCount() const
Definition matrix.hxx:374
Matrix()
Definition matrix.hxx:143
NormTraits< Matrix >::NormType norm() const
Matrix & init(const U &init)
Definition matrix.hxx:317
view_type columnVector(difference_type_1 d) const
Definition matrix.hxx:360
Matrix(const Matrix &rhs)
Definition matrix.hxx:239
Matrix(const difference_type &shape, const_pointer init, RawArrayMemoryLayout layout=RowMajor, allocator_type const &alloc=allocator_type())
Definition matrix.hxx:199
Matrix(difference_type_1 rows, difference_type_1 columns, const_reference init, allocator_type const &alloc=allocator_type())
Definition matrix.hxx:187
Matrix(difference_type_1 rows, difference_type_1 columns, const_pointer init, RawArrayMemoryLayout layout=RowMajor, allocator_type const &alloc=allocator_type())
Definition matrix.hxx:221
TemporaryMatrix< T > sum(difference_type_1 d) const
Definition matrix.hxx:406
Matrix & operator-=(MultiArrayView< 2, U, C > const &other)
Definition matrix.hxx:486
void reshape(difference_type const &newShape, const_reference init)
Definition matrix.hxx:346
void reshape(difference_type const &newShape)
Definition matrix.hxx:339
Matrix & operator=(const MultiArrayView< 2, U, C > &rhs)
Definition matrix.hxx:300
Matrix & operator+=(MultiArrayView< 2, U, C > const &other)
Definition matrix.hxx:477
Matrix(const difference_type &aShape, const_reference init, allocator_type const &alloc=allocator_type())
Definition matrix.hxx:177
void reshape(difference_type_1 rows, difference_type_1 columns)
Definition matrix.hxx:325
view_type rowVector(difference_type_1 d) const
Definition matrix.hxx:353
Matrix & operator/=(T other)
Definition matrix.hxx:536
difference_type_1 elementCount() const
Definition matrix.hxx:381
TemporaryMatrix< T > mean() const
Definition matrix.hxx:418
TemporaryMatrix< T > mean(difference_type_1 d) const
Definition matrix.hxx:429
bool isSymmetric() const
Definition matrix.hxx:388
Matrix & operator=(value_type const &v)
Definition matrix.hxx:309
difference_type_1 rowCount() const
Definition matrix.hxx:367
value_type & operator()(difference_type_1 row, difference_type_1 column)
Matrix & operator=(const Matrix &rhs)
Definition matrix.hxx:272
MultiArrayView< 2, vluae_type, StridedArrayTag > transpose() const
Matrix(const difference_type &aShape, ALLOC const &alloc=allocator_type())
Definition matrix.hxx:157
value_type operator()(difference_type_1 row, difference_type_1 column) const
void identityMatrix(MultiArrayView< 2, T, C > &r)
Definition matrix.hxx:845
void sdiv(const MultiArrayView< 2, T, C1 > &a, T b, MultiArrayView< 2, T, C2 > &r)
Definition matrix.hxx:1817
TemporaryMatrix< T > joinHorizontally(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b)
Definition matrix.hxx:1036
TemporaryMatrix< T > operator+(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b)
Definition matrix.hxx:1136
linalg::TemporaryMatrix< T > log10(MultiArrayView< 2, T, C > const &v)
Matrix< T, ALLLOC >::NormType norm(const Matrix< T, ALLLOC > &a)
void transpose(const MultiArrayView< 2, T, C1 > &v, MultiArrayView< 2, T, C2 > &r)
Definition matrix.hxx:965
TemporaryMatrix< T > joinVertically(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b)
Definition matrix.hxx:1010
MultiArrayIndex columnCount(const MultiArrayView< 2, T, C > &x)
Definition matrix.hxx:684
TemporaryMatrix< T > operator-(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b)
Definition matrix.hxx:1238
void mmul(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1605
void sub(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1212
void outer(const MultiArrayView< 2, T, C1 > &x, const MultiArrayView< 2, T, C2 > &y, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1459
void pmul(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1650
MultiArrayView< 2, T, C > rowVector(MultiArrayView< 2, T, C > const &m, MultiArrayIndex d)
Definition matrix.hxx:697
void prepareRows(...)
Standardize the rows of a matrix according to given DataPreparationGoals.
void covarianceMatrixOfRows(MultiArrayView< 2, T1, C1 > const &features, MultiArrayView< 2, T2, C2 > &covariance)
Compute the covariance matrix between the rows of a matrix features.
Definition matrix.hxx:2704
NumericTraits< T >::Promote trace(MultiArrayView< 2, T, C > const &m)
Definition matrix.hxx:801
TemporaryMatrix< T > operator*(const MultiArrayView< 2, T, C > &a, PointWise< U > b)
Definition matrix.hxx:1703
void covarianceMatrixOfColumns(MultiArrayView< 2, T1, C1 > const &features, MultiArrayView< 2, T2, C2 > &covariance)
Compute the covariance matrix between the columns of a matrix features.
Definition matrix.hxx:2664
void pdiv(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1837
void cross(const MultiArrayView< 1, T, C1 > &x, const MultiArrayView< 1, T, C2 > &y, MultiArrayView< 1, T, C3 > &r)
Definition matrix.hxx:1404
MultiArrayIndex rowCount(const MultiArrayView< 2, T, C > &x)
Definition matrix.hxx:671
void diagonalMatrix(MultiArrayView< 2, T, C1 > const &v, MultiArrayView< 2, T, C2 > &r)
Definition matrix.hxx:915
TemporaryMatrix< T > operator/(const MultiArrayView< 2, T, C > &a, PointWise< U > b)
Definition matrix.hxx:1890
void prepareColumns(...)
Standardize the columns of a matrix according to given DataPreparationGoals.
MultiArrayView< 2, T, C > columnVector(MultiArrayView< 2, T, C > const &m, MultiArrayIndex d)
Definition matrix.hxx:727
void repeatMatrix(MultiArrayView< 2, T, C1 > const &v, MultiArrayView< 2, T, C2 > &r, unsigned int verticalCount, unsigned int horizontalCount)
Definition matrix.hxx:1063
TemporaryMatrix< T > ones(MultiArrayIndex rows, MultiArrayIndex cols)
Definition matrix.hxx:889
void smul(const MultiArrayView< 2, T, C1 > &a, T b, MultiArrayView< 2, T, C2 > &r)
Definition matrix.hxx:1572
void columnStatistics(...)
MultiArrayView< 2, T, C > subVector(MultiArrayView< 2, T, C > const &m, int first, int end)
Definition matrix.hxx:761
Matrix< T, ALLLOC >::SquaredNormType squaredNorm(const Matrix< T, ALLLOC > &a)
void add(const MultiArrayView< 2, T, C1 > &a, const MultiArrayView< 2, T, C2 > &b, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1110
NormTraits< T >::SquaredNormType dot(const MultiArrayView< 2, T, C1 > &x, const MultiArrayView< 2, T, C2 > &y)
Definition matrix.hxx:1342
linalg::TemporaryMatrix< T > sign(MultiArrayView< 2, T, C > const &v)
bool isSymmetric(const MultiArrayView< 2, T, C > &v)
Definition matrix.hxx:779
linalg::TemporaryMatrix< T > round(MultiArrayView< 2, T, C > const &v)
Iterator argMaxIf(Iterator first, Iterator last, UnaryFunctor condition)
Find the maximum element in a sequence conforming to a condition.
Definition algorithm.hxx:165
int floor(FixedPoint< IntBits, FracBits > v)
rounding down.
Definition fixedpoint.hxx:667
Iterator argMin(Iterator first, Iterator last)
Find the minimum element in a sequence.
Definition algorithm.hxx:68
Iterator argMax(Iterator first, Iterator last)
Find the maximum element in a sequence.
Definition algorithm.hxx:96
NumericTraits< V >::Promote sum(TinyVectorBase< V, SIZE, D1, D2 > const &l)
sum of the vector's elements
Definition tinyvector.hxx:2073
void transformMultiArray(...)
Transform a multi-dimensional array with a unary function or functor.
bool closeAtTolerance(T1 l, T2 r, typename PromoteTraits< T1, T2 >::Promote epsilon)
Tolerance based floating-point equality.
Definition mathutil.hxx:1638
std::ptrdiff_t MultiArrayIndex
Definition multi_fwd.hxx:60
int ceil(FixedPoint< IntBits, FracBits > v)
rounding up.
Definition fixedpoint.hxx:675
Iterator argMinIf(Iterator first, Iterator last, UnaryFunctor condition)
Find the minimum element in a sequence conforming to a condition.
Definition algorithm.hxx:129