Algorithmic Solutions > LEDA > LEDA Guide > Number Types > Double Valued Vectors and Matrices > Example


Example Double Valued Vectors and Matrices

The following example shows the usage of double valued vectors and matrices. In the first part two vectors are defined and two operations for vectors are illlustrated. The second part shows matrices and some of their operations.

#include <LEDA/numbers/vector.h>
#include <LEDA/numbers/matrix.h>
#include <LEDA/core/random_source.h>

using namespace leda;

int main() 
{
  //double valued vectors

  vector v1(3),v2(3);  //define two 3d-zero-vectors;

  v1=vector(1,2,3);   
  v2=vector(3,4,5);

  v1=v1*3;   //componentwise multiplication with 3

  double x=v1*v2;   //scalar multiplication

  std::cout << "vector v1="; v1.print();std::cout << std::endl;
  std::cout << "vector v2="; v2.print();std::cout << std::endl;
  std::cout << "product v1*v2=" << x << std::endl;
  
  //-------------------------------------------------------
 
  //double valued matrices

  //generation of 9 random double values
  double values[9];
  random_source S;
  for (int i=0;i<9;i++) S >> values[i];
  
  //define 3x3-matrix with random values
  matrix A(3,3,values);  

  std::cout << "\nA=";A.print();  //print A
  std::cout << "\nA^T=";A.trans().print();       
                           //compute and print transposed of A
  std::cout << "\ndeterminant of A: " << A.det() << std::endl;
                           //compute and output determinant of A

  //define 3x6-zero-matrix and assign values
  matrix B(3,6);
  for (int i=0;i<3;i++) for (int j=0;j<6;j++) B(i,j)=i-j-1;
  std::cout << "\nB=";B.print();  //print B

  matrix C=A*3; //multiplication of matrix with double
  std::cout << "\nC=A*3: ";C.print();
  v2=A*v1;       //multiplication of matrix with vector
  std::cout << "\nvector v2=A*v1: "; v2.print();
  std::cout << std::endl;
  C=A*B;        //multiplication of two matrices
  std::cout << "\nC=A*B: ";C.print();

  v2=A.solve(v1); //v1=A*v2
  std::cout << "\nvector v2=A.solve(v1): "; v2.print();
  std::cout << std::endl;

  return 0;
}	  

See also:

Vectors and Matrices with Integer Entries

Rational Vectors

Random Sources


Manual Entries:

Double Valued Vectors

Double Valued Matrices




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH