Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
def nlinsolve_matrix(system, symbols):
r"""
reference : http://people.math.gatech.edu/~aleykin3/math4803spr13/BOOK/chapter1.pdf
page :9 example = 2.2
currently for 2 equations
Examples
>>> from sympy.solvers.solveset import nlinsolve_matrix
>>> nlinsolve_matrix([x*y - 1, 4*x**2 + y**2 - 5],[x,y])
>>> {(-1, -1), (-1/2, -2), (1/2, 2), (1, 1)}
This method will help in this type of cases(nlinsolve calling this method when
solve_poly_system cannot solve non zero dimensional nonlinear system of equations) :
>>> nlinsolve([(x + y)**2 - 4, x + y - 2],[x,y])
>>> {-y + 2}
currently it is supporting 2 variables 2 nonlinear equation.
"""
deg = []
# finding degree
for eq in system:
deg.append(Poly(eq).total_degree())
temp = str(symbols[0]) + "_%i"
length = -1
for i in deg:
length += i
# no of col = d1 + d2 + .... + dn - 1
params = [temp % i for i in range(length)]
M = Matrix([params])
# initialise Matrix with symbols + 1 columns
M = M.col_insert(length, Matrix([1]))
row_no = 1
for equation in system:
f = equation.expand(force=True)
for i1 in range(0,deg[row_no%2 ]):
f = f*(symbols[0]**i1)
f = f.expand(force=True)
# Extract coeff of symbols
coeff_list = []
i2 = length
while i2 != 0:
coeff_list.append(f.coeff(symbols[0]**(i2)))
i2 = i2 -1
# append constant term (term free from symbols)
coeff_list.append(f.as_coeff_add(symbols[0])[0])
# insert equations coeff's into rows
M = M.row_insert(row_no, Matrix([coeff_list]))
row_no += 1
# delete the initialised (Ist) trivial row
M.row_del(0)
y_eq = M.det()
y_soln = solveset_real(y_eq, symbols[1])
result = []
if y_soln is S.Reals:
sol = solveset(system[0], symbols[0])
for s in sol:
if system[1].subs(symbols[0], s) is S.Zero:
result.append(s)
elif isinstance(y_soln, ConditionSet):
return S.EmptySet
else:
for y_s in y_soln:
eq = system[0]
eq = eq.subs(symbols[1], y_s)
result.append(tuple([tuple(_invert_real(eq, FiniteSet(0), symbols[0])[1])[0], y_s]))
result = FiniteSet(*[r for r in result])
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment