Examples for 'base::solve'


Solve a System of Equations

Aliases: solve solve.default

Keywords: algebra

### ** Examples

hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, `+`) }
h8 <- hilbert(8); h8
          [,1]      [,2]      [,3]       [,4]       [,5]       [,6]       [,7]
[1,] 1.0000000 0.5000000 0.3333333 0.25000000 0.20000000 0.16666667 0.14285714
[2,] 0.5000000 0.3333333 0.2500000 0.20000000 0.16666667 0.14285714 0.12500000
[3,] 0.3333333 0.2500000 0.2000000 0.16666667 0.14285714 0.12500000 0.11111111
[4,] 0.2500000 0.2000000 0.1666667 0.14285714 0.12500000 0.11111111 0.10000000
[5,] 0.2000000 0.1666667 0.1428571 0.12500000 0.11111111 0.10000000 0.09090909
[6,] 0.1666667 0.1428571 0.1250000 0.11111111 0.10000000 0.09090909 0.08333333
[7,] 0.1428571 0.1250000 0.1111111 0.10000000 0.09090909 0.08333333 0.07692308
[8,] 0.1250000 0.1111111 0.1000000 0.09090909 0.08333333 0.07692308 0.07142857
           [,8]
[1,] 0.12500000
[2,] 0.11111111
[3,] 0.10000000
[4,] 0.09090909
[5,] 0.08333333
[6,] 0.07692308
[7,] 0.07142857
[8,] 0.06666667
sh8 <- solve(h8)
round(sh8 %*% h8, 3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    0    0    0    0    0    0    0
[2,]    0    1    0    0    0    0    0    0
[3,]    0    0    1    0    0    0    0    0
[4,]    0    0    0    1    0    0    0    0
[5,]    0    0    0    0    1    0    0    0
[6,]    0    0    0    0    0    1    0    0
[7,]    0    0    0    0    0    0    1    0
[8,]    0    0    0    0    0    0    0    1
A <- hilbert(4)
A[] <- as.complex(A)
## might not be supported on all platforms
try(solve(A))
        [,1]     [,2]     [,3]     [,4]
[1,]   16+0i  -120+0i   240+0i  -140+0i
[2,] -120+0i  1200+0i -2700+0i  1680+0i
[3,]  240+0i -2700+0i  6480+0i -4200+0i
[4,] -140+0i  1680+0i -4200+0i  2800+0i

[Package base version 4.2.3 Index]