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#include <cstring>
6
7
8template<int Rows, int Cols, typename Scalar>
11 std::memcpy(
12 result._values,
13 values,
14 sizeof(Scalar)*Rows*Cols
15 );
16 return result;
17}
18
19
20template<int Rows, int Cols, typename Scalar>
24
25
26template<int Rows, int Cols, typename Scalar>
27tarch::la::Matrix<Rows,Cols,Scalar>::Matrix( const Scalar& initialValue ) {
28 for (int i=0; i < Rows*Cols; i++) {
29 _values[i] = initialValue;
30 }
31}
32
33
34
35template<int Rows, int Cols, typename Scalar>
36tarch::la::Matrix<Rows,Cols,Scalar>::Matrix( std::initializer_list<Scalar> values ) {
37 int index = 0;
38 for (typename std::initializer_list<Scalar>::const_iterator p = values.begin(); p!=values.end(); p++) {
39 _values[index] = *p;
40 index++;
41 }
42}
43
44
45template<int Rows, int Cols, typename Scalar>
47 return Rows;
48}
49
50
51template<int Rows, int Cols, typename Scalar>
53 return Cols;
54}
55
56
57template<int Rows, int Cols, typename Scalar>
59 return Rows * Cols;
60}
61
62
63template<int Rows, int Cols, typename Scalar>
65 std::ostringstream os;
66 os << "[";
67 for (int i=0; i < Rows; i++) {
68 os << "[";
69 for (int j=0; j < Cols; j++) {
70 os << _values[i * Cols + j];
71 if (j + 1 < Cols) {
72 os << ",";
73 }
74 }
75 os << "]";
76 if (i + 1 < Rows) {
77 os << ",";
78 }
79 }
80 os << "]";
81 return os.str();
82}
83
84
85template<int Rows, int Cols, typename Scalar>
86std::string tarch::la::Matrix<Rows,Cols,Scalar>::toPrettyString(int numberOfDigits) const {
87 std::ostringstream os;
88 os << "[";
89 os << std::setiosflags(std::ios::fixed) << std::setprecision(numberOfDigits);
90 for (int i=0; i < Rows; i++) {
91 os << std::endl;
92 for (int j=0; j < Cols; j++) {
93 if ( tarch::la::abs( _values[i * Cols + j] )>=0.0) {
94 os << " ";
95 }
96 os << _values[i * Cols + j];
97 if (j + 1 < Cols) {
98 os << ", ";
99 }
100 }
101 if (i + 1 < Rows) {
102 os << ",";
103 }
104 }
105 os << std::endl;
106 os << "]";
107 return os.str();
108}
109
110
111template<int Rows, int Cols, typename Scalar>
112template <typename NewScalarType>
115 for (int i=0; i < Rows; i++) {
116 for (int j=0; j < Cols; j++) {
117 result(j,i) = operator()(j,i);
118 }
119 }
120 return result;
121}
Static (i.e.
Definition Matrix.h:42
std::string toString() const
Definition Matrix.cpph:64
std::string toPrettyString(int numberOfDigits=4) const
Definition Matrix.cpph:86
Scalar _values[Rows *Cols]
Values of the matrix components.
Definition Matrix.h:47
int cols() const
Returns the number of columns in the matrix.
Definition Matrix.cpph:52
Matrix()
Constructs a non-initialized matrix.
Definition Matrix.cpph:21
Scalar & operator()(int rowIndex, int colIndex)
Returns element at given row and column index (from 0..size-1).
Definition Matrix.h:85
int size() const
Returns the number of total elements in the matrix.
Definition Matrix.cpph:58
tarch::la::Matrix< Rows, Cols, NewScalarType > convertScalar() const
Definition Matrix.cpph:113
int rows() const
Returns the number of rows in the matrix.
Definition Matrix.cpph:46
Matrix< Rows, Cols, Scalar > buildMatrix(const Scalar *values)
Deep copy builder function.
Vector< Size, Scalar > abs(const Vector< Size, Scalar > &vector)
Computes the absolute component values of the vector, creating a temporary vector to hold the result.