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[:...
FICO
3 years agoCommunity 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)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