ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 27 May 2020 18:40:44 +0200Hypermatrices?https://ask.sagemath.org/question/51583/hypermatrices/ Hi all,
I want to store and manipulate arrays of $n\times n\times n$ numbers
$$x_{ijk}\quad\text{where}\quad 0\leq i, j, k\leq n-1$$
with Sage.
Here is what I have been doing so far:
`import numpy as np`
`n = 7`
`X = np.zeros((n, n, n))`
`X[2][4][1] = 17`
etc...
Remark: So $X$ is a so called 3-hypermatrix.
**Question:** Are NumPy arrays my best option when handling hypermatrices with Sage?
**Precision**: I will need to manipulate lists of such hypermatrices, and even hypermatrices whose elements will be hypermatrices, e.g.
$$X_{ijk}^{(abcd)}$$
and loop over such lists, e.g. for a ..., for b ..., for c ..., do ... with the 3-hypermatrix $X^{(abcd)}$.Wed, 27 May 2020 15:54:24 +0200https://ask.sagemath.org/question/51583/hypermatrices/Answer by tmonteil for <p>Hi all,</p>
<p>I want to store and manipulate arrays of $n\times n\times n$ numbers
$$x_{ijk}\quad\text{where}\quad 0\leq i, j, k\leq n-1$$
with Sage.</p>
<p>Here is what I have been doing so far:</p>
<p><code>import numpy as np</code></p>
<p><code>n = 7</code></p>
<p><code>X = np.zeros((n, n, n))</code></p>
<p><code>X[2][4][1] = 17</code></p>
<p>etc...</p>
<p>Remark: So $X$ is a so called 3-hypermatrix.</p>
<p><strong>Question:</strong> Are NumPy arrays my best option when handling hypermatrices with Sage?</p>
<p><strong>Precision</strong>: I will need to manipulate lists of such hypermatrices, and even hypermatrices whose elements will be hypermatrices, e.g.
$$X_{ijk}^{(abcd)}$$
and loop over such lists, e.g. for a ..., for b ..., for c ..., do ... with the 3-hypermatrix $X^{(abcd)}$.</p>
https://ask.sagemath.org/question/51583/hypermatrices/?answer=51587#post-id-51587It depends on your use, but if you want to build complex constructions out of them on the long term, i would suggest to define your own Hypermatrix/Hypermatrices element and parent classes, so that changing base ring, product, etc will be less painful, see the folliwing links:
- https://doc.sagemath.org/html/en/tutorial/tour_coercion.html
- https://doc.sagemath.org/html/en/thematic_tutorials/index.html#parents-elements-categories-and-algebraic-structures
- https://doc.sagemath.org/html/en/reference/structure/index.html
Now i am not sure about having numpy arrays as a backend. Those are fast, but since the `dtype` will be any object, i am not sure about the benefit.
Wed, 27 May 2020 18:40:44 +0200https://ask.sagemath.org/question/51583/hypermatrices/?answer=51587#post-id-51587