Message Boards Message Boards

Operations with arrays (pseudoinverses & products)

Dear Wolfram Community:

I wish to share a code I made to do some operations with multidimensional arrays, like tensor products, inner products and inverses/pseudoinverses.


  1. To explain in a few lines what I want to get, I will begin with an introduction using abstract symbolic notations.
  2. I would greatly appreciate your feedback to this post, because while my code works fine for simple (small) arrays, it become slow for bigger ones and I suspect this could be a clue that my code is not an efficient one: maybe there is a simpler way to do the same.


I would like to do operations like those made with ordinary vectors "v" and matrices "m", like:

v1==m.v2; x=PseudoInverse[m]; x.v1==v2;

Where m and v are defined as:

dm={dmi,dmj}; dv={dvi};
Element[m, Matrices[dm, Reals]];
Element[v1|v2, Vectors[dv, Reals]];

In other words, for example, given two arrays "A" and "B", with dimensions "dimA", "dimB":

dimA={dAi,dAj,dAk}; dimB={dBi,dBj};
Element[A, Arrays[dimA, Reals]];
Element[B, Arrays[dimB, Reals]];

I would like to compute the "ArrayInner" and the "ArrayPseudoInverse":


So, it will follow that:



Unlike the abstract symbolic tensors given above, the code is done for explicit arrays.

In short, the algorithm for inverting an array is the following:

  1. Transform the multidimensional array in a 2-dimensional one (i.e. a matrix) using a so called "unfolding map", that maps the position of each element of the array to a unique position in an "equivalent" matrix (as detailed in Kolda (2006))
  2. Invert the "equivalent matrix" using the "pseudoinverse" function. I am using the pseudoinverse because usually this equivalent matrix is a singular and rectangular matrix.
  3. Map the inverted matrix into the respective "inverted array". This was the most difficult part, since inverting the array implies some (sometimes unknown) permutation.

Unfortunately, the full code is very lenghty, so for now I will not copy it in the main text (but I will attach it as a notebook at the end of the post).


As an example, I will post the result of inverting an order-3 {I,J,K} array, given in explicit form in the following figure:

1) array A

enter image description here

2) "unfolded array" uA

enter image description here

3) "unfolded pseudoinverse" uX

enter image description here

4) inverted array X

enter image description here

5) Tests to be sure the result is a true inverse: A.X.A==A; X.A.X == X

enter image description here


This post is a follow-up of a question I asked in this forum before finding the solution:

Unfold a tensor into a matrix to do inversion/pseudoinversion?

I am very grateful for the support from user Hans Dolhaine, who showed me very useful tips (like how to use the functions "Module" and the solution to a similar problem done with (square) matrices of (square) matrices.


*) Tamara G. Kolda (2006); "Multilinear operators for higher-order decompositions "; SANDIA REPORT SAND2006-2081

*) Mao-lin Liang, Bing Zheng & Rui-juan Zhao (2018): Tensor inversion and its application to the tensor equations with Einstein product, Linear and Multilinear Algebra

2 Replies

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: Moderation Team

Dear Wolfram Moderation Team:

Good morning, I am very thankfully surprised to you for selecting my post for the "Staff Picks" column!

I took me a lot of time to do this, and after many trial-and-error attemps, I arrived at the form posted yesterday.

Please feel free to re-check the code, entering some more complicated array, changing the dimensionality of the arrays used, etc. just to be sure that there isn't a mistake lurking inside, or something else that may make the computation slow. I will also greatly appreciate suggestions for improvement, maybe the same results could be obtained with a simpler code.

Best regards,

Alberto Silva Ariano

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract