ggebbie / UnitfulLinearAlgebra.jl

Low-cost linear algebra functions for matrices with units
MIT License
6 stars 3 forks source link

inconsistency with whether or not you have have two identical dims #58

Closed b-r-hamilton closed 1 year ago

b-r-hamilton commented 1 year ago

ULA.jl (at pull request 57) won't create a UnitfulDimMatrix if you declare two identical dimensions, returns the following error

julia> m2 = UnitfulDimMatrix(rand(5,5), fill(K, 5), fill(K, 5), dims = (X = 1:5, X = 1:5))
ERROR: syntax: field name "X" repeated in named tuple around REPL[4]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

But, you can create a matrix with two identical dimensions through multiplication

julia> using UnitfulLinearAlgebra, Unitful, DimensionalData
[ Info: Precompiling UnitfulLinearAlgebra [c14bd059-d406-4571-8f61-9bd20e53c30b]
julia> K = u"K"
K
julia> m1 = UnitfulDimMatrix(rand(5,5), fill(K, 5), fill(K, 5), dims = (X = 1:5, Y = 1:5));
julia> m3 = UnitfulDimMatrix(rand(5,5), fill(K, 5), fill(K, 5), dims = (Y = 1:5, X = 1:5));
julia> m1 * m3
5×5 UnitfulDimMatrix{Float64,2} with dimensions: 
  X Sampled{Int64} 1:5 ForwardOrdered Regular Points,
  X Sampled{Int64} 1:5 ForwardOrdered Regular Points

Is this the intended behavior?

b-r-hamilton commented 1 year ago

It works if you declare dimensions using what I think of as the "function syntax"

julia> m2 = UnitfulDimMatrix(rand(5,5), fill(K, 5), fill(K, 5), dims = (X(1:5), X(1:5)))

Is this the preferred syntax?

ggebbie commented 1 year ago

Statements like this return a NamedTuple in Julia.

julia> typeof((y = 1, x = 2))
NamedTuple{(:y, :x), Tuple{Int64, Int64}}

NamedTuples can't have duplicated names.

As for the intended behavior, I have been making Dimensions and then it is ok to repeat them.

julia> @dim Years "years"
julia> E = UnitfulDimMatrix(randn(5,5),fill(K,5),fill(K,5),dims=(Years(years),Years(years)))
5×5 UnitfulDimMatrix{Float64,2} with dimensions: 
  Years Sampled{Int64} 1:5 ForwardOrdered Regular Points,
  Years Sampled{Int64} 1:5 ForwardOrdered Regular Points
           1          2          3          4         5
 1  -1.18243   -1.15145  -0.983284   0.384822  0.991283
 ⋮                                                    ⋮
 4  -1.67197  -0.559185   0.061846  -0.556178  0.522961
 5   1.57964  -0.524349  -0.619997   0.988597  -2.17795
b-r-hamilton commented 1 year ago

Got it - happy to close this one then!