sum() - TypeError: cannot evaluate symbolic expression numerically

396 views
Skip to first unread message

mrkvon

unread,
Feb 12, 2012, 2:13:13 PM2/12/12
to sage-support
Hello,

what is wrong with this, please? I tried to sum some matrix elements
and got error below. I tried to sum by other means (comment # and ##
in example below) without problem. I even defined my own summation
function (analogic to ##), which at the end made similar error.
I don't think I try to evaluate numerically, so why this error?
I'm new to Sage and even to Python, so it may be trivial.

Input:

x=var('x')
lore4=matrix([[sinh(x),0,0,cosh(x)],[0,1,0,0],[0,0,1,0],[cosh(x),
0,0,sinh(x)]])
k = var('k')
#lore4[0,0]+lore4[1,1]+lore4[2,2]+lore4[3,3]
##z=var('z')
##z=0
##for k in range(0,4):
## z=z+lore4[k,k]
##z
sum(lore4[k,k],k,0,3)

Output:

Traceback (most recent call last): ##for k in range(0,4):
File "", line 1, in <module>

File "/tmp/tmpkNwukQ/___code___.py", line 12, in <module>
exec compile(u'sum(lore4[k,k],k,_sage_const_0 ,_sage_const_3 )
File "", line 1, in <module>

File "matrix0.pyx", line 847, in
sage.matrix.matrix0.Matrix.__getitem__ (sage/matrix/matrix0.c:4669)
File "expression.pyx", line 734, in
sage.symbolic.expression.Expression.__int__ (sage/symbolic/
expression.cpp:4597)
File "expression.pyx", line 4052, in
sage.symbolic.expression.Expression._numerical_approx (sage/symbolic/
expression.cpp:18089)
TypeError: cannot evaluate symbolic expression numerically

Thank you.

D. S. McNeil

unread,
Feb 12, 2012, 2:25:24 PM2/12/12
to sage-s...@googlegroups.com
On Sun, Feb 12, 2012 at 2:13 PM, mrkvon <michal....@gmail.com> wrote:
> Hello,
>
> what is wrong with this, please? I tried to sum some matrix elements
> and got error below. I tried to sum by other means (comment # and ##
> in example below) without problem. I even defined my own summation
> function (analogic to ##), which at the end made similar error.
> I don't think I try to evaluate numerically, so why this error?

Actually, you _are_ trying to evaluate numerically, simply not what
you thought you were. :^)

The problem with

sum(lore4[k,k],k,0,3)

isn't the sum; it's the lore4[k,k]:

----
sage: lore4[k,k]
[...]


TypeError: cannot evaluate symbolic expression numerically

---

It's trying to convert the k to an integer index to access the matrix
elements. By the way, whenever Sage (or Python) gives me a TypeError
I break everything down into its pieces like this and try to find
which one is causing the problems. It doesn't always work, but I'd
say 75% of the time it's an input formatting error on my part that I
can find.

Anyway, I'm not sure if Sage matrices can be indexed by symbolic
variables like this. [Whenever I say you can't do something in Sage
someone proves me wrong, but I'm not familiar with the trick if there
is one.]

In this case, rather than using the symbolic sum 4-term calling style
(expression, var, v0, v1), I'd write it with a generator expression
instead:

---
sage: sum(lore4[k,k] for k in [0..3])
2*sinh(x) + 2
---

or something like that.


Doug

Jason Grout

unread,
Feb 13, 2012, 6:40:20 AM2/13/12
to sage-s...@googlegroups.com
On 2/12/12 1:25 PM, D. S. McNeil wrote:
> Anyway, I'm not sure if Sage matrices can be indexed by symbolic
> variables like this. [Whenever I say you can't do something in Sage
> someone proves me wrong, but I'm not familiar with the trick if there
> is one.]


You're right. Matrices can only be indexed by things that can be
converted to integers (or slices thereof), IIRC.

Jason


mrkvon

unread,
Feb 13, 2012, 5:53:22 PM2/13/12
to sage-support
Thanks for solution and explanation! (Yet difference is still a little
mysterious to me...) Works fine also with multiple sums. It would be
impossible to work with tensors without this.
It would be nice to have some easy way to construct and handle tensors
(Einstein summation convention, multidimensional matrices...)) in
Sage. But that's a different topic...
Reply all
Reply to author
Forward
0 new messages