"I know that you can use an outer constructor so that the type of the
arguments determines the type parameter used. But it isn't clear how to do
that if the arguments give only a subset of the type parameters, and the
rest need to be given explicitly. I was wondering if that is maybe just
not possible."
I did not follow this question, which may mean you are asking something
that is on your mind and not yet clear from your example.
What part of your example relates only a subset of the type parameters?
On Wednesday, August 26, 2015 at 12:46:07 AM UTC-4, Jeffrey Sarnoff wrote:
>
> I assumed the content of the vector were the angles, and that you wanted
> to abstract the sequence of application of those angles -- your 321.
> If you do not want to fix the type (you should have a good reason, Julia
> is more at ease when the type is not Any) -- just remove 'Float64'
>
> type EulerRotation{I,J,K}
> angles::Vector{Any}
> end
>
> typealias EulerRot321 EulerRotation{3,2,1}
> typealias EulerRot123 EulerRotation{1,2,3}
>
> function getEulerAngles(x::EulerRotation)
> p = typeof(x).parameters
> [x.angles[i] for i in p]
> end
>
> testEulerRot321 = EulerRot321([10,20,30])
> testEulerRot123 = EulerRot123([10//7,20//7,30//7])
>
> julia> getEulerAngles(testEulerRot321)
> 3-element Array{Any,1}:
> 30
> 20
> 10
>
> julia> getEulerAngles(testEulerRot123)
> 3-element Array{Any,1}:
> 10//7
> 20//7
> 30//7
>
> if your vectors are always uniformly typed, you can do this to avoid
> passing around Vector{Any}
>
> function getEulerAngles(x::EulerRotation)
> p = typeof(x).parameters
> t = typeof(x.angles[1])
> (t)[x.angles[i] for i in p]
> end
>
> julia> getEulerAngles(testEulerRot321)
> 3-element Array{Int64,1}:
> 30
> 20
> 10
>
> If you are trying to accomplish something else, let me know using
> different words.
>
> On Wednesday, August 26, 2015 at 12:26:44 AM UTC-4, [email protected]
> wrote:
>>
>> I think the part that is making things difficult for me is that I'm not
>> assuming Float64. I could do it by calling the inner constructor, like you
>> do in your example, but I think I'd have to explicitly give the element
>> type:
>>
>> EulerAngles{Float64, 321}([10.0, 20.0, 30.0])
>>
>> I know that you can use an outer constructor so that the type of the
>> arguments determines the type parameter used. But it isn't clear how to do
>> that if the arguments give only a subset of the type parameters, and the
>> rest need to be given explicitly. I was wondering if that is maybe just
>> not possible.
>>
>> In my original post, my [1.0, 2.0, 3.0] array is the Euler angles
>> themselves. I can see how that could be confused for the Euler sequence,
>> so here I used [10.0, 20.0, 30.0] instead.
>>
>> On Tuesday, August 25, 2015 at 11:11:34 PM UTC-5, Jeffrey Sarnoff wrote:
>>>
>>> Are you looking to do this?
>>>
>>> julia> type EulerRotation{I,J,K}
>>> angles::Vector{Float64}
>>> end
>>>
>>> julia> function getEulerAngleSeq(x::EulerRotation)
>>> p = typeof(x).parameters
>>> Float64[x.angles[i] for i in p]
>>> end
>>> getEulerAngleSeq (generic function with 1 method)
>>>
>>> julia> testRotSeq321 = EulerRotation{3,2,1}([1.0,2.0,3.0])
>>> EulerRotation{3,2,1}([1.0,2.0,3.0])
>>>
>>> julia> getEulerAngleSeq(testRotSeq321)
>>> 3-element Array{Float64,1}:
>>> 3.0
>>> 2.0
>>> 1.0
>>>
>>> julia> testRotSeq132 = EulerRotation{1,3,2}([1.0,2.0,3.0])
>>> EulerRotation{1,3,2}([1.0,2.0,3.0])
>>>
>>> julia> getEulerAngleSeq(testRotSeq132)
>>> 3-element Array{Float64,1}:
>>> 1.0
>>> 3.0
>>> 2.0
>>>
>>>
>>>
>>> On Tuesday, August 25, 2015 at 11:29:08 PM UTC-4, [email protected]
>>> wrote:
>>>>
>>>> Hi, everyone.
>>>>
>>>> I've been tinkering with creating a package for dealing with Euler
>>>> angles, using a very recent build of v0.4. Rather than just rehash
>>>> Matlab's functions, I wanted to take advantage of the type system, to
>>>> learn
>>>> more about parametric types and constructors.
>>>>
>>>> I definitely want the Euler rotation sequence and the angles to be
>>>> bound together. One straightforward way to do this would of course be:
>>>>
>>>> type EulerAngles{T <: Number}
>>>> seq::Int
>>>> angles::Vector{T}
>>>> end
>>>>
>>>> Which would work fine, but I'd end up with a huge if block for every
>>>> valid sequence. So I thought I'd try moving the sequence into the type
>>>> definition. I think I would declare the type like this:
>>>>
>>>> type EulerAngles{T <: Number, s}
>>>> angles::Vector{T}
>>>> end
>>>>
>>>> But then I can't figure out the best way to provide constructors for
>>>> this type. What I want is something like this:
>>>>
>>>> EulerAngles{321}([1.0, 2.0, 3.0])
>>>>
>>>> But I can't figure out how to get this to work. Is this kind of
>>>> constructor even possible with the type I showed above?
>>>>
>>>> I was able to get something very similar to work, by making functions
>>>> that look like constructors with the sequence in their names, like this:
>>>>
>>>> EulerAngles321([1.0, 2.0, 3.0])
>>>>
>>>> Which works, but gives the impression that EulerAngles321 is actually a
>>>> type. But it seems that typealiases can't have constructors, and I get an
>>>> error trying to define a typealias and a function with the same name.
>>>> Even
>>>> with this limitation, though, this seems like an OK solution.
>>>>
>>>> If I really wanted to have types with those names, I could have an
>>>> abstract EulerAngles type, and then have concrete types like
>>>> EulerAngles321. But that seems like overkill (in terms of the number of
>>>> types I'd create). Though it would work.
>>>>
>>>> Does anyone have any guidance on how to best approach this?
>>>>
>>>> Thanks for the help!
>>>>
>>>> Daniel
>>>>
>>>