# why does Sage find problems when calculating with objects put in a list ?

I define the following function:

def vbl(c, ex):
l = len(c)
V = 1
for i in range(n):
V = V * [c[i]**ex[i]]
return V


I then compute

 sage: vbl(['x, y, z'], [2, 3, 4])


expecting to get

x^2**y^3**z^4


TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'


Yet I can compute x^2, y^3 and z^4 after declaring x, y and z as variables:

sage: var('x, y, z')


What happens?

edit retag close merge delete

when I entered vbl(['x,y,z'],[2,3,4], I expected the above program to return x^2y^3z^4 . Instead, it returns the following error : TypeError : unsupported operand type(s) for ** or pow(): 'str' and 'int'. Yet I can do the following calcinations : X^2,Y^3 and Y^4 when I declared X,Y and Z as variables (var('x,y,z)). What happend ?

I edited your question to add the part that you had written as a comment. If you are happy with the edit, please delete your comment (and I'll delete mine).

Sort by » oldest newest most voted

You have probably mistyped your attempt, which gives another error....

After a few guesses, I supposed that you meant :

def vbl(c,ex):
l=len(c)
V=1
for i in range(l):
V=V*(c[i]**ex[i])
return V


which gives :

sage: vbl([x, y, z],[2, 3, 4])
x^2*y^3*z^4


but :

sage: vbl(["x, y, z"],[2, 3, 4])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

[ Snip... ]
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'


[x, y, z] is a list of three symbolic variables, but ["x, y, z"] is a list of one string...

Note :

sage: prod(map(lambda a,b:a**b,[x, y, z],[2, 3, 4]))
x^2*y^3*z^4
`
more

Many thanks to Emmanuel Charpentier for finding the errors, now the code works well; many thanks also for the precious note, I am very glad !