Classes of the Business Analysis Module can be used with other SourcePro C++ modules to solve specialized problems in business and research. The following example uses classes of both the Linear Algebra Module and the Essential Tools Module. In this example, a number of systems of equations are solved by means of a single LU decomposition. The collection classes of the Essential Tools Module are useful for managing these huge amounts of data.
First, the program reads a matrix from the standard input stream. The symmetric matrix RWSymMat<double> is decomposed, if possible, by creating an RWSymFact<double> object. The program then creates an Essential Tools Module list object, RWTPtrDlist<DoubleVec>, which holds the vectors for solving the system. The list is filled until the end of file is reached; vectors are stored by pointers. Finally, the systems are solved using the list iterator class, RWTPtrDlistIterator from the Essential Tools Module, and the solutions are printed.
The following example was built and tested using Solaris 8 / Forte 6, update 2, 32-bits (both static and dynamic builds). Depending on your environment and the options chosen when you build your SourcePro libraries, minor syntactical changes may be needed to build this example.
// This example uses classes from three modules: Linear Algebra, // Essential Math, and Essential Tools. It declares a matrix A, // reads the collection of vectors b, and solves the equation Ax=b // for every vector in the collection. // From the Linear Algebra Module #include <rw/lapack/symmat.h> #include <rw/lapack/symfct.h> // From the Essential Math Module #include <rw/math/mathvec.h> // From the Essential Tools Module #include <rw/tpdlist.h> // Include header for iostreams #include <iostream> using std::cout; using std::cin; using std::endl; int main() { // Declare a matrix of doubles RWSymMat<double> A; cout << "Please input the size of the symmetric matrix\n" << "followed by its contents\n" << "in the form 3x3 [4 5 7 7 9 5 2 3 6]." << endl; // Read matrix from input stream cin >> A; // Construct and check factorization RWSymFact<double> LU(A); if (LU.fail()) { cout << "Could not solve system, " << "A is probably singular" << endl; return 1; } // Declare a pointer to vector of doubles RWMathVec<double> *ptr; // Declare list of pointers to vectors of doubles RWTPtrDlist<RWMathVec<double> > blist; cout << "Please input the vectors to be multiplied\n" << "followed by EOF.\n" << "Use the form [9 5 2]." << endl; // Read all vectors from input stream until EOF while ( true ) { // Allocate memory for vector ptr = new RWMathVec<double>(); // Read vector cin >> *ptr; // Check if EOF read if( cin.eof() ) { // If yes, stop iterating delete ptr; break; } // Insert vector to list blist.insert(ptr); } // Declare iterator for list of vectors RWTPtrDlistIterator<RWMathVec<double> > itr(blist); // For all vectors in list, solve equation // using LU decomposition prepared before for( int i = 1; itr(); i++ ) { cout << "Solution # "<< i <<" is x = " << LU.solve(*itr.key()) << endl; } // Don't forget to clean up all elements in list! blist.clearAndDestroy(); return 0; } |
Program output:
Please input the size of the symmetric matrix followed by its contents in the form 3x3 [4 5 7 7 9 5 2 3 6]. 3x3 [4 5 7 7 9 5 2 3 6] Please input the vectors to be multiplied followed by EOF. Use the form [9 5 2]. [9 5 2] ˆZ Solution # 1 is x = [ -3.5 3.33333 -0.166667 ] |
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.