Introduction to SourcePro : Introduction to SourcePro® : SourcePro Analysis : An Example Using the Linear Algebra and Essential Tools Modules
An Example Using the Linear Algebra and Essential Tools Modules
Classes of the Business Analysis Module can be used with other SourcePro 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<RWMathVec<double> >, 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 RWTPtrDlist::iterator from the Essential Tools Module, and the solutions are printed.
Example 5 – Using the Linear Algebra, Essential Math, and Essential Tools Modules
 
// 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>
 
// From the C++ Standard Library
#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
RWTPtrDlist<RWMathVec<double> >::iterator iter =
blist.begin();
 
// For all vectors in list, solve equation
// using LU decomposition prepared before
for (int i = 1; iter != blist.end(); ++iter, ++i) {
cout << "Solution # " << i << " is x = "
<< LU.solve(**iter) << 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
]