ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 25 Jun 2023 17:18:23 +0200Substitution of variable in polynomial not working as expected.https://ask.sagemath.org/question/69493/substitution-of-variable-in-polynomial-not-working-as-expected/The `subs` method behaves differently in two related settings:
- univariate polynomials over SR
- multivariate polynomials over SR
when trying to substitute a value for some variable involved as a coefficient.
In the univariate case:
R.<x> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
returns `x`, as expected.
But in the multivariate case:
R.<x, y> = SR[]
a = SR.var('a')
P = a*x
P.subs(a=1)
returns `a*x`.jllbSun, 25 Jun 2023 17:18:23 +0200https://ask.sagemath.org/question/69493/Help with subshttps://ask.sagemath.org/question/66762/help-with-subs/This minimal non-working example returns an output of 'z'. I want 'z^3'! Any help?
B = FunctionField(QQ,'z')
B.inject_variables()
g = z
g.subs(z=z^3)zazFri, 03 Mar 2023 15:24:48 +0100https://ask.sagemath.org/question/66762/How can I substitute an unknown function in an expression with a known function?https://ask.sagemath.org/question/60468/how-can-i-substitute-an-unknown-function-in-an-expression-with-a-known-function/I have defined an unknown function "p" with command
p=function("p")
Then I deduced another function, which contains "p":
aws(time)=921600*(-1333.33340000000*time + 2000)/(-3.36000004800000e6*p(time) + 3840000)
Now I would like to plot the function aws(time), by subtituting p(time) with a known function, like:
p(time)=time^0.54
I discovered I can't use .subs() as with free variables, so this doesn't work:
aws.subs(p(time)==time^0.54)
Then how can I substitute the known function, to be able to plot aws(time) finally?
KonstantinTue, 28 Dec 2021 03:11:56 +0100https://ask.sagemath.org/question/60468/Substituting vars of PowerSeries over RRhttps://ask.sagemath.org/question/59637/substituting-vars-of-powerseries-over-rr/ What is the intended way to substitute variables of PowerSeries (or equivalently LaurentSeries) over RR?
The following code does not produce the correct result:
sage: P.<x> = PowerSeriesRing(RR)
sage: p = x**2+2*x+3
sage: p.subs(x=x**2)
3.00000000000000 + 2.00000000000000*x + 1.00000000000000*x^2
In ZZ, the previous example works correctly:
sage: P.<x> = PowerSeriesRing(ZZ)
sage: p = x**2+2*x+3
sage: p.subs(x=x**2)
3 + 2*x^2 + x^4
Working with PolynomialRings over RR also seems to work correctly, I do however need to work with negative exponents (i.e., LaurentSeries) and therefore cannot easily convert my expressions to polynomials before substituting.david-berghausMon, 08 Nov 2021 11:30:20 +0100https://ask.sagemath.org/question/59637/An error when use subshttps://ask.sagemath.org/question/59656/an-error-when-use-subs/ I defined a simple function as follows.
def BarInvolution(q,l):
r1=l
r=r1.subs(q=1/q)
return r
Then I use the following codes.
R=PolynomialRing(QQ,'a',15)
a=R.gens()
BarInvolution(a[5], a[5]+1)
But it has the following errors:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-212-6b33633fbe0d> in <module>()
1
----> 2 BarInvolution(a[Integer(5)], a[Integer(5)])
<ipython-input-209-4f401596da47> in BarInvolution(q, l)
1 def BarInvolution(q,l):
2 r1=l
----> 3 r=r1.subs(q=Integer(1)/q)
4
5 return r
/Users/jianrongli/SageMath/local/lib/python3.7/site-packages/sage/rings/polynomial/multi_polynomial_libsingular.pyx in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.subs (build/cythonized/sage/rings/polynomial/multi_polynomial_libsingular.cpp:29147)()
3485 gd = parent.gens_dict(copy=False)
3486 for m,v in kw.iteritems():
-> 3487 m = gd[m]
3488 for i from 0 < i <= _ring.N:
3489 if p_GetExp((<MPolynomial_libsingular>m)._poly, i, _ring) != 0:
KeyError: 'q'
How to fix this problem? Thank you very much!
lijr07Tue, 09 Nov 2021 11:51:07 +0100https://ask.sagemath.org/question/59656/subs in vector fieldhttps://ask.sagemath.org/question/55527/subs-in-vector-field/ Hello,
var('a')
E.<x,y,z>=EuclideanSpace()
vf=E.vector_field([a**2,a*x,a*x*y]) # arbitrary vector field
vf.subs(a==1).display()
gives back the vector field without substituting the value of a
a^2 e_x + a*x e_y + a*x*y e_z
How does the funciton subs work with vector fields?
Thanks alot for your help.
curios_mindMon, 01 Feb 2021 04:11:15 +0100https://ask.sagemath.org/question/55527/Simple trigonometric equation solvinghttps://ask.sagemath.org/question/55268/simple-trigonometric-equation-solving/I was a MapleV user in the past (~25 years ago) an I'm very pleased to find such an elegant solution the sage is.
I try to see how it works trying to solve an easy trigonometry calculation:
var('r,l,m')
ex = l/sin(m/2/r)-r
solve(ex,r)
[r == l/sin(1/2*m/r)]
Do not want, ok. To simplify I put some real values m and l and tried again:
exval = ex.subs(l=7.2,m=10)
solve(exval,r)
[r == 36/5/sin(5/r)]
still not solving, r=3.68357 is what I'm searching for. What would be a right way to proceed?
Thank you in advance, v42
vladimir42000Thu, 14 Jan 2021 14:46:18 +0100https://ask.sagemath.org/question/55268/algebraic conditionhttps://ask.sagemath.org/question/55248/algebraic-condition/ Hi,
I have a very simple problem: I have a parameter a which is either 1 or -1. I have some very complicated expressions in which this parameter occurs raised to integer powers a^2, a^3, a^4 etc. I would like to implify those equations substituting the condition a^2=1, but with expr.subs a^3, a^4 etc are not replaced by a, 1, etc.
I would appreciate some help in this issue.tomasortinTue, 12 Jan 2021 13:42:57 +0100https://ask.sagemath.org/question/55248/assignment vs. subs()https://ask.sagemath.org/question/54215/assignment-vs-subs/What am I missing? I can assign "t=H" but subs(t=H) errors out.
reset('t')
I4=4*identity_matrix(5)
t=3
var('t')
t2 = t^2 #random formula example
t=I4
display(t2,t^2)
a1=t2.subs(t=I4)rrogersWed, 11 Nov 2020 16:49:34 +0100https://ask.sagemath.org/question/54215/subs: why it accepts one form but not the other?https://ask.sagemath.org/question/51880/subs-why-it-accepts-one-form-but-not-the-other/Hello,
Consider the following expression
y=function("y")(x)
z=-3*diff(y,x)/y^4
I would like to replace diff(y,x)/y^4 by, say, 2/5
I tried,
z.subs(diff(y,x)/y^4==2/5).show()
But this didn't work (threw back at me the original expression)
I tried to replace y^4 by 5, and to do that I did
z.subs(y^4==5).show()
It didn't work. However,
z.subs(1/y^4==5).show()
worked!. I am confused!
If I tried to replace just y with 5^(1/4)
z.subs(y==(5)^(1/4)).show()
it worked! so it cannot replace y^4 with 5, but it can replace y with (5)^1/4.
To get what I want I finally did
z.subs(diff(y,x)==2).subs(1/y^4==5).show()
I got this by trial and error. Shouldn't the first one
z.subs(diff(y,x)/y^4==2/5).show()
have worked in the first place? What is the logic behind this that it didn't work?curios_mindWed, 10 Jun 2020 03:51:05 +0200https://ask.sagemath.org/question/51880/subs: for function and its derivativehttps://ask.sagemath.org/question/51881/subs-for-function-and-its-derivative/ Hello,
if you have an expression like
y=function("y")(x)
f=diff(y,x)+5*y
and would like replace y by x^2,
I would normally call
f.subs(y==x^2).show()
But this only substitutes the y in the second term, but not the function y in the derivative. So, in order to do that I do
f.subs(y==x^2).subs(diff(y,x)==diff(x^2,x)).show()
This works and I can live with this, but I was wondering if there is a "short cut" to replace the function y in the second term as well as the function y in the derivative at the same time?
I am looking for something similar to
(D[y[x],x]+5y[x])/.y->Function[x,x^2]
in Mathematica.
curios_mindWed, 10 Jun 2020 04:05:24 +0200https://ask.sagemath.org/question/51881/Direct substitution vs "subs" methodhttps://ask.sagemath.org/question/46068/direct-substitution-vs-subs-method/Suppose I define
var('a b c')
formula = a^2 + 5*b + c
Then I would like to substitute some values, like a=2, b=2 and c=4. As far as I'm aware, there are two options to do this:
formula(a=2, b=2, c=4)
and
formula.subs(a=2, b=2, c=4)
In this particular case, is there any difference between this two options? Is there any difference in some other case(s)?
Thanks in advance for you answers!dsejasTue, 09 Apr 2019 21:33:04 +0200https://ask.sagemath.org/question/46068/Sage subs() function include product conditionhttps://ask.sagemath.org/question/44578/sage-subs-function-include-product-condition/Code:
from brial import *
katan=declare_ring([Block('x',32),Block('k',80)],globals())
r1=x(3)*x(8) + x(7) + x(10)*x(12) + x(18) + x(23) + k(1) + k(16)
r2 = r1.subs({x(3)*x(8)+x(7)+ x(10)*x(12)+ x(18) + x(23) + k(1) + k(16) : 0})
print(r2)
this is a sage code, make r1 equals a polynomial include some variables, then use the subs() function to fix some variable, in this case, let the polynomial equals 0, so the expected result of r2 is 0, but the sage gives the result is
x(7) + x(10)*x(12) + x(18) + x(23) + k(1) + k(16)
i know , some thing wrong with the x(3)*x(8), but i don't know how to make it right.linuxCipherTue, 04 Dec 2018 11:41:19 +0100https://ask.sagemath.org/question/44578/How can I get back an expression for free variables in solve function.https://ask.sagemath.org/question/39396/how-can-i-get-back-an-expression-for-free-variables-in-solve-function/I have a number of linear equations on some symbols A,B,C,D,E,F,G. When I solve them in Sagemath, I get some free variables in the solution.
$A,B,C,D,E,F,G=var('A,B,C,D,E,F,G')$
$eqns=[A+B==0,C+D+E==0,F+G==0,A+E-F==0]$
$solution=solve(eqns,A,B,C,D,E,F,G)$
Sage gives the following solution:
$[[A == r1, B == -r1, C == r1 - r2 - r3, D == r3, E == -r1 + r2, F == r2, G == -r2]]$
Now I can ask Sage to give me an expression of any combination of the symbols, for example:
$(A+C).subs(solution)$, then I get $2*r1 - r2 - r3$.
For my purpose I would now like to have an expression for any of the free variables. If I assign $r1=var('r1')$ and ask Sage $s1.subs(solution)$ I get back $r1$ again. But I would lik to get back an expression in terms of $A,B,C,D,E,F,G$.
Any suggestions on how to do this?TheBeiramMon, 06 Nov 2017 11:29:40 +0100https://ask.sagemath.org/question/39396/subs() function gives KeyError when keyword is a list memberhttps://ask.sagemath.org/question/32426/subs-function-gives-keyerror-when-keyword-is-a-list-member/ I have a multivariate Boolean polynomial my_poly in xi (x0,x1,...,etc) and want to substitute one of the variables (say e.g. x0=0). my_poly subs(x0=0) works but I need to determine the exact variable (left hand side of '=') and value (right hand side of '=') at run-time depending on some conditions. The problem is subs() function do not accept expression on the left hand side of '=' and I have many xi variables, so if else is not practical. How can I solve this issue?adnanbaysalWed, 03 Feb 2016 10:25:06 +0100https://ask.sagemath.org/question/32426/Evaluating a symbolic expression for a Graphhttps://ask.sagemath.org/question/9768/evaluating-a-symbolic-expression-for-a-graph/I'm able to do this:
sage: f = function('radius', nargs=1, evalf_func=Graph.radius)
sage: f(graphs.HouseGraph())
2
But not this:
sage: var('G')
sage: expr = f(G)
sage: expr.subs(G=graphs.HouseGraph())
...
TypeError: no canonical coercion from <class 'sage.graphs.graph.Graph'> to Symbolic Ring
What am I missing? Is it not possible to use symbolic expressions like this?
patronicsThu, 14 Feb 2013 14:50:33 +0100https://ask.sagemath.org/question/9768/Substitution using Dictionary with Matrix as Valuehttps://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/As a newcomer to SAGE, trying to use it to do symbolic linear algebra, I am wondering why substitution of a variable using a dictionary doesn't work in this case:
sage: aMatrix = matrix(SR,1,1)
sage: var('aVariable')
aVariable
sage: aDict = {}
sage: aDict[aVariable] = aMatrix
sage: aDict[aVariable]
[0]
but:
sage: aVariable.subs(aDict)
....
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:16025)()
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:11265)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent_old.so in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3369)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8912)()
TypeError: no canonical coercion from Full MatrixSpace of 1 by 1 dense matrices over Symbolic Ring to Symbolic Ring
Functionality to substitute matrices for variables seems to be indispensable to doing symbolic linear algebra, so I am sure there is a proper way to do this.
heatkernelThu, 14 Jun 2012 17:58:48 +0200https://ask.sagemath.org/question/9075/