1

I need to create an $n\times a \times b$ array in GAP. How do I do that fast?

I tried putting

e := NullMat(a,b);
Array := [];
for i in [1..n] do
Array[i] := e;
od;

However, then when I try to change a single entry, GAP changes it in every copy of $e$. And this is far from ideal.

  • If there is another way of initialising a multidimensional array, I'll be really happy too. – Paweł Piwek Aug 15 '19 at 19:04
  • The reason my version isn't working may be due to NullMat being mutable. – Paweł Piwek Aug 15 '19 at 19:07
  • 2
    In related programming languages such as javascript, objects are technically pointers to memory. Modifying one modifies the memory to which it is pointing, causing all other variables who point to the same memory to be affected as well. Surely there should be a "new" keyword so that you can initialize new memory rather than reuse the same pointer each time. Something like changing the line from Array[i] := e to Array[i] := new NullMat(a,b) because setting it to e each time is just pointing it to the same memory location each and every time. – JMoravitz Aug 15 '19 at 19:08
  • See this related question explaining the phenomenon in javascript quite well. – JMoravitz Aug 15 '19 at 19:09
  • 2
    Indeed, what @JMoravitz writes is what happens. e is a pointer to the same object. You could use StructuralCopy(e) to get an equal, but disjoint object. – ahulpke Aug 15 '19 at 20:13

1 Answers1

1

Got it! The following construction works alright. However, I don't know how to do that in an arbitrary dimension.

Array := [];
for i in [1..n] do
    Array[i] := NullMat(a,b);
od;