1
$\begingroup$

I'm working a lot with nested lists of $(x,y)$ coordinates (of a FEM-like mesh, but that's not important), like so:

nodes = { {{0,0}, {0,1}, {0,2}}, {{1,0}, {1,1}, {1,2}} }

This has the nice convenient structure that nodes[[i,j]] gives me the $(x,y)$ coordinates of my "$(i,j)$th" node, so that nodes[[i,j]][[1]] is my $x$ coordinate and nodes[[i,j]][[2]] is my $y$ coordinate. This works nicely for my purposes.

How would I construct a variable size array of this kind using array[] et. al? If I use for instance

nodes = array[array[X,2],{4,4}]

I get a 4-by-4 array where nodes[[i,j]] is of the form {X[1],X[2]}[1,1]. This is close to what I want, and I could change a bunch of my code to work with this, but I'd rather not - I'd love to have something where nodes[[i,j]] looks like this:

{X[i,j][1], X[i,j][2]}

The reason I'd like to do this is I have defined some functions that take an array of nodes as input and computes some quantity, i.e. F[nodes_]:= (*something*), and I'd like to compute derivatives of this function with respect to each of its variables, i.e. $F_{x_i}$ or $F_{y_j}$.

I've tried various things with no success, and I've also tried looking for a similar construct in the documentation and here on SE, but I didn't see anything. Any ideas?

Edit: I had too many square braces in some of my code before. Fixed.

$\endgroup$
  • $\begingroup$ I'm not quite sure what is it that you want. Does Array[{x[##][1], x[##][2]} &, {4, 4}] work for you? Also, shouldn't array be upper case? $\endgroup$ – seismatica Aug 7 '14 at 0:40
2
$\begingroup$

A simpler way to give an array whose $(i,j)$ element is {X[i,j][1], X[i,j][2]} is to just use Table:

m = 3;
n = 4;
Table[X[i, j][k], {i, m}, {j, n}, {k, 2}]

Output is:

{{{X[1, 1][1], X[1, 1][2]}, {X[1, 2][1], X[1, 2][2]}, {X[1, 3][1], 
   X[1, 3][2]}, {X[1, 4][1], X[1, 4][2]}}, {{X[2, 1][1], 
   X[2, 1][2]}, {X[2, 2][1], X[2, 2][2]}, {X[2, 3][1], 
   X[2, 3][2]}, {X[2, 4][1], X[2, 4][2]}}, {{X[3, 1][1], 
   X[3, 1][2]}, {X[3, 2][1], X[3, 2][2]}, {X[3, 3][1], 
   X[3, 3][2]}, {X[3, 4][1], X[3, 4][2]}}}

Is this what you are looking for?

$\endgroup$
  • $\begingroup$ I'll have to check to see if this works when I'm back at my office tomorrow. $\endgroup$ – icurays1 Aug 7 '14 at 1:45
  • $\begingroup$ +1 on your answer but I prefer Array for this as: (1) it does not introduce additional Symbols (2) the array dimensions are adjacent e.g. {7, 7, 2} (3) it is easy to enter a list of dimensions, e.g. dims = {4, 4, 2}; Array[X[#, #2][#3] &, dims]. See my answer below. $\endgroup$ – Mr.Wizard Aug 7 '14 at 4:02
2
$\begingroup$

I believe this does what you requested:

nodes = Array[X[#, #2][#3] &, {7, 7, 2}];

Check:

nodes[[2, 3]]
nodes[[5, 4]]
nodes[[7, 1]]
{X[2, 3][1], X[2, 3][2]}
{X[5, 4][1], X[5, 4][2]}
{X[7, 1][1], X[7, 1][2]}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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