Ginv {lazy.mat} | R Documentation |
Calculates Generalized Inverse of the input matrix X.
Ginv( X, MP = 0, method = 1, orth = 0, MP1 = 0, method1 = 1, MP2 = 0, method2 = 1, nopermute = 0, eps = 1e-08, MASS = 1, tol = .Machine$double.eps, chkginv = 0, epsc = 1e-08 )
X |
An input matrix |
MP |
= 1 to calculate Moor-Penrose inverse. |
method |
See the descriptions below. |
orth |
Option for |
MP1 |
The value of MP option when this function is used recursively. |
method1 |
The value of method option when this function is used recursively. |
MP2 |
The value of MP option when this function is used recursively. |
method2 |
The value of method option when this function is used recursively. |
nopermute |
= 1 not allow the permutation of rows/cols by |
eps |
Tolerance for pivot in |
MASS |
= 0 not to use |
tol |
A relative tolerance to detect zero singular values. |
chkginv |
= 1 to check if the result is valid. |
epsc |
tolerance for chkinv. |
The input matrix X
must be a real matrix.
The combination of MP= and method= options defines the method to
calculate the Moor-Penrose and a generalized invers as follows.
Denoting a generalized inverse of X
as ginv(X)
,
MP=1 1 svd: If X=U %*% D %*% t(V), MP(X)= V %*% ginv(D) %*% t(U) 2 QR: If X=Q %*% R, MP(X)=t(R)%*%inv(R%*%t(R))%*%inv(t(Q)%*%Q)%*%t(Q) 3 ginv1-1: MP(X)=t(X)%*%X%*%ginv(t(X)%*%X%*%t(X)%*%X)%*%t(X) 3 ginv1-2: MP(X)=t(X)%*%ginv(X%*%t(X)%*%X%*%t(X))%*%X%*%t(X) 4 ginv2: MP(X)=t(X)%*%ginv(X%*%t(X))%*%X%*%ginv(t(X)%*%X)%*%t(X) MP=0 1 Sweep: Make X square by augmenting 0's and use matSwp 2 QR: If X=Q %*% R, ginv(X)=ginv(R)%*%t(Q) 3 rank factorization: If X=B %*% C, ginv(X)=ginv(C)%*%ginv(B) 4 ginv1-1: ginv(X)=ginv(t(X)%*%X) %*% t(X) 4 ginv1-2: ginv(X)=X %*% ginv(X %*% t(X) )
Since Moor-Penrose inverse is unique, if MP=1
the above four methods should result in the same matrix.
If MP=0
the result may vary.
When MP=1, method=1, MASS=1
, the algorithm should be equivalent to
ginv
of MASS package.
orth is available in the following combinations of (MP,method). MP=1, method=2 MP=2, method=3 MP1 is available in the following combinations of (MP,method). MP=0, method=2 MP=0, method=2, 3, 4 method1 is available in the following combinations of (MP,method). MP=1, method=3, 4 MP=0, method=2, 3, 4 MP2 is available in the following combinations of (MP,method). MP=0, method=3 method2 is available in the following combinations of (MP,method). MP=1, method=4 MP=0, method=3
Note that, in the older version prior to 20200703,
MP=0
meant always use matSwp
to calculate g-inverse.
Since it is possible to calculate Moor-Penrose inverse using matSwp
,
this option is misleading!!
Also, in the old version,
MASS=0
was the default setting
and if MP=0
, a generalized inverse of a rectangular matrix
cannot be calculated using matSwp
. (Caused error.)
In this version, MASS=1
is the default setting and
if MP=0
, the Moor-Penrose inverse of a rectangular matrix
will be calculated using matSwp
.
Note that the following matrices generated from ginvX
are also a generalized inverses of X
satisfying condition g1 because
(I-X%*%ginvX)) %*% X = 0, X %*% (I-ginvX%*%X)) = 0, Q(X) %*% X = 0
. See the examples below.
ginvX = Ginv(X, ....) ginvX1 = ginvX + Y %*% (I-X%*%ginvX) ginvX2 = ginvX + (I-ginvX%*%X) %*% Y ginvX3 = ginvX + Y %*% Q( X, Ginv=1 )
where Q(X, Ginv=1)
is the projection operator to the orthogonal
complement of the column space of X
calculated with
Ginv=1
option and Y
is any ncol(X) x nrow(X)
matrix.
set.seed(1701) nr=9 nc=6 num=0:9 A=matrix(sample(num,nr*nc,replace=1),nr,nc) A=cbind(0,A[,1],0,A[,2],0,A[,1]+A[,2],0) A=rbind(0,A[1,],0,A[2,],0,A[3:nrow(A),],0) rownames(A)=paste("r",1:nrow(A),sep="") colnames(A)=paste("c",1:ncol(A),sep="") ### A=1:3 ### A=t(A) MP=1 for( method in 1:4 ){ Print(MP,method) ginvA=Ginv( A, MP=MP, method=method, chkginv=2 ) Print(A,round(ginvA, 5)) } MP=0 for( method in 1:4 ){ Print(MP,method) ginvA=Ginv( A, MP=MP, method=method, chkginv=2 ) Print(A,round(ginvA, 5)) } # generating generalized inverses from a generalized inverse set.seed(1701) Y=matrix( sample( 0:9, ncol(A)*nrow(A), replace=1), ncol(A), nrow(A) ) ginvA=Ginv( A, MP=0, method=1 ) Print(ginvA) ginvA2=ginvA + Y%*%(diag(nrow(A))-A%*%ginvA) temp=chkginv( A, ginvA2, print=2 ) Print(ginvA2) ginvA3=ginvA + (diag(ncol(A))-ginvA%*%A)%*%Y temp=chkginv( A, ginvA3, print=2 ) Print(ginvA3) ginvA4 =ginvA + Y%*%Q( A, Ginv=1 ) temp=chkginv( A, ginvA4, print=1) Print(ginvA4)