Ask Your Question
1

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

asked 2020-07-20 13:21:13 +0100

andriam gravatar image

updated 2020-07-21 17:15:53 +0100

slelievre gravatar image

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

Instead, I get an error:

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 flag offensive close merge delete

Comments

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 ?

andriam gravatar imageandriam ( 2020-07-20 13:23:03 +0100 )edit

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).

slelievre gravatar imageslelievre ( 2020-07-21 17:16:47 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-07-20 15:05:15 +0100

Emmanuel Charpentier gravatar image

updated 2020-07-20 15:17:06 +0100

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
edit flag offensive delete link more

Comments

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 !

andriam gravatar imageandriam ( 2020-07-20 15:29:34 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-07-20 13:21:13 +0100

Seen: 573 times

Last updated: Jul 21 '20