I need to solve a system of ODEs for a function that has a complicated tensorial structure. I believe that NDSolve can deal with this scenario since version 9. Yet, in some cases it cannot figure out the dimensionality of the dependent variable from the initial conditions. That is why I want to map a tensorial function onto a tensor of functions. I do as follows:

Vector (rank-1):

Through[Array[y, {2}][t]]
(* {y[1][t], y[2][t]} *)

Matrix (rank-2):

Through /@ Through[Array[y, {2, 2}][t]]
(* {{y[1, 1][t], y[1, 2][t]}, {y[2, 1][t], y[2, 2][t]}} *)

Tensor (rank-3, naïve approach does not work):

Through /@ Through /@ Through[Array[y, {2, 2, 2}][t]]

Q. What is the proper way of automatically obtaining a rank-$k$ tensorial function?

Why is it needed?

The main goal is to minimize syntactic sugar for solving tensorial ODEs.

1. I will start from an example that can be found in the manual for NDSolve:

a={{1,  1,  1,  1},{1,  2,  1,  2},{1,  1,  3,  1},{1,  2,  1,  4}};

This is a standard approach, where MA automatically figures out that y[x] is a vector. Nice.

2. But we can do the same slightly differently:


MA solves here for 4 scalar functions instead {Y[1][x],Y[2][x],Y[3][x],Y[4][x]}, which is sometimes preferable. We can plot them as follows


3. Now, define a matrix function (two equivalent ways are possible)


Notice that adding a dimension makes the syntax less and less transparent, and the commands become lengthier. Nonetheless, we can solve also this problem


What is disturbing is that going from a vector to a matrix equation more and more syntactic sugar needs to be added, like Thread/@Thread and Flatten. And it will be even less transparent for tensorial functions.

Extended question. What can be done to reduce this burden?


1 Answer 1


You can achieve what you want in the first part of your question using a Map and the levels argument. I've packed this into a function so you can change the function y, dimension, tensor-rank, or argument list (currently just {t}) if needs be:

genTensor[v_, dim_, rank_, args_] :=
  Map[Apply[#, args] &, Array[v, ConstantArray[dim, rank]], {rank}] 

(* 2d vector *)
(* result: {y[1][t], y[2][t]} *)

(* 2d matrix *)
(* result: {{y[1, 1][t], y[1, 2][t]}, {y[2, 1][t], y[2, 2][t]}} *)

(* 2d 3-tensor *)
(* result: {{{y[1, 1, 1][t], y[1, 1, 2][t]}, {y[1, 2, 1][t], y[1, 2, 2][t]}},
   {{y[2, 1, 1][t], y[2, 1, 2][t]},  {y[2, 2, 1][t], y[2, 2, 2][t]}}} *)
  • $\begingroup$ Thank you, it is nice to have this approach. $\endgroup$
    – yarchik
    Jun 28, 2020 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.