next up previous contents
Next: Mathematical Functions Up: Slate++ Version 1.2 TUTORIAL Previous: Setting Up Slate Vectors   Contents

Subsections

Basic Operations

Addition


#include <slate.h>

int main() {

  //
  // Vectors and Matrices with the same dimensions and type can be added
  // together.
  //
  Matrix<double> a(1,2,1,2,19.0);
  Matrix<double> b(3,4,1,2,10.0);
  Matrix<double> c(2,2);

  c = a + b;
  cout << c << endl;

  Vector<int> d(3,9,4);
  Vector<int> e(1,7,2);
  Vector<int> f;

  f = d + e;
  cout << f << endl;

  //
  // Scalar elements can be added to Vectors and Matrices.
  //
  c = c + 14.0;
  cout << c << endl;

  c = a + 12.0 + b + 1.0 + c;
  cout << c << endl;

  f = d + 7;
  cout << f << endl;

  f = 9 + d + 24;
  cout << f << endl;
}

Subtraction


#include <slate.h>

int main() {

  //
  // Vectors and Matrices with the same dimensions and type can be
  // substracted from one another.
  //
  Matrix<double> a(1,2,1,2,19.0);
  Matrix<double> b(3,4,1,2,10.0);
  Matrix<double> c(2,2);

  c = a - b;
  cout << c << endl;

  Vector<int> d(3,9,4);
  Vector<int> e(1,7,2);
  Vector<int> f;

  f = d - e;
  cout << f << endl;

  //
  // Scalar elements can be subtracted from Vectors and Matrices. 
  //
  c = c - 14.0;
  cout << c << endl;

  c = 2.0 - a - 12.0 - b - 1.0 - c;
  cout << c << endl;

  f = d - 7;
  cout << f << endl;

  f = 9 - d - 24;
  cout << f << endl;

  //
  // The unary operator ('-') can also be used to negate a Vector or Matrix.
  //
  cout << -f << endl;
  cout << -c << endl;
}

Multiplication


#include <slate.h>

int main() {

  //
  // Vectors and Matrices with the same dimensions and type can be
  // multiplied element-by-element.
  //
  Matrix<double> a(1,2,1,2,19.0);
  Matrix<double> b(3,4,1,2,10.0);
  Matrix<double> c(2,2);

  c = elem_mul(a,b);
  cout << c << endl;

  Vector<int> d(3,9,4);
  Vector<int> e(1,7,2);
  Vector<int> f;

  f = elem_mul(d,e);
  cout << f << endl;

  //
  // Scalar elements can be multiplied Vectors and Matrices. 
  //
  c = c * 0.1;
  cout << c << endl;

  c = 2.0 * a * 2.0;
  cout << c << endl;

  f = d * 7;
  cout << f << endl;

  f = 9 * d * 3;
  cout << f << endl;

  //
  // Matrix multiplication between conformant Matrices.  The result
  // is another Matrix.
  //
  Matrix<int> p(1,3,1,3,2);
  Matrix<int> q(1,3,1,3,3);
  p(1,1) = 4;
  q(3,3) = 5;
  cout << p * q << endl;
  cout << q * p << endl;
  cout << q * p * p * p << endl;

  //
  // Matrix multiplication between conformant Matrices and Vectors.  The
  // result is another Vector.
  //
  Vector<int> r(1,3,10);
  r(3) = 5;
  p(1,3) = 3;
  p(3,1) = 1;
  cout << p << endl;
  cout << r << endl;
  cout << p * r << endl;
  cout << r * p << endl;

  //
  // Dot product between two Vectors.
  //
  cout << dot(r,r) << endl;
}

Division


#include <slate.h>

int main() {

  //
  // Vectors and Matrices with the same dimensions and type can be
  // divided by one another element-by-element.
  //
  Matrix<double> a(1,2,1,2,19.0);
  Matrix<double> b(3,4,1,2,10.0);
  Matrix<double> c(2,2);

  c = a / b;
  cout << c << endl;

  Vector<double> d(3,9,4.0);
  Vector<double> e(1,7,2.0);
  Vector<double> f;

  f = d / e;
  cout << f << endl;

  //
  // Scalar elements can be divided by Vectors and Matrices, and
  // vice versa.  This is an element-by-element division.
  //
  c = c / 14.0;
  cout << c << endl;

  c = 2.0 / a / 12.0 / b / 1.0 / c;
  cout << c << endl;

  f = d / 7.0;
  cout << f << endl;

  f = 9.0 / d / 24.0;
  cout << f << endl;
}

Comparisons


#include <slate.h>

int main() {

  //
  // Vectors and Matrices with the same dimensions and type can compared
  // element-by-element.
  //
  Matrix<double> a(1,2,1,2,19.0);
  Matrix<double> b(3,4,1,2,10.0);
  a(1,1) = 10.0;

  cout << (a < b) << endl;    // TRUE only if *all* elements of a < b
  cout << (a <= b) << endl;
  cout << (a == b) << endl;
  cout << (a >= b) << endl;
  cout << (a > b) << endl;

  //
  // Comparisons can be made with scalars.
  //
  cout << (a < 10.0) << endl;
  cout << (a <= 10.0) << endl;
  cout << (a == 10.0) << endl;
  cout << (a >= 10.0) << endl;
  cout << (a < 10.0) << endl;

  cout << (10.0 < a) << endl;
  cout << (10.0 <= a) << endl;
  cout << (10.0 == a) << endl;
  cout << (10.0 >= a) << endl;
  cout << (10.0 > a) << endl;
}

Complex Values


#include <slate.h>

int main() {

  //
  // Complex values can be used, drawing from C's <complex> classes.
  //
  Matrix< complex<double> > a(1,2,1,2,complex<double>(19.0,0));
  Matrix< complex<double> > b(3,4,1,2,complex<double>(10.0,0));
  a(1,1) = complex<double> (0.0,3.0);

  cout << a << endl;

  //
  // All arithmetic operations defined over complex numbers can be
  // performed the same as any other type.
  //
  cout << a + a << endl;
  cout << complex<double>(3.0,4.0) * a << endl;
}

Changing Type


#include <slate.h>

int main() {

  //
  // New Vectors and Matrices with different types can be formed. 
  // Suppose we have a double Matrix which we want to float.  We create
  // a new float Matrix as follows:
  //
  Matrix<double> a(1,2,1,2,19.0);
  Matrix<float> b;
  b = a.convert(b);
  cout << b << endl;

  Vector<double> c(1,5,12.5);
  Vector<int> d;
  d = c.convert(d);
  cout << d << endl;
}



Brian Thorndyke 2003-11-15