Skip to content

Take part of a matrix by range (#443) - #683

Merged
Happypig375 merged 1 commit into
ASC-Community:masterfrom
Rafael-SOWNet:feat/matrix-ranges
Aug 4, 2026
Merged

Happypig375 merged 1 commit into
ASC-Community:masterfrom
Rafael-SOWNet:feat/matrix-ranges

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Closes #443.

var a = MathS.Matrices.Matrix(3, 3,
    1, 2, 3,
    4, 5, 6,
    7, 8, 9);

a[1.., ..]     // 4 5 6 / 7 8 9
a[.., ..2]     // 1 2 / 4 5 / 7 8
a[1..3, 1..3]  // 5 6 / 8 9
a[^1.., ^1..]  // 9

The result is an ordinary matrix sharing nothing with the one it came from, so it transposes, multiplies and compares like any other.

Both extents have to be written out

@Happypig375 asked on the issue for a[1.., ..] rather than a[1..], and that is what this does — there is no one-range indexer, so the shorter form does not compile.

The reason it would be ambiguous is already in the type: this[int i] gives the i-th row of a matrix but the i-th element of a vector, since a vector is a matrix of one column. A single range would have to inherit that and mean one thing for one shape and another for the other. With both extents written out, a vector is cut by its first extent like anything else and Column[1.., ..] drops its first element.

Edges

An empty range is refused with InvalidMatrixOperationException rather than answered with a matrix of no rows, because there is no such matrix. A range reaching past the matrix throws ArgumentOutOfRangeException from Range.GetOffsetAndLength itself, so the message names the real problem.

System.Range compiles on both net7.0 and netstandard2.0 here, so the API is the same on both — I checked before writing it rather than assuming.

Verification

Ten tests covering each form above, the whole-matrix case, a vector, symbolic entries carried over untouched, and both refusals. 3890 unit tests and 127 F# tests on both target frameworks, none failing.

These tests do not fail before the change — they do not compile, since the indexer they call does not exist. That is the only "before" a new API has.

a[1.., ..] is every row of a matrix but the first, a[..2, 1..3] the first two
rows of its second and third columns, and a[^1.., ^1..] its bottom right entry.
The result is an ordinary matrix sharing nothing with the one it came from.

Both extents have to be written out, which is what the issue's own discussion
asks for. A matrix indexed by one number gives a row while a vector indexed by
one number gives an element, so a single range would have to mean one thing here
and the other there; there is no one-range indexer to write, so the question
cannot come up.

An empty range is refused rather than answered with a matrix of no rows, since
there is no such matrix; a range reaching past the matrix throws from the range
itself. Ten tests, 3890 in all, 127 F#.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.14%. Comparing base (90c00a8) to head (302c48c).
⚠️ Report is 53 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #683      +/-   ##
==========================================
- Coverage   80.99%   80.14%   -0.85%     
==========================================
  Files         155      156       +1     
  Lines       13687    12648    -1039     
  Branches     1957     2064     +107     
==========================================
- Hits        11086    10137     -949     
+ Misses       1990     1914      -76     
+ Partials      611      597      -14     

☔ View full report in Codecov by Harness.
📢 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.

@Happypig375
Happypig375 merged commit 35477fd into ASC-Community:master Aug 4, 2026
24 checks passed
@Rafael-SOWNet
Rafael-SOWNet deleted the feat/matrix-ranges branch August 4, 2026 20:48
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.

Range support for matrices

3 participants