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.Sat, 30 May 2020 06:27:12 +0200what is the equivalent function of mathematica's FullForm in Sage?https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/ i'm trying to find the internal representation of matrices in sage. In Mathematica we can simply use FullForm, and this will show that matrices are internally lists in Mathematica. But i could not find any function that does the same in Sage!Wed, 27 May 2020 22:11:13 +0200https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/Answer by John Palmieri for <p>i'm trying to find the internal representation of matrices in sage. In Mathematica we can simply use FullForm, and this will show that matrices are internally lists in Mathematica. But i could not find any function that does the same in Sage!</p>
https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?answer=51598#post-id-51598There is no single "internal representation". If a matrix is sparse, then its entries are represented by a dictionary. If not, its entries are a list. You can get either as follows:
sage: m = matrix(2,2, {(1,1): 3})
sage: m.is_sparse()
True
sage: m.dict()
{(1, 1): 3}
sage: m.list()
[0, 0, 0, 3]
sage: m = matrix(2,2, [1,2,3,4])
sage: m.is_sparse()
False
sage: m.dict()
{(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
sage: m.list()
[1, 2, 3, 4]
Is there something particular you are trying to do?
Thu, 28 May 2020 05:20:16 +0200https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?answer=51598#post-id-51598Comment by sal for <p>There is no single "internal representation". If a matrix is sparse, then its entries are represented by a dictionary. If not, its entries are a list. You can get either as follows:</p>
<pre><code>sage: m = matrix(2,2, {(1,1): 3})
sage: m.is_sparse()
True
sage: m.dict()
{(1, 1): 3}
sage: m.list()
[0, 0, 0, 3]
sage: m = matrix(2,2, [1,2,3,4])
sage: m.is_sparse()
False
sage: m.dict()
{(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
sage: m.list()
[1, 2, 3, 4]
</code></pre>
<p>Is there something particular you are trying to do?</p>
https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?comment=51602#post-id-51602I have a homework assignment, where i'm asked to find how certain objects (e.g. matrices) are represented in a CAS. The question is of course a general question and can be solved in other CASs, but since i'm already comfortable with Sage i tried to solve this question using Sage. To make the question clear i will give an example for the internal structure of x - y + z, where x,y,z are all variables, i.e var(x y z).
etb = ExpressionTreeBuilder(vars=('x','y','z')
x = etb.var('x')
y = etb.var('y')
z = etb.var('z')
v = x - y + z
print(v)
for this code you get something like: add(sub(v_0,v_1),v_2). This shows how a symbolic expression like x -y +z is represented in Sage, and i'm trying to do the same for matrices, but it did not work so far.Thu, 28 May 2020 12:13:11 +0200https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?comment=51602#post-id-51602Comment by sal for <p>There is no single "internal representation". If a matrix is sparse, then its entries are represented by a dictionary. If not, its entries are a list. You can get either as follows:</p>
<pre><code>sage: m = matrix(2,2, {(1,1): 3})
sage: m.is_sparse()
True
sage: m.dict()
{(1, 1): 3}
sage: m.list()
[0, 0, 0, 3]
sage: m = matrix(2,2, [1,2,3,4])
sage: m.is_sparse()
False
sage: m.dict()
{(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
sage: m.list()
[1, 2, 3, 4]
</code></pre>
<p>Is there something particular you are trying to do?</p>
https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?comment=51603#post-id-51603If you use the FullForm function in Mathematica, for example on the matrix {{1, 1},{2, 3}} you will get:
List[List[1,1],List[2,3]]Thu, 28 May 2020 12:15:35 +0200https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?comment=51603#post-id-51603Comment by John Palmieri for <p>There is no single "internal representation". If a matrix is sparse, then its entries are represented by a dictionary. If not, its entries are a list. You can get either as follows:</p>
<pre><code>sage: m = matrix(2,2, {(1,1): 3})
sage: m.is_sparse()
True
sage: m.dict()
{(1, 1): 3}
sage: m.list()
[0, 0, 0, 3]
sage: m = matrix(2,2, [1,2,3,4])
sage: m.is_sparse()
False
sage: m.dict()
{(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
sage: m.list()
[1, 2, 3, 4]
</code></pre>
<p>Is there something particular you are trying to do?</p>
https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?comment=51637#post-id-51637As I said, there is no single internal representation: it depends on whether the matrix is dense or sparse. So what are you hoping for? Probably the best way to understand internal representations for Sage matrices is to read the source code: https://git.sagemath.org/sage.git/tree/src/sage/matrix, and for example https://git.sagemath.org/sage.git/tree/src/sage/matrix/constructor.pyxSat, 30 May 2020 06:27:12 +0200https://ask.sagemath.org/question/51590/what-is-the-equivalent-function-of-mathematicas-fullform-in-sage/?comment=51637#post-id-51637