You are on page 1of 3

Electrical and Computer Engineering

Vietnamese - German University


OBJECT-ORIENTED PROGRAMMING
Programming Assignment 2

1 Sketch of linear algebra


In linear algebra, let v w ∈ Rm and A, B ∈ Rm×n where m and n are the number of rows (size of
vectors) and number of columns, respectively, e.g.,

   
v0 a00 a01 ... a0,j ... a0,n−1



v1 
 a10 a11 ... a1,j 
 ... a1,n−1
  ..  .. .. ..  .. ..
  .  . . . .. .  . .
v=
 vi  ,
 A=
 ai,0
,
   ai,1 ... ai,j . . . a2,n−1  
 ..   .. .. .. .. .. .. 
 .   . . . . . . 
vm−1 an−1,0 an−1,1 . . . an−1,2 . . . an−1,n−1

or in short form,

v = (vi ), w = (wi ), A = (aij ), B = (bij )

1. Vector/Matrix summation:

A + B = (aij + bij ), i = 0, . . . , m − 1, j = 0, . . . , n − 1 (1)

2. Matrix-Vector multiplication: possible if v ∈ Rn and A ∈ Rm×n

m−1
X
w = Av = (wi ) = aik ∗ vk , i = 0, . . . , m − 1 (2)
k=0

3. Matrix-Matrix multiplication: possible if A ∈ Rm×l and A ∈ Rl×n . The product is C ∈ Rm×n

l−1
X
C = A ∗ B = (cij ) = aik ∗ bkj , i = 0, . . . , m − 1, j = 0, . . . , n − 1 (3)
k=0

4. Dot product: let α ∈ R be a scalar. The dot product of 2 vectors of the same size is

m−1
X
α=v·w = v i ∗ wi (4)
i=0
ECE Programming 2 Page 2 of 3

2 Programming exercises
Write a C++ program to compute the following

1. Use dynamic memory to allocate v, w, t, u ∈ Rm , and A ∈ Rm×m , B ∈ Rm×m , C ∈ Rm×n ,


D ∈ Rn×m , E ∈ Rm×m , where m and n are from the input. All these are of type double.

2. Initialize v, w, A, B, D with random numbers, and t = u = 0, C = 0, E = 0

3. Print them out to the screen

4. Compute t = v + w, C = A + B, u = C ∗ t, E = C ∗ D, α = v · u. Use assert to check if


the sizes of the vectors or matrices are valid for each computation.

5. Print the results to the screen

6. Manually de-allocate using delete.

7. Use, e.g., MATLAB, to double compare the errors between the two obtained results.

1 # include < iostream >


2 # include < cassert > // for assert
3 # include < cstdlib > // for random g e n e r a t o r rand ()
4 using namespace std ;
5
6 const double ZERO = 0 . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
7 const int m ;
8 const int n ;
9 int main ()
10 {
11 int m , n ;
12 cout << " Input ␣ size ␣ m : " << endl ;
13 cin >> m ;
14 cout << " Input ␣ size ␣ n : " << endl ;
15 cin >> n ;
16
17 double alpha ( ZERO ) ;
18 double *v , *w , *t , * u ;
19 double ** A , ** B , ** C , ** D , ** E ;
20
21 int sizeV , sizeW , sizeT , sizeU ;
22 int rowA , colA ;
23 int rowB , colB ;
24 int rowC , colC ;
25 int rowD , colD ;
26 int rowE , colE ;
27
28 // dynamic memory a l l o c a t i o n with new
29
30 // i n i t i a l i z e v , w , A , B , D with random number from 1 to 10
31 for ( int i = 0 ; i < sizeV ; ++ i )
32 v [ i ] = ( double )( 1 + rand () % 10 ) ;
33
34 // set t = u = 0 , C = 0 , E = 0
35 for ( int i = 0 ; i < sizeT ; ++ i )
36 t [ i ] = ZERO ;
37
38 // print the i n i t i a l i z e d objects to the screen
39
40 // do the r e q u i r e d c o m p u t a t i o n s with assert
41 // vector a d d i t i o n
42 assert ( sizeV == sizeW ) ; // returns runtime error if not true

Page 2
ECE Programming 2 Page 3 of 3

43 for ( int i = 0 ; i < sizeT ; ++ i )


44 t[i] = v[i] + w[i];
45
46 // matrix a d d i t i o n
47
48 // matrix - vector m u l t i p l i c a t i o n
49
50 // matrix - matrix m u l t i p l i c a t i o n
51
52 // dot product
53
54 // print the results to the screen
55
56 // m a n u a l l y d e a l l o c a t e the p o i n t e r s
57
58 return 0 ;
59 }

Listing 1: Coding1.cpp

Page 3

You might also like