Re: [julia-users] how to make my own 3d array type

2015-11-25 Thread Evan Mason
Great, thanks.  So, I now have:

immutable MyType2{T,N,A<:AbstractArray, I<:Int} <: AbstractArray{T,N}
var :: A
nstar :: I
end

MyType2{T,N}(var::AbstractArray{T,N}, nstar::Int) = MyType2{T,N,
typeof(var), typeof(nstar)}(var, nstar)

Base.linearindexing(::Type{MyType2}) = Base.LinearFast()
Base.size(S::MyType2) = (S.nstar,)

and I found base/linalg/eigen.jl helpful in figuring this out.


On Wed, Nov 25, 2015 at 12:53 PM, Tim Holy  wrote:

> Check out the "Types" section of the manual, esp. the part about "Composite
> Types."
>
> Also, see base/subarray.jl if you want an example of a complex but full-
> featured AbstractArray type.
>
> Best,
> --Tim
>
> On Wednesday, November 25, 2015 03:24:39 AM Evan wrote:
> > Thanks Tim,
> >
> > Following the link you suggested I made the following type which does
> what
> > I want:
> >
> > immutable MyType{T,N,A<:AbstractArray} <: AbstractArray{Float64,3}
> > conf_lims :: A
> > end
> > MyType{T,N}(conf_lims::AbstractArray{T,N}) =
> MyType{T,N,typeof(conf_lims)}(
> > conf_lims)
> > Base.size(S::MyType) = (S.count,)
> > Base.linearindexing(::Type{MyType}) = Base.LinearFast();
> >
> >
> > What I haven't figured out is how to add more than just the one variable;
> > for instance I'd like to have a "count" variable which would be an
> integer?
> >
> > On Monday, November 16, 2015 at 3:45:43 PM UTC+1, Tim Holy wrote:
> > > Totally different error (it's a missing size method). You also need to
> > > read
> > > this:
> > > http://docs.julialang.org/en/stable/manual/interfaces/#abstract-arrays
> > >
> > > --Tim
> > >
> > > On Monday, November 16, 2015 06:08:15 AM Evan wrote:
> > > > Thank you, Tim, I understand the motivation for the approach in the
> link
> > > > you sent.
> > > >
> > > > I'm still unable to create the type that I want. I actually want two
> > >
> > > arrays
> > >
> > > > in my type, one 2d and the other 3d.
> > > >
> > > > First though, I'm still stuck on just the 2d when I try to implement
> > >
> > > your
> > >
> > > > suggestion:
> > > > julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > > >
> > > >var::A
> > > >
> > > >end
> > > >
> > > > julia> MyType{T,N}(var::AbstractArray{T,N}) =
> > >
> > > MyType{T,N,typeof(var)}(var)
> > >
> > > > MyType{T,N,A<:AbstractArray{T,N}}
> > > >
> > > > julia> aa = MyType(zeros(2,3))
> > > > Error showing value of type MyType{Float64,2,Array{Float64,2}}:
> > > > ERROR: MethodError: `size` has no method matching
> > >
> > > size(::MyType{Float64,2,
> > >
> > > > Array{Float64,2}})
> > > >
> > > > Closest candidates are:
> > > >   size{T,n}(::AbstractArray{T,n}, ::Any)
> > > >   size(::Any, ::Integer, ::Integer, ::Integer...)
> > > >
> > > >  in showarray at show.jl:1231
> > > >  in anonymous at replutil.jl:29
> > > >  in with_output_limit at ./show.jl:1271
> > > >  in writemime at replutil.jl:28
> > > >  in display at REPL.jl:114
> > > >  in display at REPL.jl:117
> > > >  [inlined code] from multimedia.jl:151
> > > >  in display at multimedia.jl:162
> > > >  in print_response at REPL.jl:134
> > > >  in print_response at REPL.jl:121
> > > >  in anonymous at REPL.jl:624
> > > >  in run_interface at ./LineEdit.jl:1610
> > > >  in run_frontend at ./REPL.jl:863
> > > >  in run_repl at ./REPL.jl:167
> > > >  in _start at ./client.jl:420
> > > >
> > > > julia>
> > > >
> > > > Performance is important, but it's not clear what I should use in
> place
> > >
> > > of
> > >
> > > > abstract types; I've tried replacing AbstractArray with just Array
> but
> > >
> > > that
> > >
> > > > does not appear to work.
> > > >
> > > > On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote:
> > > > > This fixes two problems:
> > > > >
> > > > > type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > > > >
> > > > > var::A
> > > > >
> > > > > end
> > > > >
> > > > > MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
> > > > >
> > > > >
> > > > >
> > > > > If performance matters, you should not use abstract types for
> fields.
> > >
> > > See
> > >
> > >
> > >
> http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguo
> > >
> > > > > us-fields-in-types-interact-with-the-compiler and the section after
> > >
> > > that.
> > >
> > > > > --Tim
> > > > >
> > > > > On Monday, November 16, 2015 03:54:15 AM Evan wrote:
> > > > > > For a 2d array the following works:
> > > > > >
> > > > > > type mytype{T}
> > > > > >
> > > > > > var :: AbstractMatrix{T}
> > > > > >
> > > > > > end
> > > > > >
> > > > > > julia> t = mytype(zeros(4, 3))
> > > > > >
> > > > > > mytype{Float64}(4x3 Array{Float64,2}:
> > > > > >  0.0  0.0  0.0
> > > > > >  0.0  0.0  0.0
> > > > > >  0.0  0.0  0.0
> > > > > >  0.0  0.0  0.0)
> > > > > >
> > > > > > But how do I extend this to a 3d array?
> > > > > >
> > > > > > julia> t = mytype(zeros(2, 4, 3))
> > > > > > ERROR: MethodError: `convert` has no method matching
> > > > >
> > > > > convert(::Type{mytype{T
> > 

Re: [julia-users] how to make my own 3d array type

2015-11-25 Thread Tim Holy
One more small tweak: Int is a concrete type, so you can use nstar::Int in the 
type definition (you don't need to add another type parameter).

Glad to hear you're off to the races now.

Best,
--Tim

On Wednesday, November 25, 2015 06:29:33 PM Evan Mason wrote:
> Great, thanks.  So, I now have:
> 
> immutable MyType2{T,N,A<:AbstractArray, I<:Int} <: AbstractArray{T,N}
> var :: A
> nstar :: I
> end
> 
> MyType2{T,N}(var::AbstractArray{T,N}, nstar::Int) = MyType2{T,N,
> typeof(var), typeof(nstar)}(var, nstar)
> 
> Base.linearindexing(::Type{MyType2}) = Base.LinearFast()
> Base.size(S::MyType2) = (S.nstar,)
> 
> and I found base/linalg/eigen.jl helpful in figuring this out.
> 
> On Wed, Nov 25, 2015 at 12:53 PM, Tim Holy  wrote:
> > Check out the "Types" section of the manual, esp. the part about
> > "Composite
> > Types."
> > 
> > Also, see base/subarray.jl if you want an example of a complex but full-
> > featured AbstractArray type.
> > 
> > Best,
> > --Tim
> > 
> > On Wednesday, November 25, 2015 03:24:39 AM Evan wrote:
> > > Thanks Tim,
> > > 
> > > Following the link you suggested I made the following type which does
> > 
> > what
> > 
> > > I want:
> > > 
> > > immutable MyType{T,N,A<:AbstractArray} <: AbstractArray{Float64,3}
> > > 
> > > conf_lims :: A
> > > 
> > > end
> > > MyType{T,N}(conf_lims::AbstractArray{T,N}) =
> > 
> > MyType{T,N,typeof(conf_lims)}(
> > 
> > > conf_lims)
> > > Base.size(S::MyType) = (S.count,)
> > > Base.linearindexing(::Type{MyType}) = Base.LinearFast();
> > > 
> > > 
> > > What I haven't figured out is how to add more than just the one
> > > variable;
> > > for instance I'd like to have a "count" variable which would be an
> > 
> > integer?
> > 
> > > On Monday, November 16, 2015 at 3:45:43 PM UTC+1, Tim Holy wrote:
> > > > Totally different error (it's a missing size method). You also need to
> > > > read
> > > > this:
> > > > http://docs.julialang.org/en/stable/manual/interfaces/#abstract-arrays
> > > > 
> > > > --Tim
> > > > 
> > > > On Monday, November 16, 2015 06:08:15 AM Evan wrote:
> > > > > Thank you, Tim, I understand the motivation for the approach in the
> > 
> > link
> > 
> > > > > you sent.
> > > > > 
> > > > > I'm still unable to create the type that I want. I actually want two
> > > > 
> > > > arrays
> > > > 
> > > > > in my type, one 2d and the other 3d.
> > > > > 
> > > > > First though, I'm still stuck on just the 2d when I try to implement
> > > > 
> > > > your
> > > > 
> > > > > suggestion:
> > > > > julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > > > > 
> > > > >var::A
> > > > >
> > > > >end
> > > > > 
> > > > > julia> MyType{T,N}(var::AbstractArray{T,N}) =
> > > > 
> > > > MyType{T,N,typeof(var)}(var)
> > > > 
> > > > > MyType{T,N,A<:AbstractArray{T,N}}
> > > > > 
> > > > > julia> aa = MyType(zeros(2,3))
> > > > > Error showing value of type MyType{Float64,2,Array{Float64,2}}:
> > > > > ERROR: MethodError: `size` has no method matching
> > > > 
> > > > size(::MyType{Float64,2,
> > > > 
> > > > > Array{Float64,2}})
> > > > > 
> > > > > Closest candidates are:
> > > > >   size{T,n}(::AbstractArray{T,n}, ::Any)
> > > > >   size(::Any, ::Integer, ::Integer, ::Integer...)
> > > > >  
> > > > >  in showarray at show.jl:1231
> > > > >  in anonymous at replutil.jl:29
> > > > >  in with_output_limit at ./show.jl:1271
> > > > >  in writemime at replutil.jl:28
> > > > >  in display at REPL.jl:114
> > > > >  in display at REPL.jl:117
> > > > >  [inlined code] from multimedia.jl:151
> > > > >  in display at multimedia.jl:162
> > > > >  in print_response at REPL.jl:134
> > > > >  in print_response at REPL.jl:121
> > > > >  in anonymous at REPL.jl:624
> > > > >  in run_interface at ./LineEdit.jl:1610
> > > > >  in run_frontend at ./REPL.jl:863
> > > > >  in run_repl at ./REPL.jl:167
> > > > >  in _start at ./client.jl:420
> > > > > 
> > > > > julia>
> > > > > 
> > > > > Performance is important, but it's not clear what I should use in
> > 
> > place
> > 
> > > > of
> > > > 
> > > > > abstract types; I've tried replacing AbstractArray with just Array
> > 
> > but
> > 
> > > > that
> > > > 
> > > > > does not appear to work.
> > > > > 
> > > > > On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote:
> > > > > > This fixes two problems:
> > > > > > 
> > > > > > type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > > > > > 
> > > > > > var::A
> > > > > > 
> > > > > > end
> > > > > > 
> > > > > > MyType{T,N}(var::AbstractArray{T,N}) =
> > > > > > MyType{T,N,typeof(var)}(var)
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > If performance matters, you should not use abstract types for
> > 
> > fields.
> > 
> > > > See
> > 
> > http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguo
> > 
> > > > > > us-fields-in-types-interact-with-the-compiler and the section
> > > > > > after
> > > > 
> > > > that.
> > > > 
> > > > > > --Tim
> > > > > > 
> > > > > > 

Re: [julia-users] how to make my own 3d array type

2015-11-25 Thread Tim Holy
Check out the "Types" section of the manual, esp. the part about "Composite 
Types."

Also, see base/subarray.jl if you want an example of a complex but full-
featured AbstractArray type.

Best,
--Tim

On Wednesday, November 25, 2015 03:24:39 AM Evan wrote:
> Thanks Tim,
> 
> Following the link you suggested I made the following type which does what
> I want:
> 
> immutable MyType{T,N,A<:AbstractArray} <: AbstractArray{Float64,3}
> conf_lims :: A
> end
> MyType{T,N}(conf_lims::AbstractArray{T,N}) = MyType{T,N,typeof(conf_lims)}(
> conf_lims)
> Base.size(S::MyType) = (S.count,)
> Base.linearindexing(::Type{MyType}) = Base.LinearFast();
> 
> 
> What I haven't figured out is how to add more than just the one variable;
> for instance I'd like to have a "count" variable which would be an integer?
> 
> On Monday, November 16, 2015 at 3:45:43 PM UTC+1, Tim Holy wrote:
> > Totally different error (it's a missing size method). You also need to
> > read
> > this:
> > http://docs.julialang.org/en/stable/manual/interfaces/#abstract-arrays
> > 
> > --Tim
> > 
> > On Monday, November 16, 2015 06:08:15 AM Evan wrote:
> > > Thank you, Tim, I understand the motivation for the approach in the link
> > > you sent.
> > > 
> > > I'm still unable to create the type that I want. I actually want two
> > 
> > arrays
> > 
> > > in my type, one 2d and the other 3d.
> > > 
> > > First though, I'm still stuck on just the 2d when I try to implement
> > 
> > your
> > 
> > > suggestion:
> > > julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > > 
> > >var::A
> > >
> > >end
> > > 
> > > julia> MyType{T,N}(var::AbstractArray{T,N}) =
> > 
> > MyType{T,N,typeof(var)}(var)
> > 
> > > MyType{T,N,A<:AbstractArray{T,N}}
> > > 
> > > julia> aa = MyType(zeros(2,3))
> > > Error showing value of type MyType{Float64,2,Array{Float64,2}}:
> > > ERROR: MethodError: `size` has no method matching
> > 
> > size(::MyType{Float64,2,
> > 
> > > Array{Float64,2}})
> > > 
> > > Closest candidates are:
> > >   size{T,n}(::AbstractArray{T,n}, ::Any)
> > >   size(::Any, ::Integer, ::Integer, ::Integer...)
> > >  
> > >  in showarray at show.jl:1231
> > >  in anonymous at replutil.jl:29
> > >  in with_output_limit at ./show.jl:1271
> > >  in writemime at replutil.jl:28
> > >  in display at REPL.jl:114
> > >  in display at REPL.jl:117
> > >  [inlined code] from multimedia.jl:151
> > >  in display at multimedia.jl:162
> > >  in print_response at REPL.jl:134
> > >  in print_response at REPL.jl:121
> > >  in anonymous at REPL.jl:624
> > >  in run_interface at ./LineEdit.jl:1610
> > >  in run_frontend at ./REPL.jl:863
> > >  in run_repl at ./REPL.jl:167
> > >  in _start at ./client.jl:420
> > > 
> > > julia>
> > > 
> > > Performance is important, but it's not clear what I should use in place
> > 
> > of
> > 
> > > abstract types; I've tried replacing AbstractArray with just Array but
> > 
> > that
> > 
> > > does not appear to work.
> > > 
> > > On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote:
> > > > This fixes two problems:
> > > > 
> > > > type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > > > 
> > > > var::A
> > > > 
> > > > end
> > > > 
> > > > MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
> > > > 
> > > > 
> > > > 
> > > > If performance matters, you should not use abstract types for fields.
> > 
> > See
> > 
> > 
> > http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguo
> > 
> > > > us-fields-in-types-interact-with-the-compiler and the section after
> > 
> > that.
> > 
> > > > --Tim
> > > > 
> > > > On Monday, November 16, 2015 03:54:15 AM Evan wrote:
> > > > > For a 2d array the following works:
> > > > > 
> > > > > type mytype{T}
> > > > > 
> > > > > var :: AbstractMatrix{T}
> > > > > 
> > > > > end
> > > > > 
> > > > > julia> t = mytype(zeros(4, 3))
> > > > > 
> > > > > mytype{Float64}(4x3 Array{Float64,2}:
> > > > >  0.0  0.0  0.0
> > > > >  0.0  0.0  0.0
> > > > >  0.0  0.0  0.0
> > > > >  0.0  0.0  0.0)
> > > > > 
> > > > > But how do I extend this to a 3d array?
> > > > > 
> > > > > julia> t = mytype(zeros(2, 4, 3))
> > > > > ERROR: MethodError: `convert` has no method matching
> > > > 
> > > > convert(::Type{mytype{T
> > > > 
> > > > > }}, ::Array{Float64,3})
> > > > > This may have arisen from a call to the constructor mytype{T}(...),
> > > > > since type constructors fall back to convert methods.
> > > > > 
> > > > > Closest candidates are:
> > > > >   call{T}(::Type{T}, ::Any)
> > > > >   convert{T}(::Type{T}, ::T)
> > > > >   mytype{T}(::AbstractArray{T,2})
> > > > >  
> > > > >  in call at essentials.jl:56



Re: [julia-users] how to make my own 3d array type

2015-11-25 Thread Evan
Thanks Tim,

Following the link you suggested I made the following type which does what 
I want:

immutable MyType{T,N,A<:AbstractArray} <: AbstractArray{Float64,3}
conf_lims :: A
end
MyType{T,N}(conf_lims::AbstractArray{T,N}) = MyType{T,N,typeof(conf_lims)}(
conf_lims)
Base.size(S::MyType) = (S.count,)
Base.linearindexing(::Type{MyType}) = Base.LinearFast();


What I haven't figured out is how to add more than just the one variable; 
for instance I'd like to have a "count" variable which would be an integer?




On Monday, November 16, 2015 at 3:45:43 PM UTC+1, Tim Holy wrote:
>
> Totally different error (it's a missing size method). You also need to 
> read 
> this: 
> http://docs.julialang.org/en/stable/manual/interfaces/#abstract-arrays 
>
> --Tim 
>
> On Monday, November 16, 2015 06:08:15 AM Evan wrote: 
> > Thank you, Tim, I understand the motivation for the approach in the link 
> > you sent. 
> > 
> > I'm still unable to create the type that I want. I actually want two 
> arrays 
> > in my type, one 2d and the other 3d. 
> > 
> > First though, I'm still stuck on just the 2d when I try to implement 
> your 
> > suggestion: 
> > julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N} 
> >var::A 
> >end 
> > 
> > julia> MyType{T,N}(var::AbstractArray{T,N}) = 
> MyType{T,N,typeof(var)}(var) 
> > MyType{T,N,A<:AbstractArray{T,N}} 
> > 
> > julia> aa = MyType(zeros(2,3)) 
> > Error showing value of type MyType{Float64,2,Array{Float64,2}}: 
> > ERROR: MethodError: `size` has no method matching 
> size(::MyType{Float64,2, 
> > Array{Float64,2}}) 
> > Closest candidates are: 
> >   size{T,n}(::AbstractArray{T,n}, ::Any) 
> >   size(::Any, ::Integer, ::Integer, ::Integer...) 
> >  in showarray at show.jl:1231 
> >  in anonymous at replutil.jl:29 
> >  in with_output_limit at ./show.jl:1271 
> >  in writemime at replutil.jl:28 
> >  in display at REPL.jl:114 
> >  in display at REPL.jl:117 
> >  [inlined code] from multimedia.jl:151 
> >  in display at multimedia.jl:162 
> >  in print_response at REPL.jl:134 
> >  in print_response at REPL.jl:121 
> >  in anonymous at REPL.jl:624 
> >  in run_interface at ./LineEdit.jl:1610 
> >  in run_frontend at ./REPL.jl:863 
> >  in run_repl at ./REPL.jl:167 
> >  in _start at ./client.jl:420 
> > 
> > julia> 
> > 
> > Performance is important, but it's not clear what I should use in place 
> of 
> > abstract types; I've tried replacing AbstractArray with just Array but 
> that 
> > does not appear to work. 
> > 
> > On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote: 
> > > This fixes two problems: 
> > > 
> > > type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N} 
> > > 
> > > var::A 
> > > 
> > > end 
> > > 
> > > MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var) 
> > > 
> > > 
> > > 
> > > If performance matters, you should not use abstract types for fields. 
> See 
> > > 
> > > 
> http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguo 
> > > us-fields-in-types-interact-with-the-compiler and the section after 
> that. 
> > > 
> > > --Tim 
> > > 
> > > On Monday, November 16, 2015 03:54:15 AM Evan wrote: 
> > > > For a 2d array the following works: 
> > > > 
> > > > type mytype{T} 
> > > > 
> > > > var :: AbstractMatrix{T} 
> > > > 
> > > > end 
> > > > 
> > > > julia> t = mytype(zeros(4, 3)) 
> > > > 
> > > > mytype{Float64}(4x3 Array{Float64,2}: 
> > > >  0.0  0.0  0.0 
> > > >  0.0  0.0  0.0 
> > > >  0.0  0.0  0.0 
> > > >  0.0  0.0  0.0) 
> > > > 
> > > > But how do I extend this to a 3d array? 
> > > > 
> > > > julia> t = mytype(zeros(2, 4, 3)) 
> > > > ERROR: MethodError: `convert` has no method matching 
> > > 
> > > convert(::Type{mytype{T 
> > > 
> > > > }}, ::Array{Float64,3}) 
> > > > This may have arisen from a call to the constructor mytype{T}(...), 
> > > > since type constructors fall back to convert methods. 
> > > > 
> > > > Closest candidates are: 
> > > >   call{T}(::Type{T}, ::Any) 
> > > >   convert{T}(::Type{T}, ::T) 
> > > >   mytype{T}(::AbstractArray{T,2}) 
> > > >   
> > > >  in call at essentials.jl:56 
>
>

Re: [julia-users] how to make my own 3d array type

2015-11-16 Thread Evan
Thank you, Tim, I understand the motivation for the approach in the link 
you sent.

I'm still unable to create the type that I want. I actually want two arrays 
in my type, one 2d and the other 3d.

First though, I'm still stuck on just the 2d when I try to implement your 
suggestion:
julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
   var::A
   end

julia> MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
MyType{T,N,A<:AbstractArray{T,N}}

julia> aa = MyType(zeros(2,3))
Error showing value of type MyType{Float64,2,Array{Float64,2}}:
ERROR: MethodError: `size` has no method matching size(::MyType{Float64,2,
Array{Float64,2}})
Closest candidates are:
  size{T,n}(::AbstractArray{T,n}, ::Any)
  size(::Any, ::Integer, ::Integer, ::Integer...)
 in showarray at show.jl:1231
 in anonymous at replutil.jl:29
 in with_output_limit at ./show.jl:1271
 in writemime at replutil.jl:28
 in display at REPL.jl:114
 in display at REPL.jl:117
 [inlined code] from multimedia.jl:151
 in display at multimedia.jl:162
 in print_response at REPL.jl:134
 in print_response at REPL.jl:121
 in anonymous at REPL.jl:624
 in run_interface at ./LineEdit.jl:1610
 in run_frontend at ./REPL.jl:863
 in run_repl at ./REPL.jl:167
 in _start at ./client.jl:420

julia>

Performance is important, but it's not clear what I should use in place of 
abstract types; I've tried replacing AbstractArray with just Array but that 
does not appear to work.




On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote:
>
> This fixes two problems: 
>
> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N} 
> var::A 
> end 
>
> MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var) 
>
>
>
> If performance matters, you should not use abstract types for fields. See 
>
> http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguous-fields-in-types-interact-with-the-compiler
>  
> and the section after that. 
>
> --Tim 
>
>
> On Monday, November 16, 2015 03:54:15 AM Evan wrote: 
> > For a 2d array the following works: 
> > 
> > type mytype{T} 
> > var :: AbstractMatrix{T} 
> > end 
> > 
> > julia> t = mytype(zeros(4, 3)) 
> > mytype{Float64}(4x3 Array{Float64,2}: 
> >  0.0  0.0  0.0 
> >  0.0  0.0  0.0 
> >  0.0  0.0  0.0 
> >  0.0  0.0  0.0) 
> > 
> > 
> > 
> > But how do I extend this to a 3d array? 
> > 
> > julia> t = mytype(zeros(2, 4, 3)) 
> > ERROR: MethodError: `convert` has no method matching 
> convert(::Type{mytype{T 
> > }}, ::Array{Float64,3}) 
> > This may have arisen from a call to the constructor mytype{T}(...), 
> > since type constructors fall back to convert methods. 
> > Closest candidates are: 
> >   call{T}(::Type{T}, ::Any) 
> >   convert{T}(::Type{T}, ::T) 
> >   mytype{T}(::AbstractArray{T,2}) 
> >  in call at essentials.jl:56 
>
>

Re: [julia-users] how to make my own 3d array type

2015-11-16 Thread Tim Holy
Totally different error (it's a missing size method). You also need to read 
this:
http://docs.julialang.org/en/stable/manual/interfaces/#abstract-arrays

--Tim

On Monday, November 16, 2015 06:08:15 AM Evan wrote:
> Thank you, Tim, I understand the motivation for the approach in the link
> you sent.
> 
> I'm still unable to create the type that I want. I actually want two arrays
> in my type, one 2d and the other 3d.
> 
> First though, I'm still stuck on just the 2d when I try to implement your
> suggestion:
> julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
>var::A
>end
> 
> julia> MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
> MyType{T,N,A<:AbstractArray{T,N}}
> 
> julia> aa = MyType(zeros(2,3))
> Error showing value of type MyType{Float64,2,Array{Float64,2}}:
> ERROR: MethodError: `size` has no method matching size(::MyType{Float64,2,
> Array{Float64,2}})
> Closest candidates are:
>   size{T,n}(::AbstractArray{T,n}, ::Any)
>   size(::Any, ::Integer, ::Integer, ::Integer...)
>  in showarray at show.jl:1231
>  in anonymous at replutil.jl:29
>  in with_output_limit at ./show.jl:1271
>  in writemime at replutil.jl:28
>  in display at REPL.jl:114
>  in display at REPL.jl:117
>  [inlined code] from multimedia.jl:151
>  in display at multimedia.jl:162
>  in print_response at REPL.jl:134
>  in print_response at REPL.jl:121
>  in anonymous at REPL.jl:624
>  in run_interface at ./LineEdit.jl:1610
>  in run_frontend at ./REPL.jl:863
>  in run_repl at ./REPL.jl:167
>  in _start at ./client.jl:420
> 
> julia>
> 
> Performance is important, but it's not clear what I should use in place of
> abstract types; I've tried replacing AbstractArray with just Array but that
> does not appear to work.
> 
> On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote:
> > This fixes two problems:
> > 
> > type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
> > 
> > var::A
> > 
> > end
> > 
> > MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
> > 
> > 
> > 
> > If performance matters, you should not use abstract types for fields. See
> > 
> > http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguo
> > us-fields-in-types-interact-with-the-compiler and the section after that.
> > 
> > --Tim
> > 
> > On Monday, November 16, 2015 03:54:15 AM Evan wrote:
> > > For a 2d array the following works:
> > > 
> > > type mytype{T}
> > > 
> > > var :: AbstractMatrix{T}
> > > 
> > > end
> > > 
> > > julia> t = mytype(zeros(4, 3))
> > > 
> > > mytype{Float64}(4x3 Array{Float64,2}:
> > >  0.0  0.0  0.0
> > >  0.0  0.0  0.0
> > >  0.0  0.0  0.0
> > >  0.0  0.0  0.0)
> > > 
> > > But how do I extend this to a 3d array?
> > > 
> > > julia> t = mytype(zeros(2, 4, 3))
> > > ERROR: MethodError: `convert` has no method matching
> > 
> > convert(::Type{mytype{T
> > 
> > > }}, ::Array{Float64,3})
> > > This may have arisen from a call to the constructor mytype{T}(...),
> > > since type constructors fall back to convert methods.
> > > 
> > > Closest candidates are:
> > >   call{T}(::Type{T}, ::Any)
> > >   convert{T}(::Type{T}, ::T)
> > >   mytype{T}(::AbstractArray{T,2})
> > >  
> > >  in call at essentials.jl:56



Re: [julia-users] how to make my own 3d array type

2015-11-16 Thread Tim Holy
This fixes two problems:

type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
var::A
end

MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)



If performance matters, you should not use abstract types for fields. See 
http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguous-fields-in-types-interact-with-the-compiler
 and the section after that.

--Tim


On Monday, November 16, 2015 03:54:15 AM Evan wrote:
> For a 2d array the following works:
> 
> type mytype{T}
> var :: AbstractMatrix{T}
> end
> 
> julia> t = mytype(zeros(4, 3))
> mytype{Float64}(4x3 Array{Float64,2}:
>  0.0  0.0  0.0
>  0.0  0.0  0.0
>  0.0  0.0  0.0
>  0.0  0.0  0.0)
> 
> 
> 
> But how do I extend this to a 3d array?
> 
> julia> t = mytype(zeros(2, 4, 3))
> ERROR: MethodError: `convert` has no method matching convert(::Type{mytype{T
> }}, ::Array{Float64,3})
> This may have arisen from a call to the constructor mytype{T}(...),
> since type constructors fall back to convert methods.
> Closest candidates are:
>   call{T}(::Type{T}, ::Any)
>   convert{T}(::Type{T}, ::T)
>   mytype{T}(::AbstractArray{T,2})
>  in call at essentials.jl:56



[julia-users] how to make my own 3d array type

2015-11-16 Thread Evan
For a 2d array the following works:

type mytype{T}
var :: AbstractMatrix{T}
end

julia> t = mytype(zeros(4, 3))
mytype{Float64}(4x3 Array{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0)



But how do I extend this to a 3d array?

julia> t = mytype(zeros(2, 4, 3))
ERROR: MethodError: `convert` has no method matching convert(::Type{mytype{T
}}, ::Array{Float64,3})
This may have arisen from a call to the constructor mytype{T}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  call{T}(::Type{T}, ::Any)
  convert{T}(::Type{T}, ::T)
  mytype{T}(::AbstractArray{T,2})
 in call at essentials.jl:56