Skip to content

Commit f010542

Browse files
committed
remove np.matrix dependencies from module code
1 parent a4986b6 commit f010542

File tree

7 files changed

+39
-29
lines changed

7 files changed

+39
-29
lines changed

control/bdalg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def connect(sys, Q, inputv, outputv):
334334
K[inp,outp-1] = 1.
335335
elif outp < 0 and -outp >= -sys.outputs:
336336
K[inp,-outp-1] = -1.
337-
sys = sys.feedback(sp.matrix(K), sign=1)
337+
sys = sys.feedback(sp.array(K), sign=1)
338338

339339
# now trim
340340
Ytrim = sp.zeros( (len(outputv), sys.outputs) )
@@ -343,4 +343,4 @@ def connect(sys, Q, inputv, outputv):
343343
Utrim[u-1,i] = 1.
344344
for i,y in enumerate(outputv):
345345
Ytrim[i,y-1] = 1.
346-
return sp.matrix(Ytrim)*sys*sp.matrix(Utrim)
346+
return sp.array(Ytrim) * sys * sp.array(Utrim)

control/frdata.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# External function declarations
5252
import numpy as np
5353
from numpy import angle, array, empty, ones, \
54-
real, imag, matrix, absolute, eye, linalg, where, dot
54+
real, imag, absolute, eye, linalg, where, dot
5555
from scipy.interpolate import splprep, splev
5656
from .lti import LTI
5757

@@ -80,6 +80,10 @@ class FRD(LTI):
8080
8181
"""
8282

83+
# Allow NDarray * StateSpace to give StateSpace._rmul_() priority
84+
# https://docs.scipy.org/doc/numpy/reference/arrays.classes.html#numpy.class.__array_priority__
85+
__array_priority__ = 11 # override ndarray and matrix types
86+
8387
epsw = 1e-8
8488

8589
def __init__(self, *args, **kwargs):
@@ -433,12 +437,13 @@ def feedback(self, other=1, sign=-1):
433437
# TODO: vectorize this
434438
# TODO: handle omega re-mapping
435439
for k, w in enumerate(other.omega):
436-
fresp[:, :, k] = self.fresp[:, :, k].view(type=matrix)* \
440+
fresp[:, :, k] = np.dot(
441+
self.fresp[:, :, k],
437442
linalg.solve(
438-
eye(self.inputs) +
439-
other.fresp[:, :, k].view(type=matrix) *
440-
self.fresp[:, :, k].view(type=matrix),
441-
eye(self.inputs))
443+
eye(self.inputs) + np.dot(other.fresp[:, :, k],
444+
self.fresp[:, :, k]),
445+
eye(self.inputs))
446+
)
442447

443448
return FRD(fresp, other.omega, smooth=(self.ifunc is not None))
444449

control/mateqn.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
Author: Bjorn Olofsson
4242
"""
4343

44-
from scipy import shape, size, asarray, asmatrix, copy, zeros, eye, dot
44+
from scipy import shape, size, array, asarray, copy, zeros, eye, dot
4545
from scipy.linalg import eigvals, solve_discrete_are, solve
4646
from .exception import ControlSlycot, ControlArgument
4747

@@ -703,8 +703,8 @@ def dare(A, B, Q, R, S=None, E=None, stabilizing=True):
703703
if S is not None or E is not None or not stabilizing:
704704
return dare_old(A, B, Q, R, S, E, stabilizing)
705705
else:
706-
Rmat = asmatrix(R)
707-
Qmat = asmatrix(Q)
706+
Rmat = array(R, ndmin=2)
707+
Qmat = array(Q, ndmin=2)
708708
X = solve_discrete_are(A, B, Qmat, Rmat)
709709
G = solve(B.T.dot(X).dot(B) + Rmat, B.T.dot(X).dot(A))
710710
L = eigvals(A - B.dot(G))

control/modelsimp.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import numpy as np
4848
from .exception import ControlSlycot
4949
from .lti import isdtime, isctime
50-
from .statesp import StateSpace
50+
from .statesp import StateSpace, ssmatrix
5151
from .statefbk import gram
5252

5353
__all__ = ['hsvd', 'balred', 'modred', 'era', 'markov', 'minreal']
@@ -96,7 +96,7 @@ def hsvd(sys):
9696
w, v = np.linalg.eig(WoWc)
9797

9898
hsv = np.sqrt(w)
99-
hsv = np.matrix(hsv)
99+
hsv = np.array(hsv, ndmin=2) # was np.matrix(hsv)
100100
hsv = np.sort(hsv)
101101
hsv = np.fliplr(hsv)
102102
# Return the Hankel singular values
@@ -406,16 +406,17 @@ def markov(Y, U, M):
406406
"""
407407

408408
# Convert input parameters to matrices (if they aren't already)
409-
Ymat = np.mat(Y)
410-
Umat = np.mat(U)
409+
Ymat = np.array(Y)
410+
Umat = np.array(U)
411411
n = np.size(U)
412412

413413
# Construct a matrix of control inputs to invert
414414
UU = Umat
415415
for i in range(1, M-1):
416-
newCol = np.vstack((0, UU[0:n-1,i-2]))
416+
#! TODO: second index on UU doesn't seem right; could be neg or pos??
417+
newCol = np.vstack((0, np.reshape(UU[0:n-1,i-2], (-1,1))))
417418
UU = np.hstack((UU, newCol))
418-
Ulast = np.vstack((0, UU[0:n-1,M-2]))
419+
Ulast = np.vstack((0, np.reshape(UU[0:n-1,M-2], (-1,1))))
419420
for i in range(n-1,0,-1):
420421
Ulast[i] = np.sum(Ulast[0:i-1])
421422
UU = np.hstack((UU, Ulast))

control/statefbk.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ def acker(A, B, poles):
238238
Gains such that A - B K has given eigenvalues
239239
240240
"""
241-
# Convert the inputs to matrices
242-
a = np.mat(A)
243-
b = np.mat(B)
241+
# Convert the inputs to matrices (arrays)
242+
a = np.array(A)
243+
b = np.array(B)
244244

245245
# Make sure the system is controllable
246246
ct = ctrb(A, B)
@@ -252,9 +252,9 @@ def acker(A, B, poles):
252252

253253
# Place the poles using Ackermann's method
254254
n = np.size(p)
255-
pmat = p[n-1]*a**0
255+
pmat = p[n-1] * np.linalg.matrix_power(a, 0)
256256
for i in np.arange(1,n):
257-
pmat = pmat + p[n-i-1]*a**i
257+
pmat = pmat + np.dot(p[n-i-1], np.linalg.matrix_power(a, i))
258258
K = np.linalg.solve(ct, pmat)
259259

260260
K = K[-1][:] # Extract the last row
@@ -388,13 +388,13 @@ def ctrb(A,B):
388388
"""
389389

390390
# Convert input parameters to matrices (if they aren't already)
391-
amat = np.mat(A)
392-
bmat = np.mat(B)
391+
amat = np.array(A)
392+
bmat = np.array(B)
393393
n = np.shape(amat)[0]
394394
# Construct the controllability matrix
395395
ctrb = bmat
396396
for i in range(1, n):
397-
ctrb = np.hstack((ctrb, amat**i*bmat))
397+
ctrb = np.hstack((ctrb, np.dot(np.linalg.matrix_power(amat, i), bmat)))
398398
return ctrb
399399

400400
def obsv(A, C):
@@ -417,14 +417,14 @@ def obsv(A, C):
417417
"""
418418

419419
# Convert input parameters to matrices (if they aren't already)
420-
amat = np.mat(A)
421-
cmat = np.mat(C)
420+
amat = np.array(A)
421+
cmat = np.array(C)
422422
n = np.shape(amat)[0]
423423

424424
# Construct the controllability matrix
425425
obsv = cmat
426426
for i in range(1, n):
427-
obsv = np.vstack((obsv, cmat*amat**i))
427+
obsv = np.vstack((obsv, np.dot(cmat, np.linalg.matrix_power(amat, i))))
428428
return obsv
429429

430430
def gram(sys,type):

control/timeresp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def forced_response(sys, T=None, U=0., X0=0., transpose=False,
310310
# [ u(dt) ] = exp [ 0 0 I ] [ u0 ]
311311
# [u1 - u0] [ 0 0 0 ] [u1 - u0]
312312

313-
M = np.bmat([[A * dt, B * dt, np.zeros((n_states, n_inputs))],
313+
M = np.block([[A * dt, B * dt, np.zeros((n_states, n_inputs))],
314314
[np.zeros((n_inputs, n_states + n_inputs)),
315315
np.identity(n_inputs)],
316316
[np.zeros((n_inputs, n_states + 2 * n_inputs))]])

control/xferfcn.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class TransferFunction(LTI):
9191
discrete time system with unspecified sampling time.
9292
"""
9393

94+
# Allow NDarray * StateSpace to give StateSpace._rmul_() priority
95+
# https://docs.scipy.org/doc/numpy/reference/arrays.classes.html#numpy.class.__array_priority__
96+
__array_priority__ = 11 # override ndarray and matrix types
97+
9498
def __init__(self, *args):
9599
"""TransferFunction(num, den[, dt])
96100

0 commit comments

Comments
 (0)