简体   繁体   中英

Solving the large system of equations , N equations_N unknowns (as numbered_symbols [x1,x2,x3,..] ) with complex coefficients

By double "for" loops,and performing algebraic operations on an array of numbered_symbols [x1,x2,x3,..]) and some given numpy nd_arrays (here: A(2,4) and B(4)),a number of expressions (here: D1[0],D1[1],D2[0],D2[1]) were generated. The aim is that,these sympy expressions containing unknown variables[x1,x2,x3,..] must be solved by method : "linsolve" of sympy. In the more general case,i have to use a large number of symbols and name them by this way. Elements of nd_arrays (here: A(2,4) and B(4)), could be complex. For some reason I should not use matrix in numpy and sympy. Here is the minimum code that explains the problem:

import sympy as sp
import numpy as np
from sympy.solvers.solveset import linsolve

x = sp.symbols('x1:5')

A = np.array([[3,2,4,6],[1,3,4,3]])
#A = np.array([[3+5j,2-1j,4+1j,6+4j],[1-6j,3+1j,4+7j,3-2j]])
B = np.array([5,8,1,4])
C1 = np.zeros ((2,4), dtype=object)
C2 = np.zeros ((2,4), dtype=object)
D1 = np.zeros (2, dtype=object)
D2 = np.zeros (2, dtype=object)

for i in range (2):
    for j in range (4):

        C1[i,j] = x[j]*A[i,j] + 2*B[j]
        C2[i,j] = x[j]/(4*A[i,j])-B[j]

D1 = np.ndarray.tolist(np.sum (C1,axis=1))
D2 = np.ndarray.tolist(np.sum (C2,axis=1))

ans = linsolve (D1[0],D1[1],D2[0],D2[1],x1,x2,x3,x4)

print(C1)
print(C2)
print(D1)
print(D2)

print(ans)    

The problem appears here: although x1,x2,x3,x4 have already been introduced as Symbol,this error appears:

NameError: name 'x1' is not defined

How to fix the Error?

Another question : when (A) is selected as a Complex 2d array, then sympy expressions which are elements of lists:D1,D2 ( D1[0],D1[1],D2[0],D2[1] ) appear as complex combinations of the form 'I' Instead of 1j.

How to fix the Error?

Another question : Because of the large number of equations and unknowns, it is not possible for me to write "linsolve" as :

ans = linsolve (D1[0],D1[1],D2[0],D2[1],x1,x2,x3,x4)

And i need to write it as :

ans = linsolve (D1,D2,x1:x4)

Or a form like that.

What should i do? thanks.

Python variables are only defined if you explicitly define them. You've defined the variable x to point to the tuple (x1, x2, x3) but you never defined the Python variables x1, x2, x3. Make sure you understand the difference between Python variables and SymPy Symbols. This guide should help.

To define x1, x2, x3, x4, use

x1, x2, x3, x4 = x = symbols('x1:4')

This also sets x to the whole tuple, as before.

For the other question, linsolve requires the first argument to be the system as a list, and the remaining arguments to be the symbols. You can use Pythons argument unpacking syntax to group things, like

linsolve([*D1, *D2], *x)

Alternately, you could just construct a single list D . In general, it will probably be easier for you if you use lists instead of NumPy arrays here, as you aren't really taking advantage of the vectorizing features of NumPy, and the dynamic size features of lists will make them much easier to manipulate.

import sympy as sp
import numpy as np
from sympy import I
from sympy.solvers.solveset import linsolve

x1,x2,x3,x4 = x = sp.symbols('x1:5', real = False)

#A = np.array([[3,2,4,6],[1,3,4,3]])
A = np.array([[3+5j,2-1j,4+1j,6+4j],[1-6j,3+1j,4+7j,3-2j]] , dtype= np.complex128)
B = np.array([5,8,1,4])
C1 = np.zeros ((2,4), dtype=object)
C2 = np.zeros ((2,4), dtype=object)
D1 = np.zeros (2, dtype=object)
D2 = np.zeros (2, dtype=object)

for i in range (2):
    for j in range (4):

        C1[i,j] = x[j]*A[i,j] + 2*B[j]
        C2[i,j] = x[j]/(4*A[i,j])-B[j]

D1 = np.ndarray.tolist(np.sum (C1,axis=1))
D2 = np.ndarray.tolist(np.sum (C2,axis=1))


ans = linsolve ([*D1,*D2], *x)

#print(C1)
#print(C2)
#print(D1)
#print(D2)

print(ans)

Answer:

{(-346.682690175513 - 221.750958290075*I, 197.37151730263 - 237.866928341266*I, -233.916687288553 - 10.5779385566957*I, 334.096855104832 + 335.268786804827*I)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM