1 | initial version |
Hello, @Cyrille! Maybe you need somebody more Sage-savvy than me, because I don't have a satisfactory/efficient answer for this question, but I can give you a "good enough" solution until you find something better.
Suppose that instead of your last line you write
tmp = diff(EU(w0,g*w0-c,p),w0)
print(latex(tmp))
That will show you the output
-g {\left(p - 1\right)} \mathrm{D}_{0}\left(U\right)\left(g w_{0} - c\right) + p \frac{\partial}{\partial w_{0}}U\left(w_{0}\right)
(This is what show()
converts to a beautiful formula in SageCell.) By reading this LaTeX code, you will see that the derivative of U
is written in two different ways: \mathrm{D}_{0}\left(U\right)
($\mathrm{D}_{0}\left(U\right)$) and \frac{\partial}{\partial w_{0}}U
($\frac{\partial}{\partial w_{0}}U$). In order to obtain a U'
instead of those inelegant versions, you can use the replace()
method from the str
Python data type. Here is the 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))
tmp = latex(diff(EU(w0,g*w0-c,p),w0)) # this converts the result to LaTeX syntax
tmp = tmp.replace(r'\mathrm{D}_{0}\left(U\right)', "U'") # replace the first U'
tmp = tmp.replace(r'\frac{\partial}{\partial w_{0}}U', "U'") # replace the second U'
show(LatexExpr(tmp)) # show the formula
Some clarifications are in order. First, notice that I used "raw strings" or "r-strings", which are the ones with a leading r
, like r'\mathrm{D}_{0}\left(U\right)'
. This is in order to avoid Sage/Python from interpreting the backslash ("\
") as an escape character. If you don't want to use raw strings, you should duplicate every backslash, as in '\\mathrm{D}_{0}\\left(U\\right)'
(notice this string doesn't have a leading r
.) Second, it might surprise you to see the LatexExpr()
function in the last line of the previous code. That actually is something I just learned: when you use the replace()
method, the result is a character string. Although we know that this string is the LaTeX code of a formula, Sage doesn't know it, so if you were to use show(tmp)
instead of what I wrote, you would get the LaTeX code printed in the screen instead of that code being executed to obtain the formula. In order to force Sage to execute the LaTeX code, I used the LatexExpr()
constructor.
I must insist that this is a not-so-optimal solution, whose purpose is to help while other solution appears, so perhaps you would like to wait for another option.
I hope this helps!