Skip to content

Conversation

@Antoinemarteau
Copy link
Contributor

@Antoinemarteau Antoinemarteau commented Sep 12, 2025

I looked at MultiValue indexing in detail to address #1160.

  • support for indexing with Colon()/:, end, ::StaticArray, UnitRanges, ::SOneTo, and ::Array
  • support for keys
  • fixed lastindex(a,d), eachindex(::IndexStyle, a)
  • changed eachindex and CartesianIndices to return StaticArrays.SOneTo instead of Base.OneTo
  • consistency with StaticArrays: the return value is statically typed (a scalar or MultiValue) iff all indices are statically inferable, otherwise an Array is returned.

E.g:

julia> t = TensorValue(1:9...)
TensorValue{3, 3, Int64, 9}(1, 2, 3, 4, 5, 6, 7, 8, 9)

julia> t[:,2]
VectorValue{3, Int64}(4, 5, 6)

julia> t[1:end-1,begin+1:end]
2×2 Matrix{Int64}:
 4  7
 5  8

julia> t[SOneTo(2),SOneTo(3)]
TensorValue{2, 3, Int64, 6}(1, 2, 4, 5, 7, 8)

julia> t[Base.OneTo(2),Base.OneTo(3)]
2×3 Matrix{Int64}:
 1  4  7
 2  5  8

julia> t[MVector(1,2),CartesianIndex(3)]
VectorValue{2, Int64}(7, 8)

julia> t[1:2,CartesianIndex(3)]
2-element Vector{Int64}:
 7
 8

julia> t[ isodd.(SMatrix{3,3}(1:9...))]
5-element Vector{Int64}:
 1
 3
 5
 7
 9

julia> mask = [true false false; false true false; false false true]
3×3 Matrix{Bool}:
 1  0  0
 0  1  0
 0  0  1

julia> t[mask]
3-element Vector{Int64}:
 1
 5
 9

Also, I added new constructors for MultiValue (sub)types taking SArray/MArrays and SArray/MArray's and Array's constructors taking MultiValue objects that use the existing associated convert methods. The advantage is you don't need to specify the exact size of the returned value, it's inferred from the argument

MultiValue( SMatrix{2,2}(1:4...) )  # TensorValue{2,2,Int,4}(1, 2, 3, 4)
SArray( VectorValue(1:2) ) # SVector{2,Int}(1, 2)
Array( VectorValue(1:2) ) # Int[1, 2]

but you can also specify struct arguments if you want to be sure of what you get or convert components

MVector{2,Float64}( VectorValue(1:2) ) # MVector{2,Float64}(1.0, 2.0).
Vector{Float64}( VectorValue(1:2) ) # Float64[1.0, 2.0] 

- support for indexing with `Colon()`/`:`, `end`, `::StaticArray`,
  `SOneTo`, and `::Array`
- support for `axes` and `keys`
- fixed `lastindex(a,d)`, `eachindex(::IndexStyle, a)`
- changed `eachindex` and `CartesianIndices` to return `StaticArrays.SOneTo` instead
  of `Base.OneTo`
- consistency with StaticArrays: the return value is statically typed (a
  scalar or `MultiValue`) iff all indices are statically inferable, otherwise
  an `Array` is returned.

eachindex could still be optimized for tensor types that store all
its physical components (no dependant or / statically known components)
by switchin to `IndexLinear()` style.
@Antoinemarteau Antoinemarteau marked this pull request as ready for review September 16, 2025 06:03
@codecov
Copy link

codecov bot commented Sep 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.30%. Comparing base (34681f0) to head (97a2f72).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1162      +/-   ##
==========================================
+ Coverage   89.27%   89.30%   +0.02%     
==========================================
  Files         211      211              
  Lines       26692    26710      +18     
==========================================
+ Hits        23829    23853      +24     
+ Misses       2863     2857       -6     
Flag Coverage Δ
drivers 0.00% <0.00%> (ø)
tests 89.30% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Antoinemarteau
Copy link
Contributor Author

This is merge-able, closes #1160.

@JordiManyer JordiManyer merged commit 5fa00e6 into gridap:master Sep 19, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants