Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17 C-implementation PAP’ = LDL’ Speaker: Lung-Sheng Chien Reference: [1] H. M. Deitel, P. J. Deitel, C++ How to Program 5-th edition.

Similar presentations


Presentation on theme: "Chapter 17 C-implementation PAP’ = LDL’ Speaker: Lung-Sheng Chien Reference: [1] H. M. Deitel, P. J. Deitel, C++ How to Program 5-th edition."— Presentation transcript:

1 Chapter 17 C-implementation PAP’ = LDL’ Speaker: Lung-Sheng Chien Reference: [1] H. M. Deitel, P. J. Deitel, C++ How to Program 5-th edition

2 OutLine Data structure of lower triangle matrix Function overloading in C++ Implementation of PAP’ =LDL’

3 6123-6 12-8-134 3 -71 -6416 Logical index : 2D row-major based 6 12-8 3-13-7 -6416 col-major based 6 12 -8 3 -13 -7 -6 4 1 6 row-major versus col-major We choose col-major representation

4 6 12 3 -6 -8 -13 4 -7 1 6 1 2 3 4 5 6 7 8 9 10 column-major based 6 12-8 3-13-7 -6416 0 column-major: method 1 (square-matrix based) 6123-6 12-8-134 3 -71 -6416 is useless 1 2 is forbidden 3 Keep properties 4 is valid only for ? 5

5 6 12 3 -6 -8 -13 4 -7 1 6 1 2 3 4 5 6 7 8 9 10 column-major based 6 12 -8 3 -13 -7 -6 4 1 6 0 column-major: method 2 (triangle-matrix based) 6123-6 12-8-134 3 -71 -6416 is useless 1 2 is forbidden 3 Keep properties 4 Index rule is more complicated

6 Data structure of matrix and its method lowerTriangleMatrix.h The same name as those in matrix.h A is symmetric, but we only store lower triangle part method 1

7 Constructor [1] lowerTriangleMatrix.cpp 12 1 2

8 Constructor [2] lowerTriangleMatrix.cpp 6 12 3 -6 -8 -13 4 -7 1 6 1 2 3 4 5 6 7 8 9 10 0 3 4 3 4 6123-6 12-8-134 3 -71 -6416 When matrix A is symmetric, it must be square and we store lower triangle part of A, Difference from full matrix is 34 Index rule

9 destructor lowerTriangleMatrix.cpp 6 12 3 -6 -8 -13 4 -7 1 6 1 2 3 4 5 6 7 8 9 10 0 1 2 3 2 free pointer array 3 free lowerTriangleMatrix handler Pointer arithmetic, remember to do casting

10 Input/Output lowerTriangleMatrix.cpp 1 2 1 2 No matter what A is symmetric or not, we only report lower triangle part

11 Simple driver main.cpp Advantage of method 1: index rule is the same as that of full matrix, one only notices is valid only for Question: how can we verify is valid only for automatically?

12 duplicate of a lower triangle matrix lowerTriangleMatrix.cpp 6 12 3 -6 -8 -13 4 -7 1 6 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 0 Question: This copy routine is different from that in matrix.cpp, which one is better?

13 Matrix norm [1] lowerTriangleMatrix.cpp “norm_sym” and “norm_nonsym” are private, means that user don’t need to know them and we declare them in lowerTriangleMatrix.cpp, NOT in lowerTriangleMatrix.h which user looks at

14 lowerTriangleMatrix.cpp Matrix norm [2] 6123-6 12-8-134 3 -71 -6416 symmetric : norm_sym 6 12-8 3-13-7 -6416 nonsymmetric : norm_nonsym

15 y = Ax only A is lower trianlge matrix (either symmetric or non-symmetric), x and y are full matrices.

16 OutLine Data structure of lower triangle matrix Function overloading in C++ Implementation of PAP’ =LDL’

17 Function overloading in C++ C++ enables several functions fo the same name to be defined, as long as these functions have different sets of parameter (1) parameter type (2) number of parameters (3) order of parameter type when an overloaded function is called, the C++ compiler selects the proper function by examining (1) number (2) types (3) order of the arguments in the call. the only one function which cannot be overloaded is "main" since it is the entry point. Advantage: overloading functions that perform closely related tasks can make programs more readable

18 void zeros( matrixHandler *, integer m, integer n, orderVar sel ) ; void zeros( int_matrixHandler *, integer m, integer n, orderVar sel ) ; void zeros( lowerTriangleMatrixHandler*, integer m, integer n, orderVar sel, int isSym ) ; Function overloading of matrix data type constructor 1 void dealloc( matrixHandler Ah ) ; void dealloc( int_matrixHandler Ah ) ; void dealloc( lowerTriangleMatrixHandler Ah ) ; destructor 2 void disp( matrixHandler Ah, ostream& out ) ; void disp( int_matrixHandler Ah, ostream& out ) ; void disp( lowerTriangleMatrixHandler Ah, ostream& out ) ; I/O 3 doublereal norm( matrixHandler Ah, matrix_keyword p ) ; doublereal norm( lowerTriangleMatrixHandler Ah, matrix_keyword p ) ; metric 4

19 OutLine Data structure of lower triangle matrix Function overloading in C++ Implementation of PAP’ =LDL’

20 Algorithm ( PAP’ = LDL’, partial pivot ) [1] Given symmetric indefinite matrix, construct initial lower triangle matrix use permutation vector to record permutation matrix let, and verification

21 we have compute update original matrix combines all lower triangle matrix and store in while, where Algorithm ( PAP’ = LDL’, partial pivot ) [2] (1) we store lower triangle matrix L and block diagonal matrix D into storage of matrix A (2) permutation matrix P and pivot sequence pivot are of type int_matrix

22 Algorithm ( PAP’ = LDL’, partial pivot ) [3] compute 1 Case 1:pivotno interchange do 1x1 pivot : 2 then Technique problem: if we store L into memory block of A, then will overwritesuch that fails. We use a temporary storage

23 Algorithm ( PAP’ = LDL’, partial pivot ) [4] do 1x1 pivot : We must update lower triangle part of matrix A only.

24 Algorithm ( PAP’ = LDL’, partial pivot ) [5] 3 and When case 1 is not satisfied, we compute maximum of off-diagonal elements in column r

25 Algorithm ( PAP’ = LDL’, partial pivot ) [6] 4 Case 2: pivot no interchange do 1x1 pivot : 5 then 6 and The same as code in case 1

26 Algorithm ( PAP’ = LDL’, partial pivot ) [7] Case 3:pivotdo interchange define permutationto do symmetric permutation 7 To compute, we only update lower triangle of 1 2 3 1 2 3 8

27 Algorithm ( PAP’ = LDL’, partial pivot ) [8] 1 2 3 8 then

28 Algorithm ( PAP’ = LDL’, partial pivot ) [9] To compute We only update lower triangle matrix 9 then we store lower triangle matrix L into storage of matrix A

29 Algorithm ( PAP’ = LDL’, partial pivot ) [10] do 1x1 pivot : 10 then 11 and The same as code in case 1 Case 4:pivotdo interchange define permutation, changeto 12

30 Algorithm ( PAP’ = LDL’, partial pivot ) [11] 1 2 3 4 To compute, we only update lower triangle of do interchange row/col 13

31 Algorithm ( PAP’ = LDL’, partial pivot ) [12] then 14 do interchange row do 2x2 pivot : 15 then

32 Algorithm ( PAP’ = LDL’, partial pivot ) [13] do 2x2 pivot :

33 Algorithm ( PAP’ = LDL’, partial pivot ) [14] 16 and endwhile

34 Example: partial pivot Bunch-Kaufman (partial pivot) 6123 -8-13 3 -7 -6 4 1 416 6 12-8 8 1 1 0-0.5 1 -0.68750.5938-0.6875 1 20111243 6 12-8 0-0.5 8 -0.68750.5938-0.6875

35 driver for example [1] main.cpp Test example We need full matrix representation ofof linear system and permutation matrix P, pivot sequence pivot

36 6 12-8 0-0.5 8 -0.68750.5938-0.6875 driver for example [2]

37 Report Implement PAP’ = LDL’ and linear solver in C/C++ describe program structure How to verify your program Comparison with MATLAB implementation Memory usage (do you need extra storage?) Speedup strategy


Download ppt "Chapter 17 C-implementation PAP’ = LDL’ Speaker: Lung-Sheng Chien Reference: [1] H. M. Deitel, P. J. Deitel, C++ How to Program 5-th edition."

Similar presentations


Ads by Google