Keeping expressions short and concise

100 views
Skip to first unread message

Andre Bolle

unread,
Apr 19, 2022, 8:50:54 AM4/19/22
to sympy
When I differentiate the function

ψ = exp(I*(k*x))

I get

i*k*exp(I*(k*x)).    [which is  i*k*ψ]

Is there a way to get exp(I*(k*x)) substituted with  ψ, in order to get the shorter expression i*k*ψ ?

Thanks,
André





gu...@uwosh.edu

unread,
Apr 19, 2022, 9:16:25 AM4/19/22
to sympy
Is this the behavior you are trying to get?


>>> from algebra_with_sympy import *
>>> algwsym_config.output.human_text=True
>>> var('psi k x')
(psi, k, x)
>>> eq1=Eqn(psi, exp(I*k*x))
>>> eq1
psi = exp(I*k*x)
>>> eq2 =diff(eq1,x)
>>> eq2
Derivative(psi, x) = I*k*exp(I*k*x)
>>> eq2.subs({exp(I*k*x):psi})
Derivative(psi, x) = I*k*psi

Jonathan

gu...@uwosh.edu

unread,
Apr 19, 2022, 10:24:00 AM4/19/22
to sympy
Alternative path that works better with complicated derivatives. Also shows the nicer output inside Jupyter notebooks.
Screen Shot 2022-04-19 at 9.23.21 AM.png

Andre Bolle

unread,
Apr 19, 2022, 10:44:17 AM4/19/22
to sympy
Jonathan,

Hi there and thanks for your assistance.

Is this the behavior I am trying to get? Quite possibly. Having trouble installing "algebra_with_sympy". Do I need to be admin? My installation attempt is as follows:

pip install -U Algebra-with-SymPy
... lots of stuff, followed finally by
ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'C:\\Users\\python\\AppData\\Local\\Temp\\pip-uninstall-pns2p03y\\jupyter-kernel.exe'
Consider using the `--user` option or check the permissions.

Thanks again,
Andre

Andre Bolle

unread,
Apr 19, 2022, 10:56:06 AM4/19/22
to sympy
That is precisely what I was looking for.

Alan Bromborsky

unread,
Apr 19, 2022, 11:24:51 AM4/19/22
to sy...@googlegroups.com

I am working on a linear differential operator class for sympy (Dop.py). Here is a Jupyter notebook with a simple example -



Note that lap*f gives a function but f*lap gives differential operator.
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/3ad5c732-248a-4e56-9b1b-c195b1c13435n%40googlegroups.com.

gu...@uwosh.edu

unread,
Apr 19, 2022, 12:47:10 PM4/19/22
to sympy
> pip install -U Algebra-with-SymPy
>... lots of stuff, followed finally by
>ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'C:\\Users\\python\\AppData\\Local\\Temp\\pip-uninstall-pns2p03y\\jupyter-kernel.exe'
>Consider using the `--user` option or check the permissions.

Andre,

I am not a regular Windows user so this is just a guess. My experience is with *nix flavors (MacOS, BSD, Linux). You may need to try `python -m pip install --user Algebra_with_Sympy` or something like that. I would do it inside a virtual environment, so that you can isolate it from other setups. I usually use pipenv to set those up.

Jonathan


Jonathan Gutow

unread,
Apr 19, 2022, 12:54:47 PM4/19/22
to sy...@googlegroups.com
Alan,

I have thought about this a little too. I have not had time to work on it recently. The issue I ran into is that to make this work well in SymPy you really need the concept of an infinitesimal dx, dy, dz, etc. Things got circular when I tried to implement that using the sympy definition of functions. It might be worth comparing notes. I would really like to be able to work with total differentials and do integrations on expressions containing infinitesimals in the Leibniz style notation.

Jonathan


On Apr 19, 2022, at 10:24 AM, Alan Bromborsky <abrom...@gmail.com> wrote:


CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.


I am working on a linear differential operator class for sympy (Dop.py). Here is a Jupyter notebook with a simple example -

<sdop_test.jpg>

Alan Bromborsky

unread,
Apr 19, 2022, 1:20:39 PM4/19/22
to sy...@googlegroups.com

Look at -

https://galgebra.readthedocs.io/en/latest/

gradient (geometric derivative) and associated operators implemented with the same interface.  Nothing to do with integration implemented.  Attached is the code for Dop.py and the test code in Jupyter notebook.  The problem with the differential operator class is that in order to use them to stuff a sympy Matrix they have to be sympy expressions and I don't know if that is possible without changing some of the sympy core.  You might want to look at geometric calculus -

https://en.wikipedia.org/wiki/Geometric_calculus

galgebra includes all of the differential geometric calculus.  Could you give me examples of what you want to do in pdf/latex format files.  Essentially all that Dop.py does is provide a different api to using sympy derivatives.  If all is required is changing the api that would not be too difficult.  A integrator class could be written with a more natural api.  What would be difficult is if you had an integrand with unevaluated differential expressions.  I don't think sympy can return f for the integral of (df/dx)dx without first differentiating and then integrating.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
Dop.py
sdop_test.ipynb

Jonathan Gutow

unread,
Apr 19, 2022, 1:48:59 PM4/19/22
to sy...@googlegroups.com


On Apr 19, 2022, at 12:20 PM, Alan Bromborsky <abrom...@gmail.com> wrote:

I don't think sympy can return f for the integral of (df/dx)dx without first differentiating and then integrating.

Alan,

Yep, that is essentially the sticking point. It relates to the fact that there is no concept of an infinitesimal in the sympy core. I am interested in things like the total differential of f wrt multiple variables. For example: df = (df/dx)dx + (df/dy)dy + …, where these are partial derivatives and implicitly everything but the variable of differentiation is held constant. I use these mostly when doing thermodynamics, but there are other applications.

I will see if I can get time to look at what you are doing with galgebra.

Thanks,

Jonathan

Alan Bromborsky

unread,
Apr 19, 2022, 2:28:52 PM4/19/22
to sy...@googlegroups.com

Since I have no rant control I proselytize the following graphics software (free) for generating publication quality graphics whenever I have a likely target -

https://galgebra.readthedocs.io/en/latest/

look at the "3D Graphs" and "WebGL" galleries.  I especially like this one (you can zoom, rotate, and translate it interactively) -

https://asymptote.sourceforge.io/gallery/3Dwebgl/Klein.html

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

Alan Bromborsky

unread,
Apr 19, 2022, 10:19:18 PM4/19/22
to sy...@googlegroups.com

This might be relevant -

https://physics.stackexchange.com/questions/32296/introduction-to-differential-forms-in-thermodynamics

If you find anything of interest in the link you should check it out with someone in the math department.  The limit to my knowledge of differential forms is that dx is an element in the dual space.  If we are in (x,y,z) space then dx is a linear mapping from (x,y,z) to the reals and dx(x,y,z) = x, dy(x,y,z) = y, and dz(x,y,z) = z.

On 4/19/22 12:54 PM, Jonathan Gutow wrote:
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

Alan Bromborsky

unread,
Apr 19, 2022, 10:47:08 PM4/19/22
to sy...@googlegroups.com

I have attached a paper that also might be relevant.  A rigorous derivation of the thermodynamic equations in terms of differential forms.  Unless you are familiar with differential forms (again I am not) will require consulting with a real mathematician.

On 4/19/22 12:54 PM, Jonathan Gutow wrote:
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
MathThermoStates.pdf

Andre Bolle

unread,
Apr 19, 2022, 11:48:27 PM4/19/22
to sympy
Jonathan,

I managed to install.

python -m pip install --user Algebra_with_Sympy

Here's what I did. You will notice that the second derivative couldn't see the 'x', which had been replaced by psi.

I do like the equation annotation.  (eq1), (eq2), (eq3), etc. Nice.

Andre

On Tuesday, April 19, 2022 at 3:24:00 PM UTC+1 gu...@uwosh.edu wrote:
Capture.PNG

Jonathan Gutow

unread,
Apr 20, 2022, 8:37:33 AM4/20/22
to sy...@googlegroups.com
On Apr 19, 2022, at 10:48 PM, Andre Bolle <andre...@gmail.com> wrote:

Here's what I did. You will notice that the second derivative couldn't see the 'x', which had been replaced by psi.
Yes, that is a “feature” of sympy, which assumes that all symbols that do not have an explicit dependence on the variable of differentiation are constants. Most of the time that works well. You can get around that by specifying that psi is a function of x. See the sympy documentation on functions. I have been fiddling with how to make it take more general derivatives, but need to figure out a definition of an infinitesimal that will function consistently within the sympy environment.


I do like the equation annotation.  (eq1), (eq2), (eq3), etc. Nice.
Glad you like it. I sometimes collapse the code blocks. This then leaves you with the results of each step as might be provided in a traditional derivation. You can also pretty it up with comments in markdown cells between the each step.

Jonathan

Chris Smith

unread,
Apr 20, 2022, 9:03:18 AM4/20/22
to sympy
>  assumes that all symbols that do not have an explicit dependence on the variable of differentiation are constants

`idiff` will allow you to do the differentiation of symbols without functions, e.g. `dydx for idiff(2*x - y**2, y, x) -> 1/y`

/c

Aaron Meurer

unread,
Apr 26, 2022, 2:41:04 PM4/26/22
to sy...@googlegroups.com
On Wed, Apr 20, 2022 at 6:37 AM Jonathan Gutow <gu...@uwosh.edu> wrote:
>
>
> On Apr 19, 2022, at 10:48 PM, Andre Bolle <andre...@gmail.com> wrote:
>
> Here's what I did. You will notice that the second derivative couldn't see the 'x', which had been replaced by psi.
>
> Yes, that is a “feature” of sympy, which assumes that all symbols that do not have an explicit dependence on the variable of differentiation are constants. Most of the time that works well. You can get around that by specifying that psi is a function of x. See the sympy documentation on functions. I have been fiddling with how to make it take more general derivatives, but need to figure out a definition of an infinitesimal that will function consistently within the sympy environment.

If you're making a custom object, you can redefine various
differentiation methods to make it work correctly with derivatives
(the docs for this aren't great right now, but I hope to improve that
soon).

Aaron Meurer

Aaron Meurer

>
>
> I do like the equation annotation. (eq1), (eq2), (eq3), etc. Nice.
>
> Glad you like it. I sometimes collapse the code blocks. This then leaves you with the results of each step as might be provided in a traditional derivation. You can also pretty it up with comments in markdown cells between the each step.
>
> Jonathan
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/615CE6C5-8B01-4ED3-8D3E-3F7E16C1B5D1%40uwosh.edu.
Reply all
Reply to author
Forward
0 new messages