cinand returns the unique solution vector x with Ax = b to
cout. If no such unique vector exists, a message is printed. NEW If you are not familiar with Gaussian elimination, you can program the following alternative functionality: Your C++ program reads dimensions m and n and a sparse coefficient matrix A and a sparse vector b from
cinand returns the the sparse vector y with y = Ab to
cout. Sparse vectors for both alternatives are sparse matrices with n=1.
The public interface and data structure for the spares matrix is
The header file contains already written members for I/O so
that you can test your program easily. A sample file containing
the input to the sparse matrix A is
Note that the template class needs a Field. We assume
the Field type has constructors from
ints, copy constructors,
the operators +, -, *, /, =, ==
operator<< are appropriately overloaded
to provide I/O.
Please submit a file
and several test programs.
modular.cppcontains the class
z_pwhich implements a mathematical field modulo a 15-bit prime residue. Your main function should read in the prime first and place it in a static data member of the class.
doublefor the mathematical field. There is a problem with the pivot search in
sparsemat<double>::linsolveas very small entries could be used as pivot elements. This is addressed next.
fuzzy.cppwraps the type
==returns true if the arguments are within a preset ``fuzz'' value, which is a small static member of type
put_value. You may wish to look, or try your solution first.
Instantiating the template
for many mathematical fields may cause code bloat, because
each of the member functions gets compiled for each of the
mathematical fields. The task here is to compute with all of three
mathematical fields discussed above while
using a single class
field_base is an abstract base class
for the derived classes that implement the double, modular,
and fuzzy arithmetic. All member functions used in
There is a problem with the constructors, as they cannot be virtual.
clone(), which is default the copy method,
can be declared
abstract, i.e., virtual).
operator returns a copy
of the mathematical field element in the matrix.
In addition, STL's
make_pair template function
uses the copy constructor for initializing the pairs data members.
However, the copy constructor of
cannot return a
clone of the element, that is
a copy of the derived class object, because no constructors can be
A solution to this problem is to use a separate class
abstract_field as the abstract class and
An instance of the
abstract_field class contains
a pointer to the
field_base object, which actually
must be derived class object. Now all constructors and the
can be written in terms of the virtual
clone, etc. of
The header file
is a prototype for such a design. Note that the static
default_elem_ptr gets initialized
by the function
point to a derived object before each of the derived classes is used.
Note: you should not have to change anything in your
template class. The class
abstract_field can be
thought of as a ``wrapped'' pointer type.
Please submit a file
contains the missing member function definitions, the derived
class declarations and their member definitions and
a main program that exercises all derived classes.