Skip to contents

Solves the linear system a %*% x = b for x. Uses LU decomposition with partial pivoting internally, so a need only be square and non-singular.

Usage

nv_solve(a, b)

# S3 method for class 'AnvlArray'
solve(a, b, ...)

Arguments

a

(arrayish)
Square non-singular matrix with exactly 2 axes. Can be any numeric data type: a and b are promoted to a common data type and that is then converted to the default float data type (see default_dtypes()) where it is not a float already, since the decomposition is a float one. An R value assumes the other operand's data type within its data type category, and settles on the default float when neither has one.

b

(arrayish)
Right-hand side, vector of length n or matrix with n rows. Promoted together with a – see a.

...

No additional arguments.

Value

(arrayish)
The solution x such that a %*% x = b, with b's shape and the operands' common data type – or the default float data type (see default_dtypes()) where that was an integer one.

Details

$$A x = b$$ $$P A = L U$$ $$L U x = P b$$ $$L y = P b$$ $$U x = y$$

Shapes

  • a: (n, n)

  • b: (n,) or (n, k)

  • output: same shape as b

Examples

# the solution has `b`'s shape and the operands' common data type
a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64")
b <- nv_matrix(c(1, 2), nrow = 2, dtype = "f64")
nv_solve(a, b)
#> AnvlArray
#>   1.5000
#>  -0.8333
#> [ CPUf64{2,1} ] 

# an integer system is solved at the default float data type
nv_solve(nv_matrix(c(3L, 1L, 1L, 2L), nrow = 2), nv_array(c(9L, 8L)))
#> AnvlArray
#>  2.0000
#>  3.0000
#> [ CPUf32{2} ]