0

I have this code in SymPy:

from sympy import *
par_amplitude, par_const_force = symbols('Delta_U f')
equation = par_amplitude * par_const_force
print(equation.evalf(subs={'f':3, 'Delta_U':2}))

The output is obviously 6.

My problem occurs when I have access to only one parameter, like so:

print(equation.evalf(subs={'Delta_U':2}))

Instead of expected 2 * f I get Delta_U * f.

What am I doing wrong? Is there a way to evaluate an expression, where not all parameters are available?

EDIT:

Yes, I am after a mixed numerical/symbolic output. Imagine a situation where there is a lot of parameters (generated automatically, so you're not even sure how many), and there is a function supplying all the subs values. If you were to miss just one substitution value, you get basically nothing out of evalf.

alex
  • 10,900
  • 15
  • 70
  • 100

2 Answers2

0

You can substitute in your symbolic expression before evaluating numerically:

print(equa.subs({'Delta_U':2}))
print(equa.subs({'Delta_U':2}).evalf())

Substitution also yields 6 even without numerical evaluation:

print(equa.subs({'Delta_U':2, 'f':3}))

EDIT: I have an idea why evalf(...) will not return 2*f. I assume that evalf tries to only return completely numerical result, but 2*f is clearly something mixed. A numerical 2 and a symbolic f.

laolux
  • 1,445
  • 1
  • 17
  • 28
0

This is a known issue and there is discussion about it here.

smichr
  • 16,948
  • 2
  • 27
  • 34
  • I would say the case described in your link differs from the one here. In the link the problem is cases like the numerical evaluation of `1/0`, whereas here no such numerical problems arise. I assume the problem is that the OP wants `evalf` to return something mixed with numerical `2` and symbolic `f`. – laolux Aug 12 '17 at 10:54
  • 1
    referenced on the page I site is a list of related issues, e.g. https://github.com/sympy/sympy/issues/6974 is similar to the case given here. – smichr Aug 14 '17 at 02:38