Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of numpy.matrix not recommended #93

Open
akeeman opened this issue May 1, 2020 · 9 comments
Open

Use of numpy.matrix not recommended #93

akeeman opened this issue May 1, 2020 · 9 comments

Comments

@akeeman
Copy link

akeeman commented May 1, 2020

The use of numpy.matrix is discuraged since numpy 1.15

From the docs(1.15 / stable):

Note
It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Using regular arrays gives some problems, like in the first example:

import numpy as np
from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray

s = CyClpSimplex()

# Add variables
x = s.addVariable('x', (3, 1))  # should be 2-d now
y = s.addVariable('y', (2, 1))  # should be 2-d now

# Create coefficients and bounds
A = np.array([[1., 2., 0], [1., 0, 1.]])  # use array
B = np.array([[1., 0, 0], [0, 0, 1.]])  # use array
D = np.array([[1., 2.], [0, 1]])  # use array
a = CyLPArray([5, 2.5])
b = CyLPArray([4.2, 3])
x_u = CyLPArray([2., 3.5])

# Add constraints
s += A @ x <= a  # use matmul operator
s += 2 <= B @ x + D @ y <= b  # use matmul operator
s += y >= 0
s += 1.1 <= x[1:3] <= x_u

# Set the objective function
c = CyLPArray([1., -2., 3.])
s.objective = c @ x + 2 * y.sum()  # use matmul operator

# Solve using primal Simplex
s.primal()
print(s.primalVariableSolution['x'])
Traceback (most recent call last):
  File "/path/to/example.py", line 20, in <module>
    s += A @ x <= a
ValueError: matmul: Input operand 1 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

Process finished with exit code 1
@tkralphs
Copy link
Member

tkralphs commented May 1, 2020

Ah, OK, I did not realize this. It seems to me that using the matmul operator is probably not going to work in any case, since this operation is not defined for the classes within CyLP. Do you have the same issue if you use the regular mul operator?

@tkralphs
Copy link
Member

tkralphs commented May 1, 2020

Hmm, using the mul operator also gives an error, but a different one. It's not exactly clear to me what's going on. It doesn't appear that np.matrix is actually being used anywhere underneath, just in the example, to construct the matrix. The matrices are stored internally using scipy's sparse matrix classes. Somehow, there is a shape mismatch.

@GalPerelman
Copy link

Has anyone been able to find a workaround for this issue?

@GalPerelman
Copy link

I found that np.asmatrix solves the problem
As simple as:
A = np.asmatrix([[1., 2., 0],[1., 0, 1.]])

The NumPy documentation does not contain the warning about future use:
https://numpy.org/doc/stable/reference/generated/numpy.asmatrix.html

@akeeman
Copy link
Author

akeeman commented Feb 17, 2021

That should not be the case as you still work with the matrix class, while you are encouraged not to, as the class may be removed in the future:

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

I so guess the documentation could be improved.

From the source:

@set_module('numpy')
def asmatrix(data, dtype=None):
   """
   <docs left out for brevity>
   """
   return matrix(data, dtype=dtype, copy=False)

@GalPerelman
Copy link

@akeeman you are absolutely right
I should have had to check it out myself

@tkralphs
Copy link
Member

Sorry, haven't had a chance to dig into this.

@GalPerelman
Copy link

For future similar questions, I'm using this workaround:

from scipy import sparse
A= sparse.csr_matrix(A)

@tkralphs
Copy link
Member

This sounds like it should be the right thing to do.

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

No branches or pull requests

3 participants