YANE-Framework Tutorial 1.1.0

# examples/linearequation/src/example_lgs.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, *A_vector;
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                 A_vector = new double [ example->dimensionRows ( )
00047                         * example->dimensionColumns ( ) ];
00048         }
00050         {
00051                 throw yane::Utils::MemoryException ( __DEBUGMESS (
00052                         "Memory allocation failed" ) );
00053         }
00054         example->getA ( A );
00055         example->getB ( b );
00056         cout << "Systemmatrix: A = " << endl;
00057         for ( int i = 0; i < example->dimensionRows ( ); i++ )
00058         {
00059                 for ( int j = 0; j < example->dimensionColumns ( ); j++ )
00060                 {
00061                         cout << fixed << setw ( 5 ) << setprecision ( 2 ) << A [ i ] [ j ]
00062                                 << " ";
00063                 }
00064                 cout << endl;
00065         }
00066         cout << endl << endl;
00067         cout << "Data values: b = " << endl;
00068         for ( int i = 0; i < example->dimensionRows ( ); i++ )
00069         {
00070                 cout << fixed << setw ( 5 ) << setprecision ( 2 ) << b [ i ] << " ";
00071         }
00072         cout << endl << endl;
00073         for ( int i = 0; i < example->dimensionRows ( ); i++ )
00074         {
00075                 memcpy ( & A_vector [ i * example->dimensionColumns ( ) ], A [ i ],
00076                         sizeof(double) * example->dimensionColumns ( ) );
00077         }
00078
00079         try
00080         {
00081                 solver->initialize ( A_vector, example->dimensionRows ( ),
00082                         example->dimensionColumns ( ), b, example->dimensionB ( ) );
00083                 solver->solve ( x );
00084         }
00085         catch ( yane::Utils::Exception e )
00086         {
00087                 e.what ( );
00088         }
00089
00090         cout << "Result: x = " << endl;
00091         for ( int i = 0; i < example->dimensionColumns ( ); i++ )
00092         {
00093                 cout << fixed << setw ( 5 ) << setprecision ( 2 ) << x [ i ] << " ";
00094         };
00095         cout << endl << endl;
00096
00097         delete solver;
00098         delete example;
00099         delete [ ] x;
00100
00101         return 0;
00102 }
```