Listing 2: Demonstrates using the matrix class

#include <iostream>
#include <iomanip>
#include <complex>

#include "matrix.h"
#include "poly.h"

using namespace std;

template<class T>
ostream& operator<<( ostream& o, const matrix<T>& M )
{
   o << endl;
   for(int i=0;i<M.rows;i++) {
     o << "[ ";
     for(int j=0;j<M.cols;j++)
        o << M(i,j) << " ";
     o << "]" << endl;
   }
   o << endl;
   return o;
}

//not shown: test1, test2, test3 -- mb
//...

void test4()
{
   cout << endl;
   cout << " solve a system of polynomial equations:" << endl
        << endl
        << "   (x + 1)X   -   (x-1) X     =   4x " << endl
        << "           1             2" << endl
        << "   (x)X       -   (x-1) X     =   3x - 1" << endl
        << "       1                 2" << endl
        << endl
        << "  the solution should be:" << endl
        << "   X   = x+1   X   = x-1" << endl
        << "    1           2 " << endl
        << endl;

   matrix< polynomial<double> > C(2,2);
   matrix< polynomial<double> > N(2,1);

   // x+1
   C(0,0)[1] = 1;
   C(0,0)[0] = 1;

   // -(x-1)
   C(0,1)[1] = -1;
   C(0,1)[0] = 1;

   //  4x
   N(0,0)[1] = 4;

   //  x
   C(1,0)[1] = 1;

   //  -(x-1)
   C(1,1)[1] = -1;
   C(1,1)[0] = 1;

   //  3x - 1
   N(1,0)[1] = 3;
   N(1,0)[0] = -1;

   cout << "C = " << C;
   cout << "N = " << N;
   // solve
   cout << "N.leftdiv(C) = " << N.leftdiv(C);
}

//not shown: test5 -- mb
//...

void run( void (*f)() )
{
   try {
      f();
   } catch(exception x) {
      cout << x.what() << endl;
   }
}

int main(void)
{
   run(test1);
   run(test2);
   run(test3);
   run(test4);
   run(test5);
   return 0;
}
//End of File