sympy AttributeError: Pow object has no attribute sin

2024/7/7 7:14:33

I have read this SO post which says namespace conflict is one reason for this error. I am falling to this error frequently. So, I'd like to learn what exactly is happening here? What is expected by the library?

EDIT: fun = lambda x: 4*x*(np.sin(x**2) - 3)*np.cos(x**2) comes from a test case, so practically I am bound to use it as function 'fun'. Sorry for missing that information. Kindly discuss respecting this constraint.

EDIT2: This is an error reproducing code, not the full script. Task is to calculate differentiation of an input function that can evaluate numpy arrays by using a forward difference approximation with a perturbation ∆=10 −8.

Code:

import sympy
import numpy as np # TESTING...
x = sympy.Symbol('x')
fun = lambda x:  4*x*(np.sin(x**2) - 3)*np.cos(x**2)  
print fun
h = 10e-8  #perturbation
print fun(x)
print fun(x+h)
df = (fun(x+h) - fun(x)) / h
print "diff is:", df

Error:

<function <lambda> at 0x000000001068E2E8>
Traceback (most recent call last):File "<ipython-input-75-0582d8ebb11b>", line 1, in <module>runfile('D:/test_hw3.py', wdir='D:')File "D:\anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfileexecfile(filename, namespace)File "D:\anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfileexec(compile(scripttext, filename, 'exec'), glob, loc)File "D:/test_hw3.py", line 23, in <module>print fun(x)File "D:/test_hw3.py", line 20, in <lambda>fun = lambda x:  4*x*(np.sin(x**2) - 3)*np.cos(x**2)
AttributeError: 'Pow' object has no attribute 'sin'
Answer

You are supposed to use sympy.sin/cos instead of np.sin/cos. Numpy does not know how to work with sympy expressions.

fun = lambda x: 4 * x * (sympy.sin(x**2) - 3) * sumpy.cos(x**2)  

On the other hand, if you must keep np.sin, then you should not send a sympy variable to fun. Instead, df should become a function itself.

fun = lambda x:  4*x*(np.sin(x**2) - 3)*np.cos(x**2)  
h = 1e-8df = lambda x: (fun(x+h) - fun(x)) / h# usage:
df( np.array([1.0, 2.0, 3.0, 4.0]) )

BTW, sympy already has a diff function to compute the derivative.

df = sympy.diff(fun(x), x)
https://en.xdnf.cn/q/119757.html

Related Q&A

Tkinter unbinding key event issue

In the code below, pressing the space bar twice results in two successive beeps. I want to avoid this and instead disable the key while the first beep is happening. I thought unbinding the space key mi…

Is there a way to find the largest change in a pandas dataframe column?

Im trying to find the largest difference between i and j in a series where i cannot be before j. Is there an efficient way to do this in pandas:x = [1, 2, 5, 4, 2, 4, 2, 1, 7] largest_change = 0for i i…

Updating scikit-learn to latest version with Anaconda environment fails with http error 000

I use Anaconda3 installed on my pc Win10 64bits. I noticed it runs with an outdated scikit learn version (0.21.3), and I am trying to update it (0.24.1 available on https://repo.anaconda.com/pkgs/main/…

Python RegEx remove new lines (that shouldnt be there)

I got some text extracted and wish to clean it up by RegEx.I have learned basic RegEx but not sure how to build this one:str = this is a line that has been cut. This is a line that should start on a …

Python CSV writer

I have a csv that looks like this:HA-MASTER,CategoryID 38231-S04-A00,14 39790-S10-A03,14 38231-S04-A00,15 39790-S10-A03,15 38231-S04-A00,16 39790-S10-A03,16 38231-S04-A00,17 39790-S10-A03,17 38231-S04-…

How to perform standardization on the data in GridSearchCV?

How to perform standardizing on the data in GridSearchCV?Here is the code. I have no idea on how to do it.import dataset import warnings warnings.filterwarnings("ignore")import pandas as pd …

how to find the permutations of string? python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Unicode category for commas and quotation marks

I have this helper function that gets rid of control characters in XML text:def remove_control_characters(s): #Remove control characters in XML textt = ""for ch in s:if unicodedata.category(c…

Uppercase every other word in a string using split/join

I have a string: string = "Hello World" That needs changing to: "hello WORLD" Using only split and join in Python. Any help? string = "Hello World" split_str = string.spl…

BeautifulSoup get text from tag searching by Title

Im scrapping a webpage with python that provides different documents and I want to retrieve some information from them. The document gives the information in two ways, theres this one where it gives it…