Skip to content

Commit a3276b7

Browse files
committed
Refactor api, keep old api working
1 parent 42962f2 commit a3276b7

File tree

2 files changed

+39
-44
lines changed

2 files changed

+39
-44
lines changed

control/modelsimp.py

+24-34
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def era(YY, m, n, nin, nout, r):
402402
raise NotImplementedError('This function is not implemented yet.')
403403

404404

405-
def markov(*args, **kwargs):
405+
def markov(*args, m=None, transpose=False, dt=True, truncate=False):
406406
"""markov(Y, U, [, m])
407407
408408
Calculate the first `m` Markov parameters [D CB CAB ...]
@@ -420,12 +420,12 @@ def markov(*args, **kwargs):
420420
the input data is less than the desired number of Markov parameters (a
421421
warning message is generated in this case).
422422
423-
The function can be called with either 1, 2, or 3 arguments:
423+
The function can be called with either 1, 2 or 3 arguments:
424424
425-
* ``K, S, E = lqr(response)``
426-
* ``K, S, E = lqr(respnose, m)``
427-
* ``K, S, E = lqr(Y, U)``
428-
* ``K, S, E = lqr(Y, U, m)``
425+
* ``H = markov(response)``
426+
* ``H = markov(respnose, m)``
427+
* ``H = markov(Y, U)``
428+
* ``H = markov(Y, U, m)``
429429
430430
where `response` is an `TimeResponseData` object, and `Y`, `U`, are 1D or 2D
431431
array and m is an integer.
@@ -446,26 +446,20 @@ def markov(*args, **kwargs):
446446
Number of Markov parameters to output. Defaults to len(U).
447447
dt : True of float, optional
448448
True indicates discrete time with unspecified sampling time,
449-
positive number is discrete time with specified sampling time.
450-
It can be used to scale the markov parameters in order to match
451-
the impulse response of this library.
452-
Default values is True.
449+
positive number is discrete time with specified sampling time.It
450+
can be used to scale the markov parameters in order to match the
451+
impulse response of this library. Default is True.
453452
truncate : bool, optional
454-
Do not use first m equation for least least squares.
455-
Default value is False.
453+
Do not use first m equation for least least squares. Default is False.
454+
transpose : bool, optional
455+
Assume that input data is transposed relative to the standard
456+
:ref:`time-series-convention`. For TimeResponseData this parameter
457+
is ignored. Default is False.
456458
457459
Returns
458460
-------
459461
H : ndarray
460462
First m Markov parameters, [D CB CAB ...]
461-
462-
463-
Notes
464-
-----
465-
It works for SISO and MIMO systems.
466-
467-
This function does comply with the Python Control Library
468-
:ref:`time-series-convention` for representation of time series data.
469463
470464
References
471465
----------
@@ -494,25 +488,21 @@ def markov(*args, **kwargs):
494488
transpose = args[0].transpose
495489
if args[0].transpose and not args[0].issiso:
496490
Umat, Ymat = np.transpose(Umat), np.transpose(Ymat)
497-
index = 1
491+
if (len(args) == 2):
492+
m = args[1]
493+
elif (len(args) > 2):
494+
raise ControlArgument("too many positional arguments")
498495
else:
499496
if (len(args) < 2):
500497
raise ControlArgument("not enough input arguments")
501-
Umat = np.array(args[0], ndmin=2)
502-
Ymat = np.array(args[1], ndmin=2)
503-
transpose = kwargs.pop('transpose', False)
498+
Umat = np.array(args[1], ndmin=2)
499+
Ymat = np.array(args[0], ndmin=2)
504500
if transpose:
505501
Umat, Ymat = np.transpose(Umat), np.transpose(Ymat)
506-
index = 2
507-
508-
509-
if (len(args) > index):
510-
m = args[index]
511-
else:
512-
m = None
513-
514-
dt = kwargs.pop('dt', True)
515-
truncate = kwargs.pop('truncate', False)
502+
if (len(args) == 3):
503+
m = args[2]
504+
elif (len(args) > 3):
505+
raise ControlArgument("too many positional arguments")
516506

517507
# Make sure the number of time points match
518508
if Umat.shape[1] != Ymat.shape[1]:

control/tests/modelsimp_test.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def testMarkovSignature(self):
4545
m = 3
4646
Htrue = np.array([1., 0., 0.])
4747

48-
H = markov(Y, U, m, transpose=False)
48+
H = markov(Y, U, m=m, transpose=False)
4949
np.testing.assert_array_almost_equal(H, Htrue)
5050

5151
response.transpose=False
52-
H = markov(response, m)
52+
H = markov(response, m=m)
5353
np.testing.assert_array_almost_equal(H, Htrue)
5454

5555
# Make sure that transposed data also works
@@ -68,20 +68,25 @@ def testMarkovSignature(self):
6868
H = markov(response, m)
6969
np.testing.assert_array_almost_equal(H, Htrue)
7070

71+
H = markov(Y, U, m=m)
72+
np.testing.assert_array_almost_equal(H, Htrue)
73+
74+
H = markov(response, m=m)
75+
np.testing.assert_array_almost_equal(H, Htrue)
76+
7177
# Test example from docstring
72-
# TODO: There is a problem here
73-
# Htrue = np.array([1., 0.5, 0.])
78+
# TODO: There is a problem here, last markov parameter does not fit
79+
# the approximation error could be to big
80+
Htrue = np.array([0, 1., -0.5])
7481
T = np.linspace(0, 10, 100)
7582
U = np.ones((1, 100))
7683
T, Y = forced_response(tf([1], [1, 0.5], True), T, U)
77-
H = markov(Y, U, 3, transpose=False)
78-
#np.testing.assert_array_almost_equal(H, Htrue)
84+
H = markov(Y, U, 4, transpose=False)
85+
np.testing.assert_array_almost_equal(H[:3], Htrue[:3])
7986

80-
T = np.linspace(0, 10, 100)
81-
U = np.ones((1, 100))
8287
response = forced_response(tf([1], [1, 0.5], True), T, U)
83-
H = markov(response, 3)
84-
#np.testing.assert_array_almost_equal(H, Htrue)
88+
H = markov(response, 4)
89+
np.testing.assert_array_almost_equal(H[:3], Htrue[:3])
8590

8691
# Test example from issue #395
8792
inp = np.array([1, 2])

0 commit comments

Comments
 (0)