ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 24 Mar 2019 13:43:13 -0500SUBSTITUTIONS IN SYMBOLIC EXPRESSIONShttp://ask.sagemath.org/question/45894/substitutions-in-symbolic-expressions/ Hi,
I would like to know if it is possible to substitute expressions inside some equation or expression.
Consider the following expression:
$$(r+1)\cdot(r+2)+g+a$$
Then, I would like to say that I want this same expression for $c+d = 2\cdot r + a$.
Is this possible?
Can I implement something like this in SAGE?idriskameniSun, 24 Mar 2019 13:43:13 -0500http://ask.sagemath.org/question/45894/Substitute differential operators in an expression.http://ask.sagemath.org/question/44598/substitute-differential-operators-in-an-expression/Let's say I am taking derivatives of an expression involving unknow function.
var('x,a,b');
f=(x^(a+b)).function(x,a,b);
h=function('h',nargs=1)(x);
g=h(f(x,a,b));
dg=diff(g,x);
dg
This gives the output
(a + b)*x^(a + b - 1)*D[0](h)(x^(a + b))
How do I replace `D[0](h)(x^(a + b))` with something like `direvative_of_h(x^(a + b))`?
In this simple case, I can just do this manually using `.operands`, but if I have a rather complicated equation, how I can I do it?
ablmfThu, 06 Dec 2018 09:15:21 -0600http://ask.sagemath.org/question/44598/sum and Substitutehttp://ask.sagemath.org/question/43804/sum-and-substitute/Hi
why Ds (like D ) does not give a numerical value ?
fs_d=r"!n \triangleq n! \sum_{i = 0}^{n}\frac{(-1)^i}{i!}"
#https://en.wikipedia.org/wiki/Derangement
forget()
var('n,l')
assume(l, 'integer')
assume(l>0)
assume(l, 'integer')
assume(n, 'integer')
assume(n>l)
assume(n, 'integer')
# Derangement
D=function('D')(n,l)
Dn=function('Dn')(n,l)
Dn=factorial(n)*sum(((-1)^l/factorial(l)),l,0,n)
D=factorial(7)*sum(((-1)^l/factorial(l)),l,0,7)
Ds=Dn.substitute({n:7})
show(LatexExpr(fs_d))
show('D : ',D)
show('Ds : ',Ds)
ortolljMon, 01 Oct 2018 10:07:21 -0500http://ask.sagemath.org/question/43804/Substituting a complex embedding for a number field elementhttp://ask.sagemath.org/question/40006/substituting-a-complex-embedding-for-a-number-field-element/Is there a way to take an element of a number field (or a polynomial or a number field) and replace the generator of the field by one of its complex embeddings? For example, something like
sage: K.<a> = NumberField(x^2 - 3)
sage: (3*a + 5).substitute(a=a.complex_embeddings()[1])
It's feasible to do this with `sage_eval`, but hopefully there is a better way.jaebondThu, 07 Dec 2017 23:34:33 -0600http://ask.sagemath.org/question/40006/Substitute expression for function in differential equationhttp://ask.sagemath.org/question/38130/substitute-expression-for-function-in-differential-equation/ This is related to [8293](https://ask.sagemath.org/question/8293/substitute-formal-function-by-an-expression-in-a-differential-equation/), but a lot has changed since then, so I wonder if there is a way to achieve this now.
Expanding on an example from [26114](https://ask.sagemath.org/question/26114/why-is-basic-arithmetic-disallowed-on-symbolic-functions/), I define a differential equation based on unevaluated functions and then want to substitute one of the functions (H) by an expression (T_s^2):
sage: var('T_s')
sage: B = function('B')(T_s)
sage: E = function('E')(T_s)
sage: H = function('H')(T_s)
sage: eq_B_TS = B == H/E
sage: eq1 = diff(eq_B_TS, T_s)
sage: eq1
diff(B(T_s), T_s) == -H(T_s)*diff(E(T_s), T_s)/E(T_s)^2 + diff(H(T_s), T_s)/E(T_s)
sage: eq1.subs(H == T_s^2)
diff(B(T_s), T_s) == -T_s^2*diff(E(T_s), T_s)/E(T_s)^2 + diff(H(T_s), T_s)/E(T_s)
I expected this output:
diff(B(T_s), T_s) == -T_s^2*diff(E(T_s), T_s)/E(T_s)^2 + 2*T_s/E(T_s)
However, the substitution was not carried out inside the differential. Is this a missing feature or a bug? Basically, I would like to be able to formulate general symbolic equations including differentials and integrals, and then insert explicit expressions for the different elements that would allow evaluation of the differentials and integrals. stanThu, 29 Jun 2017 16:12:29 -0500http://ask.sagemath.org/question/38130/Substituting variable/functionhttp://ask.sagemath.org/question/36994/substituting-variablefunction/I got a problem with substituting a solution into another equation. I could break down my problem to a simple example:
r = var('r');
p = function('p')(r);
v = var('v');
v = p + p.derivative(r);
p1(r)=desolve(p.derivative(r)==0,p,ivar=r); p1(r)
v.subs(p(r)==p1)
Output:
_C
_C + diff(p(r), r)
What's happening here? Why is p substituted, but not diff(p,r)=0?
What's the correct way to do this substitution?ceeeSun, 19 Mar 2017 10:07:00 -0500http://ask.sagemath.org/question/36994/Issues with substitutionhttp://ask.sagemath.org/question/36748/issues-with-substitution/I have a list L = [a13 == a10, a14 == a11, a15 == a12, a16 == a7, a17 == a8, a18 == a9]
I then have a running through a loop giving it these values
a = 1
a = 2*a15*a16 + 2*a13*a17 + 2*a13*a18 +1849
etc
I have
print(a)
a.subs(L)
print(a)
and it indicates no change, but I would of thought/ expected substitution to of taken place. Maybe I am being idiot, but please tell me where. I also posted this on stack overflow, sorry if I am not meant to post on both.
Thanks.AnomMon, 27 Feb 2017 11:33:19 -0600http://ask.sagemath.org/question/36748/substitute() raises exception where python does nothttp://ask.sagemath.org/question/32744/substitute-raises-exception-where-python-does-not/ In my usual workflow, I derive symbolic equations and then substitute their arguments by values stored in dictionaries to obtain numerical solutions. Now I found out that the substitute() command raises division by zero exception where standard python does not. This is a bit of a problem, as these exceptions prevent my code from evaluating all data sets once such an exception occurs and I don't want to wrap each line of code in a try-except construct. Does anyone know how the exception could be avoided? Here is an example:
var('y a x')
eq_y = y == a/x
vdict = {}
vdict[a] = 1.
vdict[x] = 0.
vdict[a]/vdict[x]
+infinity
However, if I substitute vdict into eq_y, I get an exception:
eq_y.subs(vdict)
Traceback (click to the left of this block for traceback)
...
ValueError: power::eval(): division by zero
stanWed, 09 Mar 2016 08:23:25 -0600http://ask.sagemath.org/question/32744/Substitution of several variableshttp://ask.sagemath.org/question/32720/substitution-of-several-variables/
Let $f=f(x_1(t),x_2(t))$ be defined as follows:
sage: var('t mu')
sage: x=list()
sage: x.append( function('x1')(t) )
sage: x.append( function('x2')(t) )
sage: f = x[0] - mu*x[0]^2*x[1]
sage: f
$f = x_1(t) - \mu x_1(t)^2 x_2(t)$.
Now, I need to to substitute the term containing $x_1^2(t) x_2(t)$ in $f$ by some new auxiliary variable $x_3(t)$, that is, to obtain $f = x_1(t) - \mu x_3(t) $. However, this code doesn't work:
sage: x.append( function('x3')(t) )
sage: f.subs({x[0]^2*x[1] : x[2]})
Some ideas? Thanks.
This code is motivated by symbolic manipulation with ODEs (think of $f$ as being the right-hand side term in the autonomous ODE system $\dot{x}(t) = f(x(t))$ ).mforetsSat, 05 Mar 2016 05:36:51 -0600http://ask.sagemath.org/question/32720/subs() function gives KeyError when keyword is a list memberhttp://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 03:25:06 -0600http://ask.sagemath.org/question/32426/High memory usage when substituting variableshttp://ask.sagemath.org/question/29444/high-memory-usage-when-substituting-variables/I need to make a lot of variable substitutions in multivariate polynomials. However, I only need to store one polynomial at a time and I only need to make one substitution at a time. Nonetheless sage uses a lot of memory. This memory is not freed until sage is killed.
Here is a small example:
var("x y z")
# A polynomial in three variables (The Trott quartic):
poly=12^2*(x^4+y^4)-15^2*(x^2+y^2)*z^2+350*x^2*y^2+9^2*z^4
poly=poly.polynomial(ZZ)
pnt=[1,2,3]
# Makes a certain variable subsitution, defined by pt, n times.
def test_subs(f,pt,n):
for i in xrange(n):
temp=f.substitute(x=x+pt[0]*z,y=y+pt[1]*z,z=pt[2]*z)
#temp=f(x=x+pt[0]*z,y=y+pt[1]*z,z=pt[2]*z) #This also uses a lot of memory.
When i run `test_subs(poly,pnt,100000)` I can see the memory usage ticking up from around 1% up to nearly 2%. if I then run the function again the ticking starts at 2%, and so on. In the real problem I have my program eventually fills up all memory and then crashes.
Can you see why this is happening and do you know how to prevent it from happening?Olof BergvallTue, 15 Sep 2015 04:09:49 -0500http://ask.sagemath.org/question/29444/Substitute expressions with cos and sinhttp://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/ I'm trying to simplify a symbolic expression obtained from sage integration. It contains terms of cos(theta)^2*sin(theta)^2 and many others. I wanted to collect the coefficients of cos(theta)^2*sin(theta)^2 and used subs_expr for this purpose. Here's a simplified version of what I'm trying to do:
var('theta,x,Psi')
y(theta)=cos(theta)^2*sin(theta)^2*cos(Psi)
y.subs_expr(cos(theta)^2*sin(theta)^2==x)
The result I got was "theta |--> cos(Psi)*cos(theta)^2*sin(theta)^2", not the expected "x*cos(Psi)". How can I get my expected result?
LiangMon, 24 Aug 2015 18:18:52 -0500http://ask.sagemath.org/question/29349/Equations solvinghttp://ask.sagemath.org/question/25479/equations-solving/ Hi everybody, i've got this two equations:
eqq0: V_f*cos(delta(t))=v(t)
eqq1: V_f*sin(delta(t))=l*diff(psi(t))
I need to isolate V_f from the first equation, obtaining V_f=v(t)/cos(delta(t)) and substitute it in the second equation. Then i have to isolate diff(psi(t))=[v(t)*tan(delta(t))]/l.
I did :
a=solve (eqq0,V_f)
V_ff=a.right()
but it says *"AttributeError: 'Sequence_generic' object has no attribute 'right'"*
How can i solve this two equations?
Thank you.
SilviaSun, 11 Jan 2015 13:16:36 -0600http://ask.sagemath.org/question/25479/Substitute x value in a linear regression equation instead of writing it out?http://ask.sagemath.org/question/10286/substitute-x-value-in-a-linear-regression-equation-instead-of-writing-it-out/Thanks to this handy post, I was able to generate a linear regression graph:
http://stackoverflow.com/questions/454606/how-can-i-do-regression-analysis-in-sage
My input thus far in sage 5.9:
r = [[0,76.4], [1,78.5], [2,81.5], [3,87.8],
[4,88.4], [5,92.4], [6,94.0], [7,95.0]]
var('a,b')
(a, b) #It seems this statement is extraneous
model(x) = a*x+b
find_fit(r, model)
plot(model(a = find_fit(r, model)[0].rhs(),
b = find_fit(r,model)[1].rhs()),
0,15, ymin = 0, ymax = 150)
What would be the most practical way of substituting the x value to generate an answer instead of:
Executing: find_fit(r, model)
Then copying and pasting the answer: [a == 2.8690476152412128, b == 76.70833334665576]
And then having to manually write out, where number 15 represents what I wanted to put substitute for x: 2.8690476152412128 * (15) + 76.70833334665576
Also 1 of my other threads has an unsolved question, incase anyone's interested:
http://ask.sagemath.org/question/2732/display-y-interceptbxdinWed, 26 Jun 2013 06:35:12 -0500http://ask.sagemath.org/question/10286/How to recursively substitute from global name space?http://ask.sagemath.org/question/9533/how-to-recursively-substitute-from-global-name-space/In sage 3.x, I used the below function to automatically substitute into an expression all assignments I have made previously. How can I get it to work in Sage 5.x or is there a better way now?
def subsg(variable, *args):
"""
Substitutes all definitions in the global name space for the variables contained in 'variable' and returns the result.
INPUT:
variable -- a symbolic variable
*args -- an optional dictionary containing additional parameter definitions
EXAMPLE:
sage: var ('a b c d')
a b c
sage: f = a*x
sage: a = 2*b
sage: b = c^2
sage: cons1 = dict(c = d^3)
sage: subsg(f,cons1)
2*d^6*x
Or, if cons1 is not to be used:
sage: subsg(f)
2*c^2*x
"""
expr1 = variable
expr2 = variable.subs(globals()).subs(*args)
while expr1 <> expr2:
expr1 = expr2
expr2 = expr1.subs(globals()).subs(*args)
return expr1
stanWed, 14 Nov 2012 02:56:27 -0600http://ask.sagemath.org/question/9533/substitution in expressionhttp://ask.sagemath.org/question/10134/substitution-in-expression/Hi,
After reading [Simplify shenanigans](http://ask.sagemath.org/question/2587/simplify-shenanigans), I am very confused about the possibility of doing
sage: sin(x).subs(sin(x) == 3)
3
Which becomes even more funny with
sage: SR(2).subs(SR(2) == pi)
pi
I thought one may only set values to variable but no: I can set value to integers !
Is there a concrete example where this is useful ? Or this is just one more argument to avoid the use of the symbolic ring ?
Anyway the substitution is not at all consistent with the behavior of the symbolic ring. If such substitutions are allowed, then the expression `SR(2) + SR(2)` should not be simplified !vdelecroixFri, 17 May 2013 23:57:53 -0500http://ask.sagemath.org/question/10134/Simplify shenaniganshttp://ask.sagemath.org/question/10129/simplify-shenanigans/I've noticed a strange (and annoying) behaviour in sage concerning symbolic substitution and simplification
def demonstrate_simplify_shenanigans():
sym_func = function('func',nargs=1)
my_expression = sym_func(0)+sym_func(1)+sym_func(2)
print
print 'without simplify'
print my_expression.subs(func(0)==1)
print
print 'with simplify in expression'
print my_expression.simplify().subs(func(0)==1)
print
print 'with simplify in both expression and substitution'
print my_expression.simplify().subs(func(0).simplify()==1)
demonstrate_simplify_shenanigans()
The output is
without simplify
func(1) + func(2) + 1
with simplify in expression
func(0) + func(1) + func(2)
with simplify in both expression and substitution
func(1) + func(2) + 1
Why is there a different behaviour depending on whether simplify was called? Even though `func(0)` and `func(0).simplify()` seem identical.bolverkFri, 17 May 2013 19:48:32 -0500http://ask.sagemath.org/question/10129/