On Tuesday, December 31, 2013 7:13:10 AM UTC-5, Christian Groll wrote:
>
> 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?
>
You could simply tell users not to access simplex.points directly.
Instead, override getindex/setindex! so that you can do simplex[i] for
simplex <: nDimSimplex. Your setindex! method could enforce the invariants
(e.g. dividing by sum(x) after every change to renormalize, and throwing an
error for negative entries.)
(I would also make nDimSimplex a subtype of AbstractVector{Float64}. For
AbstractVector subtypes, you should normally provide methods for at least:
size, getindex, setindex!, and similar.)