Forum Discussion
FICO
3 years agoCommunity Manager
xpress.Sum speed and behavior on numpy arrays
As a follow-up to my question posted recently below, please consider the following code snippet:
import xpress as xp
import numpy as np
x = xp.vars(5, 5, 5, name = 'x')
sum1 = np.array([xp.Sum([x[:, :, k] for k in range(5)])], dtype=xp.npexpr)
sum1 = sum1.squeeze()
sum2 = np.array([[xp.Sum(x[i, j, k] for k in range(5)) for j in range(5)] for i in range(5)], dtype=xp.npexpr)
sum3 = xp.Sum(x)- Why is the performance of sum1 so much worse than sum2 at scale (>order of magnitude)? It seems that eliminating the for loops should be faster.
- According to the documentation, sum3 should just sum over the first axis and return a 2-D (N-1) array. However, it flattens out the same to a scalar (0-D array).
- The documentation advises against the sum3 formulation if making only one pass on the variable. Why is this case? It's not doing what it should be anyway.
3 Replies
- FICOCommunity Manager
Hi Thomas,
- It is not clear why it is faster to sum several small arrays than to sum one large array. I will plan to look into this in a future release.
- I think the behaviour of the Sum function must have changed at some point (probably quite long ago by now) but the documentation was not updated. I will get the documentation fixed.
- Which part of the documentation are you referring to here?
- FICOCommunity Manager
Hi Chris,
Thank you again for your rapid reply.
- Thank you for looking into this. I think it is quite an important feature for speed performance in matrix formulations.
- Thank you.
- I am referrring to this documentation: https://www.fico.com/fico-xpress-optimization/docs/latest/solver/optimizer/python/HTML/chNumpy.html I think it is kind of a moot point now since the xpress.Sum returns a scalar over a a 2D array instead of a 1D array. I am specifically referring to this excerpt:
m,n = 1000,10 a = np.random.random((m,n)) x = xp.vars(m, n) sum_0d = xp.Sum(xp.Sum(a*x)) sum_1d = xp.Sum(a*x) The above example has a poor performance, and it is advised to avoid using xpress.Sum as such on a multi-dimensional array. If a scalar sum of all elements of the array is sought, such as sum(sum(a)) for the numerical array above, we strongly advise to flatten the array first, and run instead xpress.Sum(b.flatten()) if b is a multiarray of expressions. The multiarray has dtype equal to xpress.npexpr in order to be used for array operations. If only one pass is required, then it is better to explicitly create a vector whose elements are defined with a call to xpress.Sum: prod = a*x sum_0d = xp.Sum(prod.flatten()) sum_1d = np.array([xp.Sum(prod[i,j] for i in range(m)) for i in range(n)], dtype=xp.npexpr) - FICOCommunity Manager
Thanks, I will look at updating that documentation section too
Related Content
Related Content
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...3 years agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum3 unread comments3 unread0Views0likes3Comments- 0Views0likes2Comments