Ask Your Question

Revision history [back]

Ragarding the first question, you should understand that when you write

sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))

A is a Python name, that is a kind of pointer to the matrix object, but the matrix object doesn't know anything about which names point to it. For example, you can then do:

sage: B = A

Here B is just another Python name pointing to the very same object in memory as you can check with:

sage: B is A
True

So, if you want to type A.as_symbol(), there is nowhere in the matrix object that registered the fact that the matrix has "A" as a string representation.

Of course, there could be cumbersome ways to try to find one of the Python names of the matrix object within the locals() dictionary and pick the first one that appears, but it is kind of artificial (maybe a good exercise to understand what appends with Python names though).

Regarding the second question, it is a bit different, since the symbolic variable knows a string reprentation, and you can even tune a latex one as follows:

sage: A = SR.var('A', latex_name="\Bold{A}")
sage: B = SR.var('B', latex_name="\Bold{B}")
sage: c = SR.var('c')
sage: A*B
A*B
sage: latex(A*B)
{\Bold{A}} {\Bold{B}}

sage: e = (A+B)*c
sage: e
(A + B)*c
sage: e.expand()
A*c + B*c
sage: latex(e.expand())
{\Bold{A}} c + {\Bold{B}} c

Ragarding the first question, you should understand that when you write

sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))

A is a Python name, that is a kind of pointer to the matrix object, but the matrix object doesn't know anything about which names point to it. For example, you can then do:

sage: B = A

Here B is just another Python name pointing to the very same object in memory as you can check with:

sage: B is A
True

So, if you want to type A.as_symbol(), there is nowhere in the matrix object that registered the fact that the matrix has "A" as a string representation.

Of course, there could be cumbersome ways to try to find one of the Python names of the matrix object within the locals() dictionary and pick the first one that appears, but it is kind of artificial (maybe a good exercise to understand what appends with Python names though).though). A limited list is provided by show_identifiers()

Regarding the second question, it is a bit different, since the symbolic variable knows a string reprentation, and you can even tune a latex one as follows:

sage: A = SR.var('A', latex_name="\Bold{A}")
sage: B = SR.var('B', latex_name="\Bold{B}")
sage: c = SR.var('c')
sage: A*B
A*B
sage: latex(A*B)
{\Bold{A}} {\Bold{B}}

sage: e = (A+B)*c
sage: e
(A + B)*c
sage: e.expand()
A*c + B*c
sage: latex(e.expand())
{\Bold{A}} c + {\Bold{B}} c

Ragarding the first question, you should understand that when you write write:

sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))

A is a Python name, that is a kind of pointer to the matrix object, but the matrix object doesn't know anything about which names point to it. For example, you can then do:

sage: B = A

Here B is just another Python name pointing to the very same object in memory as you can check with:

sage: B is A
True

So, if you want to type A.as_symbol(), there is nowhere in the matrix object that registered the fact that the matrix has "A" as a string representation.

Of course, there could be cumbersome ways to try to find We can define a function that finds one of the Python names of the matrix object within the locals()globals() dictionary and pick the first one that appears, appears (and that is not ugly like _42), but it is kind of artificial (maybe a good exercise to understand what appends with Python names though). A limited list is provided by show_identifiers()

artificial, like:

sage: def sym(a): ....: for i in globals(): ....: if not i.startswith('_') and globals()[i] is a: ....: return SR.var(i, latex_name="Bold{{{}}}".format(i))

You have:

sage: latex(sym(A))
{\Bold{A}}

But of course:

sage: latex(sym(B))
{\Bold{A}}

Regarding the second question, it is a bit different, since the symbolic variable knows a string reprentation, reprentation of itself, and you can even tune a latex one as follows:

sage: A = SR.var('A', latex_name="\Bold{A}")
sage: B = SR.var('B', latex_name="\Bold{B}")
sage: c = SR.var('c')
sage: A*B
A*B
sage: latex(A*B)
{\Bold{A}} {\Bold{B}}

sage: e = (A+B)*c
sage: e
(A + B)*c
sage: e.expand()
A*c + B*c
sage: latex(e.expand())
{\Bold{A}} c + {\Bold{B}} c

Ragarding the first question, you should understand that when you write:

sage: A = matrix(2,2,var('a', 'b', 'c', 'd'))

A is a Python name, that is a kind of pointer to the matrix object, but the matrix object doesn't know anything about which names point to it. For example, you can then do:

sage: B = A

Here B is just another Python name pointing to the very same object in memory as you can check with:

sage: B is A
True

So, if you want to type A.as_symbol(), there is nowhere in the matrix object that registered the fact that the matrix has "A" as a string representation.

We can define a function that finds one of the Python names of the matrix object within the globals() dictionary and pick the first one that appears (and that is not ugly like _42), but it is kind of artificial, like:

sage: def sym(a):
....:     for i in globals():
....:         if not i.startswith('_') and globals()[i] is a:
....:             return SR.var(i, latex_name="Bold{{{}}}".format(i))

latex_name="\Bold{{{}}}".format(i))

You have:

sage: latex(sym(A))
{\Bold{A}}

But of course:

sage: latex(sym(B))
{\Bold{A}}

Regarding the second question, it is a bit different, since the symbolic variable knows a string reprentation of itself, and you can even tune a latex one as follows:

sage: A = SR.var('A', latex_name="\Bold{A}")
sage: B = SR.var('B', latex_name="\Bold{B}")
sage: c = SR.var('c')
sage: A*B
A*B
sage: latex(A*B)
{\Bold{A}} {\Bold{B}}

sage: e = (A+B)*c
sage: e
(A + B)*c
sage: e.expand()
A*c + B*c
sage: latex(e.expand())
{\Bold{A}} c + {\Bold{B}} c