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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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.





Change for i in range (4) to for i in range(1, 4). Do the same for the other one, but do 3 instead of four.
– Trooper Z
Aug 11 at 16:31


for i in range (4)


for i in range(1, 4)





This isn't a solution to the question. It's a way to improve your code.
– Trooper Z
Aug 11 at 17:14






but with this modification, the zero index of arrays is removed, why should I do this and how does this improve the code?
– homa
Aug 11 at 17:24






What is your expected output here?
– asmeurer
Aug 11 at 18:14






I want to get an expression that its variables are : x1, x2, x3,...
– homa
Aug 11 at 18:19




2 Answers
2



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.


x


(x1, x2, x3)



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.


x



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


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.


D





Thanks for clarification, The problem was solved with your help.
– homa
Aug 13 at 23:06


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)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard