next up previous contents
Next: Interfacing Slate with External Up: Slate++ Version 1.2 TUTORIAL Previous: Mathematical Functions   Contents

Subsections

Linear Algebra

Transpose


#include <slate.h>

int main() {

  //
  // Take the tranpose of a Matrix.
  //
  Matrix<double> a(1,2,1,3,3.4);
  a(1,2) = 5.9;
  cout << a << endl;
  cout << transpose(a) << endl;

  //
  // Also works for complex Matrices.
  //
  Matrix< complex<double> > c(1,2,1,3,complex<double>(2.0,1.0));
  c(2,1) = complex<double>(4.0,-1.0);

  cout << c << endl;
  cout << transpose(c) << endl;
}

Hermitian Conjugate


#include <slate.h>

int main() {

  //
  // Take the Hermitian conjugate of the Matrix.  This is only sensible
  // for complex Matrices.
  //
  Matrix< complex<double> > c(1,2,1,3,complex<double>(2.0,1.0));
  c(2,1) = complex<double>(4.0,-1.0);

  cout << c << endl;
  cout << dag(c) << endl;

}

Norms


#include <slate.h>

int main() {

  ////////////////////////////////////////////////////////////////////////////
  // 
  // Matrix
  //

  //
  // Frobenius norm.
  //
  Matrix<double> a(1,3,1,3,3.4);
  a(1,1) = 5.9;
  a(3,3) = 18.0;
  cout << a << endl;
  cout << normFro(a) << endl;
  
  //
  // Matrix 1-norm.
  //
  cout << norm1(a) << endl;
  
  //
  // Matrix infinity-norm.
  //
  cout << normInf(a) << endl;
  
  //
  // Also works for complex Matrices.
  //
  Matrix< complex<double> > b(1,3,1,3,complex<double>(1.0,2.0));
  b(1,1) = complex<double>(0.0,-1.0);
  b(3,3) = complex<double>(5.0,5.0);
  cout << b << endl;
  
  cout << normFro(b) << endl;
  cout << norm1(b) << endl;
  cout << normInf(b) << endl;
  
  // ///////////////////////////////////////////////////////////////////////
  //
  // Vector
  //
  
  //
  // Vector 1-norm
  //
  Vector<double> c(1,4,-3.0);
  c(1) = 7.0;
  cout << c << endl;
  cout << norm1(c) << endl;

  //
  // Vector 2-norm
  //
  cout << norm2(c) << endl;

  //
  // Vector infinity-norm
  //
  cout << normInf(c) << endl;

  //
  // Works for complex Vectors as well.
  //
  Vector< complex<double> > d(1,4,complex<double>(2.0,-6.0));
  d(1) = complex<double>(9.0,10.0);

  cout << d << endl;
  cout << norm1(d) << endl;
  cout << norm2(d) << endl;
  cout << normInf(d) << endl;
}

Trace


#include <slate.h>

int main() {

  //
  // Take the trace of a Matrix.  Only sensible for square matrices.
  //
  Matrix<double> a(1,3,1,3,3.4);
  a(1,1) = 5.9;
  cout << a << endl;
  cout << trace(a) << endl;

  //
  // Also works for complex Matrices.
  //
  Matrix< complex<double> > c(1,3,1,3,complex<double>(2.0,1.0));
  c(2,2) = complex<double>(4.0,-1.0);

  cout << c << endl;
  cout << trace(c) << endl;
}

Determinant


#include <slate.h>

int main() {

  // 

  // Want to find the determinant of:
  //
  //    [  2   3   1    2 ]
  //    [ -2   4  -1    5 ]
  //    [  3   7  1/2   1 ]
  //    [  6   9   3    7 ]
  //

  // create the matrix.
  Matrix<double> m(4, 4);
  m(1, 1) = 2.0; m(1, 2) = 3.0; m(1, 3) = 1.0; m(1, 4) = 2.0;
  m(2, 1) = -2.0; m(2, 2) = 4.0; m(2, 3) = -1.0; m(2, 4) = 5.0;
  m(3, 1) = 3.0; m(3, 2) = 7.0; m(3, 3) = 0.5; m(3, 4) = 1.0;
  m(4, 1) = 6.0; m(4, 2) = 9.0; m(4, 3) = 3.0; m(4, 4) = 7.0;

  // find the determinant.
  double d = det(m);
  cout << d << endl;
}

Inverse


#include <slate.h>

int main() {

  // Want to find the inverse of the following:
  //
  //    [  2   0   1   2 ]
  //    [  1   1   0   2 ]
  //    [  2  -1   3   1 ]
  //    [  3  -1   4   3 ]
  //

  // create the matrix.
  Matrix<double> m(4, 4);
  m(1, 1) = 2; m(1, 2) = 0; m(1, 3) = 1; m(1, 4) = 2;
  m(2, 1) = 1; m(2, 2) = 1; m(2, 3) = 0; m(2, 4) = 2;
  m(3, 1) = 2; m(3, 2) = -1; m(3, 3) = 3; m(3, 4) = 1;
  m(4, 1) = 3; m(4, 2) = -1; m(4, 3) = 4; m(4, 4) = 3;

  try {
    Matrix<double> inv = inverse(m);
    cout << m * inv << endl;
  } catch (int error) {
    cout << "matrix is singular" << endl;
  }
}

System of Linear Equations


#include <slate.h>

int main() {

  // Want to solve the following:
  //
  //    [  1  -1   2  -1 ]     [  6 ]
  //    [  1   0  -1   1 ]     [  4 ]
  //    [  2   1   3  -4 ] x = [ -2 ]
  //    [  0  -1   1  -1 ]     [  5 ]
  //

  // create the matrix.
  Matrix<double> m(4, 4);
  m(1, 1) = 1; m(1, 2) = -1; m(1, 3) = 2; m(1, 4) = -1;
  m(2, 1) = 1; m(2, 2) = 0; m(2, 3) = -1; m(2, 4) = 1;
  m(3, 1) = 2; m(3, 2) = 1; m(3, 3) = 3; m(3, 4) = -4;
  m(4, 1) = 0; m(4, 2) = -1; m(4, 3) = 1; m(4, 4) = -1;

  // create the resultant vector.
  Vector<double> b(4);
  b(1) = 6;
  b(2) = 4;
  b(3) = -2;
  b(4) = 5;

  // solve for x.
  try {
    Vector<double> x = solve(m, b);
    cout << x << endl;
    cout << m * x << endl;
  } catch (int error) {
    cout << "matrix is singular" << endl;
  }
}



Brian Thorndyke 2003-11-15