user1438310
user1438310

Reputation: 787

User defined type in Julia for "marking" purposes

In my code I need to keep track of two kinds of data items, namely the original data items and the transformed data items. Both original and transformed data items I store as Array{Float64,1}. However, I need to keep track if a data item is a transformed one or not as some functions in my code work with the original data items and some with the transformed ones.

In order to ensure correctness and avoid ever passing a transformed data item to a function that is supposed to work with the original data item, I was thinking that I could create a type called Transformed. This type could be used in the function declarations thus enforcing correctness.

Of course I looked in the documentation, but this did not help me enough. I guess what I need to do is something like:

primitive type Transformed :< Array{Float64,1} end

but of course this doesn't work (it is not even a primitive!)

Do I have to go for a struct? any suggestions? Cheers.

Upvotes: 2

Views: 459

Answers (2)

niczky12
niczky12

Reputation: 5063

In case you don't want to access your arrays with the .data field you might consider using StaticArrays. There might be some other already existing data structures, but this was the first I could think of.

You can declare these with SVector or with some macros. The benefit of this is that then you can check whether an array is a StaticArray or a regular one quite easily, but you can just use them as regular arrays.

julia> using StaticArrays

julia> original_array = SVector(1.5,2,3)
3-element StaticArrays.SArray{Tuple{3},Float64,1,3}:
 1.5
 2.0
 3.0

julia> transformed_array = Array{Float64,1}(original_array + 1.3)
3-element Array{Float64,1}:
 2.8
 3.3
 4.3

julia> # these will give you StaticArrays
       original_array + transformed_array
3-element StaticArrays.SArray{Tuple{3},Float64,1,3}:
 4.3
 5.3
 7.3

julia> transformed_array + original_array
3-element StaticArrays.SArray{Tuple{3},Float64,1,3}:
 4.3
 5.3
 7.3

julia> # matrix multiplication still works
       original_array' * transformed_array
23.699999999999996

julia> original_array * transformed_array'
3×3 Array{Float64,2}:
 4.2  4.95   6.45
 5.6  6.6    8.6
 8.4  9.9   12.9

julia> # same for reductions
       sum(transformed_array)
10.399999999999999

One obvious drawback is that you can't grow these StaticArrays. But I assumed if you want to keep your original values then this is not a problem anyways.

Also, as shown above, be careful when operating on them as you might get StaticArrays back. Then you will need to explicitly convert the results into Arrays.

Upvotes: 0

rdeits
rdeits

Reputation: 107

Yes, you likely want a struct or a mutable struct containing an Array{Float64, 1}. You can learn more about these in the Julia manual section on composite types (i.e. structs): https://docs.julialang.org/en/stable/manual/types/#Composite-Types-1

A simple example would be:

struct Transformed
  data::Array{Float64, 1}
end

Upvotes: 3

Related Questions