Skip to content
ForceBru edited this page Mar 29, 2016 · 9 revisions

#How to use Matrix in your project

You can use this code template and then insert the code in the examples to see how it works

#include <iostream>
#include "Matrix.hpp"

using namespace std;

int main (void) {
  Matrix::Init() // initialize Matrix (do this only once)
  
  // code in the examples goes here...

  return 0;
}

After replacing // code in the examples goes here... with real code save this file as, for example, Matrix_Code.cpp and compile it like this: g++ -I Matrix Matrix_Code.cpp -o Matrix_Code, where Matrix is the path where you have the header files which you obtained after cloning this repo.

##Constructing Matrix

There are several ways to initialize a Matrix object.

  • With no parameters

    Matrix a;
  • Specifying shape

    Matrix a(2, 3); // 2 rows and 3 columns
  • Initializing with data contained in a file

    Matrix a("data.txt"); // provide filename (std::string only)
  • Filling with data contained in a vector or a vector of vectors

    vector<double> row = {1, 2, 3, 4, 5};
    vector< vector<double> > data = {
       {1, 2},
       {2, 3},
       {3, 4}
    };
    
    Matrix a(row), b(data); // note that the shape will be calculated automatically

You can also construct an empty matrix and then fill it using the methods described in "Populating matrices with data".

The most complete example is Matrix_test/Tests.cpp. Here is a list of all mathematical operations currently available and examples of usage.

  • addition and substraction
  • matrix + matrix and matrix - matrix
Matrix a(2, 3), b(2, 3), c;

a.Random(0, 1); // random values in range [0, 1]
b.Zeros();

c = a + b;

if (c != a) std::cout << "Something went wrong, please raise an issue!";
else std::cout << "Test passed";

c -= a;

if (c != b) std::cout << "Something went wrong, please raise an issue!";
else std::cout << "Test passed";
  • matrix + number and matrix - number (element-wise and interchangeable)
Matrix a(2, 3);

a.Zeros();

a += 6.2;

if (a[0][0] != 6.2) std::cout << "Something went wrong, please raise an issue!";
else std::cout << "Test passed";
  • multiplication
  • matrix * matrix and matrix * number (last case interchangeable)
Matrix a(2, 3), b(3, 4), c(2, 4);

a.Random();
b.Random();

c = a * b;

cout << c * 3.1 << endl;
  • division
  • number / matrix and matrix / number only (element-wise)
  • raising to power of two
Matrix a(2, 2); // square matrices only

cout << sqr(a) << endl;
  • matrix exponentiation (element-wise)
Matrix a(3, 5);

cout << exp(a) << endl;
  • negation
Matrix a(2, 4);

a.Random();

  //too easy
cout << -a << endl;

##Vectorizing functions It's very easy to vectorize a function with Matrix:

#include <iostream>
#include "Matrix.hpp"

Matrix sigmoid(Matrix& z) {
    return 1.0 / (1.0 + exp(-z));
}

int main (void) {
    Matrix N(3, 5);
    
    N.Random(0, 1);
    
    Matrix result = sigmoid(N);
    
    cout << result.Prettify() << endl;
    
    return 0;
}

##Populating matrices with data

  • with random values, ones or zeros
Matrix a(2, 3), b(3, 4), c(4, 5);

a.Random(0, 1);
b.Ones();
c.Zeros();
  • with values from a file
Matrix a;  // shape will be calculated automatically
           // according to data in the file
a.FromFile("matrix_a.txt");
  • with values from a vector
vector<double> row = {1, 2, 3, 4, 5};
 vector< vector<double> > data = {
    {1, 2},
    {2, 3},
    {3, 4}
 };

 Matrix a, b; 
 a.FromData(row); // note that the shape will be calculated automatically
 b.FromData(data);

###Matrix file format Input data row by row and column by column. Separate numbers with a whitespace, rows with new lines.

Example:

0.234 0.456 0.678
0.123 0.345 0.567

This will be converted into a 2X3 matrix.

###Outputting matrices If you want a matrix be shown to the user, they probably might not want to see numbers with a lot of precision. You may find it useful to use a function called Prettify to make your matrices look nice to the users.

Matrix a(2, 3);

a.Random(0, 2);

cout << a.Prettify() << ends; // output matrices with three digits of precision

But if you need to save a matrix to a file doing this will lose (quite a bit of) precision. You'd better output it without using Prettify then.

##Getting properties of matrices

  • Rows and columns count
    • Rows() - get the number of rows in a matrix
    • Cols() - returns the number of columns in a matrix
  • Determine the type of a matrix
    • IsVect() - returns true if Matrix is a vector (one-row matrix) and false otherwise
    • IsCol() - returns true if Matrix represents a column (one-column matrix) and false otherwise
    • IsNum() - returns true when a matrix contains only one number and false otherwise
    • IsSquare(unsigned n) - check whether a matrix is square (pass the number of rows as a parameter)

Clone this wiki locally