Comments (16)
Use erf.([1,1])
. Nowadays this is the way to apply scalar functions elementwise to vectors in Julia.
from specialfunctions.jl.
The deprecation message is wrong, though; see also #37.
from specialfunctions.jl.
Thanks stevengj! This is a help. Although lambdifying via SymPy will remain an issue.
from specialfunctions.jl.
What's the problem with SymPy?
from specialfunctions.jl.
The same error occurs if you happen to run a lambdified SymPy function over a vector if it contains an erf expression.
from specialfunctions.jl.
Why wouldn't you use a dot call for that too?
from specialfunctions.jl.
Ah, yes. I should have specified:
If solve() is used, special functions such as erf may be generated. Converting these to erf.() could be problematic.
from specialfunctions.jl.
I still don't understand. SymPy itself is generating code that calls erf
on a vector? Or SymPy is returning a scalar function f
, which involves erf, that you want to apply to a vector? (In the latter case you could call f.(x)
, no?) Can you give a specific example?
from specialfunctions.jl.
To evaluate on a vector, “lambdify” is needed as far as I know. So “solve” generates what could be a multivariate expression and each variable can be evaluated over a vector after lambdifying and specifying the variables and their matched vectors. I’m on a dinky machine at the moment so please forgive the formatting.
from specialfunctions.jl.
Can you please give a specific example (i.e. a small code snippet) using SymPy, in which the lack of a vector method for erf
is a problem?
from specialfunctions.jl.
$using SymPy
$@syms x
#some solution is generated by solve()
$sol1
erf(x)
$f=sol1
$g= lambdify(f)
$X=[1 2]
$g(X)
from specialfunctions.jl.
Can you be more specific about the solution generated by solve
? In your example, the problem is solved by the dotted version g.(X)
:
using SpecialFunctions, SymPy
g = lambdify(erf(symbols("x")))
g.([1.0, 2]) # --> 2-element Array{Float64,1}:
0.842701
0.995322
from specialfunctions.jl.
Follow-up:
I thought Lambdify was working for scalars but I was mistaken
using SymPy
R = symbols("R",real=true)
sigma = symbols("\sigma",real=true,positive=true);
fR = 1/sqrt(sigma^2)*exp(-R^2/(2*sigma^2));
fR_int = integrate(fR,R,0,1)
$\frac{\sqrt{2} \sqrt{\pi}}{2} \operatorname{erf}{\left (\frac{\sqrt{2}}{2 \sigma} \right )}$
evalf(fR_int(sigma=>1))
0.855624391892149
So erf works for scalars with this subs
alias. But with lambdify it's not the case:
fR_intλ = lambdify(fR_int,[sigma])
(::#223) (generic function with 1 method)
fR_intλ(1)
erf(0.7071067811865476,) has been moved to the package SpecialFunctions.jl.
Run Pkg.add("SpecialFunctions") to install SpecialFunctions on Julia v0.6 and later,
and then run `using SpecialFunctions`.
Stacktrace:
[1] ##657(::Int64) at ./<missing>:0
[2] invokelatest(::Function, ::Int64, ::Vararg{Int64,N} where N) at ./essentials.jl:370
[3] (::SymPy.##223#224)(::Int64, ::Vararg{Int64,N} where N) at /Users/MYUSERNAME/.julia/v0.6/SymPy/src/lambdify.jl:150
[4] include_string(::String, ::String) at ./loading.jl:515
Attempting a workaround with replace
is unsuccessful:
fR_int_s = string(fR_int)
fR_int_s = replace(fR_int_s,r"erf",s"erf.")
fR_int_alt = eval(parse(fR_int_s))
$1.2533141373155 \operatorname{erf}{\left (\frac{0.707106781186548}{\sigma} \right )}$
fR_int_alt(sigma=>1)
$$0.855624391892149$$
fR_int_altλ = lambdify(fR_int_alt)
(::#223) (generic function with 1 method)
fR_int_altλ(1)
erf(0.707106781186548,) has been moved to the package SpecialFunctions.jl.
Run Pkg.add("SpecialFunctions") to install SpecialFunctions on Julia v0.6 and later,
and then run `using SpecialFunctions`.
Stacktrace:
[1] ##658(::Int64) at ./<missing>:0
[2] invokelatest(::Function, ::Int64, ::Vararg{Int64,N} where N) at ./essentials.jl:370
[3] (::SymPy.##223#224)(::Int64, ::Vararg{Int64,N} where N) at /Users/MYUSERNAME/.julia/v0.6/SymPy/src/lambdify.jl:150
[4] include_string(::String, ::String) at ./loading.jl:515
And also for vectors:
fR_int_altλ([1,1])
erf([0.707107, 0.707107],) has been moved to the package SpecialFunctions.jl.
Run Pkg.add("SpecialFunctions") to install SpecialFunctions on Julia v0.6 and later,
and then run `using SpecialFunctions`.
Stacktrace:
[1] ##658(::Array{Int64,1}) at ./<missing>:0
[2] invokelatest(::Function, ::Array{Int64,1}, ::Vararg{Array{Int64,1},N} where N) at ./essentials.jl:370
[3] (::SymPy.##223#224)(::Array{Int64,1}, ::Vararg{Array{Int64,1},N} where N) at /Users/MYUSERNAME/.julia/v0.6/SymPy/src/lambdify.jl:150
[4] include_string(::String, ::String) at ./loading.jl:515
I'm not sure if this is more an issue for the SymPy devs, but this functionality was present in v0.5
from specialfunctions.jl.
Oy, neglected to import SpecialFunctions
. On doing so, everything works except the final
vector expression.
from specialfunctions.jl.
Can it be that lambdify
expects a SymFunction
of one parameter, not a constant as you wrote for fR_int
? I remember that there was something strange with the way SymPy
assigns free variables: maybe this has been fixed?
using SpecialFunctions, SymPy
R = symbols("R",real=true)
sigma = symbols("σ",real=true, positive=true)
fR = 1/sqrt(sigma^2)*exp(-R^2/(2*sigma^2))
fR_int = integrate(fR,R,0,1)
g = lambdify(fR_int(sigma))
g.([1, 2]) --> 2-element Array{Float64,1}:
0.855624
0.479925
EDIT: I re-read your post and only now I understand that you came to the same conclusion. Sorry.
from specialfunctions.jl.
No, you're right... dotting the final lambdified expression works fine with vectors. I'll go ahead and close this as Stephen's suggestion earlier was correct.
from specialfunctions.jl.
Related Issues (20)
- Add logabsgamma(::Complex) HOT 3
- incorrect reference HOT 2
- Address known-broken ChainRules tests
- Add gamma(::Complex{BigFloat}) HOT 1
- Inverse of erfi
- Reduce/remove inner allocations (e.g. `gamma_inc_taylor`, `gamma_inc_asym`) HOT 9
- Segfault in besselj HOT 2
- Regularized Kumer Functions? HOT 1
- Generalize beta, logbeta, etc. to any number of arguments
- Accuracy of `digamma` function can be improved HOT 1
- What about `gamma_inc_inv`'s derivative ? HOT 2
- `expint(nu,z)` has bad precision for `nu != 1`
- Failing to build on Apple M1 Sonoma HOT 1
- Registering Functions with `Symbolics.jl` HOT 1
- Large negative real number arguments return NaN for expintx HOT 1
- Rewrite all AMOS code in julia HOT 5
- Internals of the incomplete gamma function does not work with `ForwardDiff.jl`'s `Dual()` type due to enforced floating point precision HOT 4
- Extensive use of abs for complex tolerances HOT 2
- Allocations in the sum over a product of `besselj(::Int64, ::Float32)`
- Use of `expint` with ForwardDiff
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from specialfunctions.jl.