# Use numpy to print the product of a matrix by a random vector.
#
# Uses xpress.Dot to on a matrix and a vector. Note that the NumPy dot operator
# works perfectly fine here.
#
# (C) 1983-2025 Fair Isaac Corporation
import numpy as np
import xpress as xp
p = xp.problem()
x = [p.addVariable() for i in range(5)]
p.addConstraint(xp.Sum(x) >= 2)
p.setObjective(xp.Sum(x[i]**2 for i in range(5)))
p.optimize()
A = np.array(range(30)).reshape(6, 5) # A is a 6x5 matrix.
sol = np.array(p.getSolution()) # Suppose it's a vector of size 5.
columns = A*sol # Not a matrix-vector product!
v = xp.Dot(A, sol) # This is a matrix-vector product A*sol.
print(v)
|
# Uses the xpress.Dot() operator to formulate constraints simply.
#
# Note that the NumPy dot operator is not suitable here as the
# result is an expression in the Xpress variables.
#
# (C) 1983-2025 Fair Isaac Corporation
import xpress as xp
import numpy as np
A = np.random.random(30).reshape(6, 5) # A is a 6x5 matrix.
Q = np.random.random(25).reshape(5, 5) # Q is a 5x5 matrix.
p = xp.problem()
# Add a NumPy array of variables.
x = p.addVariables(5)
x0 = np.random.random(5) # Random vector.
Q += 4 * np.eye(5) # Add 5 * the identity matrix.
# 6 constraints (rows of A).
Lin_sys = xp.Dot(A, x) <= np.array([3, 4, 1, 4, 8, 7])
# One quadratic constraint.
Conv_c = xp.Dot(x, Q, x) <= 1
p.addConstraint(Lin_sys, Conv_c)
p.setObjective(xp.Dot(x-x0, x-x0))
p.optimize()
|