User Tools

Site Tools


tools:matrix:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
tools:matrix:start [2017/03/17 12:28] – [Accessing Elements] graftools:matrix:start [2022/04/20 12:45] – [Vectors and Matrices] ursgraf
Line 2: Line 2:
 The blocks in the control system are connected among each other with the aid of signals. As described in [[eeros_architecture:control_system:signals|]] they carry a timestamp and a value. The value could be a simple type such as an integer or float. Quite often it will consist of a vector or a matrix. A three dimensional vector of type ''double'' can be declared and initialized as follows: The blocks in the control system are connected among each other with the aid of signals. As described in [[eeros_architecture:control_system:signals|]] they carry a timestamp and a value. The value could be a simple type such as an integer or float. Quite often it will consist of a vector or a matrix. A three dimensional vector of type ''double'' can be declared and initialized as follows:
 <code cpp> <code cpp>
-  Vector<3, double> v;  +Vector<3, double> v; 
-  v << 1.5, -2, 0;+
 </code> </code>
 The declaration could be simplified to  The declaration could be simplified to 
 <code cpp> <code cpp>
-  Vector3<> v;  +Vector3<> v; 
-  v << 1.5, -2, 0;+
 </code> </code>
-as ''Vector3'' is a predefined type with three dimensions and the default element type is ''double''. A matrix of 3 times 3 with element type ''int'' could be defined as+as ''Vector3'' is a predefined type with three dimensions and the default element type is ''double''. A ''Vector3'' is identical to the type ''Matrix<3,1,double>''. The vector could be initialized as follows
 <code cpp> <code cpp>
-  Matrix<33int> m;  +v = {1.5-20} 
-  << 1, 47, +<< 1.5-20; // or with input operator 
-       2, 5, 8, +Vector3 v{1,2,3}// or directly upon declaration
-       3, 6, 9;+
 </code> </code>
 +A matrix of 3 times 3 with element type ''int'' could be defined as
 +<code cpp>
 +Matrix<3, 3, int> m; 
 +m << 1, 2, 3,
 +     4, 5, 6,
 +     7, 8, 9;
 +</code>
 +The first three numbers will be filled into the first row. While the internal representation is simply a one dimensional vector, the matrix could be visualized as 
  
 +| ^col0^col1^col2^
 +^row0|1|2|3|
 +^row1|4|5|6|
 +^row2|7|8|9|
 +When declaring and initializing a matrix, the values must be given colon per colon. The same matrix as given above would be initialized as follows 
 +<code cpp>
 +Matrix<3, 3, int> m{1, 4, 7, 2, 5, 8, 3, 6, 9};
 +</code>  
 ===== Accessing Elements ===== ===== Accessing Elements =====
 You can access rows, columns or single elements of matrices with the following methods: You can access rows, columns or single elements of matrices with the following methods:
 <code cpp> <code cpp>
-Matrix<3,2> m{+m.get(0,0);  // returns element, 1 
 +m.get(1,0);  // returns element, 4 
 +m.getRow(1);  // returns row, [4,5,6] 
 +m.getCol(0);  // returns column, [1,4,7]' 
 +m.getSubMatrix<2,2>(0,1);   // returns matrix [[2,5][3,6]] 
 +</code> 
 +Single elements, rows or columns can be written with the methods //set()//, //setRow()//, or //setCol()//.  
 + 
 +The operators () and [] work as well as can be seen by the following example 
 +<code cpp> 
 +m(3);   // returns element or sets element with index 3  
 +m[3];   // returns element or sets element with index 3  
 +m(2,1);   // returns element or sets element with index 7 
 +m[2,1];   // does not work, as the [] operator cannot be defined for two parameters 
 +</code> 
 + 
 +===== Logging Matrices ===== 
 +A matrix can be logged simply by writing 
 + 
 +<code cpp> 
 +log.info() << m
 +</code> 
 +With the 3 x 3 matrix from above we would get 
 +<code> 
 +2021-09-16 17:36:53:929    I:  [ [1 4 7]' [2 5 8]' [3 6 9]' ] 
 +</code> 
 +The matrix is plotted colon per colon. To give some more examples: 
 +<code cpp> 
 +Matrix<1,1> m1{1}; 
 +Matrix<2,1> m2{1,2}; 
 +Matrix<1,2> m3{1,2}; 
 +</code> 
 +This matrices will be printed as  
 +<code> 
 +2021-09-16 17:37:18:122    I:  [1]' 
 +2021-09-16 17:37:18:122    I:  [1 2]' 
 +2021-09-16 17:37:18:122    I:  [ [1]' [2]' ]' 
 +</code> 
 + 
 +===== Matrix Operations ===== 
 +Some examples show basic matrix operations. 
 +<code cpp> 
 +Vector2 v1{1,2}; 
 +Matrix<1,2> v2{3,4}; 
 +log.info() << v1 * v2;            // will print [ [3 6]' [4 8]' ] 
 +log.info() << v2 * v1;            // will print [11]' 
 + 
 +Matrix<2,2,double> m1{1,1.5,-1,2}; 
 +log.info() << v2 * m1;            // will print [ [9]' [5]' ] 
 +log.info() << v1.transpose() * m1;// will print [ [4]' [3]' ] 
 + 
 +auto x = v2 * m1; 
 +Vector2 v4 = x.transpose(); 
 +log.info() << v4;                 // will print [9 5]' 
 +</code>
tools/matrix/start.txt · Last modified: 2022/04/20 13:29 by ursgraf