Access to all the coefficients of an expression seems to fail
For at least 3 days, I have been fighting with SageMath to construct this code which works nicely.
def use_prime(expr):
"""
Return the expression with one-variable function derivatives in prime notation.
"""
op = expr.operator()
if op:
args = expr.operands()
aargs = (use_prime(a) for a in args)
opp = op
if (isinstance(op, sage.symbolic.operators.FDerivativeOperator)
and len(args) == 1):
name = op.function().name()
primes = "’" * len(op.parameter_set())
opp = function(f"{name}{primes}")
return opp(*aargs)
else:
return expr
### 1
var('x y p w_0 θ I')
assume(p >= 0)
assume(p <= 1)
assume(θ >= 1)
U = function('U') # déclare U comme le nom d'une fonction
EU(x, y, p) = p * U(x) + (1 - p) * U(y)
w_1 = function('w_1')
w_1(w_0, π) = (w_0 - π)
w_2 = function('w_2')
w_2(w_0, π, x, I, p) = (w_0 - π) - x + I
π = function('π')
π(p, I, θ)=θ*(1-p)*I
U = function('U')
EU(x, y, p) = p * U(x) + (1 - p) * U(y)
x, y, p, w_0, θ, I, = SR.var('x y p w_0 θ I')
f = EU(w_1(w_0, π(p,I,θ)),w_2(w_0, π, x, I, π(p, I, θ)) )
h = diff(f, I)
hh = use_prime(h)
k = diff(f, I, 2)
kk = use_prime(k)
show(LatexExpr(r'''\text{L’assuré cherche à résoudre le programme suivant :} '''))
show(LatexExpr(r'''I^\star = \textrm{argmax}_{\{I\}}\left\{pU(w_1)+(1-p) U(w_2)| w_1'''
r'''=w_0 - \pi, w_2 = w_0 - \pi -x + I, \pi = \theta(1-p)I \right\} '''))
show(LatexExpr(r'''\text{ou encore : } I^\star = \textrm{argmax}_{\{I\}}\{'''),
f, LatexExpr(r'\}'))
show(LatexExpr(r'''\text{La condition d’optimalité du premier ordre est } '''
r'''d\mathbb{E}U = 0 \text{, soit : } '''))
show(LatexExpr(r'\,\,\,\,\,\,\,\,\,\,\,\,\,\, d\mathbb{E}U = '),hh, ' = 0')
d2EU = diff(EU(w_1(w_0, π), w_2(w_0, π, x, I, p), p), I, 2)
show(LatexExpr(r'''\text{La condition d’optimalité du second ordre est } '''
r'''d\mathbb{E}U = 0 \text{, soit : } '''))
show(LatexExpr(r'\,\,\,\,\,\,\,\,\,\,\,\,\,\,'), kk, ' < 0')
### 2
show(LatexExpr(r'\text{Commençons par vérifier si la condition '
r'du second ordre est vérifiée}'))
show(LatexExpr(r'\text{On va réécrire à la main la condition du second ordre '
r'de manière à obtenir une équation algébrique '
r'en substituant une variable positive}'))
show(LatexExpr(r'U2 > 0. \text{ Pour ce faire, il faut réussir à isoler } '
r'U^{\prime\prime}.'))
var('A')
U1 = hh.expand().coefficient(θ).coefficient(p).subs(2 == A).coefficient(A)
U2 = hh.coefficient(θ).coefficient(p)
show(U1)
show(U2)
eqn = (U1/U2 == (p - 1) * p * θ / ((p - 1) * θ + 1) * (p - 1))
eqn1 = ((U1/U2) == (p - 1) * p * θ / ((p - 1) * θ + 1) * (p - 1))
show(eqn1)
subs(2==A)
is not self evident since replacing a constant by another one
doesn't a priori come to mind naturally. I have been forced to take some detours
since I cannot ask what is he coefficient of say $(p-1)^2*\theta$.
My question is why.
Secondly, I would like to know if there is a more automatic way to arrive at the eqn
equation.
That is, if there is a solution to transform an equation eqn = (A-B==0)
in a second one eqn1 = (A==B)
(of course if A
and B
where only parameters or variables I could do it with solve(A==B, A)
but I use A
and B
to save typing.
Thirdly, I would like to substitute x
to I
and 1
to \theta
and look what is the result.
But nothing I have tried works. Thanks for your help.