0% found this document useful (0 votes)
13 views4 pages

ECE OOP Lab4

this can help you guys in Oriented-Ojective Programming

Uploaded by

tonynghia0812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

ECE OOP Lab4

this can help you guys in Oriented-Ojective Programming

Uploaded by

tonynghia0812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Electrical and Computer Engineering

Vietnamese - German University


OBJECT-ORIENTED PROGRAMMING
Complete the implementation of class Matrix, and further implement class Vector described
below.

ˆ Use vector.h and matrix.h to declare class Vector and Matrix respectively.

ˆ Use vector.cpp and matrix.cpp to define the corresponding methods.

ˆ Try to make as const methods as possible.

ˆ Use assert to check if the operands are valid for an operator.

1 ***************************************************************************
2 * class Vector :
3 *
4 * data member :
5 * * entry vector entries of type Double
6 * size vector size
7 *
8 * constructors and destructor :
9 * Vector ( size ) create a vector object of size size
10 * Vector ( size , val ) create a vector with entries of val
11 * Vector ( v ) copy constructor from vector v with deep copy
12 * ~ Vector () destructor
13 *
14 * member methods :
15 * getSize () return the size of vector v
16 * setEntry (i , val ) set v ( i ) = val
17 * getEntry ( i ) return v ( i )
18 * zeros () initialize vector v with zero entries
19 * ones () initialize vector v with one entries
20 * random () initialize vector v with random entries
21 * norm ( p ) compute p - norm of vector v , return a scalar
22 * print ( string name ) print the entries to the screen
23 *
24 * entry access :
25 * v(i) zero - based indexing
26 *
27 * member operators :
28 * v = w assignment
29 * v = -w
30 * v += w
31 * v -= w
32 * v *= alp alp scalar number
33 * v *= w
34 * t = v + w
35 * t = v - w
36 * w = alp * v
37 * non - member operators :
38 * alpha = dot (v , w ) return dot product of two vectors
39 ***************************************************************************/

Listing 1: class Vector

1 ***************************************************************************
2 * class Matrix :
3 *
4 * member data :
ECE Programming 4 Page 2 of 4

5 * ** entry matrix entries of type Double


6 * row number of rows
7 * col number of columns
8 *
9 * constructors and destructor :
10 * Matrix (m , n ) create an mxn matrix object
11 * Matrix (m ,n , val ) create an mxn matrix with entries of val
12 * Matrix ( A ) copy constructor from matrix A with deep copy
13 * ~ Matrix () destructor
14 *
15 * member methods :
16 * getRows () return number of rows
17 * getCols () return number of columns
18 * setEntry (i ,j , val ) set A (i , j ) = val
19 * getEntry (i , j ) return A (i , j )
20 * zeros () initialize matrix A with zero entries
21 * ones () initialize matrix A with one entries
22 * eye () initialize matrix A as an identity matrix
23 * random () initialize matrix A with random entries
24 * print ( string name ) print the entries to the screen
25 *
26 * entry access :
27 * A (i , j ) i : row , j : column index
28 *
29 * member operators :
30 * A = B assignment
31 * A = -B
32 * A += B
33 * A *= alp alp scalar number
34 * A *= B
35 * C = A + B
36 * C = A - B
37 * B = A * alp
38 * C = A * B
39 * non - member operator :
40 * B = transpose ( A )
41 ***************************************************************************/

Listing 2: class Matrix

ˆ Use the attached MatVecClass.cpp to test your implementation.

1 # include < iostream >


2 # include " vector . h " // header file for all f u n c t i o n s r e l a t i n g to vector o p e r a t i o n s
3 # include " matrix . h " // header file for all f u n c t i o n s r e l a t i n g to matrix o p e r a t i o n s
4 using namespace std ;
5
6 int main ()
7 {
8
9 const int p_INF = 100000 ;
10 double alpha = 0 ;
11
12 /*
13 * 1. create vectors and m a t r i c e s
14 */
15 Matrix A (5 ,4) , B (5 ,4) , C (5 ,4) ,
16 D (4 ,5) , E (5 ,5) ;
17 Vector v (5) , w (5) , t (5) ;
18
19 /*
20 * 2. i n i t i a l i z e vectors and m a t r i c e s
21 */
22 v . random () ;

Page 2
ECE Programming 4 Page 3 of 4

23 w . random () ;
24 t . zeros () ;
25
26 A . random () ;
27 B . random () ;
28 C . zeros () ;
29 D . random () ;
30 E . zeros () ;
31
32 /*
33 * 3. print out the i n i t i a l i z e d vectors and m a t r i c e s
34 */
35 v . print ( " Vector ␣ v : ␣ " ) ;
36 w . print ( " Vector ␣ w : ␣ " ) ;
37 t . print ( " Vector ␣ t : ␣ " ) ;
38 A . print ( " Matrix ␣ A : ␣ " ) ;
39 B . print ( " Matrix ␣ B : ␣ " ) ;
40 C . print ( " Matrix ␣ C : ␣ " ) ;
41 D . print ( " Matrix ␣ D : ␣ " ) ;
42 E . print ( " Matrix ␣ E : ␣ " ) ;
43
44
45 /*
46 * 4. compute vector norms
47 */
48 cout << " || ␣ v ␣ || _2 ␣ = ␣ " << v . norm (2) << endl ;
49 cout << " || ␣ v ␣ || _max ␣ = ␣ " << v . norm ( p_INF ) << endl ;
50
51 /*
52 * 5. o p e r a t i o n s
53 */
54 // ====== for vectors = = = = = = = =
55 // === adding 2 vectors
56 t = v + w;
57 t . print ( " Vector ␣ t ␣ = ␣ v ␣ + ␣ w : ␣ " ) ;
58
59 // === scalar - vector m u l t i p l i c a t i o n
60 t = v * alpha ;
61 t . print ( " Vector ␣ t ␣ = ␣ v * alpha : ␣ " ) ;
62
63 // === i n c r e a m e n t
64 ++ t ;
65 t . print ( " Vector ␣ ++ t : ␣ " ) ;
66
67 // === dot product of 2 vectors of the same size
68 beta = dot (v , w ) ;
69 cout << " beta ␣ = ␣ dot (v , ␣ w ) ␣ = ␣ " << beta << endl ;
70
71 // ====== for m a t r i c e s = = = = = = = =
72 // ==== adding 2 m a t r i c e s
73 E . zeros () ;
74 C = A + B;
75 C . print ( " Matrix ␣ C ␣ = ␣ A ␣ + ␣ B : ␣ " ) ;
76 E = C + B ; // should return error
77 // ==== matrix - matrix m u l t i p l i c a t i o n
78 E = A * D;
79 E . print ( " Matrix ␣ E ␣ = ␣ A ␣ * ␣ D : ␣ " ) ;
80 // ==== matrix - scalar m u l t i p l i c a t i o n
81 E = E * alpha ;
82 E . print ( " Matrix ␣ E ␣ = ␣ E * alpha : ␣ " ) ;
83 // ==== i n c r e a m e n t
84 ++ E ;
85 E . print ( " Matrix ␣ ++ E : ␣ " ) ;
86 // ==== complex matrix o p e r a t i o n
87 Matrix F (5 , 5 , 0.0) ;
88 F = E *( A * transpose ( B ) + C * D ) ;

Page 3
ECE Programming 4 Page 4 of 4

89 F . print ( " ++ E *(( A * transpose ( B )) ␣ + ␣ ( C * D )): ␣ " ) ;


90 // ==== matrix - vector m u l t i p l i c a t i o n
91 t = E * v;
92 t . print ( " Vector ␣ t ␣ = ␣ E * v : ␣ " ) ;
93
94 return 0 ;
95 }

Listing 3: MatVecClass.cpp

Page 4

You might also like