solvetensor.RdWe can formulate linear equation systems with tensors. This functions solves these systems or gives a least squares fit of minimal norm.
# S3 method for class 'tensor'
solve(a,b,i,j=i,...,allowSingular=FALSE,eps=1E-10,by=NULL)The a of ax=b
The a of ax=b
The dimensions of the equation in a
The dimensions of the equation in b
A boolean, indicating the that a least squares fit should be generated with singular equations systems.
further arguments for generic use
The limit for the smallest singular value in inversion
the operation is done in parallel for these dimensions
a tensor such that ax=b as good as possible for each combination of by values.
A tensor can be seen as a linear mapping of a tensor to a tensor. Let denote \(R_i\) the space of real tensors with dimensions \(i_1...i_d\).
Solves the equation for \(a_{i_1...i_dk_1...k_p}\), \(b_{j_1...j_dl_1...l_q}\) and \(x_{k_1...k_pl_1...l_q}\) the equation $$ \sum_{k_1,...,k_p} a_{i_1...i_dk_1...k_p}x_{k_1...k_pl_1...l_q}= b_{j_1...j_dl_1...l_q}$$.
R1 <- matrix(rnorm(9),nrow=3)
R1i <- solve(R1)
R2 <- to.tensor(R1,c(a=3,b=3),what=1:2)
R2i <- to.tensor(R1i,c(b=3,a=3),what=1:2)
inv.tensor(R2,"a","b") - R2i
#> b
#> a [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#> attr(,"class")
#> [1] "tensor" "matrix"
inv.tensor(R2,"a","b",allowSingular=TRUE) - R2i
#> b
#> a [,1] [,2] [,3]
#> [1,] -2.220446e-16 3.330669e-16 2.220446e-16
#> [2,] 1.110223e-15 -4.440892e-16 -4.440892e-16
#> [3,] 1.665335e-16 -6.938894e-17 -1.110223e-16
#> attr(,"class")
#> [1] "tensor" "matrix"
inv.tensor(rep(R2,4,1,"K"),"a","b",by="K") - rep(R2i,4,1,"K")
#> , , 1
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#>
#> , , 2
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#>
#> , , 3
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#>
#> , , 4
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] 0 0 0
#> [2,] 0 0 0
#> [3,] 0 0 0
#>
#> attr(,"class")
#> [1] "tensor"
inv.tensor(rep(R2,4,1,"K"),"a","b",by="K",allowSingular=TRUE) - rep(R2i,4,3,"K")
#> , , 1
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] -2.220446e-16 3.330669e-16 2.220446e-16
#> [2,] 1.110223e-15 -4.440892e-16 -4.440892e-16
#> [3,] 1.665335e-16 -6.938894e-17 -1.110223e-16
#>
#> , , 2
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] -2.220446e-16 3.330669e-16 2.220446e-16
#> [2,] 1.110223e-15 -4.440892e-16 -4.440892e-16
#> [3,] 1.665335e-16 -6.938894e-17 -1.110223e-16
#>
#> , , 3
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] -2.220446e-16 3.330669e-16 2.220446e-16
#> [2,] 1.110223e-15 -4.440892e-16 -4.440892e-16
#> [3,] 1.665335e-16 -6.938894e-17 -1.110223e-16
#>
#> , , 4
#>
#> b
#> a [,1] [,2] [,3]
#> [1,] -2.220446e-16 3.330669e-16 2.220446e-16
#> [2,] 1.110223e-15 -4.440892e-16 -4.440892e-16
#> [3,] 1.665335e-16 -6.938894e-17 -1.110223e-16
#>
#> attr(,"class")
#> [1] "tensor"
R3 <- to.tensor(rnorm(15),c(a=3,z=5))
mul.tensor(R2i,"b",mul.tensor(R2,"a",R3)) # R3
#> z
#> a [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.7645733 -0.002574686 1.3969230 0.5453780 -0.71234219
#> [2,] 0.5644686 0.588749938 0.8254166 -0.5363845 -0.09456646
#> [3,] -0.1753214 0.175317158 0.5547305 -1.8392675 0.24301597
#> attr(,"class")
#> [1] "tensor" "matrix"
solve.tensor(R2i,R3[[z=1]],"a")
#> [1] -0.6411188 0.7863138 -0.4681300
#> attr(,"class")
#> [1] "tensor"
mul.tensor(R2,"a",R3[[z=1]])
#> [1] -0.6411188 0.7863138 -0.4681300
#> attr(,"class")
#> [1] "tensor"
solve.tensor(R2i,R3,"a")
#> z
#> b [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.6411188 -0.21426231 -1.4221872 0.3874779 0.4559804
#> [2,] 0.7863138 -0.08542275 -1.3466502 5.0082949 -0.6490440
#> [3,] -0.4681300 0.14586747 0.3214056 -2.4464106 0.4786915
#> attr(,"class")
#> [1] "tensor" "matrix"
mul.tensor(R2,"a",R3)
#> z
#> b [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.6411188 -0.21426231 -1.4221872 0.3874779 0.4559804
#> [2,] 0.7863138 -0.08542275 -1.3466502 5.0082949 -0.6490440
#> [3,] -0.4681300 0.14586747 0.3214056 -2.4464106 0.4786915
#> attr(,"class")
#> [1] "tensor" "matrix"
solve.tensor(R2i,R3[[z=1]],"a",allowSingular=TRUE)
#> [1] -0.6411188 0.7863138 -0.4681300
#> attr(,"class")
#> [1] "tensor"
mul.tensor(R2,"a",R3[[z=1]])
#> [1] -0.6411188 0.7863138 -0.4681300
#> attr(,"class")
#> [1] "tensor"
solve.tensor(R2i,R3,"a",allowSingular=TRUE)
#> z
#> b [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.6411188 -0.21426231 -1.4221872 0.3874779 0.4559804
#> [2,] 0.7863138 -0.08542275 -1.3466502 5.0082949 -0.6490440
#> [3,] -0.4681300 0.14586747 0.3214056 -2.4464106 0.4786915
#> attr(,"class")
#> [1] "tensor" "matrix"
mul.tensor(R2,"a",R3)
#> z
#> b [,1] [,2] [,3] [,4] [,5]
#> [1,] -0.6411188 -0.21426231 -1.4221872 0.3874779 0.4559804
#> [2,] 0.7863138 -0.08542275 -1.3466502 5.0082949 -0.6490440
#> [3,] -0.4681300 0.14586747 0.3214056 -2.4464106 0.4786915
#> attr(,"class")
#> [1] "tensor" "matrix"
solve.tensor(rep(R2i,4,1,"K"),R3[[z=1]],"a",by="K")
#> K
#> b [,1] [,2] [,3] [,4]
#> [1,] -0.6411188 -0.6411188 -0.6411188 -0.6411188
#> [2,] 0.7863138 0.7863138 0.7863138 0.7863138
#> [3,] -0.4681300 -0.4681300 -0.4681300 -0.4681300
#> attr(,"class")
#> [1] "tensor" "matrix"
rep(mul.tensor(R2,"a",R3[[z=1]]),4,1,"K")
#> b
#> K [,1] [,2] [,3]
#> [1,] -0.6411188 0.7863138 -0.46813
#> [2,] -0.6411188 0.7863138 -0.46813
#> [3,] -0.6411188 0.7863138 -0.46813
#> [4,] -0.6411188 0.7863138 -0.46813
#> attr(,"class")
#> [1] "tensor" "matrix"
solve.tensor(rep(R2i,4,1,"K"),rep(R3[[z=1]],4,1,"K"),"a",by="K")
#> K
#> b [,1] [,2] [,3] [,4]
#> [1,] -0.6411188 -0.6411188 -0.6411188 -0.6411188
#> [2,] 0.7863138 0.7863138 0.7863138 0.7863138
#> [3,] -0.4681300 -0.4681300 -0.4681300 -0.4681300
#> attr(,"class")
#> [1] "tensor" "matrix"
rep(mul.tensor(R2,"a",R3[[z=1]]),4,1,"K")
#> b
#> K [,1] [,2] [,3]
#> [1,] -0.6411188 0.7863138 -0.46813
#> [2,] -0.6411188 0.7863138 -0.46813
#> [3,] -0.6411188 0.7863138 -0.46813
#> [4,] -0.6411188 0.7863138 -0.46813
#> attr(,"class")
#> [1] "tensor" "matrix"