oreilly_essential_math_for_data_science_book icon indicating copy to clipboard operation
oreilly_essential_math_for_data_science_book copied to clipboard

subs() errors out with a standard Python function

Open theRocket opened this issue 2 years ago • 0 comments

https://github.com/thomasnield/oreilly_essential_math_for_data_science_book/blob/930169272a09108c089545667dcbbfec68ae286e/chapter_1/1_20_deriviatve_subst.py#L12

This example does not use sympy so subs() throws an error:

❯ python -V
Python 3.12.0

❯ python 1_20_deriviatve_subst.py
4.0
Traceback (most recent call last):
  File "/Users/ricker/Code/learning/oreilly_essential_math_for_data_science_book/chapter_1/1_20_deriviatve_subst.py", line 12, in <module>
    print(dx_f.subs(x,2)) # prints 4
          ^^^^^^^^^
AttributeError: 'function' object has no attribute 'subs'

I'm not sure how you want to present it, but if I change the code like follows, it will run successfully (the function f(x) was not used):

from sympy import *

x = symbols('x')
dx_fx = 2*x

# slope_at_2 = dx_fx(2.0)

print(type(dx_fx))

# Calculate the slope at x = 2
dx_f_2 = dx_fx.subs(x,2)
print(dx_f_2.doit())

❯ python 1_20_deriviatve_subst.py
<class 'sympy.core.mul.Mul'>
4

theRocket avatar Nov 21 '23 23:11 theRocket