且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从python中的非线性方程组中找到复​​杂的根

更新时间:2022-01-30 19:49:56

遇到此类问题时,我尝试将我的函数重写为实部和虚部的数组.例如,如果f是您的函数,它需要复杂的输入数组x(为简单起见,假设x的大小为2)

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 2
    return [x[0]-3*x[1]+1j+2, x[0]+x[1]]  # <-- for example

def 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 4
    x = [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])]

新功能real_f可以在fsolve中使用:同时求解该函数的实部和虚部,将输入自变量的实部和虚部视为独立的.

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.