22 #ifndef KJB_M_MATRIX_D_H
23 #define KJB_M_MATRIX_D_H
34 #include <boost/array.hpp>
38 #ifdef KJB_HAVE_BST_SERIAL
39 #include <boost/serialization/access.hpp>
40 #include <boost/serialization/array.hpp>
75 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED = false>
76 class Matrix_d :
public boost::array<Vector_d<NCOLS>, NROWS>
79 typedef boost::array<Vector_d<NCOLS>, NROWS> Base;
82 template <
bool condition, std::
size_t value1, std::
size_t value2>
85 template <std::
size_t value1, std::
size_t value2>
86 struct if_then_<false, value1, value2>{
static const std::size_t
value = value2;};
92 using typename Base::value_type;
93 using typename Base::iterator;
94 using typename Base::const_iterator;
95 using typename Base::reverse_iterator;
96 using typename Base::const_reverse_iterator;
97 using typename Base::reference;
98 using typename Base::const_reference;
99 using typename Base::size_type;
100 using typename Base::difference_type;
108 using Base::max_size;
109 using Base::operator[];
126 template <
class Iterator>
131 typedef typename std::iterator_traits<Iterator>::value_type RowVector;
137 typename Base::iterator oit = this->begin();
138 for (
size_t i = 0;
i < NROWS; ++
i, ++it, ++oit)
140 if (it->size() != NCOLS)
142 std::copy(it->begin(), it->end(), oit->begin());
284 static_cast<Base&
>(*this));
291 static_cast<const Base&
>(*this));
298 Base::operator=(other);
312 return minus_equals_dispatch_(second);
322 return minus_equals_dispatch_(second);
343 return plus_equals_dispatch_(second);
353 return plus_equals_dispatch_(second);
382 template <std::
size_t IN_COLS>
389 template <std::
size_t IN_COLS>
408 template <std::
size_t IN_ROWS,
bool IN_TRANSPOSED>
412 return *
this = *
this * other;
436 result[row] += (*
this)(row, col) * v[col];
442 result[row] += (*
this)(row, col) * v[col];
450 return (*
this) *= 1/s;
473 return (*
this)[col][row];
477 return (*
this)[row][col];
481 inline const double&
operator()(
size_t row,
size_t col)
const
486 return (*
this)[col][row];
490 return (*
this)[row][col];
497 for(
size_t c = 0; c <
num_cols; ++c)
499 row[c] = TRANSPOSED ? (*this)[c][
r] : (*this)[
r][c];
513 col[
r] = TRANSPOSED ? (*this)[c][
r] : (*this)[
r][c];
524 return matrix_equality_dispatch_(op2);
529 return matrix_equality_dispatch_(op2);
536 return !((*this) == op2);
541 return !((*this) == op2);
557 template <
class Matrix_type>
558 Matrix_d& assignment_dispatch_(Matrix_type& other);
560 template <
class Matrix_op>
561 Matrix_d& plus_equals_dispatch_(
const Matrix_op& second);
563 template <
class Matrix_op>
564 Matrix_d& minus_equals_dispatch_(
const Matrix_op& second);
567 template <
class Matrix2>
568 bool matrix_equality_dispatch_(
const Matrix2& m2)
const;
571 #ifdef KJB_HAVE_BST_SERIAL
572 template <
class Archive>
573 void serialize(Archive &ar,
const unsigned int )
575 ar & ::boost::serialization::base_object<Base>(*this);
588 template<std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
602 template <
class Matrix_type_1,
class Matrix_type_2>
606 template<std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
609 template<std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
613 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
616 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
619 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
622 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
628 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
629 std::ostream& operator<<(std::ostream& ost, const Matrix_d<NROWS, NCOLS, TRANSPOSED>& mat);
635 template <std::
size_t D>
641 template <std::
size_t D>
644 template <std::
size_t NROWS, std::
size_t NCOLS>
664 template <std::
size_t R, std::
size_t C,
bool T>
670 template <std::
size_t R, std::
size_t C,
bool T,
class Binary_function>
672 Binary_function binary_op)
674 double result = init;
675 for (
size_t r = 0;
r < R;
r++)
677 for (
size_t c = 0; c < C; c++)
679 result = binary_op(result, mat(
r,c));
686 template <std::
size_t R, std::
size_t C>
687 double max_abs_difference(
const Matrix_d<R,C,false>& op1,
const Matrix_d<R,C,false>& op2 );
692 template <std::
size_t R, std::
size_t C,
bool T>
693 double max(
const Matrix_d<R,C, T>& mat);
698 template <std::
size_t R, std::
size_t C,
bool T>
699 double min(
const Matrix_d<R,C, T>& mat);
Vector_d< num_cols > get_row(size_t r) const
Definition: m_matrix_d.h:494
Definition: gr_opengl.h:41
Matrix_d< 2, 2 > Matrix2
Definition: m_matrix_d.h:580
Matrix_d operator+(const Matrix_d< NCOLS, NROWS,!TRANSPOSED > &second) const
Definition: m_matrix_d.h:356
bool operator!=(const Matrix_d &op2) const
Definition: m_matrix_d.h:534
static size_t get_num_cols()
Definition: m_matrix_d.h:463
Matrix_d operator-(const Matrix_d &second) const
Definition: m_matrix_d.h:315
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
Matrix_d< 4, 4, false > Matrix4
Definition: gr_opengl.h:41
double trace(const Matrix_d< D, D > &m)
Definition: m_matrix_d.impl.h:421
Matrix_d & swap(Matrix_d &other)
Definition: m_matrix_d.h:275
Matrix_d< num_rows, IN_COLS > operator*(const Matrix_d< num_cols, IN_COLS, false > &second) const
Definition: m_matrix_d.h:383
const Matrix_d< NROWS, NCOLS,!TRANSPOSED > & transpose() const
Definition: m_matrix_d.h:288
double accumulate(const Matrix_d< R, C, T > &mat, double init)
Definition: m_matrix_d.impl.h:432
Definition: m_concept.h:30
void set_col(size_t c, const Vector_d< D > &col)
Definition: m_matrix_d.impl.h:169
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Vector_d< num_rows > get_col(size_t c) const
Definition: m_matrix_d.h:508
Matrix_d operator*(double s) const
Definition: m_matrix_d.h:416
Matrix_d operator-(const Matrix &second) const
Definition: m_matrix_d.h:332
Matrix_d operator/(double s) const
Definition: m_matrix_d.h:453
Int_matrix::Value_type max_abs_difference(const Int_matrix &op1, const Int_matrix &op2)
Find the largest difference between two matrices.
Definition: l_int_matrix.h:1364
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
Matrix_d & operator*=(const Matrix_d< IN_ROWS, IN_ROWS, IN_TRANSPOSED > &other)
Definition: m_matrix_d.h:409
Matrix_d< Matrix_type_1::num_rows, Matrix_type_2::num_cols > matrix_multiply_dispatch_(const Matrix_type_1 &m1, const Matrix_type_2 &m2)
Definition: m_matrix_d.impl.h:257
Matrix_d & operator/=(double s)
Definition: m_matrix_d.h:448
r
Definition: APPgetLargeConnectedEdges.m:127
bool operator==(const Matrix_d< NCOLS, NROWS,!TRANSPOSED > &op2) const
Definition: m_matrix_d.h:527
Image operator-(const Image &im1, const Image &im2)
Subtract two images.
Definition: i_image.h:843
#define KJB_THROW(ex)
Definition: l_exception.h:46
Matrix outer_product(const Vector &v1, const Vector &v2)
Definition: m_matrix.h:2293
#define KJB_STATIC_ASSERT(x, y)
Definition: l_cxx11.h:55
Matrix_d operator-() const
negation
Definition: m_matrix_d.impl.h:107
Image operator+(const Image &op1, const Image &op2)
Add two images.
Definition: i_image.h:834
Matrix_d< 3, 3 > Matrix3
Definition: m_matrix_d.h:581
Matrix_d & operator+=(const Matrix_d &second)
Definition: m_matrix_d.h:341
Matrix_d(Iterator begin)
Fill constructor.
Definition: m_matrix_d.h:127
Matrix matrix_inverse(const Matrix &op1)
Invert this matrix.
Definition: m_matrix.cpp:730
Matrix_d & operator-=(const Matrix_d< NCOLS, NROWS,!TRANSPOSED > &second)
Definition: m_matrix_d.h:320
Matrix_d< num_rows, IN_COLS > operator*(const Matrix_d< IN_COLS, num_cols, true > &second) const
Definition: m_matrix_d.h:390
static size_t get_num_rows()
Definition: m_matrix_d.h:458
bool operator!=(const Matrix_d< NCOLS, NROWS,!TRANSPOSED > &op2) const
Definition: m_matrix_d.h:539
void symmetric_eigs(const kjb::Matrix3 &A, double &eig1, double &eig2, double &eig3)
get eigenvalues of symmetric matrix A in ascending order
Definition: m_matrix_d.cpp:160
Matrix_d()
Definition: m_matrix_d.impl.h:37
static const std::size_t value
Definition: m_matrix_d.h:83
Matrix_d & operator=(const Matrix_d &other)
assignment
Definition: m_matrix_d.h:296
Matrix_d operator+(const Matrix_d &second) const
Definition: m_matrix_d.h:346
Matrix create_identity_matrix(int rank)
Construct an identity matrix of specified rank.
Definition: m_matrix.h:1795
Matrix_d operator-(const Matrix_d< NCOLS, NROWS,!TRANSPOSED > &second) const
Definition: m_matrix_d.h:325
bool operator!=(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1274
void set_row(size_t r, const Vector_d< D > &row)
Definition: m_matrix_d.impl.h:151
bool operator==(const Matrix_d &op2) const
Definition: m_matrix_d.h:522
bool operator==(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for exact equality between two matrices.
Definition: l_int_matrix.cpp:218
Vector_d< num_rows > operator*(const Vector_d< num_cols > &v) const
Definition: m_matrix_d.h:428
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
Matrix_d & operator*=(double s)
multiplication by a scalar
Definition: m_matrix_d.impl.h:139
Matrix_d & operator-=(const Matrix_d &second)
Definition: m_matrix_d.h:310
static const std::size_t num_cols
Definition: m_matrix_d.h:88
Definition: m_matrix_d.h:83
double & operator()(size_t row, size_t col)
Definition: m_matrix_d.h:468
void resize(size_t rows, size_t cols)
Definition: m_matrix_d.h:263
Definition: g_quaternion.h:37
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Matrix_d< NROWS, NCOLS,!TRANSPOSED > & transpose()
Definition: m_matrix_d.h:281
for m
Definition: APPgetLargeConnectedEdges.m:64
const double & operator()(size_t row, size_t col) const
Definition: m_matrix_d.h:481
Support for error handling exception classes in libKJB.
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
Matrix_d & operator+=(const Matrix_d< NCOLS, NROWS,!TRANSPOSED > &second)
Definition: m_matrix_d.h:351
static const std::size_t num_rows
Definition: m_matrix_d.h:89
Matrix create_random_matrix(int num_rows, int num_cols)
Definition: m_matrix.h:1853