Skip to content

implement indexing for transfer function #246

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 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions control/tests/xferfcn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import unittest
import numpy as np
from control.statesp import StateSpace, _convertToStateSpace
from control.xferfcn import TransferFunction, _convertToTransferFunction
from control.statesp import StateSpace, _convertToStateSpace, rss
from control.xferfcn import TransferFunction, _convertToTransferFunction, ss2tf
from control.lti import evalfr
from control.exception import slycot_check
# from control.lti import isdtime
Expand Down Expand Up @@ -536,6 +536,25 @@ def testMIMO(self):
np.testing.assert_array_almost_equal(H.num[1][0], H2.num[1][0])
np.testing.assert_array_almost_equal(H.den[1][0], H2.den[1][0])

def testIndexing(self):
tm = ss2tf(rss(5, 3, 3))

# scalar indexing
sys01 = tm[0, 1]
np.testing.assert_array_almost_equal(sys01.num[0][0], tm.num[0][1])
np.testing.assert_array_almost_equal(sys01.den[0][0], tm.den[0][1])

# slice indexing
sys = tm[:2, 1:3]
np.testing.assert_array_almost_equal(sys.num[0][0], tm.num[0][1])
np.testing.assert_array_almost_equal(sys.den[0][0], tm.den[0][1])
np.testing.assert_array_almost_equal(sys.num[0][1], tm.num[0][2])
np.testing.assert_array_almost_equal(sys.den[0][1], tm.den[0][2])
np.testing.assert_array_almost_equal(sys.num[1][0], tm.num[1][1])
np.testing.assert_array_almost_equal(sys.den[1][0], tm.den[1][1])
np.testing.assert_array_almost_equal(sys.num[1][1], tm.num[1][2])
np.testing.assert_array_almost_equal(sys.den[1][1], tm.den[1][2])

def testMatrixMult(self):
"""MIMO transfer functions should be multiplyable by constant
matrices"""
Expand Down
40 changes: 40 additions & 0 deletions control/xferfcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,46 @@ def __pow__(self, other):
if other < 0:
return (TransferFunction([1], [1]) / self) * (self**(other+1))

def __getitem__(self, key):
key1, key2 = key

# pre-process
if isinstance(key1, int):
key1 = slice(key1, key1 + 1, 1)
if isinstance(key2, int):
key2 = slice(key2, key2 + 1, 1)
# dim1
start1, stop1, step1 = key1.start, key1.stop, key1.step
if step1 is None:
step1 = 1
if start1 is None:
start1 = 0
if stop1 is None:
stop1 = len(self.num)
# dim1
start2, stop2, step2 = key2.start, key2.stop, key2.step
if step2 is None:
step2 = 1
if start2 is None:
start2 = 0
if stop2 is None:
stop2 = len(self.num[0])

num = []
den = []
for i in range(start1, stop1, step1):
num_i = []
den_i = []
for j in range(start2, stop2, step2):
num_i.append(self.num[i][j])
den_i.append(self.den[i][j])
num.append(num_i)
den.append(den_i)
if self.isctime():
return TransferFunction(num, den)
else:
return TransferFunction(num, den, self.dt)

def evalfr(self, omega):
"""Evaluate a transfer function at a single angular frequency.

Expand Down