R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > # chex32.r J F Monahan August 2007 > # > # Example 3.2 Solve a general system of linear equations > # > A <- matrix( c(1,.5,0,1, 2,1,2,-1, -1,0,-.5,1.5, 0,1,1.5,0), 4, 4) > # write it out > A [,1] [,2] [,3] [,4] [1,] 1.0 2 -1.0 0.0 [2,] 0.5 1 0.0 1.0 [3,] 0.0 2 -0.5 1.5 [4,] 1.0 -1 1.5 0.0 > b <- c( .5, 1, 1.5, 2) > # write it out > b [1] 0.5 1.0 1.5 2.0 > # solve equations > x <- solve(A,b) > # write it out > x [1] -0.70 2.25 3.30 -0.90 > rm(list=ls()) # clean up > R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > # chlsky.r J F Monahan, June 2001 > # revised August 2007 > # Cholesky factorization of a positive definite matrix > A <- matrix(c(4,2,2,4, 2,5,7,0, 2,7,19,11, 4,0,11,25), 4, 4) > "matrix" [1] "matrix" > A [,1] [,2] [,3] [,4] [1,] 4 2 2 4 [2,] 2 5 7 0 [3,] 2 7 19 11 [4,] 4 0 11 25 > Lt <- chol(A) > "Cholesky factor -- transposed to be upper triangular" [1] "Cholesky factor -- transposed to be upper triangular" > Lt [,1] [,2] [,3] [,4] [1,] 2 1 1 2 [2,] 0 2 3 -1 [3,] 0 0 3 4 [4,] 0 0 0 2 > det <-prod(diag(Lt)) > "determinant" [1] "determinant" > det*det [1] 576 > # use function 'adjust' previously constructed > adjust <- function(x) { + if( x <= 0 ) c( 0, -2**31 ) else { # flag if not positive + i <- 0 + while (x > 16 ) { + x <- x/16 + i <- i + 4 + } + while (x < 1 ) { + x <- x*16 + i <- i - 4 + } + c(x,i) + } + } > # now compute -- it may have already overflowed > adjust(det*det) [1] 2.25 8.00 > > b <-c( -1, 1, 2.5, .25) > "Right hand side" [1] "Right hand side" > b [1] -1.00 1.00 2.50 0.25 > y <- forwardsolve(t(Lt),b) # lower triangular system > "intermediate" [1] "intermediate" > y [1] -0.50 0.75 0.25 0.50 > x <- backsolve(Lt,y) # upper triangular system > "Solution" [1] "Solution" > x [1] -0.8125 0.8750 -0.2500 0.2500 > rm(list=ls()) >