Tom, I read about typealias and solved the problem. Thank you very much for your help :)
Michela 2015-06-10 17:23 GMT+02:00 Tom Breloff <[email protected]>: > If I understand, you want: > pt_l::Vector{Float64} > in your definition, which is equivalent to: > pt_l::Array{Float64,1} > > Vector is just an alias for a 1-dimensional array. Somewhere in base > julia, there's a line that looks something like: > typealias Vector{T} Array{T,1} > > > When you're constructing TUnit, you can't do it by splatting one giant > vector "generators[j,:]...". You'll have to pass in "pt_l" as its own > argument, so you can control that it's a Vector{Float64}. > > > > > > On Wednesday, June 10, 2015 at 11:02:52 AM UTC-4, Michela Di Lullo wrote: > >> Thank you very much Tom. >> >> I tried the way you say, but I still get an error: >> >> *ERROR: `convert` has no method matching convert(::Type{Float64}, >> ::Array{Float64,2})* >> >> * in TUnit at no file* >> >> * in anonymous at no file:11* >> >> >> Is it correct the way I define pt_l in TUnit? >> >> >> immutable TUnit >> >> Node::Int16 >> >> a::Float64 >> >> b::Float64 >> >> c::Float64 >> >> inf::Float64 >> >> sup::Float64 >> >> ton::Int16 >> >> toff::Int16 >> >> storia0::Int16 >> >> pt0::Float64 >> >> rampa_up::Float64 >> >> rampa_dwn::Float64 >> >> rampa_up_str::Float64 >> >> rampa_dwn_str::Float64 >> >> SUC_C::Float64 >> >> tau_max::Int16 >> >> #pt_l::Array{Float64,7} >> >> pt_l::Matrix{Float64} >> >> #pt_l::Array{Float64,2} >> >> storia0UP::Float64 >> >> storia0DW::Float64 >> >> u0::Float64 >> >> ton0::Float64 >> >> toff0::Float64 >> >> #function foo(args) >> >> # #function body >> >> #end >> >> end >> >> 2015-06-10 16:36 GMT+02:00 Tom Breloff <[email protected]>: >> >>> I can't be completely sure of your goal and why you can't use a loop, >>> but if you need to create those matrices/vectors, this is much cleaner: >>> >>> gmax = size(generators, 1) >>> hmax = size(nodeload, 1) >>> inf, sup, ton, toff, storia0 = [generators[:,i] for i in 5:9] >>> >>> # pt_l here will be a Matrix{Float64} (which is equivalent to >>> Array{Float64,2}) >>> pt_l = inf .+ (sup - inf) * (0:num_PC-1)' / (num_PC-1) # note: the >>> ".+" operator broadcasts the column >>> # vector inf to >>> the (gmax x num_PC) matrix >>> >>> # the following will be Vector{Float64} (which is equivalent to >>> Array{Float64,1}) >>> storia0UP = max(storia0, 0.0) # there's a version of max/min which >>> operate on vectors... no need to use comprehensions >>> storia0DW = min(storia0, 0.0) >>> u0 = float(map(x -> x>0, storia0)) # mapping an anonymous function to >>> the vector, then converting a Vector{Bool} to Vector{Float64} which are 1's >>> and 0's >>> >>> bound0 = x -> max(0.0, min(x, hmax)) # attaching an anonymous function >>> to a variable >>> ton0 = map(bound0, (ton - storia0UP) .* u0) # mapping that function >>> toff0 = map(bound0, (toff - storia0DW) .* (1.0 - u0)) >>> >>> # note you can splat the row from generators first then fill in the >>> remaining args >>> # no need to do the "cat" step (plus you should use hcat instead of >>> cat(2, ...)) >>> tunits = [TUnit(generators[j,:]..., vec(pt_l[j,:]), storia0UP[j], >>> storia0DW[j], u0[j], ton0[j], toff0[j]) for j in 1:gmax] >>> >>> >>> However I still thing you should do: >>> >>> tunits = TUnit[] >>> for j in 1:gmax >>> pt_l = ??? >>> storia0UP = ??? >>> storia0DW = ??? >>> u0 = ??? >>> ton0 = ??? >>> toff0 = ??? >>> >>> push!(tunits, TUnit(generators[j,:]..., pt_l, storia0UP, storia0DW, u0, >>> ton0, toff0)) >>> end >>> >>> Let me know if you have any questions. >>> >>> >>> >>> >>> On Wednesday, June 10, 2015 at 8:16:10 AM UTC-4, Michela Di Lullo wrote: >>>> >>>> I still get an error...... >>>> >>>> Let's try again.. >>>> >>>> num_PC=7 >>>> >>>> immutable TUnit >>>> Node::Int16 >>>> a::Float64 >>>> b::Float64 >>>> c::Float64 >>>> inf::Float64 >>>> sup::Float64 >>>> ton::Int16 >>>> toff::Int16 >>>> storia0::Int16 >>>> pt0::Float64 >>>> rampa_up::Float64 >>>> rampa_dwn::Float64 >>>> rampa_up_str::Float64 >>>> rampa_dwn_str::Float64 >>>> SUC_C::Float64 >>>> tau_max::Int16 >>>> pt_l::Float64 >>>> storia0UP::Float64 >>>> storia0DW::Float64 >>>> u0::Float64 >>>> ton0::Float64 >>>> toff0::Float64 >>>> end >>>> >>>> generators = readdlm("$MyDataPath/gen" * "$NumBuses" * "_" * >>>> "$GenInstance" * ".dat") *#matrix of dimension 6*16* >>>> nodeload = readdlm("$MyDataPath/nodeload" * "$NumBuses" * "_" * >>>> "$NodeloadInstance" * ".dat") >>>> >>>> gmax=int(size(generators,1)) >>>> hmax=int(size(nodeload,2)) >>>> inf=generators[1:size(generators,1),5] >>>> sup=generators[1:size(generators,1),6] >>>> storia0=generators[1:size(generators,1),9] >>>> ton=generators[1:size(generators,1),7] >>>> toff=generators[1:size(generators,1),8] >>>> >>>> #here below I'm computing some other quantities, I want to add as >>>> attributes of the object TUnit, >>>> #together with the ones in generators: >>>> >>>> *pt_l=([inf[j]+(r-1)*(sup[j]-inf[j])/(num_PC-1) for j=1:gmax, >>>> r=1:num_PC]) #matrix of dimensions (gmax=6)*(num_PC=7)* >>>> * storia0UP = [max(storia0[j],0) for j=1:gmax] #vector of >>>> dimensions (gmax=6)*1* >>>> * storia0DW = -[min(storia0[j],0) for j=1:gmax] **#vector of >>>> dimensions (gmax=6)*1* >>>> * u0=[(storia0[j] > 0 ? 1 : 0) for j=1:gmax] **#vector of >>>> dimensions (gmax=6)*1* >>>> * ton0 = int16([min(hmax, max(0,(ton[j]-storia0UP[j])*u0[j])) for >>>> j=1:gmax]) **#vector of dimensions (gmax=6)*1* >>>> * toff0= int16([min(hmax, max(0,(toff[j]-storia0DW[j])*(1-u0[j]))) >>>> for j=1:gmax]) **#vector of dimensions (gmax=6)*1* >>>> >>>> generators = cat(2, generators, pt_l, storia0UP, storia0DW, u0, >>>> ton0, toff0) #matrix of dimensions (gmax=6)*28 >>>> >>>> TUnitS = Array(TUnit,size(generators,1)) >>>> >>>> for j in 1:size(generators,1) >>>> TUnitS[j] = TUnit(generators[j,1:end]...) >>>> end >>>> >>>> and I get: >>>> >>>> *ERROR: `TUnit` has no method matching TUnit(::Float64, ::Float64, >>>> ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, >>>> ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, >>>> ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, >>>> ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Int64, ::Int16, >>>> ::Int16)* >>>> >>>> * in anonymous at no file:5* >>>> >>>> How could I specify that pt_l, attribute of the object TUnit, is a >>>> Matrix and not a vector? >>>> >>>> Thanks in advance for any suggestion, >>>> >>>> Michela >>>> >>>> >>>> >>>> >>>> >>>> Il giorno martedì 9 giugno 2015 18:54:56 UTC+2, Tom Breloff ha scritto: >>>>> >>>>> Seems like this would be much easier in a loop, rather than creating >>>>> lots of temporaries only to extract them out later. >>>>> tunits = TUnit[] >>>>> for i in size(generators,1) >>>>> pt_I = # create vector >>>>> storia0UP = # create Float64 >>>>> # set other vars >>>>> push!(tunits, TUnit(generators[j,:]..., pt_I, storia0UP, <other vars >>>>> >)) >>>>> end >>>>> >>>>> >>>>> On Tuesday, June 9, 2015 at 11:37:37 AM UTC-4, Michela Di Lullo wrote: >>>>>> >>>>>> >>>>>> >>>>>> num_PC=7 >>>>>> generators = readdlm("$MyDataPath/gen" * "$NumBuses" * "_" * >>>>>> "$GenInstance" * ".dat") >>>>>> >>>>>> inf=generators[1:size(generators,1),5] >>>>>> sup=generators[1:size(generators,1),6] >>>>>> pt_l=([inf[j]+(r-1)*(sup[j]-inf[j])/(num_PC-1) for j=1:gmax, >>>>>> r=1:num_PC]) >>>>>> storia0=generators[1:size(generators,1),9] >>>>>> ton=generators[1:size(generators,1),7] >>>>>> toff=generators[1:size(generators,1),8] >>>>>> storia0UP = [max(storia0[j],0) for j=1:gmax] >>>>>> storia0DW = -[min(storia0[j],0) for j=1:gmax] >>>>>> u0=[(storia0[j] > 0 ? 1 : 0) for j=1:gmax] >>>>>> ton0 = int16([min(hmax, max(0,(ton[j]-storia0UP[j])*u0[j])) for >>>>>> j=1:gmax]) >>>>>> toff0= int16([min(hmax, max(0,(toff[j]-storia0DW[j])*(1-u0[j]))) >>>>>> for j=1:gmax]) >>>>>> >>>>>> generators = cat(2, generators, pt_l, storia0UP, storia0DW, u0, >>>>>> ton0, toff0) >>>>>> >>>>>> immutable TUnit >>>>>> Node::Int16 >>>>>> a::Float64 >>>>>> b::Float64 >>>>>> c::Float64 >>>>>> inf::Float64 >>>>>> sup::Float64 >>>>>> ton::Int16 >>>>>> toff::Int16 >>>>>> storia0::Int16 >>>>>> pt0::Float64 >>>>>> rampa_up::Float64 >>>>>> rampa_dwn::Float64 >>>>>> rampa_up_str::Float64 >>>>>> rampa_dwn_str::Float64 >>>>>> SUC_C::Float64 >>>>>> tau_max::Int16 >>>>>> pt_l >>>>>> storia0UP::Float64 >>>>>> storia0DW::Float64 >>>>>> u0::Float64 >>>>>> ton0::Float64 >>>>>> toff0::Float64 >>>>>> end >>>>>> >>>>>> TUnitS = Array(TUnit,size(generators,1)) >>>>>> >>>>>> for j in 1:size(generators,1) >>>>>> TUnitS[j] = TUnit(generators[j,1:end]...) >>>>>> end >>>>>> >>>>>> >>>>>> >>>>> >>
