Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "recover" a 3-dimensional (2 x 2 x 2) array (a cube) from 3 two dimensional matrices (the cube faces)

I have this array:

    T <- array(c(.25,.1,.1,.1,.05,.1,.1,.2),c(2,2,2))

    # , , 1
    #      [,1] [,2]
    # [1,] 0.25  0.1
    # [2,] 0.10  0.1

    # , , 2
    #      [,1] [,2]
    # [1,] 0.05  0.1
    # [2,] 0.10  0.2

I suppose it can be understood as a kind of "cube" sliced across the third dimension. It has rows (dimension 1), columns (dim 2) and "height" (dim 3), so to speak...

Now I can sum its values across one of these dimensions. There are 3 possible combinations:

    Tm1 <- apply(T0,c(1,2),sum)
    Tm2 <- apply(T0,c(1,3),sum)
    Tm3 <- apply(T0,c(2,3),sum)

Now I have this:

    #> Tm1
    #     [,1] [,2]
    #[1,]  0.3  0.2
    #[2,]  0.2  0.3

    #> Tm2
    #     [,1] [,2]
    #[1,] 0.35 0.15
    #[2,] 0.20 0.30

    #> Tm3
    #     [,1] [,2]
    #[1,] 0.35 0.15
    #[2,] 0.20 0.30

They are the cube "faces".

Is it possible to recover the original array from those 3 matrices? . In other words, is it possible to know the distribution inside this "cube" just by looking at its "faces"?

If so, how to do it? (I mean, the "algebra way" and the R algorithm...)

like image 215
RogerioJB Avatar asked Nov 23 '14 23:11

RogerioJB


2 Answers

Here is how I came up with a solution to your question. First, build the system of equations such that A %*% x = b (where x are the values to solve for, those inside T0):

n <- prod(dim(T0))
b <- c(Tm1, Tm2, Tm3)
m <- length(b)
Ti  <- array(seq_along(T0), dim(T0))
Ti1 <- unlist(apply(Ti, c(1,2), list))
Ti2 <- unlist(apply(Ti, c(1,3), list))
Ti3 <- unlist(apply(Ti, c(2,3), list))

A <- matrix(0, nrow = m, ncol = n)
A[cbind(rep(1:m, each = 2), c(Ti1, Ti2, Ti3))] <- 1

cbind(A, b)
#                          b
#  [1,] 1 0 0 0 1 0 0 0 0.30
#  [2,] 0 1 0 0 0 1 0 0 0.20
#  [3,] 0 0 1 0 0 0 1 0 0.20
#  [4,] 0 0 0 1 0 0 0 1 0.30
#  [5,] 1 0 1 0 0 0 0 0 0.35
#  [6,] 0 1 0 1 0 0 0 0 0.20
#  [7,] 0 0 0 0 1 0 1 0 0.15
#  [8,] 0 0 0 0 0 1 0 1 0.30
#  [9,] 1 1 0 0 0 0 0 0 0.35
# [10,] 0 0 1 1 0 0 0 0 0.20
# [11,] 0 0 0 0 1 1 0 0 0.15
# [12,] 0 0 0 0 0 0 1 1 0.30

A is a non-square matrix so I used a generalized inverse to solve for x:

library(MASS)
xsol <- ginv(A) %*% b
Tsol <- array(xsol, dim(T0))
Tsol

# , , 1
# 
#        [,1]   [,2]
# [1,] 0.2375 0.1125
# [2,] 0.1125 0.0875
# 
# , , 2
# 
#        [,1]   [,2]
# [1,] 0.0625 0.0875
# [2,] 0.0875 0.2125

This solution does not match your initial T0, however you can check that

apply(Tsol, c(1,2), sum)
#      [,1] [,2]
# [1,]  0.3  0.2
# [2,]  0.2  0.3

apply(Tsol, c(1,3), sum)
#      [,1] [,2]
# [1,] 0.35 0.15
# [2,] 0.20 0.30

apply(Tsol, c(2,3), sum)
#      [,1] [,2]
# [1,] 0.35 0.15
# [2,] 0.20 0.30

Conclusion? No, it is not possible to recover your original matrix. Another way to show it is that the rank qr(A)$rank of the A matrix is 7, while you have 8 unknowns. So you would need one extra bit of information, e.g. that T[1, 1] is 0.25 to recover your original array:

A <- rbind(A, c(1, rep(0, n - 1)))
b <- c(b, 0.25)
qr(A)$rank
# [1] 8
xsol <- ginv(A) %*% b
Tsol <- array(xsol, dim(T0))
Tsol
# , , 1

#      [,1] [,2]
# [1,] 0.25  0.1
# [2,] 0.10  0.1

# , , 2

#      [,1] [,2]
# [1,] 0.05  0.1
# [2,] 0.10  0.2
like image 69
flodel Avatar answered Oct 22 '22 05:10

flodel


Here is an algebraic explanation in a more general case with continuous variable. That may help to get the underlying reason you can't do that. The problem is that you can't construct the inverse map. Below, you can substitute integration sign by summation, try to find the inverse matrix, and them reach the result that flodel showed above. So, assume f is integrable in the domain of x,y, and z. Your original table is

$$ w = f(x,y,z) $$

your transformation is

$$t(x) = \int_x f(x,y,z)dx = g(y,z)$$

You want to have an inverse map from t(x) to w. This map would be

$$\frac{\partial t(x)}{\partial x} = \frac{\partial}{\partial x}\left(\int_x f(x,y,z)dx\right) = \frac{\partial}{\partial x}g(y,z) = 0$$

That is, once you integrated out x, you can't recover it from g(y,z).

like image 36
Diogo Avatar answered Oct 22 '22 04:10

Diogo