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.Mon, 30 Aug 2021 09:23:43 +0200Solve in real parametershttps://ask.sagemath.org/question/58753/solve-in-real-parameters/ I have a simple `solve()` line
var('x y')
solve(.25*x + 1.75*y, y)
Sagemath gives the good result but with rational parameters. Is there a pararameter which can keep the reals ?CyrilleMon, 30 Aug 2021 09:23:43 +0200https://ask.sagemath.org/question/58753/Inequation solution doesn't isolate the operational variablehttps://ask.sagemath.org/question/57364/inequation-solution-doesnt-isolate-the-operational-variable/This is a follow-up to
- [Ask Sage question 57318: Use solve with inequality extracted from polytope](https://ask.sagemath.org/question/57318)
- [Ask Sage question 57359: Symbolic expression not scriptable in Solve](https://ask.sagemath.org/question/57359)
One more time I encounter a problem I cannot find a solution by myself.
In the following code:
D = polytopes.dodecahedron()
DH = D.Hrepresentation()
x = list(var('x_%i' % i) for i in (0..2))
# show(DH)
Ineq=[[el.A()*vector(x) >= -el.b()] for el in DH]
# show(Ineq)
sol = [solve(ieq[0], x[0]) for ieq in Ineq]
sol1 = [el[1] for el in sol]
show(sol1)
when `ieq[0]` doesn't depend on `x[0]`, `sol1` is returned as entered.
It's perfectly normal. But I ask to solve on `x[0]`. Many answers do not
isolate `x[0]` (for instance, it is the case for `sol[2]`) which is what
I ask for. How can I code in such way that SageMath does it by default?CyrilleSun, 30 May 2021 16:25:44 +0200https://ask.sagemath.org/question/57364/Symbolic expression not scriptable in Solvehttps://ask.sagemath.org/question/57359/symbolic-expression-not-scriptable-in-solve/This is a follow-up to
- [Ask Sage question 57318: Use solve with inequality extracted from polytope](https://ask.sagemath.org/question/57318)
The answer by @slelievre to that question works nicely,
but I need to use indexed variables.
This variation of the code does not work for me:
D = polytopes.dodecahedron()
DH = D.Hrepresentation()
X = list(SR.var('x_%i' % i) for i in (0..2))
Ineq = [[el.A()*vector(X) >=el.b()] for el in DH]
show(Ineq[0][0])
sol = [solve(Ineq[i][0], x[1]) for i in range(len(Ineq))]
raising an error saying
'sage.symbolic.expression.Expression' object is not subscriptable
Why? the problem doesn't seem to come from `Ineq[0][0]` but from the variables?CyrilleSun, 30 May 2021 10:32:33 +0200https://ask.sagemath.org/question/57359/Use solve with inequality extracted from polytopehttps://ask.sagemath.org/question/57318/use-solve-with-inequality-extracted-from-polytope/Starting from a polytope, I would like to extract the inequalities
of its hyperplane representation and solve them with Sage's `solve`.
Define the dodecahedron and obtain its hyperplane representation.
sage: D = polytopes.dodecahedron()
sage: DH = D.Hrepresentation()
Pick the first inequality:
sage: DH[0]
An inequality (0, -1/2, -1/4*sqrt5 - 1/4) x + 1 >= 0
Try to feed it to `solve`:
sage: sol = solve(DH[0], x[1])
This doesn't work. So what can I do?CyrilleFri, 28 May 2021 10:32:38 +0200https://ask.sagemath.org/question/57318/Extending piecwise to 2 variables (or a variable and a parameter)https://ask.sagemath.org/question/54836/extending-piecwise-to-2-variables-or-a-variable-and-a-parameter/This piecewise function work correctly
score = piecewise([([i,i], ncand - i) for i in range(ncand)])
because `ncand` is defined earlier in my notebook. when I typeset say `score(1)` It returns what I expect. But I would like that ncand be a variable.CyrilleTue, 22 Dec 2020 11:28:43 +0100https://ask.sagemath.org/question/54836/Is it possible to substitute $U^\prime$ to $D_0(U)$ ?https://ask.sagemath.org/question/54555/is-it-possible-to-substitute-uprime-to-d_0u/The result of this code
var('x y p w0 g c')
assume(p>=0)
assume(p<=1)
U=function('U')(x)
EU(x,y,p)=p*U(x)+(1-p)*U(y)
show(EU(w0,g*w0-c,p))
show(diff(EU(w0,g*w0-c,p),w0))
uses $D_0(U)$ for the first order derivative of $U$. Is it possible to substitute $U^\prime$ to $D_0(U)$, to keep the path with textbook notations ?CyrilleFri, 04 Dec 2020 15:18:11 +0100https://ask.sagemath.org/question/54555/Substitute a function in a formal derivativehttps://ask.sagemath.org/question/54543/substitute-a-function-in-a-formal-derivative/This gives a nice result.
var('p w0 c g dc dg dp dw0')
EUa=function('EUa')(p,w0,c,g)
EUa_c=diff(EUa,c)
EUa_g=diff(EUa,g)
#
EUna=function('EUna')(p,w0,c,g)
EUna_g=diff(EUna,g)
EUna_c=diff(EUna,c)
##
dEUa=EUa_c*dc + EUa_g*dg
dEUna=EUna_c*dc + EUna_g*dg
###
show(EUa)
show(EUna)
show(dEUa)
show(dEUna)
sol=solve(dEUa==dEUna, dg)
sol=(sol[0]/dc).full_simplify()
show(sol)
But now, I would like to substitute to `EUa(p,w0,c,g) = p*U(w0)+(1-p)*U(0)` and `EUna(p,w0,c,g) = p*U(w0-c)+(1-p)*U(g*w0-c)` then, later change the unknown function `U(w)` to say `ln(w)` or `w^(1/2)`. I suppose I need to define first a function `U=function('U')(w)` after to define `w` as a variable. But all my tentatives fail.CyrilleThu, 03 Dec 2020 18:32:39 +0100https://ask.sagemath.org/question/54543/Formal implicit differentiationhttps://ask.sagemath.org/question/50991/formal-implicit-differentiation/As my question was poorly asked, I rewrite it.
I was searching for the code of first order and second order differentiation of an implicit function. Finally I found and post it.CyrilleFri, 24 Apr 2020 18:41:04 +0200https://ask.sagemath.org/question/50991/Composition of piecewise functionhttps://ask.sagemath.org/question/54466/composition-of-piecewise-function/[Remark: This is not a question but I do not know how to signal a mistake in the doc.
[http://fe.math.kobe-u.ac.jp/icms2010-dvd/SAGE/www.sagemath.org/doc/reference/sage/functions/piecewise.html](http://fe.math.kobe-u.ac.jp/icms2010-dvd/SAGE/www.sagemath.org/doc/reference/sage/functions/piecewise.html)
uses `Piecewise` which leads to an error, not `piecewise` which is good.]
Here is my question:
This code does exactly what I expect
A = 1000
g(x) = x^2
f = piecewise([[[1, 1], (A/12)],
[[2, 2], (A/(12 - 1) - 1*10)],
[[3, 3], (A/(12 - 2) - 2*10)]])
show(f(1), ", ", f(2), ", ", f(3))
fg = piecewise([[[1, 1], g(A/12)],
[[2, 2], g(A/(12 - 1) - 1*10)],
[[3, 3], g(A/(12 - 2) - 2*10)]])
show(fg(1), ", ", fg(2), ", ", fg(3))
but I have a lot of `g(x)` to compose with `f` so I would like
to know if there is a way to define a composition of functions
for piecewise functions (here defined on subset of `ZZ`).
There is also the problem where I have a function `h`
which is itself a piecewise function on the same set and I want
fgh = piecewise([[[1, 1], h(1)*g(A/12)],
[[2, 2], h(2)*g(A/(12 - 1) - 1*10)],
[[3, 3], h(3)*g(A/(12 - 2) - 2*10)]])
show(fg(1), ", ", fg(2), ", ", fg(3))CyrilleMon, 30 Nov 2020 16:53:05 +0100https://ask.sagemath.org/question/54466/sagemath fails to solve an equation and inequationhttps://ask.sagemath.org/question/54131/sagemath-fails-to-solve-an-equation-and-inequation/I do not understand why
var('R','p','x')
sol1=solve(R^(1/3)*p == x^(1/3),x)
show(sol1)
this work but this fails to isolate $x$
var('R','p','x')
sol1=solve(R^(1/3)*p <= x^(1/3),x)
show(sol1)
And that
var('R','p','x')
sol2=solve(0 == (R-x)^(1/3)*p + (1-p)* (-x)^(1/3),x)
show(sol2)
doesn't find $x$. And, as a consequence, of cours this :
var('R','p','x')
sol2=solve(0 <= (R-x)^(1/3)*p + (1-p)* (-x)^(1/3),x)
show(sol2)
fails. I have tried all simplification ways.CyrilleFri, 06 Nov 2020 12:58:08 +0100https://ask.sagemath.org/question/54131/From indexed variables to stringshttps://ask.sagemath.org/question/54026/from-indexed-variables-to-strings/ I can easily generate a list of variables with
xx = var("x", n=10, latex_name="x")
but I have many questions about this
1) `nc` is the number of variables needed. Say I need variables indexed from 10 to 20, not from 0 to 20. Am I obliged to
generate all 20 variables and let appart the 10 firsts (I have found nothing on options like n=, could it be a range ?).
2) if I have generated the following list `x=[x_1, x_2, x_3, x_4, x_5]` how may i transform it in a list of strings `x=['x_1', 'x_2', 'x_3', 'x_4', 'x_5']`
3) and what if I need to change the name of certain variable as ` 'x_3'-->'\\epsilon_2'` CyrilleSun, 25 Oct 2020 18:40:37 +0100https://ask.sagemath.org/question/54026/Creating sublist according countinuous intervalshttps://ask.sagemath.org/question/52066/creating-sublist-according-countinuous-intervals/I have a list of number say
`l = [1.25, 2.46, 3.99, 4, 5, 6, 7.12, 8.76 ,9.34, 10.42, 11.5, 12.]`
I have a list of real intervals
`Cl=[[0, 5),[5, 10),[10, 15)]`
How to create sublists of l according to the intervals in Cl ? If I was work in N there will be no difficulties.CyrilleThu, 18 Jun 2020 10:49:52 +0200https://ask.sagemath.org/question/52066/Replace all numbers < e-n by 0https://ask.sagemath.org/question/51784/replace-all-numbers-e-n-by-0/ As always two question in one.
F = random_matrix(RR,7,50)
import numpy as np
covMatrix = np.cov(F,bias=True)
Ω=matrix(covMatrix)
Λ=Ω.inverse()
Ω*Λ
1) Is there a way to ask Sama (nicname for SAgeMAth more elegant than SM -- a wink) to overcome the number less than e-n and replace it by 0 --- I have found a way : to use `change_ring(QQ)` but I want to know if therre is another way.
2) is there a way to ask Sama to display only n decimal digit.CyrilleSat, 06 Jun 2020 11:38:15 +0200https://ask.sagemath.org/question/51784/general implicit differentiationhttps://ask.sagemath.org/question/51519/general-implicit-differentiation/Recently i have been helped to write an implicit function differentiator (nice neologism isn't it). Here is the code
def implicit_derivative(V):
var("dw1, dw2")
V_w1 = diff(V, w1)
V_w2 = diff(V, w2)
# Differential
dV = V_w1 * dw1 + V_w2 * dw2
# Dérivée du premier ordre
sol=solve(dV==0, dw2)
impder=(sol[0]/dw1)
return impder
This work without difficulty for $V$ function of $w_1$ and $w_2$. But if my variables are $x$ and $y$ or say $\chi$ and $\zeta$. It will not work. I have not found the mechanism to define a general function not dependant of the name of its arguments. And here there is a second problem to find thway to associate the increase d... to its correlative argument that is if I use $\chi$ as the first variable $d\chi$ must substitute to $dw_1$.CyrilleSat, 23 May 2020 17:11:32 +0200https://ask.sagemath.org/question/51519/Strange behaviour in simplifying a functionhttps://ask.sagemath.org/question/51482/strange-behaviour-in-simplifying-a-function/ I am developping a long notebook to master Sage. But each time I fall on a problem. This time
p, D, w_0, x, I = var('p, D, w_0, x, I')
β = var('beta')
π = var('pi')
w00=8
D1=6
p1=0.5
A = matrix(SR, 2, 2, [[w_0, 1],[w_0-π,1]])
y = vector([w_0-D, w_0-D-π+I])
sol=A.solve_right(y)
sola=sol[0].full_simplify().function(D, π, w_0)
solb=sol[1].full_simplify().function(D, π, I, w_0)
f=(sola*x + solb).function(x, D, π, I, w_0)
g=f(x, D, β*I*(1-p), I, w_0).factor().collect(x).simplify().function(x, D, β, p, w_0)
h(x, D, p, w0)=g(x, D, 1, p, I, w_0).collect(D).collect(x).simplify()
show(f)
show(g)
show(h)
1) this is the main question : You can observe that `g` is independanr of `I`. But `h` is `g` with $\beta = 1$. So why `I` does reappear in h ?
2) How could we explain **SM** to write the slope of the 3 curves as I would write them in a sheet of paper ? CyrilleThu, 21 May 2020 19:57:45 +0200https://ask.sagemath.org/question/51482/Substitution in implicit functionhttps://ask.sagemath.org/question/51390/substitution-in-implicit-function/ This is the code to the implicit differentiaion of a function from the theoretical point of view
var("w1, w2, a, p")
EU(w1, w2, a, p)= p*w1^a+ (1-p)*w2^a
var("dw1, dw2")
V_w1 = diff(V, w1)
V_w2 = diff(V, w2)
# Differential
dV = V_w1 * dw1 + V_w2 * dw2
show("dV ="+latex(dV))
# Dérivée du premier ordre
sol=solve(dV==0, dw2)
show(sol[0]/dw1)
Now without to be obliged to rewrite all the commands, I would like to apply this procedure to the EU function or any other function. I have tried to use `dV.substitute_function(V, EU)` and `(sol[0]/dw1).right_hand_side()` but without success. I have an amplified problem for the second order derivative since I am nosure of my formulation due to the fact that one must substitute the first order derivative inside derivation.
CyrilleTue, 12 May 2020 18:09:05 +0200https://ask.sagemath.org/question/51390/Desattribution of a value to a variablehttps://ask.sagemath.org/question/51389/desattribution-of-a-value-to-a-variable/ Suppose I have attributed to `a` the value 3. But for the following operation I want to use anew `a` as a variable how should I operate, redefine `a` ?CyrilleTue, 12 May 2020 16:53:36 +0200https://ask.sagemath.org/question/51389/Full simplification of power doesn't workhttps://ask.sagemath.org/question/51359/full-simplification-of-power-doesnt-work/ x, a =SR.var('x a')
Uf1= x^a
ar1_x=diff(Uf1, x).full_simplify()
ar1_xx=diff(Uf1, x,2)
raar1 = -diff(Uf1, x,2)/diff(Uf1, x).full_simplify()
show(LatexExpr("U^{'}(x) ="+latex(ar1_x)))
show(LatexExpr("U^{''}(x) ="+latex(ar1_xx)))
show(LatexExpr("R_a(x) ="+latex(raar1)))
Why raar1 is not fully simplified ?CyrilleSun, 10 May 2020 00:22:33 +0200https://ask.sagemath.org/question/51359/Simplification failshttps://ask.sagemath.org/question/51052/simplification-fails/1) I have tried nearly all simplification methods, expansion, factorization, collect... on the following code for `Ra1`
var("x,a")
Ra1 = -diff(x^a, x,2)/diff(x^a, x)
show(Ra1)
but the self-evident simplification doesn't work. I wonder why. I also have tried to add assumption even I do not see why since it's only algebra
2) Is there a way to ask SM to factor on a variable ?CyrilleMon, 27 Apr 2020 11:18:46 +0200https://ask.sagemath.org/question/51052/Choosing the way a function is displayedhttps://ask.sagemath.org/question/50953/choosing-the-way-a-function-is-displayed/1) I have the following function `z=-(D*p-p*x - D + w0)/ (p-1)`. Is it possible to display it as `-(p/(1-p))*x + ((w0-D*(1-p)))/(1-p)`?
2) Can I select a part a this function --- the factor of `x`, `x` and the last part ?CyrilleThu, 23 Apr 2020 12:12:23 +0200https://ask.sagemath.org/question/50953/Strange behavior of bool()https://ask.sagemath.org/question/50942/strange-behavior-of-bool/Here is my code
var("w0, pi, D, I")
A = matrix(SR, 2, 2, [[w0, 1],[w0-pi,1]])
y = vector([w0-D, w0-D-pi+I])
sol=A.solve_right(y)
sola=sol[0].full_simplify().function(D, pi, I, w0)
solb=sol[1].full_simplify().function(D, pi, I, w0)
f(x, D, pi, I, w0)=sola*x + solb
show(f(x, D, pi, I, w0))
f_x = f.diff(x).full_simplify()
show(f_x)
f_I = f.diff(I).full_simplify()
show(f_I)
Now, I make some assumptions and I verify the sign of $f_x$ and $f_I$.
assume(I, "real")
assume(pi, "real")
assume(I > pi)
bool(f_x < 0)
assume(w0, "real")
assume(x, "real")
assume(w0 > x)
bool(f_I > 0)
What is weird is that the result doesn't change should I ask for `bool(f_x < 0)` or ` bool(f_x > 0)` and the same for $f_I$. I know that my question looks like an already asked question "symbolic functions and bool". But this time the expressions are elementary.CyrilleThu, 23 Apr 2020 06:41:02 +0200https://ask.sagemath.org/question/50942/Evaluation of a function and it's inverse with parametershttps://ask.sagemath.org/question/50878/evaluation-of-a-function-and-its-inverse-with-parameters/The following code works perfectly :
var('W1, W2, Ub, p')
a, x, y = SR.var('a,x,y')
W2=W
U(x)=x^a
assume(a, 'real')
assume(a, 'noninteger') # or alternatively 'integer'
assume(x, 'real')
assume(y, 'real')
assume(x > 0)
assume(y > 0)
U(x).substitute(x==y)
show(U(x))
V=solve(x == U(y), [y])[0].rhs().function(x)
show(V)
VV=((Ub-p*U(W2))/(1-p))^(1/a)
show(VV)
But now I want to be also able to attribute some values to the parameters and the variable to achieve an evaluation and plot according to the parameter values. I need the function and it's inverse. I have seen the methode `lambda x` in the documentation but it's end with an error.CyrilleMon, 20 Apr 2020 19:13:36 +0200https://ask.sagemath.org/question/50878/Computing the inverse of a function with parameter(s)https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/ I have adapted the code shown in the answer of "Can Sagemath compute the inverse of a function?". In the original exemple, the function was $log(x)$ so there is no parameters. But I would like to do the same with $x^a$. But even if i insert an `assume` command, I finally fall on an error. Here is my code
var('x,y,a')
assume(0<=a<=1)
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
Is there a way to obtain the result $x^{1/a}$ ?CyrilleFri, 17 Apr 2020 12:29:27 +0200https://ask.sagemath.org/question/50809/Numerical solvehttps://ask.sagemath.org/question/50808/numerical-solve/When I use a dictionary with solve, the result is product with radicals. I have found no way in the documentation to obtain a decimal expension. But If I call the solution as `sol = solve()` it's not difficult to obtain directly the decimal expansion with `sol[0].rhs().n()`. So is there an unseen option in solve to obtain this and if not could it be implemented ?CyrilleFri, 17 Apr 2020 12:27:27 +0200https://ask.sagemath.org/question/50808/How to tell to sagemath to solve for the good roothttps://ask.sagemath.org/question/50160/how-to-tell-to-sagemath-to-solve-for-the-good-root/ Hello, this is a simple problem of optimization
> %display latex
>
> var('w, gamma, alpha, beta, h, theta,
> p, A, B'); U= w^(gamma); Pi =
> p*h^(theta)- w*h; N= (U)^alpha *
> (Pi-B)^beta show(U) show(Pi) show(N)
> show(N_w)
> N_w= factor(N.diff(w))
> assume(0 < alpha <1)
> assume(0 < beta <1)
>z=solve(N_w==0, w, solution_dict=True)
>show(z)
If I say no bulshit, with the assumptions only one solution is valid. But Sagemath display all solutions ? How could it be done automaticaly ?
The factoriZation shows that
> assume(0 < alpha <1) assume(0 < beta
> <1) z=solve(N_w==0, w,
> solution_dict=True)CyrilleWed, 04 Mar 2020 13:21:18 +0100https://ask.sagemath.org/question/50160/A last substitutionhttps://ask.sagemath.org/question/48033/a-last-substitution/The result of the following code is perfectly normal.
%display latex
var('A, x, y, l, alpha, beta, R, p_x, p_y');
assume(A>0,alpha>0,beta>0,x>0,y>0);
U=A*x^(alpha)*y^(beta);
U_x= U.diff(x);
U_y= U.diff(y);
tms=(U_x/U_y).canonicalize_radical();
var('x,y')
y= function('y');
tms1=(U_x/U_y).substitute(y=y(x)).canonicalize_radical();
tms1.diff(x)
courbtms=solve(tms1.diff(x)==0, y(x).diff(x),solution_dict=True)
courbtms
but it looks like this
$\left[\frac{\partial}{\partial x}y(x) = \frac{y(x)}{x}\right]$
It's the first time I see this type of brackets. I can extract the right member of this equation
courbtms[0][y(x).diff(x)]
but what I am not able to obtain is to substitue backward $y$ to $y(x)$? I have tried
courbtms[0][y(x).diff(x)].substitute[y(x)=z]
but it obviously doesn't work.CyrilleSun, 22 Sep 2019 22:29:22 +0200https://ask.sagemath.org/question/48033/Change of programmation of implicithttps://ask.sagemath.org/question/48022/change-of-programmation-of-implicit/ In the former question "Calculus with formal functions: substitution?", one can obtain implicit differentiation by the following code
var('f,x,y')
y = function('y',x)
f = function('f',x,y)
f.diff(x)
which gives
D[1](f)(x, y(x))*D[0](y)(x) +D[0](f)(x, y(x))
But I use the last version ofg SageMath and I have the following error
TypeError: function() takes exactly 1 positional argument (2 given)
Is there some new way to handle this code ?CyrilleSun, 22 Sep 2019 10:31:09 +0200https://ask.sagemath.org/question/48022/How to substitute a function in a derivativehttps://ask.sagemath.org/question/48007/how-to-substitute-a-function-in-a-derivative/ I know that if $U(x, y) = A x^\alpha y^\beta$, I have $U_x = \alpha A x^{\alpha-1} y ^\beta$. But by substitution, I can obtain $U_x = \alpha \left(\frac{U}{x}\right\)$. Is there a way to obtain this in Sagemath. I have tried
U_x= U.diff(x).subs(A*x^(alpha)*y^(beta)== U)
but this has no effect.
CyrilleSat, 21 Sep 2019 14:29:05 +0200https://ask.sagemath.org/question/48007/Simplification and implicit functionhttps://ask.sagemath.org/question/48010/simplification-and-implicit-function/I have two question in one :
1) I have this $z= \frac{\alpha x^{\alpha-1}y^\beta y^{-\beta-1}}{\beta x^\alpha}$. There are obvious simplification since this is equal to $\frac{\alpha}{\beta}
\frac{y}{x}$.
z.full_simplify() has no impact
z.expand() simplify on $y$ but not on $x$
So what can I do to obtain the good result.
2) how to take the result and consider it as an implicit function to obtain the derivative de $y$. as a function of xCyrilleSat, 21 Sep 2019 16:21:08 +0200https://ask.sagemath.org/question/48010/how to run maxima code in Sage?https://ask.sagemath.org/question/40184/how-to-run-maxima-code-in-sage/I have the following code in maxima to calculate the laplacian of a function in parabolic coordinates.
assume(r≥0)$
assume(theta≥0,theta≤2*π)$
load(vect)$
derivabbrev:true$
scalefactors(parabolic)$
declare(f,scalar)$
depends(f,rest(parabolic))$
ev(express(laplacian(f)),diff,expand,factor);
What is the proper way to run those Maxima commands in SageMath?
Thanks,
DanieldanielvolinskiSat, 16 Dec 2017 16:47:28 +0100https://ask.sagemath.org/question/40184/