Is it possible to overwrite : to allow this head and tail concatenation syntax?
julia> (:){T}(a::T, b::Array{T}) = vcat([a], b)
Colon()
julia> a=collect(2:9);
julia> 1:a
ERROR: MethodError: no method matching colon(::Int64, ::Array{Int64,1})
Closest candidates are:
colon(::T<:Real, ::Any, !Matched::T<:Real) where T<:Real at range.jl:47
colon(::A<:Real, ::Any, !Matched::C<:Real) where {A<:Real, C<:Real} at range.jl:14
colon(::T, ::Any, !Matched::T) where T at range.jl:46
...
Related
I would like to run the function f() on all my 4 processors (intel i7) and fetch the rands sum, as follows:
using Distributed;
#everywhere function f()
return sum(rand(10000))
end
#sync for w in workers()
#async begin
res = #spawnat w f()
values[w-1] = fetch(res)
end
end
But, getting the following error:
ERROR: TaskFailedException
nested task error: MethodError: no method matching setindex!(::typeof(values), ::Float64, ::Int64)
Stacktrace:
[1] macro expansion
# ./REPL[58]:4 [inlined]
[2] (::var"#68#70"{Channel{Any}, Int64})()
# Main ./task.jl:411
Stacktrace:
[1] sync_end(c::Channel{Any})
# Base ./task.jl:369
[2] top-level scope
# task.jl:388
Please guide me in resolving the issue!
For your code the easiest way would be (assuming Julia has been run with -p 4 command line parameter or you have run addprocs(4):
julia> values = #distributed (append!) for i in 1:4
[f()]
end
4-element Vector{Float64}:
5001.232864826896
4999.244031827526
4966.883114472259
5014.022690758762
If you want to do #spawns yourself this code works:
julia> values = Vector{Float64}(undef, 4);
julia> #sync for w in workers()
#async values[w-1] = fetch(#spawnat w f())
end
julia> values
4-element Vector{Float64}:
5029.967318172736
4993.1064528029
5016.491407076979
5062.0706219606345
However your code mostly didn't work because the type of your values was not Vector{Float64}. Here is how to replicate your error:
julia> vv()=0
vv (generic function with 1 method)
julia> vv[1]=11
ERROR: MethodError: no method matching setindex!(::typeof(vv), ::Int64, ::Int64)
Here is my code:
Dates.Year(div(19973,10)) # 1997 years
Dates.Month(round(mod(19973,10)*3)) # 9 months
the above is Ok, but...
Dates.lastdayofquarter(Date(Dates.Year(div(19973,10)),Dates.Month(round(mod(19973,10)*3)),1))
# MethodError: no method matching Int64(::Year)
Closest candidates are:
Int64(!Matched::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, #Int16, Int8, UInt128, UInt16}) at boot.jl:708,
Int64(!Matched::Ptr) at boot.jl:718,
Int64(!Matched::Float32) at float.jl:706,
...
Date(::Year, ::Month, ::Int64) at types.jl:368,
top-level scope at untitled-b0de772dbeef3476c50547132427f175:73
include_string(::Function, ::Module, ::String, ::String) at loading.jl:1088
Here it is:
julia> lastdayofquarter(Date(19973 ÷ 10, (19973 % 10)*3))
1997-09-30
Note that ÷ is integer division, % is modulo operator and a Date object can be constructed by passing year and month.
Or another one-liner inspired by #phipsgabler comment:
julia> lastdayofquarter(Date(divrem(19973, 10).*(1,3)...))
1997-09-30
I am trying to take a string a = "99.99" and then convert it to be of type float. On top of that, I want to be able to convert a to an int as well. How can I do that? The built-in int() and float() functions don't appear to take strings.
julia> a = "99.99"
"99.99"
julia> float(a)
ERROR: MethodError: no method matching AbstractFloat(::String)
Closest candidates are:
AbstractFloat(::Bool) at float.jl:252
AbstractFloat(::Int8) at float.jl:253
AbstractFloat(::Int16) at float.jl:254
...
Stacktrace:
[1] float(::String) at ./float.jl:271
[2] top-level scope at REPL[2]:1
julia> Int(a)
ERROR: MethodError: no method matching Int64(::String)
Closest candidates are:
Int64(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:710
Int64(::Ptr) at boot.jl:720
Int64(::Float32) at float.jl:700
...
Stacktrace:
[1] top-level scope at REPL[3]:1
Inspired by this post.
You can use the parse(::Type{T}, ::AbstractString) function, like so:
julia> parse(Float64, "1")
1.0
When I tried to calculate
julia> -2.3^-7.6
-0.0017818389423254909
But the result given by my calculator is
0.0005506 + 0.001694 i
Just to be safe I tried it again and this time it complains. Why does it not complain when I tried it the first time?
julia> a = -2.3; b = -7.6; a^b
ERROR: DomainError with -2.6:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
Stacktrace:
[1] throw_exp_domainerror(::Float64) at ./math.jl:35
[2] ^(::Float64, ::Float64) at ./math.jl:769
[3] top-level scope at none:0
[4] eval at ./boot.jl:319 [inlined]
[5] #85 at /Users/ssiew/.julia/packages/Atom/jodeb/src/repl.jl:129 [inlined]
[6] with_logstate(::getfield(Main, Symbol("##85#87")),::Base.CoreLogging.LogState) at ./logging.jl:397
[7] with_logger(::Function, ::Atom.Progress.JunoProgressLogger) at ./logging.jl:493
[8] top-level scope at /Users/ssiew/.julia/packages/Atom/jodeb/src/repl.jl:128
This is an order of operations issue. You can see how Julia's parsing that expression:
julia> parse("-2.3^-7.6")
:(-(2.3 ^ -7.6))
and so the reason you don't have any problems is because you're actually taking 2.3 ^ (-7.6), which is 0.0017818389423254909, and then flipping the sign.
Your second approach is equivalent to making sure that the "x" in "x^y" is really negative, or:
julia> parse("(-2.3)^-7.6")
:(-2.3 ^ -7.6)
julia> eval(parse("(-2.3)^-7.6"))
ERROR: DomainError:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
Stacktrace:
[1] nan_dom_err at ./math.jl:300 [inlined]
[2] ^(::Float64, ::Float64) at ./math.jl:699
[3] eval(::Module, ::Any) at ./boot.jl:235
[4] eval(::Any) at ./boot.jl:234
And if we follow that instruction, we get what you expect:
julia> Complex(-2.3)^-7.6
0.0005506185144176565 + 0.0016946295370871215im
I am using the dataStructures.jl package. The documentation online states that once created a dictionaries
dict = DataStructures.SortedDict{Int64, Int64}
I should be able to insert key-value pairs into the data structure by the function, for example
insert!(dict, 10, 100)
but I keep getting the error
ERROR: MethodError: `insert!` has no method matching insert!(
::Type{DataStructures.SortedDict{Int64,Int64,Ord<:Base.Order.Ordering}}, ::Int64, ::Int64)
Closest candidates are:
insert!{T}(::Array{T,1}, ::Integer, ::Any)
insert!(::BitArray{1}, ::Integer, ::Any)
insert!(::PyCall.PyVector{T}, ::Integer, ::Any)
Why is this? Thanks.
I think you've created a datatype, not a dictionary:
julia> using DataStructures
julia> dict = DataStructures.SortedDict{Int64, Int64}
DataStructures.SortedDict{Int64,Int64,Ord<:Base.Order.Ordering}
julia> typeof(dict)
DataType
—the error message is saying "Don't call this function with a type as the first argument, there's no method for that."
I think this is what you want:
julia> dict = SortedDict(["a" => 1, "b" => 2])
DataStructures.SortedDict{String,Int64,Base.Order.ForwardOrdering} with 2 entries:
"a" => 1
"b" => 2
julia> typeof(dict)
DataStructures.SortedDict{String,Int64,Base.Order.ForwardOrdering}