Skip to content

add Array operator+ etc in C++ and Python, fix += in Python#1630

Merged
KrisThielemans merged 7 commits intoUCL:masterfrom
KrisThielemans:SWIG_array_operators
Oct 12, 2025
Merged

add Array operator+ etc in C++ and Python, fix += in Python#1630
KrisThielemans merged 7 commits intoUCL:masterfrom
KrisThielemans:SWIG_array_operators

Conversation

@KrisThielemans
Copy link
Copy Markdown
Collaborator

Summary: operator+, += etc now work as expected for Array and ProjDataInMemory for both C++ and Python

C++

  • Add Array::operator+ etc explicitly, returning an Array object, whereas before it would return a NumericVectorWithOffset object
  • more tests on numerical operations
  • avoid a few compiler warnings for gcc in test_Array.cxx

SWIG/Python

Fixes #863

Explicitly add operator+, operator+= in Array. This overrides the
base-class implementations with functions that have the correct
return type.

This avoids surprising problems in python as well.

In addition, we work around swig/swig#2634
by using %ignore all operator +=. This avoids a crash, but in fact
allows Python to fall back to operator+. This might be a bit slow,
but it does work.

Added testing both in C++ and Python.
Let SWIG ignore VectorWithOffset/Array constructors
that don't make sense to wrap, avoiding warnings.
Work around a SWIG bug by ignoring += (as that crashed).
Python now auto-generates += in terms of +. That might be slower,
but it works.
@KrisThielemans KrisThielemans added this to the v6.3 milestone Oct 9, 2025
@KrisThielemans KrisThielemans self-assigned this Oct 9, 2025
@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

This isn't quite ready yet, as we need the same for VoxelsOnCartesianGrid to be useful. However, I'm struggling with some compilation problems, that appear in the (somewhat crazy) functionality to grow the lhs if rhs is larger. Best might be to remove that, but that'd be better for the next major version.

- introduce type traits such as has_iterator<T>
- use those to write assign() much more generically, independent
STIR/std classes
As per previous changes for Array.

WARNING: breaks backward compatibility for
VoxelsOnCartesianGrid::operator-= and operator- when the
lhs has smaller size than the rhs.
Previously, behaviour was equivalent to first calling grow.
Now it calls error().
@KrisThielemans KrisThielemans marked this pull request as ready for review October 9, 2025 23:57
@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

@markus-jehl @danieldeidda @NikEfth this is now ready.

We can now do fun things in python like

a = b + c
b += 3

with arrays, images and ProjDataInMemory.

I'm hoping to merge this soon (if tests are ok!), so let me know if you want to check.

There's some fancy C++ with type traits going on to get the VoxelsOnCartesianGrid to compile (essentially because there are instantiations like VoxelsOnCartesianGrid<KineticParameters<1, float>>, so I had to brush up a bit.

VoxelsOnCartesianGrid::operator-= no longer grows if the rhs is larger. I couldn't get that to compile with all instantiations, including with elemT=unsigned, but I'd like to disable this surprising "auto-grow" anyway.

bin is a built-in object, so call our variable _bin
@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

Additional "release notes"

  • Same for VoxelsOnCartesianGrid
  • NumericVectorWithOffset (and hence Array/VoxelsOnCartesianGrid)::operator-= now calls error if the rhs has larger size than the lhs. (It used to be equivalent to growing the lhs (with zeroes) first).
    WARNING This breaks backwards compatibility (but only in a corner case).

Copy link
Copy Markdown
Collaborator

@casperdcl casperdcl left a comment

Choose a reason for hiding this comment

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

exc++ellent

@markus-jehl
Copy link
Copy Markdown
Contributor

Just tested it in python and it works very well - really nice feature!

Copy link
Copy Markdown
Collaborator

@danieldeidda danieldeidda left a comment

Choose a reason for hiding this comment

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

nice thanks! so should I update the recon demo with this then?

@KrisThielemans KrisThielemans merged commit 4210550 into UCL:master Oct 12, 2025
@KrisThielemans KrisThielemans deleted the SWIG_array_operators branch October 12, 2025 21:57
@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

nice thanks! so should I update the recon demo with this then?

yes please. Merge master on your branch first.

@danieldeidda
Copy link
Copy Markdown
Collaborator

What about multiplication?

@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

Should be available

danieldeidda added a commit to danieldeidda/STIR that referenced this pull request Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STIR python - volume addition issues

4 participants