I'm using julia 0.5 after run this code :
Freqsample = 100;
second = 4;
step = (Freqsample * second )-1
i get this Error :
MethodError: no method matching getindex(::Int64, ::Colon, ::UnitRange{Int64})
in -(::Int64, ::Int64) at main.jl:12
in include_string(::String, ::String) at loading.jl:441
in eval(::Module, ::Any) at boot.jl:234
in (::Atom.##65#68)() at eval.jl:40
in withpath(::Atom.##65#68, ::Void) at utils.jl:30
in withpath(::Function, ::Void) at eval.jl:46
in macro expansion at eval.jl:109 [inlined]
in (::Atom.##64#67{Dict{String,Any}})() at task.jl:60
Whats wrong with subtracting ? i'm pretty new to julia forgive if its a dumb question
You've redefined - for more types than you probably intended. The second line in the backtrace you posted is telling you that Julia called a - method in main.jl for two integers. And line one is saying that within there it's trying to do something like x[:, 1:5] at line 12, where x is an integer.
This tells me two things;
Your definition of - is probably typed too permissively. You probably didn't intend to accept integers.
You are probably shadowing the built in - definition instead of extending it. You need to import Base: - in order to add a new method to a function in the standard library.
Related
Is it possible to type function kwargs in Julia?
The following works for standard Varargs.
function int_args(args::Integer...)
args
end
int_args(1, 2, 3)
# (1, 2, 3)
int_args(1, 2, 3.0)
# ERROR: MethodError: `int_args` has no method matching int_args(::Int64, ::Int64, ::Float64)
However, when applying this same syntax to kwargs, all function calls seem to error.
function int_kwargs(; kwargs::Integer...)
kwargs
end
int_kwargs(x=1, y=2)
# ERROR: MethodError: `__int_kwargs#0__` has no method matching __int_kwargs#0__(::Array{Any,1})
Normal keyword arguments can have types, as in function f(x; a::Int=0), but this doesn't work for "rest" keyword arguments. Also note that since we currently don't dispatch on keyword arguments, the a::Int in this case is a type assertion and not a dispatch specification.
It looks like this case is not handled well, and needs a better error message at least. I'd encourage you to file an issue at https://github.com/JuliaLang/julia/issues.
I'm not sure what the syntax x::T... should mean for keyword arguments. In the case of varargs, it's clear that each element of x should have type T, but for rest keyword arguments each element is actually a symbol-value pair. Of course we could give it the meaning you describe (all values have type T), but this doesn't seem to come up very often. Keyword arguments tend to be quite heterogeneous, unlike varargs which are more like lists or arrays.
I am getting an error while running the following Julia snippet
using GR, Interact
t = 0:0.01:1
#manipulate for phi=0:0.1:6.28
plot(cos.(2π*t+phi))
end
LoadError: MethodError: no method matching +(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Float64)
Closest candidates are:
+(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:529
+(!Matched::Bool, ::T<:AbstractFloat) where T<:AbstractFloat at bool.jl:104
+(!Matched::Float64, ::Float64) at float.jl:395
...
in expression starting at C:\Users\W.Aftab\Desktop\Julia_Codes\src\003.jl:3
(::getfield(Main, Symbol("##9#10")))(::Float64) at 003.jl:4
map(::Function, ::Widget{:slider,Float64}) at Observables.jl:174
top-level scope at manipulate.jl:25
Any idea what is wrong?
The following should work better:
using GR, Interact
t = 0:0.01:1
#manipulate for phi=0:0.1:6.28
plot(cos.(2π*t.+phi)) #note the dot in ".+"
end
This is because in that expression, t is a range, and therefore 2pi*t is also a range, because in Julia the product between a scalar and a collection of value is defined to perform the product of each element of the collection by the scalar.
At each iteration in the loop, phi is a scalar. And the operation + is not defined between a scalar and a collection. It has to be explicitly broadcasted, for example using the .+ notation.
I am using a julia v0.6 code. But my julia version is v1.1. I don't know how to modify this piece of code
roadway_HOLO = open(io->read_dxf(io, Roadway, dist_threshold_lane_connect=2.0), joinpath(#__DIR__, "../data/ngsim_HOLO.dxf"), "r")
convert_curves_feet_to_meters!(roadway_HOLO)
open(io->write(io, roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
The error occurs at the last line of the code. Please help.
ERROR: LoadError: LoadError: MethodError: no method matching write(::IOStream, ::AutomotiveDrivingModels.Roadway)
Closest candidates are:
write(::IO, ::Any) at io.jl:498
write(::IO, ::Any, ::Any...) at io.jl:500
write(::IOStream, ::UInt8) at iostream.jl:378
...
Stacktrace:
I believe you are using this AutomotiveDrivingModels.jl. It seems to be that you need to pass MIME("text/plain") to the call to write in order to write your Roadway object to a text file.
open(io->write(io, MIME("text/plain"), roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
# or
open(io->write(io, MIME"text/plain"(), roadway_HOLO), joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w")
Note that you can also use do syntax with methods whose first argument is a Function like open. The call above is equivalent to the following call below.
open(joinpath(#__DIR__, "../data/ngsim_HOLO.txt"), "w") do io
write(io, MIME("text/plain"), roadway_HOLO)
end
As a side note, methods(write) should normally show the write method(s) for Roadway type and also ?write should show the docstring for this write method. If you happen to see a similar error in the future, you might want to try these to find the correct method signature.
Use Serialization to store Julia objects in a file:
Consider some custom data structure and some object.
struct Some
x::String
y::Int
end
s = Some("test 123",12345)
The above structure can be serialized with the following command:
using Serialization
open("file.bin","w") do f
serialize(f,s)
end
Now let us test deserialization:
julia> open("file.bin") do f; println(deserialize(f)==s); end
true
I'm new to Julia and it's an exciting language. I just come across some weird behavior that I cannot find an explanation online. I appreciate your help.
versioninfo()
Julia Version 0.4.0
Commit 0ff703b* (2015-10-08 06:20 UTC)
Platform Info:
System: Darwin (x86_64-apple-darwin13.4.0)
CPU: Intel(R) Core(TM) i5-4258U CPU # 2.40GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.3
I defined a structure
type mytype
x :: UInt8
y :: UInt8
mytype(n::UInt8) = new(n, 0)
end
The type contains a constructor that has a default value for one field and takes input for the other. Then I test it
mytype(1)
LoadError: MethodError: `convert` has no method matching convert(::Type{mytype}, ::Int64)
This may have arisen from a call to the constructor mytype(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, !Matched::T)
mytype(!Matched::UInt8)
while loading In[56], in expression starting on line 1
in call at essentials.jl:56
The error message is very confusing and I can't understand it. I tested that if I provide default values to the two parameters or take the two inputs, the code works well.
Thanks.
I get
julia> mytype(1)
ERROR: MethodError: `convert` has no method matching convert(::Type{mytype}, ::Int64)
This may have arisen from a call to the constructor mytype(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, ::T)
mytype(::UInt8)
in call at essentials.jl:56
To break it down, it says it tried to call the constructor, but no constructor matched. Why did no constructor match? Because there is no automatic conversion defined from Int64 (the 1) to UInt8.
It then tried converting a Int64 into mytype, but that method isn't defined either. It then gave up, and displayed the closest things to the thing it originally tried (mytype(::Int64)).
You might want to provide additional methods to handle this, or accept any integer in your constructor and use convert to force it to UInt8 - if it fails, it'll throw an exception:
julia> type mynewtype
x :: UInt8
y :: UInt8
mynewtype(n::Integer) = new(n, 0)
end
julia> mynewtype(5)
mynewtype(0x05,0x00)
In the original question,mytype 's member y will always be 0 because the type inner constructor is called with the second argument 0.
They way to construct an instance in this case is : mytype(UInt8(25)) which yields mytype2(0x19,0x00).
However, it cannot accept values for y: mytype(UInt8(5), UInt8(23)) yields an error.
In the answer, mynewtype can handle normal Int64 through the inner constructor however, it will always initialize the y with 0 as well ...
In Julia, the inner constructor initializes the members of the type.The way to properly handle different argument signatures is by defining the type:
type mytype2
x::UInt8
y::UInt8
end
and an outer constructor: mytype2(n::Integer) = mytype2(n,0) which calls the default, implicit (and invisible), inner constructor mytype2(x,y) = new(x,y).
mytype2 can be verified by mytype2(1,2) which yields mytype2(0x01,0x02) and, mytype2(1) which yields mytype2(0x01,0x00).
More info at : Julia 0.4 constructors documentation page, "Inner Constructor Methods" (recommendations for safe type definitions at the end of the section)
I'm having trouble appending to an empty vector in Julia.
v = Int64[]
append!(v,1)
append(v,1)
The append! gives the error
ERROR: `Variable` has no method matching Variable(::Int64, ::Int64, ::Int64, ::Int64)
And append gives the error
ERROR: append not defined
This is probably a basic mistake on my part, but I can't figure out why neither command is working.
If you're appending a scalar value, you want push!. If you're adding a list of elements, then you want append!. There's a good reason for the distinction, as you will probably realize if you consider what should happen if you want to build an array-of-arrays.
Typing ?append! at the REPL will show you help on the function, including a demo on how to use it. (In julia 0.4, the help has been improved and refers you to the push! function as well, but that doesn't seem to have been implemented in the current release.)