restart: # CHOLESKI'S ALGORITHM 6.6 # # To factor the positive definite n by n matrix A into LL**T, # where L is lower triangular. # # INPUT: the dimension n: entries A(I,J), 1<=I, J<=n of A. # # OUTPUT: the entries L(I,J), 1<=J<=I, 1<=I<=n of L. # print(`This is Choleski Factorization Method.\134n`): print(`This is the LDL^t Method for Positive Definite Matrices.\134n`): print(`Choice of input method`): print(`1. input from keyboard - not recommended for large matrices`): print(`2. input from a text file`): print(`Please enter 1 or 2.`): FLAG := scanf(`%d`)[1]: print(`Your input is`): print(FLAG): if FLAG = 2 then print(`The array will be input from a text file in the order`): print(`A(1,1), A(1,2), ..., A(1,N), A(2,1), A(2,2), ..., A(2,N)`): print(`..., A(N,1), A(N,2), ..., A(N,N)\134n`): print(`Place as many entries as desired on each line, but separate `): print(`entries with`): print(`at least one blank.`): print(`Has the input file been created? - enter 1 for yes or 2 for no.`): AA := scanf(`%d`)[1]: print(`Your response is`): print(AA): if AA = 1 then print(`Input the file name in the form - drive:\134\134name.ext`): print(`for example: A:\134\134DATA.DTA`): NAME := scanf(`%s`)[1]: print(`The file name is`): print(NAME): INP := fopen(NAME,READ,TEXT): OK := FALSE: while OK = FALSE do print(`Input the dimension n - an integer.`): N := scanf(`%d`)[1]: print(`N is`): print(N): if N > 0 then for I1 from 1 to N do for J from 1 to N do A[I1-1,J-1] := fscanf(INP, `%f`)[1]: od: od: OK := TRUE: fclose(INP): else print(`The number must be a positive integer.\134n`): fi: od: else print(`The program will end so the input file can be created.\134n`): fi: else OK := FALSE: while OK = FALSE do print(`Input the dimension n - an integer.`): N := scanf(`%d`)[1]: print(`N= `): print(N): if N > 0 then for I1 from 1 to N do for J from 1 to N do print(`input entry in position `,I1,J): A[I1-1,J-1] := scanf(`%f`)[1]:print(`Data is `):print(A[I1-1,J-1]): od: od: OK := TRUE: else print(`The number must be a positive integer.\134n`): fi: od: fi: if OK = TRUE then OUP := default: fprintf(OUP, `The original matrix - output by rows:\134n`): for I1 from 1 to N do for J from 1 to N do fprintf(OUP, ` %11.8f`, A[I1-1,J-1]): od: fprintf(OUP, `\134n`): od: fi: if OK = TRUE then # Step 1 A[0,0] := sqrt(A[0,0]): # Step 2 for J from 2 to N do A[J-1,0] := A[J-1,0]/A[0,0]: od: # Step 3 NN := N-1: for I1 from 2 to NN do # Step 4 KK := I1-1: S := 0: for K from 1 to KK do S := S-A[I1-1,K-1]*A[I1-1,K-1]: od: A[I1-1,I1-1] := sqrt(A[I1-1,I1-1]+S): # Step 5 JJ := I1+1: for J from JJ to N do S := 0: KK := I1-1: for K from 1 to KK do S := S - A[J-1,K-1]*A[I1-1,K-1]: od: A[J-1,I1-1] := (A[J-1,I1-1]+S)/A[I1-1,I1-1]: od: od: # Step 6 S := 0: for K from 1 to NN do S := S-A[N-1,K-1]*A[N-1,K-1]: od: A[N-1,N-1] := sqrt(A[N-1,N-1]+S): # Step 7 print(`Choice of output method:\134n`): print(`1. Output to screen\134n`): print(`2. Output to text file\134n`): print(`Please enter 1 or 2.\134n`): FLAG := scanf(`%d`)[1]:print(`Your input is `):print(FLAG): if FLAG = 2 then print(`Input the file name in the form - drive:\134\134name.ext\134n`): print(`for example: A:\134\134OUTPUT.DTA\134n`): NAME := scanf(`%s`)[1]:print(`Output file is `):print(NAME): OUP := fopen(NAME,WRITE,TEXT): else OUP := default: fi: fprintf(OUP, `CHOLESKI FACTORIZATION\134n\134n`): fprintf(OUP, `The matrix L output by rows:\134n`): for I1 from 1 to N do for J from 1 to I1 do fprintf(OUP, ` %12.8f`, A[I1-1,J-1]): od: fprintf(OUP, `\134n`): od: if OUP <> default then fclose(OUP): print(`Output file `,NAME,` created successfully`,NAME): fi: fi: SUhUaGlzfmlzfkNob2xlc2tpfkZhY3Rvcml6YXRpb25+TWV0aG9kLnwrRzYi SVpUaGlzfmlzfnRoZX5MRExedH5NZXRob2R+Zm9yflBvc2l0aXZlfkRlZmluaXRlfk1hdHJpY2VzLnwrRzYi STdDaG9pY2V+b2Z+aW5wdXR+bWV0aG9kRzYi SWZuMS5+aW5wdXR+ZnJvbX5rZXlib2FyZH4tfm5vdH5yZWNvbW1lbmRlZH5mb3J+bGFyZ2V+bWF0cmljZXNHNiI= SToyLn5pbnB1dH5mcm9tfmF+dGV4dH5maWxlRzYi STVQbGVhc2V+ZW50ZXJ+MX5vcn4yLkc2Ig== SS5Zb3VyfmlucHV0fmlzRzYi IiIi SURJbnB1dH50aGV+ZGltZW5zaW9ufm5+LX5hbn5pbnRlZ2VyLkc2Ig== SSROPX5HNiI= IiIk NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiIkYl SSlEYXRhfmlzfkc2Ig== JCIiJSIiIQ== NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiIiIiIw== SSlEYXRhfmlzfkc2Ig== JCEiIiIiIQ== NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiIiIiJA== SSlEYXRhfmlzfkc2Ig== JCIiIiIiIQ== NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiIyIiIg== SSlEYXRhfmlzfkc2Ig== JCEiIiIiIQ== NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiI0Yl SSlEYXRhfmlzfkc2Ig== JCIkRCUhIiM= NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiIyIiJA== SSlEYXRhfmlzfkc2Ig== JCIkdiMhIiM= NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiJCIiIg== SSlEYXRhfmlzfkc2Ig== JCIiIiIiIQ== NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiJCIiIw== SSlEYXRhfmlzfkc2Ig== JCIkdiMhIiM= NiVJOWlucHV0fmVudHJ5fmlufnBvc2l0aW9ufkc2IiIiJEYl SSlEYXRhfmlzfkc2Ig== JCIjTiEiIg== The original matrix - output by rows: 4.00000000 -1.00000000 1.00000000 -1.00000000 4.25000000 2.75000000 1.00000000 2.75000000 3.50000000 STpDaG9pY2V+b2Z+b3V0cHV0fm1ldGhvZDp8K0c2Ig== STUxLn5PdXRwdXR+dG9+c2NyZWVufCtHNiI= STgyLn5PdXRwdXR+dG9+dGV4dH5maWxlfCtHNiI= STZQbGVhc2V+ZW50ZXJ+MX5vcn4yLnwrRzYi SS9Zb3VyfmlucHV0fmlzfkc2Ig== IiIi CHOLESKI FACTORIZATION The matrix L output by rows: 2.00000000 -0.50000000 2.00000000 0.50000000 1.50000000 1.00000000 JSFH