Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MethodError: no method matching -(::Array{Float64,1}, ::Float64) #11

Closed
rdinnager opened this issue Nov 13, 2018 · 10 comments
Closed

MethodError: no method matching -(::Array{Float64,1}, ::Float64) #11

rdinnager opened this issue Nov 13, 2018 · 10 comments

Comments

@rdinnager
Copy link

Hi there,

I'm really happy this package exists! Thanks for making it available. However, I am having a problem getting SDEs to work (so far ODEs work perfectly).
When I run the following example from the readme I get an error from Julia:

f <- function(u,p,t) {
  du1 = p[1]*(u[2]-u[1])
  du2 = u[1]*(p[2]-u[3]) - u[2]
  du3 = u[1]*u[2] - p[3]*u[3]
  return(c(du1,du2,du3))
}
g <- function(u,p,t) {
  return(c(0.3*u[1],0.3*u[2],0.3*u[3]))
}
u0 = c(1.0,0.0,0.0)
tspan <- list(0.0,1.0)
p = c(10.0,28.0,8/3)
sol = diffeqr::sde.solve(f,g,u0,tspan,p=p,saveat=0.005)

The result is:

Error: Error happens in Julia.
MethodError: no method matching -(::Array{Float64,1}, ::Float64)
Closest candidates are:
  -(!Matched::Float64, ::Float64) at float.jl:397
  -(!Matched::Complex{Bool}, ::Real) at complex.jl:298
  -(!Matched::Missing, ::Number) at missing.jl:93
  ...
Stacktrace:
 [1] macro expansion at C:\Users\u1013287\.julia\packages\Parameters\35RKe\src\Parameters.jl:733 [inlined]
 [2] perform_step!(::StochasticDiffEq.SDEIntegrator{SOSRI,Array{Float64,1},Float64,Float64,Array{Float64,1},Float64,Float64,Float64,NoiseProcess{Float64,2,Float64,Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},typeof(DiffEqNoiseProcess.WHITE_NOISE_DIST),typeof(DiffEqNoiseProcess.WHITE_NOISE_BRIDGE),false,DataStructures.Stack{Tuple{Float64,Array{Float64,1},Array{Float64,1}}},ResettableStacks.ResettableStack{Tuple{Float64,Array{Float64,1},Array{Float64,1}}},RSWM{:RSwM3,Float64},RandomNumbers.Xorshifts.Xoroshiro128Plus},Array{Float64,1},RODESolution{Float64,2,Array{Array{Float64,1},

I am using Julia 1.0.2.

My session info:

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] nimble_0.6-11  bindrcpp_0.2.2 diffeqr_0.1.1 

loaded via a namespace (and not attached):
 [1] deSolve_1.21            progress_1.2.0          tidyselect_0.2.4        purrr_0.2.5            
 [5] pbapply_1.3-3           lattice_0.20-35         colorspace_1.3-2        viridisLite_0.3.0      
 [9] miniUI_0.1.1            htmltools_0.3.6         yaml_2.1.19             plotly_4.7.1           
[13] scico_1.0.0             rlang_0.2.1             manipulateWidget_0.10.0 pillar_1.1.0           
[17] later_0.7.3             glue_1.2.0              bindr_0.1.1             plyr_1.8.4             
[21] stringr_1.3.1           munsell_0.5.0           gtable_0.2.0            htmlwidgets_1.3        
[25] coda_0.19-2             knitr_1.20              httpuv_1.4.4.1          crosstalk_1.0.0        
[29] parallel_3.5.0          Rcpp_0.12.19            readr_1.1.1             xtable_1.8-2           
[33] scales_0.5.0            promises_1.0.1          webshot_0.5.0           magick_1.9             
[37] jsonlite_1.5            mime_0.5                ggplot2_2.2.1.9000      hms_0.4.1              
[41] digest_0.6.15           stringi_1.2.3           dplyr_0.7.7             shiny_1.1.0            
[45] grid_3.5.0              tools_3.5.0             magrittr_1.5            rgl_0.99.16            
[49] lazyeval_0.2.1          tibble_1.4.2            nichefillr_0.0.0.9000   crayon_1.3.4           
[53] tidyr_0.8.1             pkgconfig_2.0.1         data.table_1.11.4       randtoolbox_1.17.1     
[57] prettyunits_1.0.2       httr_1.3.1              assertthat_0.2.0        rstudioapi_0.8         
[61] cubature_1.4            JuliaCall_0.16.1        R6_2.3.0                rngWELL_0.10-5         
[65] igraph_1.2.1            compiler_3.5.0         

Any help with this would be much appreciated! Thanks.

@ChrisRackauckas
Copy link
Member

This ended up being an issue in the integrator itself when done out-of-place + arrays (a rare combination to do except from R). This is fixed with the release of StochasticDiffEq.jl v5.10.1. You'll need to run ]up in Julia to update the package. Thanks for the report!

@rdinnager
Copy link
Author

Just updated StochasticDiffEq and it works great now. Excited to apply this to my own model now. Thanks very much for your time!

@dlekshmi
Copy link

dlekshmi commented Aug 20, 2020

Hi

I obtain the same/ similar error, while trying the 1-D ODE example in the vignette of diffeqr.

library(JuliaCall)
library(diffeqr)

julia_setup(JULIA_HOME="/Applications/Julia-1.5.app/Contents/Resources/julia/bin")
diffeqr::diffeq_setup()


f <- JuliaCall::julia_eval("
function f(du,u,p,t)
  du = 1.01*u
end")



tspan=list(0.0,1.0)
u0=c(0.5)
sol = diffeqr::ode.solve('f',u0,tspan)

The error message I get is:
Error: Error happens in Julia. MethodError: no method matching similar(::Float64) Closest candidates are: similar(!Matched::Sundials.NVector) at /Users/dlekshmi/.julia/packages/Sundials/SKP8f/src/nvector_wrapper.jl:69 similar(!Matched::Array{T,1}) where T at array.jl:375 similar(!Matched::Array{T,2}) where T at array.jl:376 ... Stacktrace: [1] alg_cache(::Tsit5, ::Float64, ::Float64, ::Type{T} where T, ::Type{T} where T, ::Type{T} where T, ::Float64, ::Float64, ::ODEFunction{true,typeof(f),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Float64, ::Float64, ::Float64, ::Nothing, ::Bool, ::Val{true}) at /Users/dlekshmi/.julia/packages/OrdinaryDiffEq/lGGkK/src/caches/low_order_rk_caches.jl:356 [2] (::OrdinaryDiffEq.var"#172#173"{Float64,Float64,DataType,DataType,DataType,Float64,Float64,ODEFunction{true,typeof(f),UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,N

My session info is :

R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] diffeqr_0.1.3    JuliaCall_0.17.1

loaded via a namespace (and not attached):
[1] compiler_4.0.2 magrittr_1.5   tools_4.0.2    Rcpp_1.0.5     tinytex_0.25   stringi_1.4.6  knitr_1.29     stringr_1.4.0 
[9] xfun_0.16  

@ChrisRackauckas
Copy link
Member

I assume you wanted:

f <- JuliaCall::julia_eval("
function f(du,u,p,t)
du .= 1.01.*u
end")

?

@dlekshmi
Copy link

No.. I had tried that as well ... but did not work and gave the same error.

@ChrisRackauckas
Copy link
Member

Try sol = diffeqr::ode.solve('f',u0,tspan,alg="Tsit5()")

@alina-artcibasova
Copy link

I have the same problem as dlekshmi, yesterday we managed to make it work, but it requires turning the problem from 1D to 2D system of equations, and it doesn't work if the function is defined via R, like in the example. I put together an R markdown notebook, with my session info/code.

@ChrisRackauckas
Copy link
Member

I think the issue might be that c(0.5) becomes a scalar on the Julia side instead of an array, and that makes things go awry. Can you open a new issue on that?

@Madhuri97
Copy link

function updateSIR(popnvector) # some liberty here: using upper case in the function name
susceptibles = popnvector[1];
infecteds = popnvector[2];
removeds = popnvector[3];
newS = susceptibles - lambdasusceptiblesinfectedsdt
newI = infecteds + lambda
susceptiblesinfectedsdt - gaminfectedsdt #note abbreviation for gamma (see below)
newR = removeds + gaminfectedsdt
return [newS newI newR] # NB! spaces only to make this a one row of a two-dimensional array
# and note the use of "return" to specify what the function output should be
end
lambda = 1/20000 # infection rate parameter (assumes rates are per day)
gam = 1/10 # recovery rate parameter (ditto)
dt = 0.5 # length of time step in days
tfinal = 610; # respecting community values: lowercase only in the names
s0 = 10000.0 # initial susceptibles, note that we use the type Float64 from the start
i0 = 4. # initial infecteds; set this to 1. to mimic an epidemic with an index case
r0 = 0. # not always the case, of course

initialise

nsteps = round(Int64, tfinal/dt) # note the use of round() with type Int64 to ensure that nsteps is an integer
resultvals = Array(Float64, nsteps+1, 3) #initialise array of type Float64 to hold results
timevec = Array(Float64, nsteps+1) # ... ditto for time values
resultvals[1,:] = [s0, i0, r0] # ... and assign them to the first row
timevec[1] = 0. # also Float64, of course.

for step = 1:nsteps
resultvals[step+1, :] = updateSIR(resultvals[step, :]) # NB! pay careful attention to the rows being used
timevec[step+1] = timevec[step] + dt
end

I am new to julia i can not understand where the error comes can anyone please help me to solve
MethodError: no method matching Array(::Type{Float64}, ::Int64, ::Int64)
Closest candidates are:
Array(!Matched::LinearAlgebra.UniformScaling, ::Integer, ::Integer) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\LinearAlgebra\src\uniformscaling.jl:330

Stacktrace:
[1] top-level scope at In[9]:12

@ChrisRackauckas
Copy link
Member

The error message says it's using Julia v1.0 but diffeqr asks for v1.3+. Also, this looks like it's not using diffeqr and the error message isn't related? So I don't think that issue is related to this at all. Please ask general usage questions on https://discourse.julialang.org/ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants