First time here? Check out the FAQ!

Ask Your Question
0

How to substitute variables in a rational function by numbers?

asked 1 year ago

lijr07 gravatar image

I have a program in which the first part needs to use a polynomial ring in a[1], ..., a[n]. In the last part of the program, I need to substitute a[i] in a rational function by some numbers. I define

Ra=PolynomialRing(QQ,'a',n0)
Fa=Ra.fraction_field()
a=Ra.gens()

For example, I have a rational function

r1=(a[1]^2+a[2])/(a[3]+2*a[4])

I tried the following:

a_random=[2,4,2,5]
for i in [1..4]:
    a[i]=a_random[i-1]

But it has an error:

'tuple' object does not support item assignment

How to solve this problem? Thank you very much.

Preview: (hide)

Comments

1

Like that

sage: Ra=PolynomialRing(QQ,'a',6)
....: Fa=Ra.fraction_field()
....: a=Ra.gens()
sage: r1=(a[1]^2+a[2])/(a[3]+2*a[4])
sage: r1(0,1,2,3,4,5)
3/11
FrédéricC gravatar imageFrédéricC ( 1 year ago )

On a side note, indexing is Sage starts with 0, and so there is also variable a[0].

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

1 Answer

Sort by » oldest newest most voted
1

answered 1 year ago

Max Alekseyev gravatar image

updated 1 year ago

Besides providing variable values as arguments suggested by @FrédéricC, one can use method .subs(), which allow arbitrary or partial substitution - for example:

r1.subs( { a[1]:2, a[4]:2*a[2]+a[3] } )
Preview: (hide)
link

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: 1 year ago

Seen: 292 times

Last updated: Mar 26 '24