22 #ifndef KJB_M_MATRIX_D_IMPL_H
23 #define KJB_M_MATRIX_D_IMPL_H
36 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
41 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
47 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
55 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
62 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
66 typename Base::iterator it;
68 for(it = begin(); it < end(); ++it)
70 std::fill_n(it->begin(), it->size(), value);
75 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
78 return assignment_dispatch_(other);
81 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
91 return assignment_dispatch_(other);
94 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
103 return minus_equals_dispatch_(second);
106 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
110 for (
size_t row = 0; row < NROWS; row++)
112 for (
size_t col = 0; col < NCOLS; col++)
114 result[row][col] = -(*this)[row][col];
120 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
129 return plus_equals_dispatch_(second);
132 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
138 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
141 for (
size_t i = 0;
i < NROWS;
i++)
149 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
154 for(
size_t c = 0; c < num_cols; ++c)
158 (*this)[
r][c] = row[c];
162 (*this)[c][
r] = row[c];
167 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
172 for(
size_t r = 0;
r < num_rows; ++
r)
176 (*this)[
r][c] = col[
r];
180 (*this)[c][
r] = col[
r];
185 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
197 return matrix_equality_dispatch_(op2);
200 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
203 return !(*
this == op2);
206 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
207 template <
class Matrix_type>
213 for (
size_t row = 0; row < get_num_rows(); ++row)
215 for (
size_t col = 0; col < get_num_cols(); ++col)
217 (*this)(row, col) = other(row, col);
224 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
225 template <
class Matrix_op>
229 for (
size_t row = 0; row != get_num_rows(); row++)
231 for (
size_t col = 0; col != get_num_cols(); col++)
233 (*this)(row, col) += second(row, col);
240 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
241 template <
class Matrix_op>
245 for (
size_t row = 0; row != get_num_rows(); row++)
247 for (
size_t col = 0; col != get_num_cols(); col++)
249 (*this)(row, col) -= second(row, col);
256 template <
class Matrix_type_1,
class Matrix_type_2>
259 KJB_STATIC_ASSERT(Matrix_type_1::num_cols == Matrix_type_2::num_rows,
"Dimension mismatch");
261 static const size_t OUT_ROWS = Matrix_type_1::num_rows;
262 static const size_t IN_ROWS = Matrix_type_1::num_cols;
263 static const size_t IN_COLS= Matrix_type_2::num_cols;
267 for (
size_t out_row = 0; out_row < OUT_ROWS; ++out_row)
269 for (
size_t in_row = 0; in_row < IN_ROWS; ++in_row)
271 for (
size_t in_col = 0; in_col < IN_COLS; ++in_col)
273 result(out_row, in_col) += m1(out_row, in_row) * m2(in_row, in_col);
281 template <
size_t NROWS,
size_t NCOLS,
bool TRANSPOSED>
282 template <
class Matrix2>
283 bool Matrix_d<NROWS,NCOLS,TRANSPOSED>::matrix_equality_dispatch_(
const Matrix2& m2)
const
286 for (
size_t row = 0; row != get_num_rows(); row++)
288 for (
size_t col = 0; col != get_num_cols(); col++)
290 if ((*
this)(row,col) != m2(row, col))
return false;
298 template<
class Matrix_1,
class Matrix_2>
306 static size_t OUT_ROWS = m1.get_num_rows();
307 static size_t IN_ROWS = m2.get_num_rows();
308 static size_t IN_COLS = m2.get_num_cols();
311 Matrix result(OUT_ROWS, IN_COLS, 0);
313 for (
size_t out_row = 0; out_row < OUT_ROWS; ++out_row)
315 for (
size_t in_row = 0; in_row < IN_ROWS; ++in_row)
317 for (
size_t in_col = 0; in_col < IN_COLS; ++in_col)
319 result(out_row, in_col) += m1(out_row, in_row) * m2(in_row, in_col);
328 template<std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
329 inline Matrix_d<NROWS, NCOLS, TRANSPOSED>
335 template<std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
341 template<std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
348 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
354 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
360 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
366 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
372 template <std::
size_t NROWS, std::
size_t NCOLS,
bool TRANSPOSED>
373 std::ostream& operator<<(std::ostream& ost, const Matrix_d<NROWS, NCOLS, TRANSPOSED>& mat)
375 for (
size_t r = 0;
r < mat.get_num_rows(); ++
r)
377 for (
size_t c = 0; c < mat.get_num_cols(); ++c)
379 ost << mat(
r,c) <<
'\t';
386 template <std::
size_t D>
391 for (
size_t i = 0;
i <
D;
i++)
399 template <std::
size_t D>
409 template <std::
size_t NROWS, std::
size_t NCOLS>
414 for(
size_t i = 0;
i < result.size(); ++
i)
415 result[
i] = create_random_vector<NCOLS>();
424 for(
size_t i = 0;
i <
D; ++
i)
431 template <std::
size_t R, std::
size_t C,
bool T>
434 double result = init;
435 for (
size_t r = 0;
r < R;
r++)
437 for (
size_t c = 0; c < C; c++)
446 template <std::
size_t R, std::
size_t C>
451 double out = -DBL_MAX;
452 for(
size_t r = 0;
r < R; ++
r)
453 for(
size_t c = 0; c < C; ++c)
460 template <std::
size_t R, std::
size_t C,
bool T>
466 template <std::
size_t R, std::
size_t C,
bool T>
Definition: gr_opengl.h:41
Matrix_d< 2, 2 > Matrix2
Definition: m_matrix_d.h:580
bool operator!=(const Matrix_d &op2) const
Definition: m_matrix_d.h:534
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
double trace(const Matrix_d< D, D > &m)
Definition: m_matrix_d.impl.h:421
double accumulate(const Matrix_d< R, C, T > &mat, double init)
Definition: m_matrix_d.impl.h:432
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
int get_num_rows() const
Return the number of rows in the matrix.
Definition: m_matrix.h:543
int Size_type
size type of the elements
Definition: m_matrix.h:110
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
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
r
Definition: APPgetLargeConnectedEdges.m:127
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 & operator+=(const Matrix_d &second)
Definition: m_matrix_d.h:341
int get_num_cols() const
Return the number of columns in the matrix.
Definition: m_matrix.h:554
Matrix_d()
Definition: m_matrix_d.impl.h:37
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
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
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
Matrix matrix_multiply_dynamic_dispatch_(const Matrix_1 &m1, const Matrix_2 &m2)
don't call me directly
Definition: m_matrix_d.impl.h:299
Definition: g_quaternion.h:37
Definition: l_functors.h:250
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
D
Definition: APPgetLargeConnectedEdges.m:106
Definition: l_functors.h:238
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
Matrix create_random_matrix(int num_rows, int num_cols)
Definition: m_matrix.h:1853