Finding complex roots from set of non-linear equations in python

2024/5/20 13:00:15

I have been testing an algorithm that has been published in literature that involves solving a set of 'm' non-linear equations in both Matlab and Python. The set of non-linear equations involves input variables that contain complex numbers, and therefore the resulting solutions should also be complex. As of now, I have been able to get pretty good results in Matlab by using the following lines of code:

lambdas0 = ones(1,m)*1e-5;
options = optimset('Algorithm','levenberg-marquardt',...
'MaxFunEvals',1000000,'MaxIter',10000,'TolX',1e-20,...
'TolFun',1e-20);Eq = @(lambda)maxentfun(lambda,m,h,g);
[lambdasf]  = fsolve(Eq,lambdas0,options);

where h and g are a complex matrix and vector, respectively. The solution converges very well for a wide range of initial values.

I have been trying to mimic these results in Python with very little success however. The numerical solvers seem to be set up much differently, and the 'levenburg-marquardt' algorithm exists under the function root. In python this algorithm cannot handle complex roots, and when I run the following lines:

lambdas0 = np.ones(m)*1e-5sol = root(maxentfun, lambdas0, args = (m,h,g), method='lm', tol = 1e-20, options = {'maxiter':10000, 'xtol':1e-20})lambdasf = sol.x

I get the following error:

minpack.error: Result from function call is not a proper array of floats.

I have tried using some of the other algorithms, such as 'broyden2' and 'anderson', but they are much inferior to Matlab, and only give okay results after playing around with the initial conditions. The function 'fsolve' also cannot handle complex variables either.

I was wondering if there is something I am applying incorrectly, and if anybody has an idea on maybe how to properly solve complex non-linear equations in Python.

Thank you very much

Answer

When I encounter this type of problem I try to rewrite my function as an array of real and imaginary parts. For example, if f is your function which takes complex input array x (say x has size 2, for simplicity)

from numpy import *
def f(x):# Takes a complex-valued vector of size 2 and outputs a complex-valued vector of size 2return [x[0]-3*x[1]+1j+2, x[0]+x[1]]  # <-- for exampledef real_f(x1):# converts a real-valued vector of size 4 to a complex-valued vector of size 2# outputs a real-valued vector of size 4x = [x1[0]+1j*x1[1],x1[2]+1j*x1[3]]actual_f = f(x)return [real(actual_f[0]),imag(actual_f[0]),real(actual_f[1]),imag(actual_f[1])]

The new function, real_f can be used in fsolve: the real and imaginary parts of the function are simultaneously solved for, treating the real and imaginary parts of the input argument as independent.

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

Related Q&A

Python on Raspberry Pi user input inside infinite loop misses inputs when hit with many

I have a very basic parrot script written in Python that simply prompts for a user input and prints it back inside an infinite loop. The Raspberry Pi has a USB barcode scanner attached for the input.wh…

How to append two bytes in python?

Say you have b\x04 and b\x00 how can you combine them as b\x0400?

Pythonic way to write a function which modifies a list?

In python function arguments are passed by object reference. This means the simplest code to modify a list will modify the object itself.a = [1,2,3]def remove_one(b):b.remove(1)remove_one(a) print(a)T…

Trying different functions until one does not throw an exception

I have some functions which try various methods to solve a problem based on a set of input data. If the problem cannot be solved by that method then the function will throw an exception.I need to try t…

Python: Extracting lists from list with module or regular expression

Im trying to extract lists/sublists from one bigger integer-list with Python2.7 by using start- and end-patterns. I would like to do it with a function, but I cant find a library, algorithm or a regula…

Converting hard integral to lambda function with lambdify

I would like to lambdify the function Integral(t**t,(t,0,x)). It works, but my new function, which was returned by lambdify, doesnt return a number but only sympy.integrals.integrals.Integral class. Bu…

python topN max heap, use heapq or self implement?

theres heapq in python, for general usage. i want recording topN(0~20) for 10e7 records.if use heapq, should use - to translate max to min; and recording a min number of bottom, to call heapq.heappushp…

QSortFilterProxyModel returning artificial row

Im using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, Id like to return a first entry which is not present in the original model, that is, its somehow artificial.This i…

@login_required is losing the current specified language

I am using i18n_patterns to internationalize my app and its working except when I click on a link that requires login (a view protected by @login_required decorator), I am being redirected to the login…

Python slow on fetchone, hangs on fetchall

Im writing a script to SELECT query a database and parse through ~33,000 records. Unfortunately Im running into problems at the cursor.fetchone()/cursor.fetchall() phase of things.I first tried iterati…