Skip to content

Fix CsMat::set - #411

Merged
mulimoen merged 4 commits into
masterfrom
issue_410
Oct 13, 2026
Merged

mulimoen merged 4 commits into
masterfrom
issue_410

Conversation

@mulimoen

Copy link
Copy Markdown
Collaborator

Simplified to use NnzIndex directly without going through a view

@rodrigodev Do you have more tests which should be added to this PR?

@mulimoen mulimoen linked an issue Oct 12, 2026 that may be closed by this pull request
@rodrigodev

Copy link
Copy Markdown

We hit this on 0.11.5, aarch64-apple-darwin, Rust 1.98. Your test covers the exact case from my report, and I checked it: it fails on master and passes on your branch. Nothing more is needed for #410.

For reference, the reproducer as a test:

#[test]
fn issue_410() {
    use crate::TriMat;
    let mut t = TriMat::new((3, 3));
    for (r, c, v) in [
        (0, 0, 1.0),
        (0, 1, 4.0),
        (1, 1, 2.0),
        (1, 2, 5.0),
        (2, 2, 3.0),
    ] {
        t.add_triplet(r, c, v);
    }
    let mut m: CsMat<f64> = t.to_csc();

    m.set(1, 1, 20.0);
    assert_eq!(Some(20.0), m.get(1, 1).copied());
    assert_eq!(Some(4.0), m.get(0, 1).copied());
}

Before and after on the same machine:

master (1525319)

before: (1,1) = Some(2.0), (0,1) = Some(4.0)
after:  (1,1) = Some(2.0), (0,1) = Some(20.0)

issue_410 (5d2332e)

before: (1,1) = Some(2.0), (0,1) = Some(4.0)
after:  (1,1) = Some(20.0), (0,1) = Some(4.0)

The one test I would add is the same thing in CSR, since the fix routes both storage orders through nnz_index and only CSC is covered. It also fails on master and passes on your branch:

#[test]
fn issue_410_csr() {
    use crate::TriMat;
    let mut t = TriMat::new((3, 3));
    for (r, c, v) in [
        (0, 0, 1.0),
        (0, 1, 4.0),
        (1, 1, 2.0),
        (1, 2, 5.0),
        (2, 2, 3.0),
    ] {
        t.add_triplet(r, c, v);
    }
    let mut m: CsMat<f64> = t.to_csr();

    m.set(1, 2, 50.0);
    assert_eq!(Some(&50.0), m.get(1, 2));
    assert_eq!(Some(&4.0), m.get(0, 1));
}

@mulimoen
mulimoen force-pushed the issue_410 branch 2 times, most recently from 4940f6b to 4436eb1 Compare October 13, 2026 06:24
@mulimoen
mulimoen merged commit 53acede into master Oct 13, 2026
37 checks passed
@mulimoen
mulimoen deleted the issue_410 branch October 13, 2026 06:36
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.

CsMat::set may be writing a different cell than the one asked for

2 participants