Title: | Functions of Complex or Real Variable |
---|---|
Description: | Extension of several functions to the complex domain, including the matrix exponential and logarithm, and the determinant. |
Authors: | Albert Dorador and Uffe Høgsbro Thygesen |
Maintainer: | Albert Dorador <[email protected]> |
License: | GPL-2 |
Version: | 2.1 |
Built: | 2024-12-07 06:32:41 UTC |
Source: | CRAN |
Det
computes the determinant of a square matrix.
This function first checks whether the matrix is full rank or not; if not,
the value 0 is returned. This avoids relatively frequent numerical errors
that produce a non-zero determinant when in fact it is zero.
Only if the matrix is full rank does the algorithm proceed to compute the determinant.
If the matrix is complex, the determinant is computed as the product of the eigenvalues; if the matrix
is real, Det
calls the base function det
for maximum efficiency.
Det(M)
Det(M)
M |
a square matrix, real or complex. |
The determinant of M.
Albert Dorador
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) #complex matrix B <- matrix(1:4, ncol = 2) #real matrix S <- matrix(c(3, 4+3i, 0, 0), ncol = 2) #Singular matrix Det(A) Det(B) Det(S)
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) #complex matrix B <- matrix(1:4, ncol = 2) #real matrix S <- matrix(c(3, 4+3i, 0, 0), ncol = 2) #Singular matrix Det(A) Det(B) Det(S)
imaginary parts with values very close to 0 are 'zapped', i.e. treated as 0. Therefore, the number becomes real and changes its class from complex to numeric.
Imzap(x, tol = 1e-06)
Imzap(x, tol = 1e-06)
x |
a scalar or vector, real or complex. |
tol |
a tolerance, |
Albert Dorador
x1 <- 1:100 x2 <- c(1:98,2+3i,0-5i) x3 <- c(1:98,2+0i,7-0i) x4 <- complex(real = rnorm(100), imaginary = rnorm(100)) Imzap(x1) # inocuous with real vectors Imzap(x2) # 1 single element is enough to turn the vector into complex Imzap(x3) # removes extra 0i and changes class from from complex to numeric Imzap(x4) # inocuous with complex vectors with non-null complex part
x1 <- 1:100 x2 <- c(1:98,2+3i,0-5i) x3 <- c(1:98,2+0i,7-0i) x4 <- complex(real = rnorm(100), imaginary = rnorm(100)) Imzap(x1) # inocuous with real vectors Imzap(x2) # 1 single element is enough to turn the vector into complex Imzap(x3) # removes extra 0i and changes class from from complex to numeric Imzap(x4) # inocuous with complex vectors with non-null complex part
matclass
gives the name of the class of the matrix.
If the class of the matrix elements is numeric or integer, the matrix class is real by default.
matclass(M, real = TRUE)
matclass(M, real = TRUE)
M |
a matrix, of any type and dimensions. |
real |
a logical value. Set to FALSE if you want to opt out of the default treatment of numeric or integer elements. |
Albert Dorador
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) # complex matrix B <- matrix(1:4, ncol = 2) # real matrix C <- matrix(c(TRUE, TRUE, FALSE, FALSE), ncol = 2) # boolean matrix D <- matrix(c('a', 'b', 'c', 'd'), ncol = 2) # character matrix E <- matrix(c(1,2+3i,'a',TRUE),ncol=2) # 1 single character makes it a character matrix matclass(A) matclass(B) matclass(B,FALSE) matclass(C) matclass(D) matclass(E)
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) # complex matrix B <- matrix(1:4, ncol = 2) # real matrix C <- matrix(c(TRUE, TRUE, FALSE, FALSE), ncol = 2) # boolean matrix D <- matrix(c('a', 'b', 'c', 'd'), ncol = 2) # character matrix E <- matrix(c(1,2+3i,'a',TRUE),ncol=2) # 1 single character makes it a character matrix matclass(A) matclass(B) matclass(B,FALSE) matclass(C) matclass(D) matclass(E)
matexp
computes the exponential of a square matrix A i.e. .
matexp(A, ...)
matexp(A, ...)
A |
a square matrix, real or complex. |
... |
arguments passed to or from other methods. |
This function adapts function expm
from package expm
to be able to handle complex matrices, by decomposing the original
matrix into real and purely imaginary matrices and creating a real
block matrix that function expm
can successfully process.
If the original matrix is real, matexp
calls expm
directly for maximum efficiency.
The matrix exponential of A. Method used may be chosen from the options available in expm
.
Uffe Høgsbro Thygesen
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) # complex matrix B <- matrix(1:4, ncol = 2) # real matrix matexp(A) matexp(A, "Ward77") # uses Ward77's method in function expm matexp(B) matexp(B, "Taylor") # uses Taylor's method in function expm
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) # complex matrix B <- matrix(1:4, ncol = 2) # real matrix matexp(A) matexp(A, "Ward77") # uses Ward77's method in function expm matexp(B) matexp(B, "Taylor") # uses Taylor's method in function expm
matlog
computes the (principal) matrix logarithm of a square matrix.
matlog(A, ...)
matlog(A, ...)
A |
a square matrix, real or complex. |
... |
arguments passed to or from other methods. |
This function adapts function logm
from package expm
to be able to handle complex matrices, by decomposing the original matrix
into real and purely imaginary matrices and creating a real
block matrix that function logm
can successfully process.
If the original matrix is real, matlog
calls logm
directly for maximum efficiency.
Hence, for real matrices, matlog
can compute the matrix logarithm in the same instances as logm
;
for complex matrices, matlog
can compute the matrix logarithm as long as all real
eigenvalues are positive: zero eigenvalues imply singularity (and therefore the log
does not exist) and negative eigenvalues can be problematic as it may be hard and
numerically unstable to calculate Jordan blocks. See references below.
The matrix logarithm of A. Method used may be chosen from the options available in logm
.
Albert Dorador
For more on the matrix logarithm, visit https://en.wikipedia.org/wiki/Logarithm_of_a_matrix
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) # complex matrix B <- matrix(c(2, 0, 3, 4), ncol = 2) # real matrix with positive eigenvalues matlog(A) matlog(A, "Eigen") # uses Eigen method in function logm matlog(B) matlog(B, "Eigen") # uses Eigen method in function logm
A <- matrix(c(1, 2, 2+3i, 5), ncol = 2) # complex matrix B <- matrix(c(2, 0, 3, 4), ncol = 2) # real matrix with positive eigenvalues matlog(A) matlog(A, "Eigen") # uses Eigen method in function logm matlog(B) matlog(B, "Eigen") # uses Eigen method in function logm