YANE-Framework Tutorial 1.1.0

examples/linearequation/src/example_lgs_largescale.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  * Copyright (C) 2011 by www.nonlinearmpc.com                              *
00004  *                                                                         *
00005  * Authors:                                                                *
00006  *  Juergen Pannek <juergen.pannek@googlemail.com>                         *
00007  *                                                                         *
00008  ***************************************************************************/
00009 
00010 #include <iostream>
00011 #include <iomanip>
00012 
00013 #include <yane/eqsys.h>
00014 
00015 #include "Problems/electricalnetwork.h"
00016 #include "Problems/demoproblem.h"
00017 
00018 using namespace std;
00019 using namespace yane::EqSys;
00020 
00021 int main ( void )
00022 {
00023         int N = 3, iter;
00024         double * x, *b, **A;
00025 
00026         //      Demoproblem * example = new Demoproblem;
00027         ElectricalNetwork * example = new ElectricalNetwork;
00028         example->initialize ( N );
00029         //      Cholesky * solver = new Cholesky;
00030         Gauss * solver = new Gauss;
00031         //      QRhouseholder * solver = new QRhouseholder;
00032         //      QRgivens * solver = new QRgivens;
00033         //      GaussSeidel * solver = new GaussSeidel;
00034         //      Jacobi * solver = new Jacobi;
00035         //      solver->setMaxIter ( 500 );
00036 
00037         try
00038         {
00039                 A = new double * [ example->dimensionRows ( ) ];
00040                 for ( int i = 0; i < example->dimensionRows ( ); i++ )
00041                 {
00042                         A [ i ] = new double [ example->dimensionColumns ( ) ];
00043                 }
00044                 x = new double [ example->dimensionColumns ( ) ];
00045                 b = new double [ example->dimensionRows ( ) ];
00046         }
00047         catch ( std::bad_alloc )
00048         {
00049                 throw yane::Utils::MemoryException ( __DEBUGMESS (
00050                         "Allocation of x failed" ) );
00051         }
00052         example->getA ( A );
00053         example->getB ( b );
00054         cout << "Systemmatrix: A = " << endl;
00055         for ( int i = 0; i < example->dimensionRows ( ); i++ )
00056         {
00057                 for ( int j = 0; j < example->dimensionColumns ( ); j++ )
00058                 {
00059                         cout << fixed << setw ( 5 ) << setprecision ( 2 ) << A [ i ] [ j ]
00060                                 << " ";
00061                 }
00062                 cout << endl;
00063         }
00064         cout << endl << endl;
00065         cout << "Data values: b = " << endl;
00066         for ( int i = 0; i < example->dimensionRows ( ); i++ )
00067         {
00068                 cout << fixed << setw ( 5 ) << setprecision ( 2 ) << b [ i ] << " ";
00069         }
00070         cout << endl << endl;
00071 
00072         try
00073         {
00074                 solver->initializeLargescale ( A, example->dimensionRows ( ),
00075                         example->dimensionColumns ( ), b, example->dimensionB ( ) );
00076                 solver->solve ( x );
00077         }
00078         catch ( yane::Utils::Exception e )
00079         {
00080                 e.what ( );
00081         }
00082 
00083         cout << "Result: x = " << endl;
00084         for ( int i = 0; i < example->dimensionColumns ( ); i++ )
00085         {
00086                 cout << fixed << setw ( 5 ) << setprecision ( 2 ) << x [ i ] << " ";
00087         };
00088         cout << endl << endl;
00089 
00090         delete solver;
00091         delete example;
00092         delete [ ] x;
00093 
00094         return 0;
00095 }