14 #if defined(SUPPORT_STDIOSTREAM) 17 #endif // defined(SUPPORT_STDIOSTREAM) 24 #define MATRIX_GCC_4_8_WORKAROUND (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)) 26 #define MATRIX_GCC_4_8_WORKAROUND 0 32 template <
typename Type,
size_t M>
35 template<
typename Type,
size_t M,
size_t N>
38 template <
typename Type,
size_t P,
size_t Q,
size_t M,
size_t N>
41 template<
typename Type,
size_t M,
size_t N>
44 #if MATRIX_GCC_4_8_WORKAROUND 53 #if MATRIX_GCC_4_8_WORKAROUND 56 for (
size_t i = 0; i < M; i++) {
57 for (
size_t j = 0; j < N; j++) {
66 explicit Matrix(
const Type data_[M*N])
68 memcpy(_data, data_,
sizeof(_data));
71 explicit Matrix(
const Type data_[M][N])
73 memcpy(_data, data_,
sizeof(_data));
78 memcpy(_data, other.
_data,
sizeof(_data));
81 template<
size_t P,
size_t Q>
85 for (
size_t i = 0; i < M; i++) {
86 for (
size_t j = 0; j < N; j++) {
87 self(i, j) = in_slice(i, j);
109 if (
this != &other) {
110 memcpy(_data, other.
_data,
sizeof(_data));
117 memcpy(dst, _data,
sizeof(Type)*M*N);
124 for (
size_t i = 0; i < M; i++) {
125 for (
size_t j = 0; j < N; j++) {
126 dst[i+(j*M)] =
self(i, j);
146 for (
size_t i = 0; i < M; i++) {
147 for (
size_t k = 0; k <
P; k++) {
148 for (
size_t j = 0; j < N; j++) {
149 res(i, k) +=
self(i, j) * other(j, k);
162 for (
size_t i = 0; i < M; i++) {
163 for (
size_t j = 0; j < N; j++) {
164 res(i, j) =
self(i, j)*other(i, j);
176 for (
size_t i = 0; i < M; i++) {
177 for (
size_t j = 0; j < N; j++) {
178 res(i, j) =
self(i, j)/other(i, j);
190 for (
size_t i = 0; i < M; i++) {
191 for (
size_t j = 0; j < N; j++) {
192 res(i, j) =
self(i, j) + other(i, j);
204 for (
size_t i = 0; i < M; i++) {
205 for (
size_t j = 0; j < N; j++) {
206 res(i, j) =
self(i, j) - other(i, j);
219 for (
size_t i = 0; i < M; i++) {
220 for (
size_t j = 0; j < N; j++) {
221 res(i, j) = -
self(i, j);
256 for (
size_t i = 0; i < M; i++) {
257 for (
size_t j = 0; j < N; j++) {
258 res(i, j) =
self(i, j) * scalar;
267 return (*
this)*(1/scalar);
275 for (
size_t i = 0; i < M; i++) {
276 for (
size_t j = 0; j < N; j++) {
277 res(i, j) =
self(i, j) + scalar;
286 return (*
this) + (-1*scalar);
293 for (
size_t i = 0; i < M; i++) {
294 for (
size_t j = 0; j < N; j++) {
295 self(i, j) =
self(i, j) * scalar;
303 self =
self * (Type(1) / scalar);
308 *
this = (*this) + scalar;
313 *
this = (*this) - scalar;
324 return !(
self == other);
335 for (
size_t i = 0; i < M; i++) {
336 for (
size_t j = 0; j < N; j++) {
337 snprintf(buf + strlen(buf), n - strlen(buf),
"\t%8.8g",
double(
self(i, j)));
339 snprintf(buf + strlen(buf), n - strlen(buf),
"\n");
343 void print(FILE *stream = stdout)
const 346 static const size_t n = 10*N*M + M + 1;
347 char * buf =
new char[n];
349 fprintf(stream,
"%s\n", buf);
358 for (
size_t i = 0; i < M; i++) {
359 for (
size_t j = 0; j < N; j++) {
360 res(j, i) =
self(i, j);
373 template<
size_t P,
size_t Q>
379 template<
size_t P,
size_t Q>
387 return slice<1, N>(i,0);
392 return slice<1, N>(i,0);
397 return slice<M, 1>(0,j);
402 return slice<M, 1>(0,j);
413 slice<M,1>(0,j) = column;
418 memset(_data, 0,
sizeof(_data));
430 for (
size_t i = 0; i < M; i++) {
431 for (
size_t j = 0; j < N; j++) {
452 for (
size_t i = 0; i < M && i < N; i++) {
470 for (
size_t j = 0; j < N; j++) {
471 Type tmp =
self(a, j);
472 self(a, j) =
self(b, j);
485 for (
size_t i = 0; i < M; i++) {
486 Type tmp =
self(i, a);
487 self(i, a) =
self(i, b);
495 for (
size_t i=0; i<M; i++) {
496 for (
size_t j=0; j<N; j++) {
497 r(i,j) = Type(fabs((*
this)(i,j)));
505 Type max_val = (*this)(0,0);
506 for (
size_t i=0; i<M; i++) {
507 for (
size_t j=0; j<N; j++) {
508 Type val = (*this)(i,j);
519 Type min_val = (*this)(0,0);
520 for (
size_t i=0; i<M; i++) {
521 for (
size_t j=0; j<N; j++) {
522 Type val = (*this)(i,j);
534 for (
size_t i = 0; i < M; i++) {
535 for (
size_t j = 0; j < N; j++) {
536 result = result && isnan(
self(i, j));
543 template<
typename Type,
size_t M,
size_t N>
550 template<
typename Type,
size_t M,
size_t N>
557 template<
size_t M,
size_t N>
564 template<
typename Type,
size_t M,
size_t N>
567 return other * scalar;
570 template<
typename Type,
size_t M,
size_t N>
573 for (
size_t i = 0; i < M; i++) {
574 for (
size_t j = 0; j < N; j++) {
575 if (!
isEqualF(x(i,j), y(i,j), eps)) {
583 namespace typeFunction
585 template<
typename Type>
586 Type
min(
const Type x,
const Type y) {
587 bool x_is_nan = isnan(x);
588 bool y_is_nan = isnan(y);
590 if (x_is_nan || y_is_nan) {
591 if (x_is_nan && !y_is_nan) {
594 if (!x_is_nan && y_is_nan) {
599 return (x < y) ? x : y;
601 template<
typename Type>
602 Type
max(
const Type x,
const Type y) {
603 bool x_is_nan = isnan(x);
604 bool y_is_nan = isnan(y);
606 if (x_is_nan || y_is_nan) {
607 if (x_is_nan && !y_is_nan) {
610 if (!x_is_nan && y_is_nan) {
615 return (x > y) ? x : y;
617 template<
typename Type>
618 Type
constrain(
const Type x,
const Type lower_bound,
const Type upper_bound) {
619 if (lower_bound > upper_bound) {
621 }
else if(isnan(x)) {
629 template<
typename Type,
size_t M,
size_t N>
632 for (
size_t i = 0; i < M; i++) {
633 for (
size_t j = 0; j < N; j++) {
640 template<
typename Type,
size_t M,
size_t N>
642 return min(x, scalar_upper_bound);
645 template<
typename Type,
size_t M,
size_t N>
648 for (
size_t i = 0; i < M; i++) {
649 for (
size_t j = 0; j < N; j++) {
656 template<
typename Type,
size_t M,
size_t N>
659 for (
size_t i = 0; i < M; i++) {
660 for (
size_t j = 0; j < N; j++) {
667 template<
typename Type,
size_t M,
size_t N>
669 return max(x, scalar_lower_bound);
672 template<
typename Type,
size_t M,
size_t N>
675 for (
size_t i = 0; i < M; i++) {
676 for (
size_t j = 0; j < N; j++) {
683 template<
typename Type,
size_t M,
size_t N>
685 const Type scalar_lower_bound,
686 const Type scalar_upper_bound) {
688 if (scalar_lower_bound > scalar_upper_bound) {
691 for (
size_t i = 0; i < M; i++) {
692 for (
size_t j = 0; j < N; j++) {
700 template<
typename Type,
size_t M,
size_t N>
705 for (
size_t i = 0; i < M; i++) {
706 for (
size_t j = 0; j < N; j++) {
713 #if defined(SUPPORT_STDIOSTREAM) 714 template<
typename Type,
size_t M,
size_t N>
715 std::ostream& operator<<(std::ostream& os,
718 for (
size_t i = 0; i < M; ++i) {
720 for (
size_t j = 0; j < N; ++j) {
721 os << std::setw(10) << matrix(i, j);
724 os <<
"]" << std::endl;
728 #endif // defined(SUPPORT_STDIOSTREAM) Matrix< Type, N, M > T() const
void write_string(char *buf, size_t n) const
Misc.
bool operator==(const Matrix< Type, M, N > &other) const
Matrix< Type, M, N > operator-(Type scalar) const
Matrix< Type, M, N > & operator=(const Matrix< Type, M, N > &other)
Matrix< Type, M, N > constrain(const Matrix< Type, M, N > &x, const Type scalar_lower_bound, const Type scalar_upper_bound)
void operator*=(const Matrix< Type, N, P > &other)
Matrix< Type, M, N > ones()
void setCol(size_t j, const Matrix< Type, M, 1 > &column)
Matrix< Type, M, N > operator-() const
void copyTo(Type dst[M *N]) const
Type min(const Type x, const Type y)
Matrix< Type, N, M > transpose() const
void operator/=(Type scalar)
void operator+=(const Matrix< Type, M, N > &other)
void swapRows(size_t a, size_t b)
void setRow(size_t i, const Matrix< Type, N, 1 > &row_in)
Matrix< Type, M, N > operator/(Type scalar) const
const Slice< Type, M, 1, M, N > col(size_t j) const
void operator+=(Type scalar)
Matrix< Type, M, N > operator-(const Matrix< Type, M, N > &other) const
Slice< Type, 1, N, M, N > row(size_t i)
Slice< Type, P, Q, M, N > slice(size_t x0, size_t y0)
Slice< Type, M, 1, M, N > col(size_t j)
const Slice< Type, P, Q, M, N > slice(size_t x0, size_t y0) const
Matrix< Type, M, N > operator+(Type scalar) const
Type & operator()(size_t i, size_t j)
bool isEqual(const Matrix< Type, M, N > &x, const Matrix< Type, M, N > &y, const Type eps=1e-4f)
void print(FILE *stream=stdout) const
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Matrix< Type, M, N > abs() const
Matrix(const Slice< Type, M, N, P, Q > &in_slice)
Matrix< Type, M, P > operator*(const Matrix< Type, N, P > &other) const
Matrix Operations.
Matrix< Type, M, N > operator*(Type scalar) const
Scalar Operations.
void operator-=(Type scalar)
Matrix< Type, M, N > edivide(const Matrix< Type, M, N > &other) const
Matrix< float, M, N > nans()
void operator-=(const Matrix< Type, M, N > &other)
const Slice< Type, 1, N, M, N > row(size_t i) const
bool isEqualF(const Type x, const Type y, const Type eps=1e-4f)
Compare if two floating point numbers are equal.
Matrix(const Type data_[M][N])
Matrix(const Type data_[M *N])
void copyToColumnMajor(Type dst[M *N]) const
Type operator()(size_t i, size_t j) const
Accessors/ Assignment etc.
void swapCols(size_t a, size_t b)
bool operator!=(const Matrix< Type, M, N > &other) const
Matrix< Type, M, N > operator+(const Matrix< Type, M, N > &other) const
Matrix< Type, M, N > emult(const Matrix< Type, M, N > &other) const
void operator*=(Type scalar)
Matrix(const Matrix &other)
Matrix< Type, M, N > zeros()
Type constrain(const Type x, const Type lower_bound, const Type upper_bound)
Type max(const Type x, const Type y)