Using quad(str..) to integrate - python

Hello I'm trying to integrate a function stored in a variable
from scipy.integrate import quad
fun= x+1
result= quad(fun,2,0)
I know the fun should be instead a int but do you know how I can integrate a function stored this way? I tried to use lambda x:... but I want to use 'fun' and not lambda x :x+1

Related

Using Sympy on methods from the math module

As part of a wider plotting program I have a script where there is a function f defined as a lambda expression, and I need to find its derivative. My code for testing the script is:
import math
import sympy as sym
f = lambda x : math.sin(x)
sx = sym.symbols('x')
sf = sym.diff(f(sx), sx)
print(sf)
While this works fine when f is defined without using any outside methods, the above throws TypeError: can't convert expression to float because the math module's methods don't support the symbolic representations used by Sympy. However, while this works when f instead uses sym.sin, because of how the aforementioned program is structured the definition of f cannot be changed. Is there a way to symbolically interpret the math module's methods?

How do you make lambda a symbol when importing all Sympy functions?

If I import sympy with:
from sympy import *
Then how do set lambda to be a symbol and not a function?
E.g.
lambda = symbols('lambda')
In my case, likely because sympy has all its functions imported (I am working in a sympy only environment so it is convenient), I receive the error:
lambda = symbols('lambda')
^
SyntaxError: invalid syntax
Is there any way to avoid this if I am importing all the functions from sympy?
Thank you
This is because lambda is a keyword for creating lambda functions. You can't use it as your variable name. You'll have to find a new name. Even if you find a way to assign lambda to something, it won't work because it's not parsed as a potential variable name.
For example:
lambda_ = symbols('lambda')
will not have the same error.

import module within python lambda function

Is it possible to import module within a python lambda function? For example, i have a lambda function which requires to import math
import math
is_na_yes_no = lambda x: 'Yes' if math.isnan(x) else 'No'
How can I include the import math statement within the lambda function?
To clarify, I have a scenario that need to put some lambda functions in a config file and evaluate the function in some other python files, exmample:
{
"is_na_yes_no" = "lambda x: 'Yes' if math.isnan(x) else 'No'"
}
In this case, the python file that evaluating those lambda functions need to all modules required.
Thanks #L3viathan for the answer.
Here is same thing without having to import math in the module.
is_na_yes_no = lambda x: 'Yes' if __import__('math').isnan(x) else 'No'
It highlights the flexibility of python -- __import__ feature can be used in lambda functions instead of having them written out before hand.

Could I find a variable in the bessel function in python?

I am using Python to solve an equation. I added the 'Bessel function' in scipy.special, It was working. Now I want to find a variable using Bessel function. For example, I added the order(1) and value(0.44005058574) in Python, but it is not working. (in order to find the variable, I also used solver)
How I can solve the problem?
import numpy as np
import scipy.special as sc
import math
from sympy import Symbol
from sympy.solvers import solve
x=Symbol('x')
y=sc.jn(1,x)-0.44005058574
print(solve(x))
As the output is hinting, the function scipy.special.jn does not know how to handle the object x from simpy. Instead, you should use a numerical approach
>>> from scipy import optimize
>>> f = lambda x: sc.jn(1, x) - 0.44005058574
>>> root = optimize.newton(f, 1.0)
>>> print(root)
0.9999999999848267

Converting a function from sympy to numpy (attribute error)

import numpy as np
import sympy as sym
from numpy import sin
from sympy import symbols, diff
func = lambda x: sin(x)
x = symbols('x')
print diff(func(x),x)
This works if I replace my function with a polynomial, or if I place the trig function directly into the diff operator. But in this format I get AttributeError: sin.
Basically I think python can't recognize func as just being a trig function which it knows how to symbolically integrate. I could just have sympy import sin and then things would work, but then I'm stuck with func referencing sin in the sympy namespace and there are future things I want to do with func which require that it be defined using sin in the numpy namespace.
You should build your expression up symbolically using SymPy functions, and then use lambdify to convert them into things that can be evaluated with NumPy.
This simply isn't how you use sympy. You can't use normal functions in conjunction with numpy--you need to build symbolic expressions using the stuff it provides.
To write code to get the derivative of sin(x), you would do
import sympy as sym
from sympy import sin, symbols, diff
x = symbols('x')
print diff(sin(x), x)
If you have some other particular case you're having trouble with, you'll have to show it. You can't mix sympy with non-sympy stuff in this way, so there isn't some general feedback that can be provided.

Categories