Forum Discussion

FICO's avatar
FICO
Community Manager
3 years ago

Unexpected behavior when using xpress.sum over numpy array slice of xpress variables

I have an application using a 4-D numpy array of xpress variables of which I only want to sum two dimensions leaving a 2-D numpy array. This gives a strange behavior (incorrect) for the first term of each sum of the resulting matrix. Please see here with this simple example summing a 3-D array over only one dimension leaving a 2-D array:

import xpress as xp
import numpy as np
x = xp.vars(5, 5, 5, name = 'x')
sum = np.array([xp.Sum([x[:, :, j] for j in range(5)])], dtype=xp.npexpr)
sum = np.squeeze(sum)
print(sum)
[[[  x(0,0,0) + x(0,0,1) + x(0,0,2) + x(0,0,3) + x(0,0,4)
     x(0,0,1) + x(0,1,1) + x(0,1,2) + x(0,1,3) + x(0,1,4)
     x(0,0,2) + x(0,2,1) + x(0,2,2) + x(0,2,3) + x(0,2,4)
     x(0,0,3) + x(0,3,1) + x(0,3,2) + x(0,3,3) + x(0,3,4)
     x(0,0,4) + x(0,4,1) + x(0,4,2) + x(0,4,3) + x(0,4,4)]
  [  x(0,1,0) + x(1,0,1) + x(1,0,2) + x(1,0,3) + x(1,0,4)
     x(0,1,1) + x(1,1,1) + x(1,1,2) + x(1,1,3) + x(1,1,4)
     x(0,1,2) + x(1,2,1) + x(1,2,2) + x(1,2,3) + x(1,2,4)
     x(0,1,3) + x(1,3,1) + x(1,3,2) + x(1,3,3) + x(1,3,4)
     x(0,1,4) + x(1,4,1) + x(1,4,2) + x(1,4,3) + x(1,4,4)]
  [  x(0,2,0) + x(2,0,1) + x(2,0,2) + x(2,0,3) + x(2,0,4)
     x(0,2,1) + x(2,1,1) + x(2,1,2) + x(2,1,3) + x(2,1,4)
     x(0,2,2) + x(2,2,1) + x(2,2,2) + x(2,2,3) + x(2,2,4)
     x(0,2,3) + x(2,3,1) + x(2,3,2) + x(2,3,3) + x(2,3,4)
     x(0,2,4) + x(2,4,1) + x(2,4,2) + x(2,4,3) + x(2,4,4)]
  [  x(0,3,0) + x(3,0,1) + x(3,0,2) + x(3,0,3) + x(3,0,4)
     x(0,3,1) + x(3,1,1) + x(3,1,2) + x(3,1,3) + x(3,1,4)
     x(0,3,2) + x(3,2,1) + x(3,2,2) + x(3,2,3) + x(3,2,4)
     x(0,3,3) + x(3,3,1) + x(3,3,2) + x(3,3,3) + x(3,3,4)
     x(0,3,4) + x(3,4,1) + x(3,4,2) + x(3,4,3) + x(3,4,4)]
  [  x(0,4,0) + x(4,0,1) + x(4,0,2) + x(4,0,3) + x(4,0,4)
     x(0,4,1) + x(4,1,1) + x(4,1,2) + x(4,1,3) + x(4,1,4)
     x(0,4,2) + x(4,2,1) + x(4,2,2) + x(4,2,3) + x(4,2,4)
     x(0,4,3) + x(4,3,1) + x(4,3,2) + x(4,3,3) + x(4,3,4)
     x(0,4,4) + x(4,4,1) + x(4,4,2) + x(4,4,3) + x(4,4,4)]]]

You an see that the first term in each sum is not correct (only the first one). The progression of first terms should be x(0,0,0), x(0,1,0), x(0,2,0), .... You can see that certain x variables are repeated x(0,0,1), x(0,0,2), x(0,0,3) when know x variables should be repeated at all. Moreover, certain x variables never appear, e.g., x(1,0,0), x(2,0,0), x(1,1,0, x(1,2,0), etc.

Thank you for your help on this issue.

3 Replies

  • FICO's avatar
    FICO
    Community Manager

    Hi Thomas,

    Which versions of NumPy and the Xpress Python interface are you using? I can't reproduce this using NumPy 1.19.0 and the latest Xpress version, 9.0.3:

    >>> import numpy as np
    >>> x = xp.vars(5, 5, 5, name = 'x')
    >>> sum = np.array([xp.Sum([x[:, :, j] for j in range(5)])], dtype=xp.npexpr)
    >>> sum = np.squeeze(sum)
    >>> print(sum)
    [[  x(0,0,0) + x(0,0,1) + x(0,0,2) + x(0,0,3) + x(0,0,4)
        x(0,1,0) + x(0,1,1) + x(0,1,2) + x(0,1,3) + x(0,1,4)
        x(0,2,0) + x(0,2,1) + x(0,2,2) + x(0,2,3) + x(0,2,4)
        x(0,3,0) + x(0,3,1) + x(0,3,2) + x(0,3,3) + x(0,3,4)
        x(0,4,0) + x(0,4,1) + x(0,4,2) + x(0,4,3) + x(0,4,4)]
     [  x(1,0,0) + x(1,0,1) + x(1,0,2) + x(1,0,3) + x(1,0,4)
        x(1,1,0) + x(1,1,1) + x(1,1,2) + x(1,1,3) + x(1,1,4)
        x(1,2,0) + x(1,2,1) + x(1,2,2) + x(1,2,3) + x(1,2,4)
        x(1,3,0) + x(1,3,1) + x(1,3,2) + x(1,3,3) + x(1,3,4)
        x(1,4,0) + x(1,4,1) + x(1,4,2) + x(1,4,3) + x(1,4,4)]
     [  x(2,0,0) + x(2,0,1) + x(2,0,2) + x(2,0,3) + x(2,0,4)
        x(2,1,0) + x(2,1,1) + x(2,1,2) + x(2,1,3) + x(2,1,4)
        x(2,2,0) + x(2,2,1) + x(2,2,2) + x(2,2,3) + x(2,2,4)
        x(2,3,0) + x(2,3,1) + x(2,3,2) + x(2,3,3) + x(2,3,4)
        x(2,4,0) + x(2,4,1) + x(2,4,2) + x(2,4,3) + x(2,4,4)]
     [  x(3,0,0) + x(3,0,1) + x(3,0,2) + x(3,0,3) + x(3,0,4)
        x(3,1,0) + x(3,1,1) + x(3,1,2) + x(3,1,3) + x(3,1,4)
        x(3,2,0) + x(3,2,1) + x(3,2,2) + x(3,2,3) + x(3,2,4)
        x(3,3,0) + x(3,3,1) + x(3,3,2) + x(3,3,3) + x(3,3,4)
        x(3,4,0) + x(3,4,1) + x(3,4,2) + x(3,4,3) + x(3,4,4)]
     [  x(4,0,0) + x(4,0,1) + x(4,0,2) + x(4,0,3) + x(4,0,4)
        x(4,1,0) + x(4,1,1) + x(4,1,2) + x(4,1,3) + x(4,1,4)
        x(4,2,0) + x(4,2,1) + x(4,2,2) + x(4,2,3) + x(4,2,4)
        x(4,3,0) + x(4,3,1) + x(4,3,2) + x(4,3,3) + x(4,3,4)
        x(4,4,0) + x(4,4,1) + x(4,4,2) + x(4,4,3) + x(4,4,4)]]
    >>> np.version.full_version
    '1.19.0'
    >>> xp.__version__
    '9.0.3'
  • FICO's avatar
    FICO
    Community Manager

    Hi Chris,

    Thank you for your rapid reply. I was using Xpress 9.02 and NumPy 1.24. I upgraded Xpress to 9.0.4. I was having issues with downgrading Numpy to 1.19, but able to downgrade to 1.20, and am now getting the correct results. Please be aware of this issue for later versions of Numpy.

    Thanks!

  • FICO's avatar
    FICO
    Community Manager

    I will look into this issue with NumPy 1.24, thanks for raising it.