I have a 2 dimensional matrix variable which it should get a constant value. for example
#variable(model1,x[h=1:3,6]==I[h=1:3,6])
I: is a constant matrix. but in code it gives this error:
ERROR: MethodError: no method matching constructvariable!(::JuMP.Model, ::JuMP.#_error#103{Tuple{Sym
bol,Expr}}, ::Array{Float64,2}, ::Array{Float64,2}, ::Symbol, ::String, ::Float64)
would you please help me? thank you
You don't have to asign h again, just use it as index:
#variable(model1, x[h=1:3,6] == I[h,6])
Related
The methods function returns the method table of a function as also mentioned here. I am looking for an explanation on how the function works.
Consider the following example in Julia 1.7:
julia> f(a::Int64,b::Int64=1,c::Float64=1.0) = (a+b+c)
f (generic function with 3 methods)
julia> g(a::Int64,b::Int64=1;c::Float64=1.0) = (a+b+c)
g (generic function with 2 methods)
julia> methods(f)
# 3 methods for generic function "f":
[1] f(a::Int64) in Main at REPL[1]:1
[2] f(a::Int64, b::Int64) in Main at REPL[1]:1
[3] f(a::Int64, b::Int64, c::Float64) in Main at REPL[1]:1
julia> methods(g)
# 2 methods for generic function "g":
[1] g(a::Int64) in Main at REPL[1]:1
[2] g(a::Int64, b::Int64; c) in Main at REPL[1]:1
julia> f(1,1.0)
ERROR: MethodError: no method matching f(::Int64, ::Float64)
Closest candidates are:
f(::Int64) at REPL[1]:1
f(::Int64, ::Int64) at REPL[1]:1
f(::Int64, ::Int64, ::Float64) at REPL[1]:1
Stacktrace:
[1] top-level scope
# REPL[4]:1
julia> g(1,c=1.0)
3.0
julia>
It is not quite clear to me why there is no method f(::Int64, ::Float64) (hence the error). I am also wondering why there is no error for g(1,c=1.0) given that g(::Int64, ::Float64) or g(::Int64, c) are not listed as valid methods for g.
Ah, so to be a bit technical this is really more accurately a question about how type annotations, dispatch, optional arguments, and keyword arguments work in Julia; the methods function just gives you some insight into that process, but it's not the methods function that makes those decisions. To answer your individual questions
It is not quite clear to me why there is no method f(::Int64, ::Float64) (hence the error).
There is no method for this because you you can only omit optional normal (non-keyword) arguments contiguously from the last normal (non-keyword) argument. Consider the following case:
julia> f(a=1, b=1, c=1, d=1) = a + 2b +3c +4d
f (generic function with 8 methods)
julia> f(2,4)
If there were not a rule for this, the compiler would have no idea whether the 2 and 4 provided were supposed to be for a and b, or do I mean that actually I wanted the 2 to go to a and the 4 to go to d? or c? Or anything! This would be undecidable. So we have a rule, and the rule is that the first argument goes to a, the second to b, and the omitted ones are c and d. Even though you have specified defaults, you cannot omit middle arguments, you can only omit the last N optional arguments. This is just the rule, and it does not matter whether or not you have applied type annotations or not.
I am also wondering why there is no error for g(1,c=1.0) given that g(::Int64, ::Float64) or g(::Int64, c) are not listed as valid methods for g.
Firstly, there is no method for g(::Int64, ::Float64) or g(::Int64, c) because keyword arguments (in this example, c) do not participate in dispatch. There is no error for g(1,c=1.0) because when you write g(1,c=1.0), you the optional argument for b is falling back to its default, so you are actually calling g(1,1,c=1.0) When you write g(1,c=1.0), you have explicitly specified that the 1.0 is being assigned to c, so it cannot possibly be the value for b. The value for b has to fall back to it's default, 1.
I am new to Julia and I am trying to create a plot with the following:
xi2 = range(0,sqrt(6),step=1e-3)
collect(xi2)
plot(xi2, 1-xi2^2/6, label="n = 0")
When I try this though, I have the error:
MethodError: no method matching ^(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, ::Int64)
Closest candidates are:
^(::Union{AbstractChar, AbstractString}, ::Integer) at C:\Users\Acer\AppData\Local\Programs\Julia-1.7.0\share\julia\base\strings\basic.jl:721
^(::Rational, ::Integer) at C:\Users\Acer\AppData\Local\Programs\Julia-1.7.0\share\julia\base\rational.jl:475
^(::Complex{<:AbstractFloat}, ::Integer) at C:\Users\Acer\AppData\Local\Programs\Julia-1.7.0\share\julia\base\complex.jl:839
...
What am I missing here?
You want the elements of xi2 raised to the power of two, so you want element-wise operations using the dot operator:
julia> xi2 = range(0,sqrt(6),step=1e-3);
julia> plot(xi2, 1 .- xi2.^2/6, label="n = 0")
(The collect step was unnecessary, since most array operations can be performed on a range directly. And in case you did want to collect - i.e. allocate memory and make it a full array - you have to assign the result of collect to some variable. In your original code, the elements were being collected into an array, but then thrown away since the result wasn't assigned to anything.)
Tried everything except what works to get the following title in my plot: "Volume in m^3 at threshold=30". The threshold value should come from a variable called threshold.
using Plots
using LaTeXStrings
threshold = 30
plot(1:10)
title!("Volume in \$m^3\$ at threshold=$threshold")
# The following works with a hard-coded value for threshold
title!(#L_str("\\textrm{Volume in }m^3\\textrm{ at threshold=30}"))
# The following fails:
s = "\\textrm{Volume in }m^3\\textrm{ for threshold=$threshold}"
title!(#L_str(s))
Error message:
ERROR: LoadError: MethodError: no method matching
#L_str(::LineNumberNode, ::Module, ::Symbol)
Closest candidates are:
#L_str(::LineNumberNode, ::Module, ::String) at
/home/tarik/.julia/packages/LaTeXStrings/YQ4GM/src/LaTeXStrings.jl:68
in expression starting at REPL[25]:1
OK, figured out a solution by avoiding the L macro that does not seem to do much other than adding a $ sign at the beginning and end of strings.
using Plots
using LaTeXStrings
threshold = 30
s="\$\\textrm{Volume in }m^3\\textrm{ at threshold=$threshold}\$"
plot(1:30)
title!(s)
I try to compute the inverse fourier transform of a array of coefficients using ifft with Julia.
I have N complex numbers on an array organized as : Y=[Y_0,.., Y_(N-1)] representing my Fourier coefficients and by computing
ifft(Y)
I get the following error message :
MethodError: no method matching plan_bfft(::Array{Complex,1},
::UnitRange{Int64}) Closest candidates are:
plan_bfft{T<:Union{Complex{Float32},Complex{Float64}},N}(::Union{Base.ReshapedArray{T<:Union{Complex{Float32},Complex{Float64}},N,A<:DenseArray,MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N}}},DenseArray{T<:Union{Complex{Float32},Complex{Float64}},N},SubArray{T<:Union{Complex{Float32},Complex{Float64}},N,A<:Union{Base.ReshapedArray{T,N,A<:DenseArray,MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},N}}},DenseArray},I<:Tuple{Vararg{Union{Base.AbstractCartesianIndex,Colon,Int64,Range{Int64}},N}},L}},
::Any; flags, timelimit) at fft/FFTW.jl:601
plan_bfft{T<:Real}(::AbstractArray{T<:Real,N}, ::Any; kws...) at
dft.jl:205
plan_bfft{T<:Union{Integer,Rational{T<:Integer}}}(::AbstractArray{Complex{T<:Union{Integer,Rational}},N},
::Any; kws...) at dft.jl:207 ...
in #plan_ifft#15(::Array{Any,1}, ::Function, ::Array{Complex,1},
::UnitRange{Int64}) at ./dft.jl:268 in #plan_ifft#3(::Array{Any,1},
::Function, ::Array{Complex,1}) at ./dft.jl:58 in
ifft(::Array{Complex,1}) at ./dft.jl:56
Could anyone help with this?
when I ask typeof(Y) the answer is Array{Complex,1}.
Thank you
Just a guess here: ifft expects the array elements to be of type Complex{Float64}, not Complex. Furthermore,
julia> Complex<:Complex{Float64}
false
How did you get an array of Complex?
When using Complex{Float64} things work correctly:
julia> Y=complex([1.,2.,3.],[4.,3.,2.])
3-element Array{Complex{Float64},1}:
1.0+4.0im
2.0+3.0im
3.0+2.0im
julia> ifft(Y)
3-element Array{Complex{Float64},1}:
2.0+3.0im
-0.788675+0.211325im
-0.211325+0.788675im
Using Julia, I've defined a 9x10 matrix of zeros, and am trying to change a single entry , but I get the error 'setindex!' has no method matching setindex!(::Float64, ::Float64, ::Int64)
My code is:
m = zeros(9,10)
m[1][1] = 1.0
with the error pointing to the second line. typeof(m) is an Array{Float64,2}, which, as far as I can tell is mutable.
What am I doing wrong here?
To index 2-dimensional arrays, just use m[1,1].
The syntax m[1][1] would be valid for a 1-dimensional array of 1-dimensional arrays.
m = zeros(9,10)
m[1,1] = 1.0
m = Array[ [1,2], [3,4,5] ]
m[1][1]