1

My Problem

I am using Sympy v. 1.11.1 on (Jupyter Notebook) Python v. 3.8.5. I am dealing with a large Hessian, where terms such as these appear:

second-order derivatives

Pi+ and Pi- are complex Sympy symbols. However, one is the complex conjugate of the other, that is conjugate(Pi+) = Pi- and vice versa. This means that the product Pi+ * Pi- is real and the derivatives can be easily evaluated by removing the Re/Im (in one case Re(Pi+ * Pi-) = Pi+ * Pi-, in the other Im(Pi+ * Pi-) = 0).

My Question Is it possible to tell Sympy that Pi+ and Pi- are related by a complex conjugate, and it can therefore simplify the derivatives as explained above? Or does there exist some other way to simplify my derivatives?

My Attempts Optimally, I would like to find a way to express the above relation between Pi+ and Pi- to Python, such that it can make simplifications where needed throughout the code.

Initially I wanted to use Sympy global assumptions and try to set an assumption that (Pi+ * Pi-) is real. However, when I try to use global assumptions it says name 'global_assumptions' is not defined and when I try to explicitly import it (instead of import *), it says cannot import name 'global_assumptions' from 'sympy.assumptions' I could not figure out the root of this problem.

My next attempt was to replace all instances of Re(Pi+ * Pi-) -> Pi+ * Pi- etc. manually with the Sympy function subs. The code replaced these instances successfully, but never evaluated the derivatives, so I got stuck with these instead:

un-evaluated second-order derivatives

Please let me know if any clarification is needed.

I found a similar question Setting Assumptions on Variables in Sympy Relative to Other Variables and it seems from the discussion there that there does not exist an efficient way to do this. However, seeing that this was asked back in 2013, and the discussions pointed towards the possibility of implementation of a new improved assumption system within Sympy in the near future, it would be nice to know if any new such useful methods exist.

Ph_Ys321
  • 11
  • 3
  • It's best to put together a simple self-contained code example so that others can see how to reproduce what you are referring to. Please don't include links because I'm not going to follow those i.e. include the output inline here in SO. – Oscar Benjamin Nov 17 '22 at 23:10
  • I don't know exactly what you are doing but if `Pi+` and `Pi-` are complex conjugates of one another then why not declare them as `x +- I*y` where `x` and `y` are real? – Oscar Benjamin Nov 17 '22 at 23:11
  • Sorry regarding the links (they are png images of latex code), I was unable to paste the images directly into the text window, I'll look it up. I was considering defining Pi+/- as you have, but Pi+/- should up ALOT in my hessian, multiplied with other variables, some of which are also complex. I was afraid that if I decompose all my complex particles as you have, it would be hard to recover their original form if Python starts to carry out the multiplications, if that makes sense. But if you do not believe this to be the case, I would be open to try that. – Ph_Ys321 Nov 17 '22 at 23:35
  • I should also add that these derivatives show up in different parts of my code (not just my hessian), and I need to work with the original Pi+/- (and other complex variables). So it might be a bit cumbersome to every time locally decompose the complex variables when the derivatives appear, and then switch back to the original form at the end of the calculation. That is why I thought it would be convinent if Python simply knew that (Pi+ * Pi-) was real, and similar for the other complex variables. – Ph_Ys321 Nov 17 '22 at 23:42

1 Answers1

1

Given one and the other, try replacing one with conjugate(other):

>>> one = x; other = y
>>> p = one*other; q = p.subs(one, conjugate(other); im(q),re(q)
(Abs(y)**2, 0)

If you want to get back the original symbol after the simplifications wrought by the first replacement, follow up with a second replacement:

>>> p.sub(one, conjugate(other)).subs(conjugate(other), one)
x*y
smichr
  • 16,948
  • 2
  • 27
  • 34