Ask Your Question
0

Substituting into symbolic expression

asked 2024-04-03 21:50:53 +0200

Road gravatar image

updated 2024-04-04 05:09:51 +0200

Max Alekseyev gravatar image

I have a symbolic (polynomial) expression in A1 and A3. I want to substitute for A1 and A3 values from a ring, possibly other values from the Symbolic Ring, say a1 and a3.

R = SR
a1, a3 = R('a1'), R('a3')

A1, A3 = SR.var('A1, A3')
s = A1 + 2*A3 + 7*A1*A3

PR  = PolynomialRing(R, 'A1,A3')
A1, A3 = PR.gens()

print( PR(s).subs({A1:a1, A3:a3}) )

The result of the code above is 7*A1*A3 + A1 + 2*A3. The expected result is 7*a1*a3 + a1 + 2*a3. The code above works fine when R is some other ring like theGF. How would I make this work?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2024-04-03 23:07:35 +0200

Max Alekseyev gravatar image

updated 2024-04-04 05:15:30 +0200

In your code, conversion PR(s) does not map symbolic variables into polynomial ones. Instead, it creates a zero-degree polynomial, where the whole s is the free term. Also, it's a bad idea to give different variables the same name.

Still, it is possible to force the conversion you want via intermediate conversion of s to a string:

print( PR(str(s)).subs({A1:a1, A3:a3}) )

Personally, I'd prefer a more straightforward and explicit conversion and follow-up substitution like this:

PR.<A1_, A3_> = R[]
s_ = sum(s.coefficient(A1^i * A3^j) * A1_^i * A3_^j for i in range(s.degree(A1)+1) for j in range(s.degree(A3)+1))
print( s_.subs({A1_:a1, A3_:a3}) )
edit flag offensive delete link more

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: 2024-04-03 21:50:53 +0200

Seen: 97 times

Last updated: Apr 04