Skip to content

Fix sparse matrix subtraction - #4771

Open
diracdeltafunk wants to merge 3 commits into
Macaulay2:developmentfrom
aimathconference:codex/fix-smat-subtract
Open

diracdeltafunk wants to merge 3 commits into
Macaulay2:developmentfrom
aimathconference:codex/fix-smat-subtract

Conversation

@diracdeltafunk

@diracdeltafunk diracdeltafunk commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

vec_negate, which subtractInPlace uses to negate each column of the matrix being subtracted, looped while p->next != NULL instead of p != NULL. So it skipped the last stored entry of each column and crashed on an empty column. This broke subtraction of sparse mutable matrices:

k = ZZ/101;
A = mutableMatrix(matrix(k, {{7},{11}}), Dense => false);
B = mutableMatrix(matrix(k, {{2},{3}}), Dense => false);
A - B
A - mutableMatrix(k, 2, 1, Dense => false)

Before: A - B was {{9}, {8}}, and subtracting the zero matrix crashed M2. After: {{5}, {8}} and {{7}, {11}}.

Related issues and pull requests

Fixes #4707.

Testing

Built M2-unit-tests on macOS arm64. Added two engine tests: one for
all coefficients being subtracted and one for an empty column. Against
the original code, the first failed and the second exited with SIGSEGV.
With the fix, both pass via CTest (2/2).

Also checked the example above in M2 and ran all 277 engine unit tests. The second commit adds the new test to the autotools build's Makefile.files; I only built with CMake.

Release notes

Fix incorrect sparse matrix subtraction and a crash on empty columns.

AI disclosure

  • 4 — AI produced most or all of the content, which I reviewed thoroughly.

Tools and models used: OpenAI Codex (GPT-6).
How AI was used, and on which parts: Codex investigated the bug, wrote
the fix and tests, ran the checks, and drafted this description.
How I verified the result: I looked at the code

License

  • My changes use the existing license of the affected files or package.

Checklist

  • This pull request targets the development branch.
  • I have reviewed my own changes.
  • Documentation and tests are updated as needed.

Visit every node while negating a copied sparse column. The old loop
skipped the last coefficient and dereferenced null for an empty column.

Add engine tests for arithmetic and empty-column subtraction.

Fixes Macaulay2#4707.
@d-torrance
d-torrance self-requested a review October 31, 2026 13:19
@d-torrance d-torrance added Engine Macaulay2/e AI-generated This PR contains AI-generated code or this issue was written by AI labels Oct 31, 2026
@d-torrance
d-torrance requested a review from dimpase October 31, 2026 13:23
Comment thread M2/Macaulay2/e/CMakeLists.txt Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dimpase

dimpase commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

isn't NULL to be avoided in C++, and nullptr used instead ? https://en.cppreference.com/cpp/language/nullptr

@dimpase dimpase left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but the NULL question ought to be sorted

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@diracdeltafunk

Copy link
Copy Markdown
Contributor Author

Thanks, changed to nullptr -- I think you're right that this is what you're supposed to do in C++.

Claude identified some more NULLs that could be nullptrs in the codebase. I can ask Claude to hunt these down for another PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-generated This PR contains AI-generated code or this issue was written by AI Engine Macaulay2/e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SMat subtraction skips the last coefficient and crashes on empty columns

3 participants