How to generate a matrix whose indicate from 0 to 3

59 Views Asked by At

When I use:

Matrix(4, symbol = g, shape = symmetric)

I get a matrix of g_{1,1} to g_{4,4}.

However what I want is g_{0,0} to g_{3,3}.

What can I do?

2

There are 2 best solutions below

0
On BEST ANSWER

You can try this

> f := (i, j)->g(i-1, j-1);
> Matrix(4, symbol = g, shape = symmetric, f);

But this gives a function for $g$, I don't know how to set a symbol instead.

0
On

The following creates a Matrix G whose entries range from g[0,0] (which pretty-prints subscripted) to g[3,3].

G:=Matrix(4, (i,j)->g[i-1,j-1], shape = symmetric)

# Indexing into a Matrix starts at 1.
#
G[1,1], G[4,4];

# Assignments are made respecting the symmetry.
#
G[2,3] := W;
G[3,2];

G;

The above Matrix G has the symmetric indexing function. But its entries are accessed starting at 1, since its a Matrix.

In Maple you can also create an Array (rather than a Matrix) whose indexing can start at whatever you choose when you create it. So you could have its indexing start from 0, say.

However, the stock symmetric indexing function is not available for an Array whose indexing does not start at 1. But we can construct our own indexing-function (which does just what we'd expect from a symmetric indexing function) for such Arrays.

# indexing function for symmetric A whose entries don't need to start from 1.
# You only need to create this once per session (or LibraryTools:-Save it for re-use).
#
`index/ssymmetric` := proc(idx::list(nonnegint), P::Array, val::list)
   local idx1, init_val;
   if nops(idx) <> 2 then
     error "ssymmetric custom indexing requires exactly 2 indices";
   end if;
   if nargs = 2 then
     if idx[2]>idx[1] then
       P[idx[1],idx[2]];
     else
       P[idx[2],idx[1]];
     end if;
   else
     try
       if idx[2]>idx[1] then
         P[idx[1],idx[2]] := op(val);
       else
         P[idx[2],idx[1]] := op(val);
       end if;
     catch "unable to store":
       op( val );
     end try;
  end if;
end proc:


B:=Array(ssymmetric,0..3,0..3, (i,j)->`if`(j>i,g[i,j],g[j,i])):

# This Array's indexing starts at 0.
#
B[0,0], B[2,3], B[3,2];

B[1,2] := W;
B[2,1];

# To print it nicely, we can wrap it in a `Matrix` call.
Matrix(B);