quad.form()
et seqIn versions prior to 1.2-19, the emulator package included a serious bug in the quad.form()
family of functions in which the complex conjugate of the correct answer was returned (which did not matter in my usual use-case because my matrices were Hermitian). This short vignette demonstrates that the bug has been fixed. Note that the fix was considerably more complicated than simply returning the complex conjugate of the old functions’ value, which would have been terribly inefficient. The actual fix avoids taking more conjugates than absolutely necessary. The vignette checks all the functions in the series, including the ones that have not been changed such as quad.form.inv()
. First load the package:
library("emulator")
We need a helper function to create random complex matrices (NB: we cannot use the cmvnorm
package because that depends on the emulator
package):
rcm <- function(row,col){
matrix(rnorm(row*col)+1i*rnorm(row*col),row,col)
}
Then use this function to define a square matrix M
with complex entries (NB: not Hermitian!), and a couple of rectangular matrices, also complex:
rcm <- function(row,col){matrix(rnorm(row*col)+1i*rnorm(row*col),row,col)}
M <- rcm(2,2)
x <- rcm(2,3)
y <- rcm(3,2)
x1 <- rcm(2,3)
y1 <- rcm(3,2)
Set up a numerical tester function:
tester <- function(a,b,TOL=1e-13){stopifnot(all(abs(a-b)< TOL))}
(previous versions used a tolerance of 1e-15
, which was occasionally not met). Now test each function:
ht(x)
= \(x^*\) = \(\overline{x'}\) (Hermitian transpose):ht(x)=t(Conj(x))
(jj1 <- Conj(t(x)))
#> [,1] [,2]
#> [1,] 0.870649-1.226192i -1.851845+0.982651i
#> [2,] -1.702313-0.797909i 0.446614-1.507498i
#> [3,] 0.819809+2.748713i 1.222249+0.812717i
(jj2 <- t(Conj(x)))
#> [,1] [,2]
#> [1,] 0.870649-1.226192i -1.851845+0.982651i
#> [2,] -1.702313-0.797909i 0.446614-1.507498i
#> [3,] 0.819809+2.748713i 1.222249+0.812717i
(jj3 <- ht(x))
#> [,1] [,2]
#> [1,] 0.870649-1.226192i -1.851845+0.982651i
#> [2,] -1.702313-0.797909i 0.446614-1.507498i
#> [3,] 0.819809+2.748713i 1.222249+0.812717i
tester(jj1,jj3)
tester(jj2,jj3)
cprod()
= \(x^*y\):cprod(x,y)=crossprod(Conj(x),y)
(jj1 <- ht(x) %*% x1)
#> [,1] [,2] [,3]
#> [1,] 0.628905+3.888816i -0.6344387+0.0308265i 2.113318+0.964037i
#> [2,] 2.742292-0.261464i -0.8866514-1.0732534i -2.475396+0.680460i
#> [3,] -3.852238-4.838644i 1.5397820+1.5903646i 0.961218-1.467996i
(jj2 <- cprod(x,x1))
#> [,1] [,2] [,3]
#> [1,] 0.628905+3.888816i -0.6344387+0.0308265i 2.113318+0.964037i
#> [2,] 2.742292-0.261464i -0.8866514-1.0732534i -2.475396+0.680460i
#> [3,] -3.852238-4.838644i 1.5397820+1.5903646i 0.961218-1.467996i
tester(jj1,jj2)
tcprod()
= \(x y^*\):tcprod(x,y)=crossprod(x,Conj(y))
(jj1 <- ht(x1) %*% x)
#> [,1] [,2] [,3]
#> [1,] 0.6289055-3.8888157i 2.742292+0.261464i -3.852238+4.838644i
#> [2,] -0.6344387-0.0308265i -0.886651+1.073253i 1.539782-1.590365i
#> [3,] 2.1133183-0.9640366i -2.475396-0.680460i 0.961218+1.467996i
(jj2 <- cprod(x1,x))
#> [,1] [,2] [,3]
#> [1,] 0.6289055-3.8888157i 2.742292+0.261464i -3.852238+4.838644i
#> [2,] -0.6344387-0.0308265i -0.886651+1.073253i 1.539782-1.590365i
#> [3,] 2.1133183-0.9640366i -2.475396-0.680460i 0.961218+1.467996i
tester(jj1,jj2)
quad.form()
= \(x^*Mx\):quad.form(M,x)=crossprod(crossprod(M,Conj(x)),x))
(jj1 <- ht(x) %*% M %*% x)
#> [,1] [,2] [,3]
#> [1,] 0.122815-1.400712i 2.78326-4.451501i -2.309252+7.865842i
#> [2,] -3.381693+7.928883i 10.08779-3.547028i -8.504243-2.169114i
#> [3,] 6.036492-7.411751i -17.02261+6.538788i 13.310236-3.732440i
(jj2 <- quad.form(M,x))
#> [,1] [,2] [,3]
#> [1,] 0.122815-1.400712i 2.78326-4.451501i -2.309252+7.865842i
#> [2,] -3.381693+7.928883i 10.08779-3.547028i -8.504243-2.169114i
#> [3,] 6.036492-7.411751i -17.02261+6.538788i 13.310236-3.732440i
tester(jj1,jj2)
quad.form.inv()
= \(x^*M^{-1}x\):quad.form.inv(M,x)=cprod(x,solve(M,x))
(jj1 <- ht(x) %*% solve(M) %*% x)
#> [,1] [,2] [,3]
#> [1,] -2.690508+7.593258i -0.968902-6.000642i 5.156946-1.586052i
#> [2,] 4.176078+0.425136i -0.833167+1.609803i -4.134976-2.337773i
#> [3,] -1.387597-9.959677i 2.029122+5.069575i -1.424007+5.337213i
(jj2 <- quad.form(solve(M),x))
#> [,1] [,2] [,3]
#> [1,] -2.690508+7.593258i -0.968902-6.000642i 5.156946-1.586052i
#> [2,] 4.176078+0.425136i -0.833167+1.609803i -4.134976-2.337773i
#> [3,] -1.387597-9.959677i 2.029122+5.069575i -1.424007+5.337213i
max(abs(jj1-jj2))
#> [1] 0
quad.3form()
= \(x^*My\):quad.3form(M,l,r)=crossprod(crossprod(M,Conj(l)),r)
(jj1 <- ht(x) %*% M %*% x1)
#> [,1] [,2] [,3]
#> [1,] 3.908843-2.152917i -1.828889+0.722713i 0.590045+2.008506i
#> [2,] 0.319938+1.507321i -1.923701-2.884151i -5.375589+4.363379i
#> [3,] -4.599672+0.125958i 3.762240+3.441915i 7.351432-6.334776i
(jj2 <- quad.3form(M,x,x1))
#> [,1] [,2] [,3]
#> [1,] 3.908843-2.152917i -1.828889+0.722713i 0.590045+2.008506i
#> [2,] 0.319938+1.507321i -1.923701-2.884151i -5.375589+4.363379i
#> [3,] -4.599672+0.125958i 3.762240+3.441915i 7.351432-6.334776i
tester(jj1,jj2)
quad.3tform()
= \(xMy^*\):quad.3tform(M,l,r)=tcrossprod(left,tcrossprod(Conj(right),M))
(jj1 <- y %*% M %*% ht(y1))
#> [,1] [,2] [,3]
#> [1,] 8.066897-2.763975i -1.8320435+2.0486119i 4.375346-3.664933i
#> [2,] -1.358223+3.771807i 0.5520058-1.5053192i 0.556160+2.950429i
#> [3,] -2.828721+4.280566i 0.4459878-0.2521589i -1.276034+4.215993i
(jj2 <- quad.3tform(M,y,y1))
#> [,1] [,2] [,3]
#> [1,] 8.066897-2.763975i -1.8320435+2.0486119i 4.375346-3.664933i
#> [2,] -1.358223+3.771807i 0.5520058-1.5053192i 0.556160+2.950429i
#> [3,] -2.828721+4.280566i 0.4459878-0.2521589i -1.276034+4.215993i
tester(jj1,jj2)
quad.tform()
= \(xMx^*\):quad.tform(M,x)=tcrossprod(x,tcrossprod(Conj(x),M))
(jj1 <- y %*% M %*% ht(y))
#> [,1] [,2] [,3]
#> [1,] 10.092341-1.018115i -1.0222995-1.7527456i -2.494185-7.568095i
#> [2,] -2.112452+4.241155i 0.6018425-0.7411678i 2.264468+1.608533i
#> [3,] -4.920494+5.028350i 2.7502533-0.0509442i 3.578346+0.424630i
(jj2 <- quad.tform(M,y))
#> [,1] [,2] [,3]
#> [1,] 10.092341-1.018115i -1.0222995-1.7527456i -2.494185-7.568095i
#> [2,] -2.112452+4.241155i 0.6018425-0.7411678i 2.264468+1.608533i
#> [3,] -4.920494+5.028350i 2.7502533-0.0509442i 3.578346+0.424630i
tester(jj1,jj2)
quad.tform.inv()
= \(xM^{-1}x^*\):quad.tform.inv(M,x)=quad.form.inv(M,ht(x))
(jj1 <- y %*% solve(M) %*% ht(y))
#> [,1] [,2] [,3]
#> [1,] 0.9647961-0.1272935i 0.326186-1.068358i -1.929657-1.383623i
#> [2,] 0.6936876+0.4838214i -0.699690+1.519046i 3.357924+0.499942i
#> [3,] -0.6861050+2.0722335i -1.592865-1.280515i -0.270805+3.949169i
(jj2 <- quad.tform.inv(M,y))
#> [,1] [,2] [,3]
#> [1,] 0.9647961-0.1272935i 0.326186-1.068358i -1.929657-1.383623i
#> [2,] 0.6936876+0.4838214i -0.699690+1.519046i 3.357924+0.499942i
#> [3,] -0.6861050+2.0722335i -1.592865-1.280515i -0.270805+3.949169i
tester(jj1,jj2)
quad.diag()
= \(\operatorname{diag}(x^*Mx)\) = diag(quad.form())
:quad.diag(M,x)=colSums(crossprod(M,Conj(x)) * x)
(jj1 <- diag(ht(x) %*% M %*% x))
#> [1] 0.122815-1.400712i 10.087790-3.547028i 13.310236-3.732440i
(jj2 <- diag(quad.form(M,x)))
#> [1] 0.122815-1.400712i 10.087790-3.547028i 13.310236-3.732440i
(jj3 <- quad.diag(M,x))
#> [1] 0.122815-1.400712i 10.087790-3.547028i 13.310236-3.732440i
tester(jj1,jj3)
tester(jj2,jj3)
quad.tdiag()
= \(\operatorname{diag}(xMx^*)\) = diag(quad.tform())
:quad.tdiag(M,x)=rowSums(tcrossprod(Conj(x), M) * x)
(jj1 <- diag(y %*% M %*% ht(y)))
#> [1] 10.0923413-1.0181147i 0.6018425-0.7411678i 3.5783459+0.4246298i
(jj2 <- diag(quad.tform(M,y)))
#> [1] 10.0923413-1.0181147i 0.6018425-0.7411678i 3.5783459+0.4246298i
(jj3 <- quad.tdiag(M,y))
#> [1] 10.0923413-1.0181147i 0.6018425-0.7411678i 3.5783459+0.4246298i
tester(jj1,jj3)
tester(jj2,jj3)
quad.3diag()
= \(\operatorname{diag}(x^*My)\)quad.3diag(M,l,r)=colSums(crossprod(M, Conj(left)) * right)
(jj1 <- diag(ht(x) %*% M %*% x1))
#> [1] 3.908843-2.152917i -1.923701-2.884151i 7.351432-6.334776i
(jj2 <- diag(quad.3form(M,x,x1)))
#> [1] 3.908843-2.152917i -1.923701-2.884151i 7.351432-6.334776i
(jj3 <- quad.3diag(M,x,x1))
#> [1] 3.908843-2.152917i -1.923701-2.884151i 7.351432-6.334776i
tester(jj1,jj3)
tester(jj2,jj3)
quad.3tdiag()
= \(\operatorname{diag}(xMy^*)\)quad.3tdiag(M,l,r)=colSums(t(left) * tcprod(M, right))
(jj1 <- diag(y %*% M %*% ht(y1)))
#> [1] 8.066897-2.763975i 0.552006-1.505319i -1.276034+4.215993i
(jj2 <- diag(quad.3tform(M,y,y1)))
#> [1] 8.066897-2.763975i 0.552006-1.505319i -1.276034+4.215993i
(jj3 <- quad.3tdiag(M,y,y1))
#> [1] 8.066897-2.763975i 0.552006-1.505319i -1.276034+4.215993i
tester(jj1,jj3)
tester(jj2,jj3)