latex for desolve results

116 views
Skip to first unread message

Andy Howell

unread,
Sep 2, 2018, 4:42:41 PM9/2/18
to sage-...@googlegroups.com
Hello,

I'm not sure if this is a bug or not. I'm just starting to learn
differential equations. The latex returned for 'h' below has superfluous
() around the '-x' exponent.

y = var('y')
Plot1=plot_slope_field(2-y,(x,0,3),(y,0,20))
y = function('y')(x) # declare y to be a function of x
h = desolve(diff(y,x) + y - 2, y, ics=[0,7])
print expand(h)
print latex(expand(h))

5*e^(-x) + 2
5 \, e^{\left(-x\right)} + 2

It looks better with out the extra \left( and \right):

5 \, e^{-x} + 2

This is with 8.4beta2

Thanks,

Andy

Vincent Delecroix

unread,
Sep 2, 2018, 6:18:55 PM9/2/18
to sage-...@googlegroups.com
Dear Andy,

Thanks for your report.

Note that this result is not wrong with the extra parenthesis. Perhaps
a bit annoying.

SageMath is developed by volunteers and not perfect. You are very
welcome to contribute to its enhancement

http://doc.sagemath.org/html/en/developer/

Best
Vincent

Andy Howell

unread,
Sep 2, 2018, 9:15:21 PM9/2/18
to sage-...@googlegroups.com
Vincent,

I'm not expecting perfection :) It seems like a problem I might be able
to fix with a little direction. I did look though the code,but didn't
make much progress.

I know I have run under the debugger before, but for the life of me I
can remember how I did it. If I could figure out how to set a breakpoint
at latex(), I might make some headway.

Any pointers on debugging sage internals would be greatly appreciated.
Google wasn't my friend today.

Thanks,

Andy

Samuel Lelievre

unread,
Sep 3, 2018, 6:12:07 AM9/3/18
to sage-devel
Andy,

The code you are looking for is at line 891 in src/sage/symbolic/expression.pyx



Here is how I found out. Define:
```
sage: y = function('y')(x) # declare y to be a function of x
sage: h = expand(desolve(diff(y, x) + y - 2, y, ics=[0,7]))
sage: h
5*e^(-x) + 2
```

If you know Sage enough, you can guess that the method to get
the LaTeX representation is:
```
sage: h._latex_()
'5 \\, e^{\\left(-x\\right)} + 2'
```

Otherwise, you could try the `?` (get documentation) and `??` (get source
code) on the global function `latex` as follows and track things from there:
```
sage: latex?
sage: latex??
```

You can also get the documentation and source code for `h._latex_`:
```
sage: h._latex_?
sage: h._latex_??
```
and when doing that you get an indication of where the source for the code is.

Samuel Lelievre

unread,
Sep 3, 2018, 6:19:30 AM9/3/18
to sage-devel
Note that even though you stumbled upon this issue using `desolve`,
a minimal example does not have to involve `desolve`.

If you open a ticket for this, start from a minimal example. For instance:
```
sage: h = e^-x
sage: h._latex_()
'e^{\\left(-x\\right)}'
sage: latex(h)
e^{\left(-x\right)}
```

Samuel Lelievre

unread,
Sep 21, 2018, 6:47:46 PM9/21/18
to sage-devel
Andy,

three weeks ago, after the partial exploration discussed on this list,
I switched to other preoccupations and forgot to get back to this.

To recap and go a little further, here is how I would describe
the issue and some steps to explore it:

Current:

    sage: h = e^-x
    sage: latex(h)
    e^{\left(-x\right)}

Wish:

    sage: h = e^-x
    sage: latex(h)
    e^{-x}

The function `latex` (see `latex??`) calls `self._latex_`
which itself (see `h._latex_??`) calls

  self._parent._latex_element_(self)

In this case, self._parent is Sage's symbolic ring,
so the issue can be reformulated as:

Current:

    sage: h = e^-x
    sage: SR._latex_element_(h)
    'e^{\\left(-x\\right)}'

Wish:

    sage: h = e^-x
    sage: SR._latex_element_(h)
    'e^{-x}'

This method `SR._latex_element_` (see `SR._latex_element_??`)
is defined in sage/symbolic/ring.pyx, see:


Exploring further, it calls:

  char_to_str(GEx_to_str_latex(&x._gobj))

The only other place I find `GEx_to_str_latex` is in

  sage/src/sage/libs/pynac/pynac.pxd

see also (searching the sagemath/sage repo on GitHub):


Pynac's wiki has a note about the latexing of symbolic expressions:


Not sure where to go from there. This seems to be leaving the Python
world and to be calling some C or C++ library, where I'm less fluent.

I created a ticket for this issue. I hope this can get solved!

- Sage Trac ticket 26337
  Extraneous parentheses in latex(e^-x)

Andy Howell

unread,
Sep 21, 2018, 9:40:26 PM9/21/18
to sage-...@googlegroups.com

Samuel,

I am the personification of preoccupations upon preoccupations. No problem.

My meager attempt lead me to pynac as well, though not into the library itself. I do know C++, so maybe I can make some progress there.

I cloned pynac. There are some dependencies to work out to get it to build. I'll fiddle with it some more.

Thanks for your help.

Andy

--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To post to this group, send email to sage-...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages