Skip to content

Instantly share code, notes, and snippets.

@Back2Basics
Created November 27, 2013 07:44
Show Gist options
  • Save Back2Basics/7672026 to your computer and use it in GitHub Desktop.
Save Back2Basics/7672026 to your computer and use it in GitHub Desktop.
can't get this generator to yield a math expression.
import itertools as it
import math
import sympy as sy
Energy, Frequency, Kinetic_energy, Mass, Momentum, Wavelength, Work_function, Speed_of_light,Force,Acceleration= sy.symbols("Energy Frequency Kinetic_energy Mass Momentum Wavelength Work_function Speed_of_light Force Acceleration")
Symbols_list=[Energy, Frequency, Kinetic_energy, Mass,
Momentum, Wavelength, Work_function, Speed_of_light]
Equations= [sy.Eq(Energy,Mass * Speed_of_light**2),sy.Eq(Force,Mass * Acceleration)]
Term_dict={}
for x in Equations:
Term_dict[x]= x.free_symbols
def associate_2_variables(
eqn1=sy.Eq(Energy,Mass*Speed_of_light**2),
eqn2=sy.Eq(Force,Mass*Acceleration),
term1=Force,
term2=Energy):
"""associate 2 terms using 2 equations
with a common term
yielding 1 or more equations with those terms
"""
if not set([term1,term2])-(Term_dict[eqn1]^Term_dict[eqn2]):#if both terms are in the non-assocated elements/
common_terms=Term_dict[eqn1]&Term_dict[eqn2]
if common_terms:
for term in common_terms:
new_expr = eqn1.subs(term,sy.solve(eqn2,term)[0])
if new_expr:
yield(new_expr)
something=associate_2_variables()
something.next()
#StopIteration Error when I run it as a generator but yields
# the correct expression when I take it out of the generator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment