ggebbie / UnitfulLinearAlgebra.jl

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

Slicing for UnitfulDimMatrix #48

Open b-r-hamilton opened 1 year ago

b-r-hamilton commented 1 year ago

Can we incorporate the slicing methods of DimensionalData (At, Near, Interval) for UnitfulDimMatrix?

I'd like to do this so that when I calculate ETW-1E If W occurs over time indices, and E does not, I can iterate through every time index. Not sure if there's a better way to do this though!

b-r-hamilton commented 1 year ago

minimum working example

using UnitfulLinearAlgebra, Unitful, DimensionalData
m = u"m"; s = u"s"; N = 5; M = 3
d = collect(1:N)m
t = collect(1:M)s 
c = UnitfulDimMatrix(randn(N, M), fill(m, N), fill(unit(1.0), M), dims = (X = d, Ti = t))

#works, does not carry units
c[1:10] #column1 catted with column2 
c[At(3m), At(1s)]
c[Near(3.01m), Near(1.3s)]

#does not work
c[1m..2m, At(2s)]
c[X(1:2), Ti(1:2)]

The last two lines throw a MethodError: no method matching rebuild error. Also, it would be nice if the simple access methods would return units as well...

ggebbie commented 1 year ago

MWE reproduced, thanks

ggebbie commented 1 year ago

Slicing works.

getindex only returns parent value still.

refdims of vector output not displayed in REPL but it should be.

julia> using UnitfulLinearAlgebra, Unitful, DimensionalData

julia> m = u"m"; s = u"s"; N = 5; M = 3
3

julia> d = collect(1:N)m
5-element Vector{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}:
 1 m
 2 m
 3 m
 4 m
 5 m

julia> t = collect(1:M)s
3-element Vector{Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}}:
 1 s
 2 s
 3 s

julia> c = UnitfulDimMatrix(randn(N, M), fill(m, N), fill(unit(1.0), M), dims = (X = d, Ti = t))
5×3 UnitfulDimMatrix{Float64,2} with dimensions: 
  X Sampled{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}} Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}[1 m, 2 m, 3 m, 4 m, 5 m] ForwardOrdered Irregular Points,
  Ti Sampled{Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}} Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}[1 s, 2 s, 3 s] ForwardOrdered Irregular Points
              1 s          2 s           3 s
 1 m  -0.716549 m    1.59065 m    0.328252 m
   ⋮                            
 4 m    1.61691 m    0.63539 m  -0.0649536 m
 5 m   0.236709 m  -0.320499 m    0.146862 m

julia> #works, does not carry units
       c[1:10] #column1 catted with column2
10-element Vector{Float64}:
 -0.7165487924820857
 -0.5617936504901848
  0.25407103563607586
  1.6169145793075035
  0.23670937180183507
  1.5906472117655497
  1.4580104889700622
  0.8334385996333329
  0.6353900902411976
 -0.3204985043187276

julia> c[At(3m), At(1s)]
0.25407103563607586

julia> c[Near(3.01m), Near(1.3s)]
0.25407103563607586

julia> #does not work
       c[1m..2m, At(2s)]
2-element UnitfulDimMatrix{Float64,1} with dimensions: 
  X Sampled{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}} Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}[1 m, 2 m] ForwardOrdered Irregular Points
 1 m  1.59065 m
 2 m  1.45801 m

julia> c[X(1:2), Ti(1:2)]
2×2 UnitfulDimMatrix{Float64,2} with dimensions: 
  X Sampled{Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}} Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}[1 m, 2 m] ForwardOrdered Irregular Points,
  Ti Sampled{Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}} Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}[1 s, 2 s] ForwardOrdered Irregular Points
              1 s        2 s
 1 m  -0.716549 m  1.59065 m
 2 m  -0.561794 m  1.45801 m
ggebbie commented 1 year ago

partially handled in pull request #55