I am defining a struct, and I would like to make it perform some trivial transformations on the input data to simplify later calculations.
The goal is for the struct to accept just a vector of qualities
and prices
, and then also be able to access the normalized prices (that is, prices adjusted so they some to one) and some other transformations.
Here is what I am trying to do:
struct Market
qualities::Array{<:AbstractFloat, 1}
prices::Array{<:AbstractFloat, 1}
normalized_prices = prices / sum(prices)
end
Then calling
m = Market([.2, .2, .2], [1., 2., 2.])
m.normalized_prices
should yield the vector [.2, .4, .4]
.
I would also be interested in normalizing the prices in-place, i.e. something like
struct Market
qualities::Array{<:AbstractFloat, 1}
prices::Array{<:AbstractFloat, 1}
prices /= sum(prices)
end
However, both of the struct definitions above yield an error. I know that one way to do the second task is to redefine the function Market(qualities, prices)
so that it does the normalization and then constructs the market, but is there a better way?
Separately, I would also like to assert that the input vectors are of the same length:
struct Market
qualities::Array{<:AbstractFloat, 1}
prices::Array{<:AbstractFloat, 1}
@assert size(qualities) == size(prices)
end
How should I do this?