Skip to content

Conversation

Zeroto521
Copy link
Contributor

  1. Feature: Speed up MatrixVariable.sum(axis=None) via quicksum MatrixVariable.sum is slower than quicksum #1070.
  2. Changed:
    • Now returning a scaler if axis=None.
    • Before returning a scaler if the size of the result is 1.

Added a test to verify that summing a matrix variable with axis=0 returns a MatrixExpr with the expected shape. This improves coverage for matrix sum operations with axis specified.
Introduced a new test to compare performance of matrix sum versus element-wise sum. Refactored imports for clarity and consistency. Renamed performance test for better description.
Renamed the Model instance from 'm' to 'model' to avoid confusion with the integer variable 'm' and improve code clarity in the test_sum_performance function.
Replaces incorrect usage of 'm' with 'model' in the assertion within test_sum_performance to ensure the correct object is referenced.
Updated the assertion in test_matrix_sum_argument to expect shape (1,) instead of (1, 1) when summing along axis 0. This aligns the test with the actual output of the sum operation.
@Joao-Dionisio
Copy link
Member

Do you mind extending the performance tests for matrix variables and reporting the speed improvement this PR brings please?

And thank you very much, as always!

@Zeroto521
Copy link
Contributor Author

In my local environment, this change is 50x faster than before for the (180, 180) shape of the array to sum up.

@Zeroto521
Copy link
Contributor Author

Do you mind extending the performance tests for matrix variables

Sorry, I didn't get it. ‎tests/test_matrix_variable.py::test_sum_performance is done for that.

Modified the assertion in test_sum_performance to compare orig_time + 1 with matrix_time instead of orig_time. This may address timing precision or test flakiness.
@Joao-Dionisio
Copy link
Member

Oh sorry @Zeroto521, I hadn't looked at the changes yet, thank you!

Can you think of other things that people using matrix variables would want to be sped up? Things like adding big matrix variables, etc. If so, would you mind adding such performance tests? That's what I meant, if you think it's a reasonable request.

Great work with all your PRs, by the way! 💪

Copy link
Member

@Joao-Dionisio Joao-Dionisio left a comment

Choose a reason for hiding this comment

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

I don0t know how to feel about having a test that may stochastically break, but it shouldn't be a big deal. if a problem appears down the line, we just make it optional. Thank you!

@Joao-Dionisio Joao-Dionisio merged commit a6dd8ab into scipopt:master Oct 20, 2025
1 check passed
@Joao-Dionisio
Copy link
Member

Yeah, it seems like the tests are not passing, I'm gonna skip the test. Are you sure this is faster?

@Zeroto521
Copy link
Contributor Author

Yeah, it seems like the tests are not passing, I'm gonna skip the test. Are you sure this is faster?

My bad, I show use np.ndarray.sum instead of np.sum.
np.sum is still using the subclass sum method.

import numpy as np


def quicksum(a: np.ndarray):
    res = 0
    for i in a.flat:
        res += i
    return res


class Matrix(np.ndarray):
    def sum(self, *args, **kwargs):
        if kwargs.get("axis") is None:   
            print("sum via quicksum")
            return quicksum(self)
        else:
            print("sum via np.sum")
            return super().sum(*args, **kwargs)

m = Matrix((2, 2))
m.flat = [[1, 1], [1, 1]]
print(m.sum())
print("----")
print(np.ndarray.sum(m))
print("-----")
print(np.sum(m))

Output:

sum via quicksum
4.0
----
4.0
-----
sum via quicksum
4.0

@Zeroto521 Zeroto521 deleted the speed-up/matrix.sum branch October 21, 2025 07:37
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