next up previous contents index
Next: Working with sub-arrays and Up: Module TArray Previous: Using arrays   Contents   Index

Matrices and vectors

\begin{figure}
\framebox [40mm][c]{\mbox{\rule[-1mm]{0mm}{5mm} \bf TArray$<$T$>...
...[40mm][c]{\mbox{\rule[-1mm]{0mm}{5mm} \bf TVector$<$T$>$} }\\ [2mm]
\end{figure}

Vectors and matrices are 2 dimensional arrays. The array size along one dimension is equal 1 for vectors. Column vectors have NCols() = 1 and row vectors have NRows() = 1. Mathematical expressions involving matrices and vectors can easily be translated into C++ code using TMatrix and TVector objects. Matrix and Vector are typedefs for double precision float matrices and vectors. The operator * beteween matrices is redefined to perform matrix multiplication. One can then write:

  // We create a row vector
  Vector v(1000, BaseArray::RowVector);
  // Initialize values with a random sequence
  v = RandomSequence();
  // Compute the vector length (norm)
  double norm = (v*v.Transpose()).toScalar();
  cout << "Norm(v) = " << norm << endl;

This module contains basic array and matrix operations such as the Gauss matrix inversion algorithm which can be used to solve linear systems, as illustrated by the example below:


#include "sopemtx.h"
// ...
// Creation of a random 5x5 matrix
Matrix A(5,5);
A = RandomSequence(RandomSequence::Flat);
Vector X0(5);
X0 = RandomSequence(RandomSequence::Gaussian);
// Computing B = A*X0
Vector B = A*X0;
// Solving the system A*X = B 
Vector X;
LinSolve(A, B, X);
// Checking the result
Vector diff = X-X0;
cout << "X-X0= " << diff ;
double min,max;
diff.MinMax(min, max);
cout << " Min(X-X0) = " << min << " Max(X-X0) = " << max << endl;



Reza Ansari
2001-03-07