Testing for quad.form() et seq

In 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:

Test of ht(x) = \(x^*\) = \(\overline{x'}\) (Hermitian transpose):

ht(x)=t(Conj(x))

(jj1 <- Conj(t(x)))
#>                       [,1]                 [,2]
#> [1,] -0.4477310+0.2766719i  0.785738-0.6593387i
#> [2,]  0.5544726+2.1964441i -1.637182-0.4962120i
#> [3,]  0.5689505-0.8659506i -0.414737+0.4727386i
(jj2 <- t(Conj(x)))
#>                       [,1]                 [,2]
#> [1,] -0.4477310+0.2766719i  0.785738-0.6593387i
#> [2,]  0.5544726+2.1964441i -1.637182-0.4962120i
#> [3,]  0.5689505-0.8659506i -0.414737+0.4727386i
(jj3 <- ht(x))
#>                       [,1]                 [,2]
#> [1,] -0.4477310+0.2766719i  0.785738-0.6593387i
#> [2,]  0.5544726+2.1964441i -1.637182-0.4962120i
#> [3,]  0.5689505-0.8659506i -0.414737+0.4727386i
tester(jj1,jj3)
tester(jj2,jj3)

Test of cprod() = \(x^*y\):

cprod(x,y)=crossprod(Conj(x),y)

(jj1 <- ht(x) %*% x1)
#>                     [,1]                [,2]                [,3]
#> [1,] -0.067576+1.386677i  1.682464-0.946496i  2.640650-0.012582i
#> [2,]  0.502847+3.659370i -1.949211-2.333595i  0.823197-5.962069i
#> [3,]  1.092226-2.475800i -1.381254+0.933669i -3.115286+0.700187i
(jj2 <- cprod(x,x1))
#>                     [,1]                [,2]                [,3]
#> [1,] -0.067576+1.386677i  1.682464-0.946496i  2.640650-0.012582i
#> [2,]  0.502847+3.659370i -1.949211-2.333595i  0.823197-5.962069i
#> [3,]  1.092226-2.475800i -1.381254+0.933669i -3.115286+0.700187i
tester(jj1,jj2)

Test of tcprod() = \(x y^*\):

tcprod(x,y)=crossprod(x,Conj(y))

(jj1 <- ht(x1) %*% x)
#>                     [,1]                [,2]                [,3]
#> [1,] -0.067576-1.386677i  0.502847-3.659370i  1.092226+2.475800i
#> [2,]  1.682464+0.946496i -1.949211+2.333595i -1.381254-0.933669i
#> [3,]  2.640650+0.012582i  0.823197+5.962069i -3.115286-0.700187i
(jj2 <- cprod(x1,x))
#>                     [,1]                [,2]                [,3]
#> [1,] -0.067576-1.386677i  0.502847-3.659370i  1.092226+2.475800i
#> [2,]  1.682464+0.946496i -1.949211+2.333595i -1.381254-0.933669i
#> [3,]  2.640650+0.012582i  0.823197+5.962069i -3.115286-0.700187i
tester(jj1,jj2)

Test of quad.form() = \(x^*Mx\):

quad.form(M,x)=crossprod(crossprod(M,Conj(x)),x))

(jj1 <- ht(x) %*% M %*% x)
#>                     [,1]                [,2]                [,3]
#> [1,] -1.547741+1.773951i -4.852972-3.916375i  2.547922-1.362658i
#> [2,]  2.576062+0.564104i  0.891205+7.193438i -3.551549-1.616477i
#> [3,]  1.269916-1.059023i  2.172932+2.863001i -1.581963+0.713271i
(jj2 <- quad.form(M,x))
#>                     [,1]                [,2]                [,3]
#> [1,] -1.547741+1.773951i -4.852972-3.916375i  2.547922-1.362658i
#> [2,]  2.576062+0.564104i  0.891205+7.193438i -3.551549-1.616477i
#> [3,]  1.269916-1.059023i  2.172932+2.863001i -1.581963+0.713271i
tester(jj1,jj2)

Test of 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,] -1.0209226-0.0860373i -0.445781-2.952726i  1.379834+0.561140i
#> [2,]  0.1955698+0.4139097i  0.948652+2.257529i -1.024254-0.941440i
#> [3,]  0.7192430+0.4465591i -1.348483+1.550265i -0.489934-0.705593i
(jj2 <- quad.form(solve(M),x))
#>                       [,1]                [,2]                [,3]
#> [1,] -1.0209226-0.0860373i -0.445781-2.952726i  1.379834+0.561140i
#> [2,]  0.1955698+0.4139097i  0.948652+2.257529i -1.024254-0.941440i
#> [3,]  0.7192430+0.4465591i -1.348483+1.550265i -0.489934-0.705593i
max(abs(jj1-jj2))
#> [1] 0

Test of quad.3form() = \(x^*My\):

quad.3form(M,l,r)=crossprod(crossprod(M,Conj(l)),r)

(jj1 <- ht(x) %*% M %*% x1)
#>                     [,1]                [,2]                [,3]
#> [1,]  0.093017-4.852085i -1.195237+2.910809i -4.661149+4.088932i
#> [2,] -7.088161+5.548433i  3.966632+0.110987i  8.237331+1.989853i
#> [3,]  0.641402+2.505102i  1.040973-2.136414i  2.991954-2.393507i
(jj2 <- quad.3form(M,x,x1))
#>                     [,1]                [,2]                [,3]
#> [1,]  0.093017-4.852085i -1.195237+2.910809i -4.661149+4.088932i
#> [2,] -7.088161+5.548433i  3.966632+0.110987i  8.237331+1.989853i
#> [3,]  0.641402+2.505102i  1.040973-2.136414i  2.991954-2.393507i
tester(jj1,jj2)

Test of quad.3tform() = \(xMy^*\):

quad.3tform(M,l,r)=tcrossprod(left,tcrossprod(Conj(right),M))

(jj1 <- y %*% M %*% ht(y1))
#>                     [,1]                [,2]                [,3]
#> [1,]  1.847370-2.985041i -1.041960-6.111480i -5.809844+4.108842i
#> [2,]  2.305325+3.052278i  6.541041+6.131934i  2.228718-6.881059i
#> [3,] -3.722110-3.651876i -6.403922-5.047652i  0.457250+6.807920i
(jj2 <- quad.3tform(M,y,y1))
#>                     [,1]                [,2]                [,3]
#> [1,]  1.847370-2.985041i -1.041960-6.111480i -5.809844+4.108842i
#> [2,]  2.305325+3.052278i  6.541041+6.131934i  2.228718-6.881059i
#> [3,] -3.722110-3.651876i -6.403922-5.047652i  0.457250+6.807920i
tester(jj1,jj2)

Test of quad.tform() = \(xMx^*\):

quad.tform(M,x)=tcrossprod(x,tcrossprod(Conj(x),M))

(jj1 <- y %*% M %*% ht(y))
#>                     [,1]                [,2]                  [,3]
#> [1,] -8.563844+5.142571i  5.238620-6.317784i  0.3630228-0.1271832i
#> [2,]  4.444020-8.248582i  0.856724+7.263344i -2.0180267+1.7720239i
#> [3,] -0.000737+8.128512i -4.385290-7.141005i  2.6323421-0.7321181i
(jj2 <- quad.tform(M,y))
#>                     [,1]                [,2]                  [,3]
#> [1,] -8.563844+5.142571i  5.238620-6.317784i  0.3630228-0.1271832i
#> [2,]  4.444020-8.248582i  0.856724+7.263344i -2.0180267+1.7720239i
#> [3,] -0.000737+8.128512i -4.385290-7.141005i  2.6323421-0.7321181i
tester(jj1,jj2)

Test of 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,] -4.050932-2.696813i  3.875422+0.719768i  0.2829219+0.391864i
#> [2,]  3.710779-1.836986i -0.071383+2.798082i -2.5801441+0.999014i
#> [3,] -3.938931+4.172207i -0.281766-5.797920i  3.1454557+0.060243i
(jj2 <- quad.tform.inv(M,y))
#>                     [,1]                [,2]                 [,3]
#> [1,] -4.050932-2.696813i  3.875422+0.719768i  0.2829219+0.391864i
#> [2,]  3.710779-1.836986i -0.071383+2.798082i -2.5801441+0.999014i
#> [3,] -3.938931+4.172207i -0.281766-5.797920i  3.1454557+0.060243i
tester(jj1,jj2)

Test of 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] -1.547741+1.773951i  0.891205+7.193438i -1.581963+0.713271i
(jj2 <- diag(quad.form(M,x)))
#> [1] -1.547741+1.773951i  0.891205+7.193438i -1.581963+0.713271i
(jj3 <- quad.diag(M,x))
#> [1] -1.547741+1.773951i  0.891205+7.193438i -1.581963+0.713271i
tester(jj1,jj3)
tester(jj2,jj3)

Test of 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] -8.563844+5.142571i  0.856724+7.263344i  2.632342-0.732118i
(jj2 <- diag(quad.tform(M,y)))
#> [1] -8.563844+5.142571i  0.856724+7.263344i  2.632342-0.732118i
(jj3 <- quad.tdiag(M,y))
#> [1] -8.563844+5.142571i  0.856724+7.263344i  2.632342-0.732118i
tester(jj1,jj3)
tester(jj2,jj3)

Test of quad.3diag() = \(\operatorname{diag}(x^*My)\)

quad.3diag(M,l,r)=colSums(crossprod(M, Conj(left)) * right)

(jj1 <- diag(ht(x) %*% M %*% x1))
#> [1] 0.093017-4.852085i 3.966632+0.110987i 2.991954-2.393507i
(jj2 <- diag(quad.3form(M,x,x1)))
#> [1] 0.093017-4.852085i 3.966632+0.110987i 2.991954-2.393507i
(jj3 <- quad.3diag(M,x,x1))
#> [1] 0.093017-4.852085i 3.966632+0.110987i 2.991954-2.393507i
tester(jj1,jj3)
tester(jj2,jj3)

Test of quad.3tdiag() = \(\operatorname{diag}(xMy^*)\)

quad.3tdiag(M,l,r)=colSums(t(left) * tcprod(M, right))

(jj1 <- diag(y %*% M %*% ht(y1)))
#> [1] 1.847370-2.985041i 6.541041+6.131934i 0.457250+6.807920i
(jj2 <- diag(quad.3tform(M,y,y1)))
#> [1] 1.847370-2.985041i 6.541041+6.131934i 0.457250+6.807920i
(jj3 <- quad.3tdiag(M,y,y1))
#> [1] 1.847370-2.985041i 6.541041+6.131934i 0.457250+6.807920i
tester(jj1,jj3)
tester(jj2,jj3)