Comments (6)
Thanks for asking! I'll take a closer look ...
from diffeqr.
If you "translate" the example from the documentation of DifferentialEquations to R with the JuliaConnectoR, it looks like this:
Original Julia code:
using DifferentialEquations
f(u,p,t) = 1.01*u
u0 = 1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,reltol=1e-6,save_everystep=false)
sol(0.45)
"Translated" R code:
Loading the stuff:
library(JuliaConnectoR)
# if it's not installed already
Pkg <- juliaImport("Pkg")
Pkg$add("DifferentialEquations")
de <- juliaImport("DifferentialEquations")
Then the real action:
f <- function(u,p,t) {1.01*u}
u0 <- 1/2
tspan <- c(0.0, 1.0)
prob <- de$ODEProblem(f,u0,tspan)
sol <- de$solve(prob, de$Tsit5(), reltol=1e-6, save_everystep=FALSE)
sol[[2]]
The JuliaConnectoR doesn't get (yet) that sol
is also usable as function, so we need to use map
to evaluate it:
juliaCall("map", sol, 0.45)
It is also possible to specify f
as a Julia function. This is much more efficient because no communication between Julia and R is necessary during solving the equation.
f <- juliaEval("(u,p,t) -> 1.01u")
prob <- de$ODEProblem(f,u0,tspan)
sol <- de$solve(prob, de$Tsit5(), reltol=1e-6, save_everystep=FALSE)
There is one caveat: The JuliaConnectoR was designed with convenience and stability for interactive use. That's why we chose TCP as communication mechanism between Julia and R. But, of course, TCP has a higher cost for the communication when making calls between Julia and R. In every step of the iteration some binary numbers must be sent between Julia and R if the function that is to be optimized is an R function. (If it's a Julia function, this doesn't matter.) There would probably be the need for doing some further benchmarking against the current implementation of diffeqr to decide.
from diffeqr.
I tried to do some benchmarking, but I got an error in diffeqr. That is the code that I tried:
de <- juliaImport("DifferentialEquations")
f <- function(u,p,t) {1.01*u}
u0 <- 1/2
tspan <- c(0.0, 1.0)
system.time(for (i in 1:100) {
prob <- de$ODEProblem(f,u0,tspan)
sol <- de$solve(prob, reltol=1e-6)
})
library(diffeqr)
diffeqr::diffeq_setup() # edit/update (see below)
f <- function(u,p,t) {1.01*u}
u0 <- 1/2
tspan <- list(0.0, 1.0)
system.time(for (i in 1:100) {
ode.solve(f, u0 = u0, tspan = tspan, reltol=1e-6)
})
I get the following error with diffeqr when running ode.solve
:
> diffeqr::ode.solve(f, u0 = u0, tspan = tspan, reltol=1e-6)
Error: Error happens in Julia.
UndefVarError: ODEProblem not defined
Stacktrace:
[1] top-level scope at none:0
[2] eval(::Module, ::Any) at ./boot.jl:331
[3] eval_string(::String) at /opt/R/R-4.0.2/lib/R/library/JuliaCall/julia/setup.jl:195
[4] docall(::Ptr{Nothing}) at /opt/R/R-4.0.2/lib/R/library/JuliaCall/julia/setup.jl:168
I don't know what is wrong. I use R version 4.0.2, Julia version 1.4.2 and DifferentialEquations version 6.15.0.
from diffeqr.
Did you run diffeqr::diffeq_setup()
?
from diffeqr.
No, after running diffeqr::diffeq_setup()
it worked. (I added it to the code above.)
The above benchmark shows the following results on my computer (measured after first run):
When executing the code without the loop, the speed difference is not noticable for the human perception, I would say. With the loop, there is a significant difference:
Elapsed time for calling the diffeqr code in the loop above a 100 times: 0.4s
Elapsed time for calling the JuliaConnectoR code in the loop above a 100 times, using an R function: 29s
Elapsed time for calling the JuliaConnectoR code in the loop above a 100 times, using a Julia function: 1.4s
In this case, we have the additional overhead of two calls and one additional assignment for the JuliaConnectoR vs one call in the diffeqr implementation, which explains that there is still a difference when using the Julia function.
I also report that during one time of exeuting the diffeqr code, I had a segfault/system error, which crashed R.
from diffeqr.
diffeqr is probably more suitable for this specific project, though JuliaConnectoR is great!
from diffeqr.
Related Issues (20)
- Direct wrapping? HOT 7
- R dev notes
- ERROR: LoadError: MethodError: no method matching Sundials.NVector(::Array{Int64,1}) HOT 4
- 1-dimensional equations are converted to scalar equations HOT 1
- Direct definition of fast functions from R HOT 3
- named vectors in Julia function HOT 7
- modelling of events HOT 1
- Arrays of arrays in parameters cannot JIT compile HOT 4
- Getting started with diffeqr and Julia1.5.2 HOT 2
- Error with jitOptimize_ode function HOT 3
- Matrix multiplication in JIT compile HOT 1
- DDE vignette - Error happens in Julia. MethodError: no method matching promote_tspan HOT 5
- Passing parameter lists HOT 1
- Install fails on windows 10 HOT 2
- MethodError: no method matching similar_array_type(::Type{JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{UnitRange{Int64}},Tuple{Dict{Int64,Int64}}}}, ::Type{GenericAffExpr{Float64,VariableRef}})
- attempt to apply non-function HOT 5
- When running example I get a "Initial condition incompatible with functional form." HOT 7
- Error in R4.3 + Julia 1.9.2 with Mac M1 HOT 9
- MethodError: no method matching EnsembleGPUArray() HOT 7
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 diffeqr.