I think you’d need a family of types to do that. You might look at 
https://github.com/twadleigh/ImmutableArrays.jl and try to extend it.

 — John

On Dec 31, 2013, at 7:13 AM, Christian Groll <[email protected]> 
wrote:

> I already know how I could implement this for a given dimensionality. For 
> example, for the two-dimensional case I can define:
> 
> immutable twoDimSimplex
>     weight1::Float64
>     weight2::Float64    
>     
>     twoDimPortfolio(x::Float64, y::Float64) = (abs(x+y - 1) > 1e-10) ? 
> error("entries must sum to one") : new(x,y)
> end    
> 
> However, I do not get how I could extend this for the n-dimensional case. 
> Here, I thought that I would have to use one field which stores a 
> n-dimensional vector:
> 
> immutable nDimSimplex
>     points::Vector{Float64}
>     
>     nDimSimplex(x::Vector{Float64}) = (abs(sum(x) - 1) > 1e-10) ? 
> error("entries must sum to one") : new(x)
> end
> 
> Now, I think that it will not be possible to change the vector that the field 
> points to. However, the entries of the vector itself still can be changed 
> without restrictions. Any recommendations?
> 
> On Tuesday, 31 December 2013 12:33:57 UTC+1, Tim Holy wrote:
> Use an immutable 
> http://docs.julialang.org/en/release-0.2/manual/types/#immutable-composite- 
> types 
> with an inner constructor: 
> http://docs.julialang.org/en/release-0.2/manual/constructors/#inner- 
> constructor-methods 
> 
> You may find the example 
> http://docs.julialang.org/en/release-0.2/manual/constructors/#case-study- 
> rational 
> helpful. 
> 
> Best, 
> --Tim 
> 

Reply via email to