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> >
130 typedef TemporaryMatrix<T, ALLOC> temp_type;
139 typedef ALLOC allocator_type;
158 ALLOC
const & alloc = allocator_type())
167 Matrix(difference_type_1 rows, difference_type_1 columns,
168 ALLOC
const & alloc = allocator_type())
178 allocator_type
const & alloc = allocator_type())
187 Matrix(difference_type_1 rows, difference_type_1 columns, const_reference
init,
188 allocator_type
const & alloc = allocator_type())
200 allocator_type
const & alloc = allocator_type())
203 if(layout == RowMajor)
221 Matrix(difference_type_1 rows, difference_type_1 columns, const_pointer
init, RawArrayMemoryLayout layout = RowMajor,
222 allocator_type
const & alloc = allocator_type())
225 if(layout == RowMajor)
253 Matrix(
const TemporaryMatrix<T, ALLOC> &rhs)
256 this->
swap(
const_cast<TemporaryMatrix<T, ALLOC> &
>(rhs));
262 template<
class U,
class C>
286 if(this->
shape() == rhs.shape())
289 this->
swap(
const_cast<TemporaryMatrix<T, ALLOC> &
>(rhs));
299 template <
class U,
class C>
325 void reshape(difference_type_1 rows, difference_type_1 columns)
332 void reshape(difference_type_1 rows, difference_type_1 columns, const_reference
init)
395 TemporaryMatrix<T>
sum()
const
397 TemporaryMatrix<T> result(1, 1);
399 destMultiArrayRange(result),
406 TemporaryMatrix<T>
sum(difference_type_1 d)
const
409 TemporaryMatrix<T> result(
shape);
411 destMultiArrayRange(result),
418 TemporaryMatrix<T>
mean()
const
420 TemporaryMatrix<T> result(1, 1);
422 destMultiArrayRange(result),
429 TemporaryMatrix<T>
mean(difference_type_1 d)
const
432 TemporaryMatrix<T> result(
shape);
434 destMultiArrayRange(result),
447 value_type &
operator()(difference_type_1 row, difference_type_1 column);
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>
554 typedef TemporaryMatrix<T, ALLOC> temp_type;
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)
566 : BaseType(
shape, ALLOC())
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>
586 TemporaryMatrix(
const TemporaryMatrix &rhs)
589 this->
swap(
const_cast<TemporaryMatrix &
>(rhs));
593 TemporaryMatrix & init(
const U & init)
599 template <
class U,
class C>
606 template <
class U,
class C>
613 template <
class U,
class C>
620 template <
class U,
class C>
627 TemporaryMatrix & operator+=(T other)
633 TemporaryMatrix & operator-=(T other)
639 TemporaryMatrix & operator*=(T other)
645 TemporaryMatrix & operator/=(T other)
652 TemporaryMatrix &operator=(
const TemporaryMatrix &rhs);
669template <
class T,
class C>
682template <
class T,
class C>
695template <
class T,
class C>
696inline MultiArrayView <2, T, C>
711template <
class T,
class C>
712inline MultiArrayView <2, T, C>
716 return m.
subarray(first, Shape(first[0]+1, end));
725template <
class T,
class C>
726inline MultiArrayView <2, T, C>
740template <
class T,
class C>
741inline MultiArrayView <2, T, C>
745 return m.
subarray(first, Shape(end, first[1]+1));
759template <
class T,
class C>
760inline MultiArrayView <2, T, C>
765 return m.
subarray(Shape(first, 0), Shape(end, 1));
766 vigra_precondition(
rowCount(m) == 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>
787 if(m(j, i) != m(i, j))
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();
871 TemporaryMatrix<T> ret(size, size, NumericTraits<T>::zero());
873 ret(i, i) = NumericTraits<T>::one();
891 return TemporaryMatrix<T>(rows, cols, NumericTraits<T>::one());
896template <
class T,
class C1,
class C2>
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());
942template <
class T,
class C>
946 "diagonalMatrix(): input must be a vector.");
948 TemporaryMatrix<T> ret(size, size, NumericTraits<T>::zero());
964template <
class T,
class C1,
class C2>
970 "transpose(): arrays must have transposed shapes.");
993template <
class T,
class C>
1008template <
class T,
class C1,
class C2>
1009inline TemporaryMatrix<T>
1016 "joinVertically(): shape mismatch.");
1020 TemporaryMatrix<T> t(ma + mb, n, T());
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>
1035inline TemporaryMatrix<T>
1041 vigra_precondition(m ==
rowCount(b),
1042 "joinHorizontally(): shape mismatch.");
1046 TemporaryMatrix<T> t(m, na + nb, T());
1047 t.subarray(Shape(0,0), Shape(m, na)) = a;
1048 t.subarray(Shape(0, na), Shape(m, na + nb)) = b;
1062template <
class T,
class C1,
class C2>
1064 unsigned int verticalCount,
unsigned int horizontalCount)
1070 "repeatMatrix(): Shape mismatch.");
1072 for(
MultiArrayIndex l=0; l<static_cast<MultiArrayIndex>(horizontalCount); ++l)
1074 for(
MultiArrayIndex k=0; k<static_cast<MultiArrayIndex>(verticalCount); ++k)
1076 r.
subarray(Shape(k*m, l*n), Shape((k+1)*m, (l+1)*n)) = v;
1092template <
class T,
class C>
1097 TemporaryMatrix<T> ret(verticalCount*m, horizontalCount*n);
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>
1135inline TemporaryMatrix<T>
1138 return TemporaryMatrix<T>(a) += b;
1141template <
class T,
class C>
1142inline TemporaryMatrix<T>
1145 return const_cast<TemporaryMatrix<T> &
>(a) += b;
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>
1173 return TemporaryMatrix<T>(a) += b;
1177inline TemporaryMatrix<T>
1178operator+(
const TemporaryMatrix<T> &a, T b)
1180 return const_cast<TemporaryMatrix<T> &
>(a) += b;
1190template <
class T,
class C>
1191inline TemporaryMatrix<T>
1194 return TemporaryMatrix<T>(b) += a;
1198inline TemporaryMatrix<T>
1199operator+(T a,
const TemporaryMatrix<T> &b)
1201 return const_cast<TemporaryMatrix<T> &
>(b) += a;
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>
1237inline TemporaryMatrix<T>
1240 return TemporaryMatrix<T>(a) -= b;
1243template <
class T,
class C>
1244inline TemporaryMatrix<T>
1247 return const_cast<TemporaryMatrix<T> &
>(a) -= b;
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>
1283 return TemporaryMatrix<T>(a) *= -NumericTraits<T>::one();
1287inline TemporaryMatrix<T>
1290 return const_cast<TemporaryMatrix<T> &
>(a) *= -NumericTraits<T>::one();
1300template <
class T,
class C>
1301inline TemporaryMatrix<T>
1304 return TemporaryMatrix<T>(a) -= b;
1308inline TemporaryMatrix<T>
1309operator-(
const TemporaryMatrix<T> &a, T b)
1311 return const_cast<TemporaryMatrix<T> &
>(a) -= b;
1321template <
class T,
class C>
1322inline TemporaryMatrix<T>
1325 return TemporaryMatrix<T>(b.
shape(), a) -= b;
1340template <
class T,
class C1,
class C2>
1341typename NormTraits<T>::SquaredNormType
1344 typename NormTraits<T>::SquaredNormType ret =
1345 NumericTraits<typename NormTraits<T>::SquaredNormType>::zero();
1348 std::ptrdiff_t size = y.
shape(0);
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);
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
1388 "dot(): shape mismatch.");
1389 typename NormTraits<T>::SquaredNormType ret =
1390 NumericTraits<typename NormTraits<T>::SquaredNormType>::zero();
1403template <
class T,
class C1,
class C2,
class C3>
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>
1445 TemporaryMatrix<T> ret(3, 1);
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.");
1489 TemporaryMatrix<T> ret(rows, cols);
1501template <
class T,
class C>
1507 vigra_precondition(rows == 1 || cols == 1,
1508 "outer(): matrix does not represent a vector.");
1510 TemporaryMatrix<T> ret(size, size);
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>
1538 TemporaryMatrix<T> ret(N, N);
1542 ret(j, i) = x[j] * x[i];
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>
1634inline TemporaryMatrix<T>
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>
1675inline TemporaryMatrix<T>
1678 TemporaryMatrix<T> ret(a.
shape());
1701template <
class T,
class C,
class U>
1702inline TemporaryMatrix<T>
1705 return pmul(a, b.t);
1715template <
class T,
class C>
1716inline TemporaryMatrix<T>
1719 return TemporaryMatrix<T>(a) *= b;
1723inline TemporaryMatrix<T>
1724operator*(
const TemporaryMatrix<T> &a, T b)
1726 return const_cast<TemporaryMatrix<T> &
>(a) *= b;
1736template <
class T,
class C>
1737inline TemporaryMatrix<T>
1740 return TemporaryMatrix<T>(b) *= a;
1744inline TemporaryMatrix<T>
1745operator*(T a,
const TemporaryMatrix<T> &b)
1747 return const_cast<TemporaryMatrix<T> &
>(b) *= a;
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>
1801inline TemporaryMatrix<T>
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>
1862inline TemporaryMatrix<T>
1865 TemporaryMatrix<T> ret(a.
shape());
1888template <
class T,
class C,
class U>
1889inline TemporaryMatrix<T>
1892 return pdiv(a, b.t);
1902template <
class T,
class C>
1903inline TemporaryMatrix<T>
1906 return TemporaryMatrix<T>(a) /= b;
1910inline TemporaryMatrix<T>
1911operator/(
const TemporaryMatrix<T> &a, T b)
1913 return const_cast<TemporaryMatrix<T> &
>(a) /= b;
1923template <
class T,
class C>
1924inline TemporaryMatrix<T>
1927 return TemporaryMatrix<T>(b.
shape(), a) / pointWise(b);
1931using vigra::argMinIf;
1933using vigra::argMaxIf;
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)
2027 if(condition(a[k]) && a[k] < vopt)
2055template <
class T,
class C,
class UnaryFunctor>
2058 T vopt = NumericTraits<T>::min();
2060 for(
int k=0; k < a.
size(); ++k)
2062 if(condition(a[k]) && vopt < a[k])
2073template <
class T,
class C>
2076 linalg::TemporaryMatrix<T> t(v.
shape());
2081 t(j, i) = vigra::pow(v(j, i), exponent);
2086linalg::TemporaryMatrix<T>
pow(linalg::TemporaryMatrix<T>
const & v, T exponent)
2088 linalg::TemporaryMatrix<T> & t =
const_cast<linalg::TemporaryMatrix<T> &
>(v);
2093 t(j, i) = vigra::pow(t(j, i), exponent);
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>
2289struct NormTraits<
Matrix<T, ALLOC> >
2290:
public NormTraits<MultiArray<2, T, ALLOC> >
2292 typedef NormTraits<MultiArray<2, T, ALLOC> > BaseType;
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);
2462doxygen_overloaded_function(template <...>
void columnStatistics)
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);
2494template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3,
class T4,
class C4>
2497 MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & stdDev, MultiArrayView<2, T4, C4> &
norm)
2504 "columnStatistics(): Shape mismatch between input and output.");
2506 detail::columnStatisticsImpl(A, mean, stdDev);
2508 stdDev =
sqrt(stdDev / T3(m - 1.0));
2565doxygen_overloaded_function(template <...>
void rowStatistics)
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>
2585 "rowStatistics(): Shape mismatch between input and output.");
2591template <
class T1,
class C1,
class T2,
class C2,
class T3,
class C3,
class T4,
class C4>
2594 MultiArrayView<2, T2, C2> & mean, MultiArrayView<2, T3, C3> & stdDev, MultiArrayView<2, T4, C4> &
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);
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.");
2672 covariance.
init(NumericTraits<T2>::zero());
2674 detail::updateCovarianceMatrix(
rowVector(features, k), count, means, covariance);
2675 covariance /= T2(m - 1);
2686template <
class T,
class C>
2703template <
class T1,
class C1,
class T2,
class C2>
2709 "covarianceMatrixOfRows(): Shape mismatch between feature matrix and covariance matrix.");
2712 covariance.
init(NumericTraits<T2>::zero());
2714 detail::updateCovarianceMatrix(
columnVector(features, k), count, means, covariance);
2715 covariance /= T2(n - 1);
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() &&
2753 n == columnCount(offset) && 1 ==
rowCount(offset) &&
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();
2913doxygen_overloaded_function(template <...>
void prepareColumns)
2915template <
class T,
class C1,
class C2,
class C3,
class C4>
2919 DataPreparationGoals goals = ZeroMean | UnitVariance)
2921 detail::prepareDataImpl(A, res, offset, scaling, goals);
2924template <
class T,
class C1,
class C2>
2927 DataPreparationGoals goals = ZeroMean | UnitVariance)
2930 detail::prepareDataImpl(A, res, offset, scaling, goals);
2989doxygen_overloaded_function(template <...>
void prepareRows)
2991template <
class T,
class C1,
class C2,
class C3,
class C4>
2995 DataPreparationGoals goals = ZeroMean | UnitVariance)
2998 detail::prepareDataImpl(
transpose(A), tr, to, ts, goals);
3001template <
class T,
class C1,
class C2>
3004 DataPreparationGoals goals = ZeroMean | UnitVariance)
3008 detail::prepareDataImpl(
transpose(A), tr, offset, scaling, goals);
3019using linalg::ZeroMean;
3020using linalg::UnitVariance;
3021using linalg::UnitNorm;
3022using linalg::UnitSum;
Find the average pixel value in an image or ROI.
Definition inspectimage.hxx:1249
Find the sum of the pixel values in an image or ROI.
Definition multi_array.hxx:569
Base class for, and view to, vigra::MultiArray.
Definition multi_fwd.hxx:127
MultiArrayShape< actual_dimension >::type difference_type
Definition multi_array.hxx:739
MultiArrayView subarray(difference_type p, difference_type q) const
Definition multi_array.hxx:1530
const difference_type & shape() const
Definition multi_array.hxx:1650
difference_type m_shape
Definition multi_array.hxx:795
MultiArrayView & init(const U &init)
Definition multi_array.hxx:1208
MultiArrayView< N-M, T, StrideTag > bindOuter(const TinyVector< Index, M > &d) const
Definition multi_array.hxx:2186
void copy(const MultiArrayView &rhs)
Definition multi_array.hxx:1218
MultiArrayView< N-M, T, StridedArrayTag > bindInner(const TinyVector< Index, M > &d) const
pointer data() const
Definition multi_array.hxx:1900
difference_type_1 elementCount() const
Definition multi_array.hxx:1632
MultiArrayView()
Definition multi_array.hxx:829
difference_type_1 size() const
Definition multi_array.hxx:1643
MultiArrayView< N, T, StridedArrayTag > transpose() const
Definition multi_array.hxx:1569
Main MultiArray class containing the memory management.
Definition multi_fwd.hxx:131
view_type::const_reference const_reference
Definition multi_array.hxx:2516
view_type::reference reference
Definition multi_array.hxx:2512
void swap(MultiArray &other)
Definition multi_array.hxx:3072
view_type::const_pointer const_pointer
Definition multi_array.hxx:2508
void reshape(const difference_type &shape)
Definition multi_array.hxx:2863
MultiArray & operator+=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2705
view_type::difference_type_1 difference_type_1
Definition multi_array.hxx:2528
allocator_type const & allocator() const
Definition multi_array.hxx:2912
view_type::pointer pointer
Definition multi_array.hxx:2504
MultiArray & operator-=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2721
MultiArray & init(const U &init)
Definition multi_array.hxx:2853
view_type::value_type value_type
Definition multi_array.hxx:2500
MultiArray & operator=(const MultiArray &rhs)
Definition multi_array.hxx:2671
MultiArray & operator/=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2752
MultiArray & operator*=(const MultiArrayView< N, U, StrideTag > &rhs)
Definition multi_array.hxx:2736
Base class for fixed size vectors.
Definition tinyvector.hxx:640
Wrapper for fixed size vectors.
Definition tinyvector.hxx:1254
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
linalg::TemporaryMatrix< T > floor(MultiArrayView< 2, T, C > const &v)
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)
linalg::TemporaryMatrix< T > asin(MultiArrayView< 2, T, C > const &v)
linalg::TemporaryMatrix< T > exp(MultiArrayView< 2, T, C > const &v)
linalg::TemporaryMatrix< T > acos(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
linalg::TemporaryMatrix< T > ceil(MultiArrayView< 2, T, C > const &v)
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
int argMaxIf(MultiArrayView< 2, T, C > const &a, UnaryFunctor condition)
Find the index of the maximum element in a matrix subject to a condition.
Definition matrix.hxx:2056
linalg::TemporaryMatrix< T > sq(MultiArrayView< 2, T, C > const &v)
int argMax(MultiArrayView< 2, T, C > const &a)
Find the index of the maximum element in a matrix.
Definition matrix.hxx:1986
int argMin(MultiArrayView< 2, T, C > const &a)
Find the index of the minimum element in a matrix.
Definition matrix.hxx:1953
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
linalg::TemporaryMatrix< T > sqrt(MultiArrayView< 2, T, C > const &v)
void outer(const MultiArrayView< 2, T, C1 > &x, const MultiArrayView< 2, T, C2 > &y, MultiArrayView< 2, T, C3 > &r)
Definition matrix.hxx:1459
linalg::TemporaryMatrix< T > abs(MultiArrayView< 2, T, C > const &v)
linalg::TemporaryMatrix< T > sin(MultiArrayView< 2, T, C > const &v)
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
linalg::TemporaryMatrix< T > pow(MultiArrayView< 2, T, C > const &v, T exponent)
Definition matrix.hxx:2074
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
linalg::TemporaryMatrix< T > atan(MultiArrayView< 2, T, C > const &v)
TemporaryMatrix< T > ones(MultiArrayIndex rows, MultiArrayIndex cols)
Definition matrix.hxx:889
linalg::TemporaryMatrix< T > cos(MultiArrayView< 2, T, C > const &v)
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
linalg::TemporaryMatrix< T > log(MultiArrayView< 2, T, C > const &v)
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 > tan(MultiArrayView< 2, T, C > const &v)
int argMinIf(MultiArrayView< 2, T, C > const &a, UnaryFunctor condition)
Find the index of the minimum element in a matrix subject to a condition.
Definition matrix.hxx:2021
linalg::TemporaryMatrix< T > round(MultiArrayView< 2, T, C > const &v)
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