Robotsystem from Robot Group Leipzig: example.cpp Source File

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# example.cpp

Go to the documentation of this file.

    00001 /* compile this with g++ -Wall -lm -L. -lmatrix_debug  -o example example.cpp */
    00002
    00003 #include <iostream>
    00004 #include <math.h>
    00005
    00006 #include <matrix.h>
    00007
    00008
    00009 using namespace matrix;
    00010
    00011
    00012 int main(){
    00013   ////////// matrix construction
    00014   //  all matrices are initialised with 0 elements
    00015   Matrix m0;    // 0-matrix (size is 0x0)
    00016   m0.set(5,1);  // change matrix to be a 5x1 matrix (column vector)
    00017   Matrix m1(3,3); // 3x3-matrix
    00018   double data[9] = {1., 0., 1.,  0.1, 0.4, 0.5,  -2., 0.1, 2.};
    00019   m1.set(3,3,data); // change matrix to be 3x3 matrix with initialised elements
    00020   Matrix m2(3,3,data); // 3x3 matrix with initialised elements
    00021
    00022   ////////// Accessing and printing
    00023   cout << m2.val(0,0) << endl; // Element with index 0,0
    00024   m2.val(2,1) = 3.4;           // assign value to element at 2,1
    00025   cout << m2.val(2,1) << endl; // Element with index 2,1
    00026
    00027   cout << m2;                  // show matrix
    00028   cout << m2.row(0);           // show first row which is row-vector
    00029   cout << m2.column(2);        // show third column which is column-vector
    00030
    00031   ////////// Operations
    00032   Matrix m3 = m2 + m2 - m2 * m2;         // addition subtraction and multiplication
    00033   cout << m3;
    00034   Matrix m4 = (m3^T) * (m3^-1) * (m3.multMT()); // transpose and invert and fast version of matrix*matrix^T
    00035                                                // (parentheses needed because ^ bind less than *)
    00036   Matrix m5 = m4*0.3;                    // multiplication with scalar (just right side)
    00037   Matrix m6 = m4.multrowwise(m3.column(0)); // multiply m4 rowwise,
    00038                                             // each row with the appropriate element of first column of m3
    00039
    00040   Matrix m7 = m6.map(sin);                  // apply sin function to all matrix elements
    00041   cout << m7;
    00042
    00043   return 0;
    00044 }