SymPy Imaginary Number

2024/10/7 8:28:48

I'm messing around with writing some SymPy code to handle symbolic expressions with imaginary numbers.

To start out, I want to get it to take x and y as real numbers and find the solution where x=iy. So I can do this as follows.

x, y = sympy.symbols("x y", real=True)  
print(sympy.solve([x-sympy.I*y]))

(SymPy solve takes a list of values, all of which must be 0. So x-iy=0 => x=iy). SymPy will correctly tell me

[{x: 0, y: 0}]

However, if I do this a (theoretically identical) way:

x, y = sympy.symbols("x y")
print(sympy.solve([x-sympy.I*y, sympy.im(y), sympy.im(x)]))

Then now SymPy tells me

[{re(y): y, re(x): I*y, im(x): 0, x: I*y, im(y): 0}]

And this is technically correct, but hasn't done everything for me. Is this just a limitation in SymPy, or can I get it to give me x=y=0 by constraining complex x and y in this way?

Answer

Because SymPy is better at simplifying pairs of real numbers than complex numbers, the following strategy helps: set up real variables for real/imaginary parts, then form complex variables from them.

from sympy import *
x1, x2, y1, y2 = symbols("x1 x2 y1 y2", real=True)  
x = x1 + I*x2
y = y1 + I*y2

Now x and y can be used as complex variables in an equation such as yours

sol = solve([x-I*y, im(y), im(x)])
print(x.subs(sol[0]), y.subs(sol[0])) 

Output: 0 0.

https://en.xdnf.cn/q/70269.html

Related Q&A

Django 1.11 404 Page while Debug=True

Without making things difficult, I just want to show a special 404 render with staticfiles.If you set DEBUG = False you can use in urls.pyhandler404 = app.views.handler404But it is without staticfiles.…

zip()-like built-in function filling unequal lengths from left with None value

Is there a built-in function that works like zip(), but fills the results so that the length of the resulting list is the length of the longest input and fills the list from the left with e.g. None?Th…

Collection comparison is reflexive, yet does not short circuit. Why?

In python, the built in collections compare elements with the explicit assumption that they are reflexive:In enforcing reflexivity of elements, the comparison of collections assumes that for a collecti…

Anisotropic diffusion 2d images [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Export environment variables at runtime with airflow

I am currently converting workflows that were implemented in bash scripts before to Airflow DAGs. In the bash scripts, I was just exporting the variables at run time withexport HADOOP_CONF_DIR="/e…

Extracting diagonal blocks from a numpy array

I am searching for a neat way to extract the diagonal blocks of size 2x2 that lie along the main diagonal of a (2N)x(2N) numpy array (that is, there will be N such blocks). This generalises numpy.diag,…

AttributeError: Unknown property density [duplicate]

This question already has an answer here:matplotlib histogram plot density argument not working(1 answer)Closed 3 years ago.I am trying to get a hold of SciPy, but I am stuck with Unknown property dens…

Find diagonals sums in numpy (faster)

I have some board numpy arrays like that:array([[0, 0, 0, 1, 0, 0, 0, 0],[1, 0, 0, 0, 0, 1, 0, 1],[0, 0, 0, 0, 0, 0, 0, 1],[0, 1, 0, 1, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 1, 0, 0, 0],[0,…

Create dictionary from list python

I have many lists in this format:[1, O1, , , , 0.0000, 0.0000, , ] [2, AP, , , , 35.0000, 105.0000, , ] [3, EU, , , , 47.0000, 8.0000, , ]I need to create a dictionary with key as the first element in …

Outputting height of a pyramid

So for this coding exercise I have to input a number of imaginary blocks and it will tell me how many complete rows high the pyramid is. So for example if I input 6 blocks...I want it to tell me that t…