Giter Club home page Giter Club logo

Comments (16)

stevengj avatar stevengj commented on July 18, 2024

Use erf.([1,1]). Nowadays this is the way to apply scalar functions elementwise to vectors in Julia.

from specialfunctions.jl.

stevengj avatar stevengj commented on July 18, 2024

The deprecation message is wrong, though; see also #37.

from specialfunctions.jl.

benfolsom avatar benfolsom commented on July 18, 2024

Thanks stevengj! This is a help. Although lambdifying via SymPy will remain an issue.

from specialfunctions.jl.

stevengj avatar stevengj commented on July 18, 2024

What's the problem with SymPy?

from specialfunctions.jl.

benfolsom avatar benfolsom commented on July 18, 2024

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.

stevengj avatar stevengj commented on July 18, 2024

Why wouldn't you use a dot call for that too?

from specialfunctions.jl.

benfolsom avatar benfolsom commented on July 18, 2024

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.

stevengj avatar stevengj commented on July 18, 2024

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.

benfolsom avatar benfolsom commented on July 18, 2024

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.

stevengj avatar stevengj commented on July 18, 2024

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.

benfolsom avatar benfolsom commented on July 18, 2024

$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.

mzaffalon avatar mzaffalon commented on July 18, 2024

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.

benfolsom avatar benfolsom commented on July 18, 2024

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.

benfolsom avatar benfolsom commented on July 18, 2024

Oy, neglected to import SpecialFunctions. On doing so, everything works except the final
vector expression.

from specialfunctions.jl.

mzaffalon avatar mzaffalon commented on July 18, 2024

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.

benfolsom avatar benfolsom commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.