Skip to content

Instantly share code, notes, and snippets.

@marekyggdrasil
Last active January 23, 2017 22:30
Show Gist options
  • Save marekyggdrasil/39a24213ebaba6293464d116821cc334 to your computer and use it in GitHub Desktop.
Save marekyggdrasil/39a24213ebaba6293464d116821cc334 to your computer and use it in GitHub Desktop.
Problem of lambdification I encountered, lambdified function that is difference of another function and a value is not equal to zero when we put as value the evaluation of other function at same point.
from sympy import Symbol, pprint, log, lambdify
# setting symbols
g1 = Symbol("gamma1")
g2 = Symbol("gamma2")
g3 = Symbol("gamma3")
g4 = Symbol("gamma4")
rt = Symbol("rt")
# setting expressions
criteria = (g1 * log(g1, 2.0))/2.0
criteria += (g2 * log(g2, 2.0))/2.0
criteria += (g3 * log(g3, 2.0))/2.0
criteria += (g4 * log(g4, 2.0))/2.0
rooteq = criteria - rt
print "\ncriteria function: "
pprint(criteria)
print "\ncriteria function - rt: "
pprint(rooteq)
# lambdifying expressions to callable functions
tsymbols = [g1, g2, g3, g4, rt]
lambfun_criteria = lambdify(tsymbols, criteria)
lambfun_rooteq = lambdify(tsymbols, rooteq)
# example point x
x = [0.25006462253641376, 2.2501938662000542, 2.2501938662000542, 2.2501938662000542, 0.0]
# evaluating of criteria on x
rootval = lambfun_criteria(*x)
# setting rt to this evaluation
x[4] = rootval
print "\nactual evaluation of rooteq: " + str(lambfun_rooteq(*x))
print "\nexpected evaluation of rooteq: " + str(- x[4] + lambfun_criteria(*x))
from sympy import Symbol, pprint, log, lambdify
from mpmath import mp
mp.dps = 15
# setting symbols
g1 = Symbol("gamma1")
g2 = Symbol("gamma2")
g3 = Symbol("gamma3")
g4 = Symbol("gamma4")
rt = Symbol("rt")
# setting expressions
criteria = (g1 * log(g1, 2.0))/2.0
criteria += (g2 * log(g2, 2.0))/2.0
criteria += (g3 * log(g3, 2.0))/2.0
criteria += (g4 * log(g4, 2.0))/2.0
rooteq = criteria - rt
print "\ncriteria function: "
pprint(criteria)
print "\ncriteria function - rt: "
pprint(rooteq)
# lambdifying expressions to callable functions
tsymbols = [g1, g2, g3, g4, rt]
lambfun_criteria = lambdify(tsymbols, criteria, "mpmath")
lambfun_rooteq = lambdify(tsymbols, rooteq, "mpmath")
# example point x
x = [0.25006462253641376, 2.2501938662000542, 2.2501938662000542, 2.2501938662000542, 0.0]
# evaluating of criteria on x
rootval = lambfun_criteria(*x)
# setting rt to this evaluation
x[4] = rootval
print "\nactual evaluation of rooteq: " + str(lambfun_rooteq(*x))
print "\nexpected evaluation of rooteq: " + str(- x[4] + lambfun_criteria(*x))
$ python lambdifytest.py
criteria function:
0.721347520444482⋅γ₁⋅log(γ₁) + 0.721347520444482⋅γ₂⋅log(γ₂) + 0.721347520444482⋅γ₃⋅log(γ
₃) + 0.721347520444482⋅γ₄⋅log(γ₄)
criteria function - rt:
0.721347520444482⋅γ₁⋅log(γ₁) + 0.721347520444482⋅γ₂⋅log(γ₂) + 0.721347520444482⋅γ₃⋅log(γ
₃) + 0.721347520444482⋅γ₄⋅log(γ₄) - rt
actual evaluation of rooteq: 4.4408920985e-16
expected evaluation of rooteq: 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment