Skip to content

Commit d29c2d4

Browse files
committed
Simplify symmetric kwargs handling
1 parent 81d97b7 commit d29c2d4

3 files changed

Lines changed: 12 additions & 47 deletions

File tree

‎control/mateqn.py‎

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _warn_ill_conditioned_E(E):
8181
#
8282

8383

84-
def lyap(A, Q, C=None, E=None, method=None, **kwargs):
84+
def lyap(A, Q, C=None, E=None, method=None, symmetric_kwargs=None):
8585
"""Solves the continuous-time Lyapunov equation.
8686
8787
X = lyap(A, Q) solves
@@ -147,11 +147,6 @@ def lyap(A, Q, C=None, E=None, method=None, **kwargs):
147147
148148
"""
149149

150-
symmetric_kwargs = kwargs.pop("symmetric_kwargs", {})
151-
152-
# Make sure there were no extraneous keywords
153-
if kwargs:
154-
raise TypeError("unrecognized keyword(s): ", str(kwargs))
155150
# Decide what method to use
156151
method = _slycot_or_scipy(method)
157152
if method == 'slycot':
@@ -252,7 +247,7 @@ def lyap(A, Q, C=None, E=None, method=None, **kwargs):
252247
return X
253248

254249

255-
def dlyap(A, Q, C=None, E=None, method=None, **kwargs):
250+
def dlyap(A, Q, C=None, E=None, method=None, symmetric_kwargs=None):
256251
"""Solves the discrete-time Lyapunov equation.
257252
258253
X = dlyap(A, Q) solves
@@ -329,10 +324,6 @@ def dlyap(A, Q, C=None, E=None, method=None, **kwargs):
329324
330325
"""
331326

332-
symmetric_kwargs = kwargs.pop("symmetric_kwargs", {})
333-
# Make sure there were no extraneous keywords
334-
if kwargs:
335-
raise TypeError("unrecognized keyword(s): ", str(kwargs))
336327
# Decide what method to use
337328
method = _slycot_or_scipy(method)
338329

@@ -466,8 +457,8 @@ def dlyap(A, Q, C=None, E=None, method=None, **kwargs):
466457
# Riccati equation solvers care and dare
467458
#
468459

469-
def care(A, B, Q, R=None, S=None, E=None, stabilizing=True, method=None,
470-
_As="A", _Bs="B", _Qs="Q", _Rs="R", _Ss="S", _Es="E", **kwargs):
460+
def care(A, B, Q, R=None, S=None, E=None, stabilizing=True, method=None, symmetric_kwargs=None,
461+
_As="A", _Bs="B", _Qs="Q", _Rs="R", _Ss="S", _Es="E", ):
471462
"""Solves the continuous-time algebraic Riccati equation.
472463
473464
X, L, G = care(A, B, Q, R=None) solves
@@ -519,12 +510,6 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True, method=None,
519510
520511
"""
521512

522-
symmetric_kwargs = kwargs.pop("symmetric_kwargs", {})
523-
524-
# Make sure there were no extraneous keywords
525-
if kwargs:
526-
raise TypeError("unrecognized keyword(s): ", str(kwargs))
527-
528513
# Decide what method to use
529514
method = _slycot_or_scipy(method)
530515

@@ -632,8 +617,8 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True, method=None,
632617
# the gain matrix G
633618
return X, L, G
634619

635-
def dare(A, B, Q, R, S=None, E=None, stabilizing=True, method=None,
636-
_As="A", _Bs="B", _Qs="Q", _Rs="R", _Ss="S", _Es="E", **kwargs):
620+
def dare(A, B, Q, R, S=None, E=None, stabilizing=True, method=None, symmetric_kwargs=None,
621+
_As="A", _Bs="B", _Qs="Q", _Rs="R", _Ss="S", _Es="E"):
637622
"""Solves the discrete-time algebraic Riccati equation.
638623
639624
X, L, G = dare(A, B, Q, R) solves
@@ -685,12 +670,6 @@ def dare(A, B, Q, R, S=None, E=None, stabilizing=True, method=None,
685670
686671
"""
687672

688-
symmetric_kwargs = kwargs.pop("symmetric_kwargs", {})
689-
690-
# Make sure there were no extraneous keywords
691-
if kwargs:
692-
raise TypeError("unrecognized keyword(s): ", str(kwargs))
693-
694673
# Decide what method to use
695674
method = _slycot_or_scipy(method)
696675

@@ -777,8 +756,7 @@ def _slycot_or_scipy(method):
777756

778757

779758
# Utility function to check matrix dimensions
780-
def _check_shape(M, n, m, square=False, symmetric=False, name="??",
781-
symmetric_kwargs=None):
759+
def _check_shape(M, n, m, square=False, symmetric=False, name="??", symmetric_kwargs=None):
782760
"""Check the shape and properties of a 2D array.
783761
784762
This function can be used to check to make sure a 2D array_like has the
@@ -824,12 +802,9 @@ def _check_shape(M, n, m, square=False, symmetric=False, name="??",
824802

825803
# Utility function to check if a matrix is symmetric
826804
def _is_symmetric(M, symmetric_kwargs=None):
827-
M = np.atleast_2d(M)
828-
symmetric_kwargs = (
829-
symmetric_kwargs.copy() if symmetric_kwargs else {}
830-
)
805+
if symmetric_kwargs is None:
806+
symmetric_kwargs = {}
831807

832808
if np.iscomplexobj(M):
833809
return sp.linalg.ishermitian(M, **symmetric_kwargs)
834-
else:
835-
return sp.linalg.issymmetric(M, **symmetric_kwargs)
810+
return sp.linalg.issymmetric(M, **symmetric_kwargs)

‎control/stochsys.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
# contributed by Sawyer B. Fuller <minster@uw.edu>
36-
def lqe(*args, **kwargs):
36+
def lqe(*args, symmetric_kwargs=None, **kwargs):
3737
r"""lqe(A, G, C, QN, RN, [, NN])
3838
3939
Continuous-time linear quadratic estimator (Kalman filter).
@@ -136,7 +136,6 @@ def lqe(*args, **kwargs):
136136

137137
# Get the method to use (if specified as a keyword)
138138
method = kwargs.pop('method', None)
139-
symmetric_kwargs = kwargs.pop('symmetric_kwargs', {})
140139

141140
if kwargs:
142141
raise TypeError("unrecognized keyword(s): ", str(kwargs))
@@ -188,7 +187,7 @@ def lqe(*args, **kwargs):
188187

189188

190189
# contributed by Sawyer B. Fuller <minster@uw.edu>
191-
def dlqe(*args, **kwargs):
190+
def dlqe(*args, symmetric_kwargs=None, **kwargs):
192191
r"""dlqe(A, G, C, QN, RN, [, N])
193192
194193
Discrete-time linear quadratic estimator (Kalman filter).
@@ -260,7 +259,6 @@ def dlqe(*args, **kwargs):
260259

261260
# Get the method to use (if specified as a keyword)
262261
method = kwargs.pop('method', None)
263-
symmetric_kwargs = kwargs.pop('symmetric_kwargs', {})
264262

265263
if kwargs:
266264
raise TypeError("unrecognized keyword(s): ", str(kwargs))

‎control/tests/kwargs_test.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,16 @@ def test_kwarg_search(module, prefix):
9999
@pytest.mark.parametrize(
100100
"function, nsssys, ntfsys, moreargs, kwargs",
101101
[(control.append, 2, 0, (), {}),
102-
(control.care, 0, 0, ([[-1]], [[1]], [[1]], [[1]]), {'method': 'scipy'}),
103102
(control.combine_tf, 0, 0, ([[1, 0], [0, 1]], ), {}),
104-
(control.dare, 0, 0, ([[0.5]], [[1]], [[1]], [[1]]), {'method': 'scipy'}),
105103
(control.dlqe, 1, 0, ([[1]], [[1]]), {}),
106104
(control.dlqr, 1, 0, ([[1, 0], [0, 1]], [[1]]), {}),
107-
(control.dlyap, 0, 0, ([[0.5]], [[1]]), {'method': 'scipy'}),
108105
(control.drss, 0, 0, (2, 1, 1), {}),
109106
(control.feedback, 2, 0, (), {}),
110107
(control.flatsys.flatsys, 1, 0, (), {}),
111108
(control.input_output_response, 1, 0, ([0, 1, 2], [1, 1, 1]), {}),
112109
(control.lqe, 1, 0, ([[1]], [[1]]), {}),
113110
(control.lqr, 1, 0, ([[1, 0], [0, 1]], [[1]]), {}),
114111
(control.linearize, 1, 0, (0, 0), {}),
115-
(control.lyap, 0, 0, ([[-1]], [[1]]), {'method': 'scipy'}),
116112
(control.negate, 1, 0, (), {}),
117113
(control.nlsys, 0, 0, (lambda t, x, u, params: np.array([0]),), {}),
118114
(control.parallel, 2, 0, (), {}),
@@ -254,17 +250,14 @@ def test_response_plot_kwargs(data_fcn, plot_fcn, mimo):
254250
'bode': test_response_plot_kwargs,
255251
'bode_plot': test_response_plot_kwargs,
256252
'LTI.bode_plot': test_response_plot_kwargs, # tested via bode_plot
257-
'care': test_unrecognized_kwargs,
258253
'combine_tf': test_unrecognized_kwargs,
259254
'create_estimator_iosystem': stochsys_test.test_estimator_errors,
260255
'create_statefbk_iosystem': statefbk_test.TestStatefbk.test_statefbk_errors,
261-
'dare': test_unrecognized_kwargs,
262256
'describing_function_plot': test_matplotlib_kwargs,
263257
'describing_function_response':
264258
descfcn_test.test_describing_function_exceptions,
265259
'dlqe': test_unrecognized_kwargs,
266260
'dlqr': test_unrecognized_kwargs,
267-
'dlyap': test_unrecognized_kwargs,
268261
'drss': test_unrecognized_kwargs,
269262
'feedback': test_unrecognized_kwargs,
270263
'find_eqpt': iosys_test.test_find_operating_point,
@@ -282,7 +275,6 @@ def test_response_plot_kwargs(data_fcn, plot_fcn, mimo):
282275
'linearize': test_unrecognized_kwargs,
283276
'lqe': test_unrecognized_kwargs,
284277
'lqr': test_unrecognized_kwargs,
285-
'lyap': test_unrecognized_kwargs,
286278
'LTI.forced_response': statesp_test.test_convenience_aliases,
287279
'LTI.impulse_response': statesp_test.test_convenience_aliases,
288280
'LTI.initial_response': statesp_test.test_convenience_aliases,

0 commit comments

Comments
 (0)