Skip to content

Allow np.array or np.matrix for state space matrices, operations #314

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

Merged
merged 4 commits into from
Jun 30, 2019

Conversation

murrayrm
Copy link
Member

This PR addresses issue #233 regarding (eventual) deprecation of the numpy.matrix class. In this PR, state space objects are stored as either numpy.matrix (default) or numpy.array objects, with the selection determined based on the use_numpy_array() function.

For backward compatibility, the numpy.matrix class is the current default, but all operations are compatible with using numpy.array as the default storage and output class and this will be the default when numpy.matrix is deprecated.

A set of unit tests is also included for testing basic operations when the numpy.array class is used as the state space storage and output class. This PR does not convert all of the existing unit tests to use numpy.array instead of numpy.matrix, so there are still lots of warning messages. At some point we will need to update all unit tests to work with numpy.array objects (as we as done in abandoned PR #292).

@coveralls
Copy link

coveralls commented Jun 15, 2019

Coverage Status

Coverage increased (+0.06%) to 82.173% when pulling 7c9d9a6 on murrayrm:ss_matrix into e2346cd on python-control:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 82.179% when pulling 0c1e983 on murrayrm:ss_matrix into e2346cd on python-control:master.

control/bdalg.py Outdated
>>> sys = append(sys1, sys2)
>>> Q = sp.mat([ [ 1, 2], [2, -1] ]) # basically feedback, output 2 in 1
>>> Q = [[1, 2], [2, -1]]) # negative feedback interconnection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing parenthesis here looks wrong. Do we run these with doctest?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

We are not currently using doctest, but I ran the revised version manually to make sure it was OK.

eye(self.inputs))
eye(self.inputs)
+ np.dot(other.fresp[:, :, k], self.fresp[:, :, k]),
eye(self.inputs))
Copy link
Contributor

@roryyorke roryyorke Jun 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not the point of this change, but is linalg(X, eye(shape)) any better than np.linalg.inv? I'd have thought a "right divide" (linalg.solve with appropriate transposes) would have been a more standard approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this could use sp.linalg.solve instead, but since I wasn't the original author I just did the conversion to remove the np.matrix dependence. I've added a TODO for someone to fix this, so at least we don't forget about it (in principle).

@@ -41,16 +41,17 @@
Author: Bjorn Olofsson
"""

from scipy import shape, size, asarray, asmatrix, copy, zeros, eye, dot
from scipy import shape, size, array, asarray, copy, zeros, eye, dot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again not the point, but since you remove import scipy as sp, I think this should be from numpy import ...

@@ -69,7 +70,9 @@ def lyap(A,Q,C=None,E=None):
:math:`A X E^T + E X A^T + Q = 0`

where Q is a symmetric matrix and A, Q and E are square matrices
of the same dimension. """
of the same dimension.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super-familiar with PEP8 and so on, but this extra blank line seems odd.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -252,13 +253,13 @@ def acker(A, B, poles):

# Place the poles using Ackermann's method
n = np.size(p)
pmat = p[n-1]*a**0
pmat = p[n-1] * np.linalg.matrix_power(a, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.eye(a.shape) ? I suppose the matrix_power call sets the stage for the upcoming for-loop.

for i in np.arange(1,n):
pmat = pmat + p[n-i-1]*a**i
pmat = pmat + np.dot(p[n-i-1], np.linalg.matrix_power(a, i))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again somewhat beside the point of this commit, but would it be possible to use Horner's method here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a TODO for someone to do this in a separate PR.

@murrayrm
Copy link
Member Author

CI tests are failing due to issue #321. Need to fix before merging.

@murrayrm murrayrm merged commit de9e3e7 into python-control:master Jun 30, 2019
@murrayrm murrayrm deleted the ss_matrix branch July 1, 2019 05:20
@murrayrm murrayrm added this to the 0.8.3 milestone Jan 4, 2020
murrayrm pushed a commit that referenced this pull request Jan 5, 2020
PR #314 duplicates a lot of code in the test cases by introducing *_array_test.py files. Thus issue #190 addressed in PR #320 resurfaces and needs to be introduced to robust_array_test.pyas well.
repagh pushed a commit to repagh/python-control that referenced this pull request Jun 9, 2020
…hon-control#314)

* creates a new function `config.use_numpy_matrix()` that allows either `np.array` or `np.matrix` for state space matrices + unit tests

* allow D=0 to broadcast to correct size for MIMO statespace systems
repagh pushed a commit to repagh/python-control that referenced this pull request Jun 9, 2020
…bug (python-control#365)

PR python-control#314 duplicates a lot of code in the test cases by introducing *_array_test.py files. Thus issue python-control#190 addressed in PR python-control#320 resurfaces and needs to be introduced to robust_array_test.pyas well.
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

Successfully merging this pull request may close these issues.

3 participants