Hello All,
New to the forum, but I have used Sympy on a few projects in the past.
I am currently enrolled in a machine design course, and often times we have 
to solve large systems of equations.
I do not really like how any of the solver we use in class work, so I 
thought I'd right my own.
If it works well enough Id like to add a GUI front end and incorporate 
commonly used engineering functions.
I have had success compiling systems of linear equations up to about 40 
equations & 40 unknowns.
However adding any non-linear equations seems to break its back...

I was wondering if anybody here had a better way of going about this 
project.
Right now it just reads a text file full of equations, parses the strings 
into sympy Functions, and then runs sympy.solve, and prints the solution to 
a new file.

So currently operations looks like:

import solver
eqn = [] #holds all functions in file
symb = [] #holds unique symbols
sln = []  #holds all solution sets
og  = [] #holds original, unparsed functions for printing to output file
in_file = "problem_file.txt"
out_file = "solution_file.txt"

eqn , og = solver.parse_input(in_file)
symb = solver.sort_symbols(eqn)
sln = solver.solver(eqn)
print_output(out_file,sln,og)

Ill post the code below for easy reading, but I'll also attach a few demo 
files.
Any ideas on how to make this more powerful (i.e. more equations, or more 
non-linear equations)???
Running this in my sublime editor a couple times also slows my whole system 
WAY down from time to time (Ubuntu 14.04)
Thanks Gang!!



from sympy import *
from sympy.parsing.sympy_parser import parse_expr
from IPython.display import display_pretty
import string
import time
import os

init_printing(use_latex = True)

#Vars
sig_figs = 6


def sort_symbols(_functions): #Returns unique Sympy sybols from array of 
functions
_symbols  = []
temp = []
symbol_str = []
symbols_sorted = [] 

for i in _functions:
_symbols.append(i.atoms(Symbol))

for i in _symbols: #Puts all occurences of all symbols in 1 list
temp += (set.union(i))

temp = list(set(temp))          #Eliminate duplicates

for i in temp: #Convert to string so symbols may be sorted 
    symbol_str.append(str(i))

symbol_str = sorted(symbol_str)

for i in symbol_str:   #Convert string back to sympy symbols
symbols_sorted.append(parse_expr(i))

return symbols_sorted

def parse_input(_file): #Converts file into array of Sympy Functions
_functions = []  
original_functions = []
lines = []
with open(_file,'r+') as f:
lines = f.readlines()
for line in lines:
line = line[:-1]
comment = line[0] == '#' 
if not comment:
if "#" in line: #remove comments
split = string.find(line,"#") 
line = line[0:split]

original_functions.append(line) #Store original syntax

line = string.replace(line," ","") #remove white space
line = string.replace(line," ","")

if "^" in line: #Convert conventional equation syntax in sympy syntax
line = string.replace(line,"^","**")

if "=" in line:
split = string.find(line,'=')
line = "Eq(" + line[0:split] + "," + line[split + 1:len(line)] + ")"
temp = parse_expr(line)
_functions.append(temp)
return _functions, original_functions


def solver(_functions):
_solution = solve(_functions[0:len(_functions)],manual=True)     #Pass 
functions to solver #,symbols[0:len(symbols)] #passes symbols
print _solution
return _solution

def print_output(_solutions):

print "The submitted functions:"
for i in original_functions:
pretty_print(i)

print "Have the following unique variables"
for i in sort_symbols():
pretty_print(i)

print "And have solutions set(s):"
for i in _solutions:
for u in i:
print str(u) + " = " + str(Float(i[u],sig_figs))

def print_to_file(f,_solutions,_functions):
##Print File Header
_file = open(f,'w+')
header = "Brett's Sympy Solver!\nSolution Created : " + 
time.strftime("%d/%m/%Y") + "\n"
_file.write(header)
header = "Equation File : " + os.getcwd() + "/" + in_file + "\n\n" 
_file.write(header)

##Some nice underscoring
max_length = 0

for i in _solutions:
for u in i:
length = len(str(Float(i[u],sig_figs))+" = "+ str(u)) #find longest equation
if length > max_length:
max_length = length

for i in _functions:
length = len(str(i))
if length > max_length:
max_length = length

underscore = "_"
for i in range (0,(max_length-20)/2):
underscore += "_"

_file.write(underscore +"SOLUTIONS"+ underscore +"\n")
for i in _solutions:
for u in i:
_file.write(str(u) + " = " + str(Float(i[u],sig_figs))+"\n")

_file.write("\n" + underscore +"EQUATIONS"+ underscore +"\n")
#_file.write("Compiler Assumes All Equations = 0 Unless specified\n")
print len(_functions)
for i in _functions:
_file.write(str(i)+"\n")
_file.write("\n\n\n\n")

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/89416b65-b1b1-40e4-b608-1d4a93be9a1f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
##########################################################
# Brett Smith
# MECH 340: Machine Design, An Integrated Approach 4th Ed
# Case Study 2A: Hand-Operated Crimping-Tool Loading Analysis
# page 84 - 87
##########################################################
#___________________________Equations____________________________
F12x + F32x
F12y + F32y + Fh
Rh*Fh + R12x*F12y - R12y*F12x + R32x*F32y - R32y*F32x
F23x + F43x
F23y + F43y
R23x*F23y - R23y*F23x + R43x*F43y - R43y*F43x
F14x + F34x + Fc4x
F14y + F34y + Fc4y
R14x*F14y - R14y*F14x + R34x*F34y - R34y*F34x + Rc4x*Fc4y - Rc4y*Fc4x
F32x + F23x
F34x + F43x
F32y + F23y
F34y + F43y 
#__________________________Known Values_____________________________
Fc4x = -1956.3
Fc4y = 415.82
R12x = 1.40
R12y = .05
R14x = -.16
R14y = -.76
R23x =-.60
R23y = .13
R32x = 2.20
R32y = .08
R34x = .16
R34y =.76
R43x = .60
R43y = -.13
Rc4x = .45
Rc4y = .34
Rh   = -4.40
Brett's Sympy Solver!
Solution Created : 10/06/2015
Equation File : /home/waymond91/Desktop/CS2A-Problem.txt

_________SOLUTIONS_________
F12x = 1513.5781
F12y = -381.00415
F14x = 442.72185
F14y = -87.878068
F23x = 1513.5781
F23y = -327.94193
F32x = -1513.5781
F32y = 327.94193
F34x = 1513.5781
F34y = -327.94193
F43x = -1513.5781
F43y = 327.94193
Fc4x = -1956.3000
Fc4y = 415.82000
Fh = 53.062222
R12x = 1.4000000
R12y = 0.050000000
R14x = -0.16000000
R14y = -0.76000000
R23x = -0.60000000
R23y = 0.13000000
R32x = 2.2000000
R32y = 0.080000000
R34x = 0.16000000
R34y = 0.76000000
R43x = 0.60000000
R43y = -0.13000000
Rc4x = 0.45000000
Rc4y = 0.34000000
Rh = -4.4000000

_________EQUATIONS_________
F12x+F32x
F12y+F32y+Fh
Rh*Fh+R12x*F12y-R12y*F12x+R32x*F32y-R32y*F32x
F23x+F43x
F23y+F43y
R23x*F23y-R23y*F23x+R43x*F43y-R43y*F43x
F14x+F34x+Fc4x
F14y+F34y+Fc4y
R14x*F14y-R14y*F14x+R34x*F34y-R34y*F34x+Rc4x*Fc4y-Rc4y*Fc4x
F32x+F23x
F34x+F43x
F32y+F23y
F34y+F43y
Fc4x=-1956.3
Fc4y=415.82
R12x=1.40
R12y=.05
R14x=-.16
R14y=-.76
R23x=-.60
R23y=.13
R32x=2.20
R32y=.08
R34x=.16
R34y=.76
R43x=.60
R43y=-.13
Rc4x=.45
Rc4y=.34
Rh=-4.40
from sympy import *
from sympy.parsing.sympy_parser import parse_expr
from IPython.display import display_pretty
import string
import time
import os

init_printing(use_latex = True)

#Vars
sig_figs = 6
	

def sort_symbols(_functions):
	_symbols  = []
	temp = []
	symbol_str = []
	symbols_sorted = []	

	for i in _functions:
		_symbols.append(i.atoms(Symbol))

	for i in _symbols:					#Puts all occurences of all symbols in 1 list
		temp += (set.union(i))

	temp = list(set(temp))         	 	#Eliminate duplicates

	for i in temp:						#Convert to string so symbols may be sorted			
	    symbol_str.append(str(i))

	symbol_str = sorted(symbol_str)

	for i in symbol_str:			   #Convert string back to sympy symbols
		symbols_sorted.append(parse_expr(i))

	return symbols_sorted

def parse_input(_file):
	_functions = []	 
	original_functions = []
	lines = []
	with open(_file,'r+') as f:
		lines = f.readlines()
		for line in lines:
			line = line[:-1]
			comment = line[0] == '#'	
			if not comment:
				if "#" in line:											#remove comments
					split = string.find(line,"#")							
					line = line[0:split]

				original_functions.append(line)							#Store original syntax

				line = string.replace(line," ","")						#remove white space
				line = string.replace(line,"	","")

				if "^" in line:											#Convert conventional equation syntax in sympy syntax
					line = string.replace(line,"^","**")

				if "=" in line:
					split = string.find(line,'=')
					line = "Eq(" + line[0:split] + "," + line[split + 1:len(line)] + ")"
				temp = parse_expr(line)
				_functions.append(temp)
	return _functions, original_functions
	

def solver(_functions):
	_solution = solve(_functions[0:len(_functions)],manual=True)    	#Pass functions to solver #,symbols[0:len(symbols)] #passes symbols
	print _solution
	return _solution

def print_output(_solutions):

	print "The submitted functions:"
	for i in original_functions:
		pretty_print(i)

	print "Have the following unique variables"
	for i in sort_symbols():
		pretty_print(i)

	print "And have solutions set(s):"
	for i in _solutions:
		for u in i:
			print str(u) + " = " + str(Float(i[u],sig_figs))

def print_to_file(f,_solutions,_functions):
	##Print File Header
	_file = open(f,'w+')
	header = "Brett's Sympy Solver!\nSolution Created : " + time.strftime("%d/%m/%Y") + "\n"
	_file.write(header)
	header = "Equation File : " + os.getcwd() + "/" + in_file + "\n\n" 
	_file.write(header)

	##Some nice underscoring
	max_length = 0

	for i in _solutions:
		for u in i:
			length = len(str(Float(i[u],sig_figs))+" = "+ str(u))	#find longest equation
			if length > max_length:
				max_length = length

	for i in _functions:
		length = len(str(i))
		if length > max_length:
			max_length = length

	underscore = "_"
	for i in range (0,(max_length-20)/2):
		underscore += "_"
	
	_file.write(underscore +"SOLUTIONS"+ underscore +"\n")
	for i in _solutions:
		for u in i:
			_file.write(str(u) + " = " + str(Float(i[u],sig_figs))+"\n")

	_file.write("\n" + underscore +"EQUATIONS"+ underscore +"\n")
	#_file.write("Compiler Assumes All Equations = 0 Unless specified\n")
	print len(_functions)
	for i in _functions:
		_file.write(str(i)+"\n")
	_file.write("\n\n\n\n")




#input_file = open(in_file, 'r')								
#output_file = open(out_file,'w')




in_file = "P5-9-FOS.txt"
out_file = "S5-9-FOS.txt"
eqn = []
og = []
sln = []
symb = []

eqn, og = parse_input(in_file)
symb = sort_symbols(eqn)
sln = solver(eqn)
print_to_file(out_file,sln,og)

#equation_set = parse_input"test.txt")
#symbols = sort_symbols(equation_set)
#solutions = solver(equation_set)
#print solutions
"""
master = 'S5-4.txt'
param = ['A','B','C','D','E']
for i in param:	
	equation_set = []
	symbols = []
	original_functions = []
	solutions = []
	
	problem = '5-4' + i
	in_file = 'P' + problem + '.txt'
	out_file = 'S' + problem + '.txt'

	equation_set = parse_input(in_file)
	symbols = sort_symbols(equation_set)

	total_variables = len(symbols)
	total_equations = len(equation_set)
	#print "Total Variables = " + str(total_variables)
	#print "Total Equations = " + str(total_equations)
	solutions = solver(equation_set)
	print_to_file(out_file,solutions,equation_set)
	print_to_file(master,solutions,equation_set)
"""

Reply via email to