Peano
Loading...
Searching...
No Matches
Matrix.cpph
Go to the documentation of this file.
1#include "tarch/Assertions.h"
2
3
4#include <iomanip>
5
6
7template<int Rows, int Cols, typename Scalar>
11
12
13template<int Rows, int Cols, typename Scalar>
14tarch::la::Matrix<Rows,Cols,Scalar>::Matrix( const Scalar& initialValue ) {
15 for (int i=0; i < Rows*Cols; i++) {
16 _values[i] = initialValue;
17 }
18}
19
20
21
22template<int Rows, int Cols, typename Scalar>
23tarch::la::Matrix<Rows,Cols,Scalar>::Matrix( std::initializer_list<Scalar> values ) {
24 int index = 0;
25 for (typename std::initializer_list<Scalar>::const_iterator p = values.begin(); p!=values.end(); p++) {
26 _values[index] = *p;
27 index++;
28 }
29}
30
31
32template<int Rows, int Cols, typename Scalar>
34 return Rows;
35}
36
37
38template<int Rows, int Cols, typename Scalar>
40 return Cols;
41}
42
43
44template<int Rows, int Cols, typename Scalar>
46 return Rows * Cols;
47}
48
49
50template<int Rows, int Cols, typename Scalar>
52 std::ostringstream os;
53 os << "[";
54 for (int i=0; i < Rows; i++) {
55 os << "[";
56 for (int j=0; j < Cols; j++) {
57 os << _values[i * Cols + j];
58 if (j + 1 < Cols) {
59 os << ",";
60 }
61 }
62 os << "]";
63 if (i + 1 < Rows) {
64 os << ",";
65 }
66 }
67 os << "]";
68 return os.str();
69}
70
71
72template<int Rows, int Cols, typename Scalar>
73std::string tarch::la::Matrix<Rows,Cols,Scalar>::toPrettyString(int numberOfDigits) const {
74 std::ostringstream os;
75 os << "[";
76 os << std::setiosflags(std::ios::fixed) << std::setprecision(numberOfDigits);
77 for (int i=0; i < Rows; i++) {
78 os << std::endl;
79 for (int j=0; j < Cols; j++) {
80 if ( tarch::la::abs( _values[i * Cols + j] )>=0.0) {
81 os << " ";
82 }
83 os << _values[i * Cols + j];
84 if (j + 1 < Cols) {
85 os << ", ";
86 }
87 }
88 if (i + 1 < Rows) {
89 os << ",";
90 }
91 }
92 os << std::endl;
93 os << "]";
94 return os.str();
95}
96
97
98template<int Rows, int Cols, typename Scalar>
99template <typename NewScalarType>
102 for (int i=0; i < Rows; i++) {
103 for (int j=0; j < Cols; j++) {
104 result(j,i) = operator()(j,i);
105 }
106 }
107 return result;
108}
Static (i.e.
Definition Matrix.h:31
std::string toString() const
Definition Matrix.cpph:51
std::string toPrettyString(int numberOfDigits=4) const
Definition Matrix.cpph:73
int cols() const
Returns the number of columns in the matrix.
Definition Matrix.cpph:39
Matrix()
Constructs a non-initialized matrix.
Definition Matrix.cpph:8
int size() const
Returns the number of total elements in the matrix.
Definition Matrix.cpph:45
tarch::la::Matrix< Rows, Cols, NewScalarType > convertScalar() const
Definition Matrix.cpph:100
int rows() const
Returns the number of rows in the matrix.
Definition Matrix.cpph:33
double abs(double value)
Returns the absolute value of a type by redirecting to std::abs.