• Keine Ergebnisse gefunden

“Object-Oriented Programming for Scientific Computing”

N/A
N/A
Protected

Academic year: 2021

Aktie "“Object-Oriented Programming for Scientific Computing”"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

IWR, Heidelberg University Summer Semester 2015

Exercise Sheet 7 26. 05. 2015

Exercises for the Lecture Series

“Object-Oriented Programming for Scientific Computing”

Ole Klein

ole.klein@iwr.uni-heidelberg.de

To be handed in on 02. 06. 2015 before the lecture

EXERCISE1 MATRICES ANDTEMPLATES

In the lecture templates were presented as a technique of generic programming. They allow the de- velopment of algorithms regardless of underlying data structures.

In this exercise we will extend an existing implementation of a matrix class to a template class. Ma- trices are needed again and again in numerical software. In the lecture, an implementation for the number typedouble has already been presented, and on the lecture homepage you will find the class MatrixClass. Depending on the application you want, however, it may be useful to have matrices based on other types of numbers, for examplecomplex,floatorint.

Templates may reduce code redundancy, so that one has an implementation ofMatrixClassthat can be used for all the different types.

MatrixClass fordouble— Basic features:

• Matrix entries are stored as a vector of vectors:

std::vector<std::vector<double> > a_;

• The parentheses operator allows access to individual entries:

double& operator()(int i, int j);

double operator()(int i, int j) const;

• The matrix provides the arithmetic operations of scaling and addition:

MatrixClass& operator*=(double x);

MatrixClass& operator+=(const MatrixClass& b);

Additionally there are several free functions implementing arithmetic operatons based on*=and+=:

operator*(const MatrixClass& a, double x);

operator*(double x,const MatrixClass& a);

operator+(const MatrixClass& a, const MatrixClass& b);

The current version can be found on the lecture website in the following files:

• matrix_double.h

• matrix_double.cc

• test_matrix_double.cc

The first two contain the definition and implementation of theMatrixClassfordouble, while the third file contains a test program.

(2)

Exercises:

1. Template classes and functions: Change the implementation of MatrixClass that was provided to that of a template class, so that it can be used for different number ty- pes. Change the main function of the test program, so that the template variants are te- sted. The program should use all data types mentioned above, with complex referring to std::complex<double> and the required header being <complex>. Also test the matrix with your Rational class. For the Print functionality you have to define a free func- tionstd::ostream& operator<< (std::ostream& str, const Rational& r)that prints the rational number by first printing its numerator, then a “/” and then its denominator.

Note:The free functions

operator*(const MatrixClass& a, double x)

operator*(double x,const MatrixClass& a)

operator+(const MatrixClass& a, const MatrixClass& b)

have to be modified as well. Also note that the argument double x of the functions need not be of the same type as the matrix entries after templatization. Introduce further template parameters to allow for this. Your test program should also take this into account.

2. Templates and inheritance:Separate the templated classMatrixClassinto two classes:

• a classBaseMatrixClass, without numerical operators. This represents a matrix contai- ning an arbitrary data type and has the following methods:

Constructor (as inMatrixClass)

operator()(int i, int j)for access to individual entries ResizeandPrintmethods (as inMatrixClass)

• a numerical matrix classNumMatrixClassinheriting fromMatrixClass. the numerical matrix class additionally provides the arithmetic operations mentioned above.

Also modify the free functions accordingly. Your second test program does not need to test each number type again, an arithmetic test with double is enough. Instead, test your BaseMatrixClasswith strings as data, and check wether you can create and use objects of type BaseMatrixClass<NumMatrixClass<int> >. Either add a operator<< as for RationaltoBaseMatrixClass, or avoid calling itsPrint()method in this case.

Note:The first part of this exercise is worth3/5of the total points. Please hand in two distinct test programs, one for the first part of the exercise, with nametest_matrix_temp.cc, and one for the second part, with name test_matrix_inher.cc, and also use separate header files for the two

exercise parts. 20 Points

Referenzen

ÄHNLICHE DOKUMENTE

insert(p, t) Inserts the element t in front of the one the iterator p points to, and returns an iterator that points to the inserted element.. insert(p, i, j) As above, but for

replace_copy(b,e,out,v,v2) Create copy of all elements in range [b:e) re- placing elements which are equal to v with v2 , return iterator to end of copy.

• Traits can be used to specify types and values that depend on one or more template parameters. • Policies can be used to specify parts of algorithms as

Since the units are only used as a template parameter, this only affects the class type but does not require memory... Example: Numbers

This header also contains functions that provide a thread ID and the functionality to detach threads that afterwards run as a separate program (fork).. Mutual Exclusion The header

and no exercise group, because things are still being set up, but you are welcome to attend if you have questions about the lecture or exercises or something else to discuss.. E

~List (); // clean up the list and all nodes Node* first() const; // return a pointer to the first entry Node* next( const Node* n) const; // return a pointer to the node after n

Please modify your implementation again to obtain a doubly linked list: each element should also point to its predecessor.. What is