Skip to contents

Computes the determinant of a square matrix in the modulus / sign decomposition matching base R's base::determinant(). For the plain scalar determinant, use nv_det().

Usage

nv_determinant(x, logarithm = TRUE)

# S3 method for class 'AnvlArray'
determinant(x, logarithm = TRUE, ...)

Arguments

x

(arrayish)
One input, a square matrix with exactly 2 axes. Can be any numeric data type: a float keeps its own, and an integer one is converted to the default float data type (see default_dtypes()). An R value materializes at its default data type and is converted in the same way.

logarithm

(logical(1))
If TRUE (default), return the log of the absolute determinant.

...

No additional arguments.

Value

(named list of two arrayish)
Elements modulus and sign, both scalar arrayish with x's data type – or the default float data type (see default_dtypes()) where x was an integer one. The full determinant is sign * exp(modulus) (with logarithm = TRUE) or sign * modulus (with logarithm = FALSE).

Details

For computing the determinant, we use: $$P A = L U$$ $$\det(L) = 1$$ $$\det(A) = \det(U) / \det(P) = \mathrm{sign}(P^{-1}) \, \prod_i U_{ii} = \mathrm{sign}(P) \, \prod_i U_{ii}$$

Matching base R's det_ge_real, the magnitude is computed in log space when logarithm = TRUE (\(\sum_i \log|U_{ii}|\)) and as a direct product when logarithm = FALSE (\(\prod_i |U_{ii}|\)).

Examples

# `modulus` and `sign` are scalars with the matrix's data type
a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64")
nv_determinant(a)
#> $modulus
#> AnvlArray
#>  1.7918
#> [ CPUf64{} ] 
#> 
#> $sign
#> AnvlArray
#>  -1
#> [ CPUf64{} ] 
#> 
nv_determinant(a, logarithm = FALSE)
#> $modulus
#> AnvlArray
#>  6
#> [ CPUf64{} ] 
#> 
#> $sign
#> AnvlArray
#>  -1
#> [ CPUf64{} ] 
#>