The Basic Mathematical Formats and Operations

Vectors and Matrices

In order to create a neural network, vectorial and matricial albegra are of fundamental importance. This way it was essential to create a series of classes adapted to these calculations.

There are two base template classes for algebraic calculations: VectorData and MatrixData. As classes names show, these classes organize information in unidimentional (VectorData) and bi-dimentional matrices (MatrixData). These templates can be used anywhere in the library, but essentially they form the base classes from which others derive. RealVec and RealMat are classes derived from these templates.

The class RealVec represents a vector of real numbers, and contains several operators and methods for vectorial calculations. These include:

  1. addition, subtraction, multiplication, division operations between a scalar and a vector;
  2. addition, subtraction, multiplication, division operations between two vectors (element by element);
  3. several other mathematical functions and operations that can be applied to the vector elements, such as exponential, dot-product, logarithm, inversion, absolute values, mean, mean square error, among others.

RealMat represents a bi-dimentional matrix of real numbers, that also contains several operators and methods for matricial calculations. These include:

  1. addition, subtraction, multiplication, division operations between a scalar and a matrix;
  2. addition, subtraction, multiplication, division operations between a two matrices (element by element);
  3. operations between a vector and a matrix;
  4. operations between matrices;
  5. several other mathematical functions and operations that can be applied to the matrix elements, such as exponential, scaling, inversion, normalization, among others.

These classes are strongly interrelated. For instance the indexation operator of a RealMat returns a RealVec containing the desired ith line. This allows the user to use RealVec operations in the lines of RealMat objects, as exemplified in the code below.

#include "types.h"
RealVec aRealVec;                           // Creates a vector of dimension 0 (empty vector)
aRealVec << 0.2 << -1.2 << 2.2 << 0.8;      // Adds 4 new elements to the vector
aRealVec.size();                            // The vector dimension is now 4
aRealVec[2] = +3.4;                         // Modifies the third element of the vector
RealMat mat( 5, 4 );                        // Creates a matrix with 5 rows and 4 columns
mat[3].assign( aRealVec );                  // Assign aRealVec to the matrix 4th row 
mat[3].exp();                               // Calculates the exponential values for all elements of the 4th row

// mat[3].exp() is equivalent to the following calculation (which is performed element-by-element)
for( int i=0; i < mat.cols(); i++ ) {
    mat[3][i] = exp( mat[3][i] );
}
BerliOS Developer Logo Valid XHTML 1.0 Transitional Valid CSS!