|
48 | 48 | """ |
49 | 49 |
|
50 | 50 | from collections.abc import Iterable |
| 51 | +from copy import deepcopy |
| 52 | +from itertools import chain |
| 53 | +from re import sub |
| 54 | +from warnings import warn |
51 | 55 |
|
52 | 56 | # External function declarations |
53 | 57 | import numpy as np |
54 | | -from numpy import angle, array, empty, finfo, ndarray, ones, \ |
55 | | - polyadd, polymul, polyval, roots, sqrt, zeros, squeeze, exp, pi, \ |
56 | | - where, delete, real, poly, nonzero |
57 | 58 | import scipy as sp |
58 | | -from scipy.signal import tf2zpk, zpk2tf, cont2discrete |
| 59 | +from numpy import angle, array, delete, empty, exp, finfo, ndarray, nonzero, \ |
| 60 | + ones, pi, poly, polyadd, polymul, polyval, real, roots, sqrt, squeeze, \ |
| 61 | + where, zeros |
59 | 62 | from scipy.signal import TransferFunction as signalTransferFunction |
60 | | -from copy import deepcopy |
61 | | -from warnings import warn |
62 | | -from itertools import chain |
63 | | -from re import sub |
64 | | -from .lti import LTI, _process_frequency_response |
65 | | -from .iosys import InputOutputSystem, common_timebase, isdtime, \ |
66 | | - _process_iosys_keywords |
| 63 | +from scipy.signal import cont2discrete, tf2zpk, zpk2tf |
| 64 | + |
| 65 | +from . import config |
67 | 66 | from .exception import ControlMIMONotImplemented |
68 | 67 | from .frdata import FrequencyResponseData |
69 | | -from . import config |
| 68 | +from .iosys import InputOutputSystem, NamedSignal, _process_iosys_keywords, \ |
| 69 | + _process_subsys_index, common_timebase, isdtime |
| 70 | +from .lti import LTI, _process_frequency_response |
70 | 71 |
|
71 | 72 | __all__ = ['TransferFunction', 'tf', 'zpk', 'ss2tf', 'tfdata'] |
72 | 73 |
|
@@ -761,48 +762,30 @@ def __pow__(self, other): |
761 | 762 |
|
762 | 763 | def __getitem__(self, key): |
763 | 764 | if not isinstance(key, Iterable) or len(key) != 2: |
764 | | - raise IOError('must provide indices of length 2 for transfer functions') |
| 765 | + raise IOError( |
| 766 | + "must provide indices of length 2 for transfer functions") |
| 767 | + |
| 768 | + # Convert signal names to integer offsets (via NamedSignal object) |
| 769 | + iomap = NamedSignal( |
| 770 | + np.empty((self.noutputs, self.ninputs)), |
| 771 | + self.output_labels, self.input_labels) |
| 772 | + indices = iomap._parse_key(key) |
| 773 | + outdx, outputs = _process_subsys_index( |
| 774 | + indices[0], self.output_labels, slice_to_list=True) |
| 775 | + inpdx, inputs = _process_subsys_index( |
| 776 | + indices[1], self.input_labels, slice_to_list=True) |
765 | 777 |
|
766 | | - key1, key2 = key |
767 | | - if not isinstance(key1, (int, slice)) or not isinstance(key2, (int, slice)): |
768 | | - raise TypeError(f"system indices must be integers or slices") |
769 | | - |
770 | | - # pre-process |
771 | | - if isinstance(key1, int): |
772 | | - key1 = slice(key1, key1 + 1, 1) |
773 | | - if isinstance(key2, int): |
774 | | - key2 = slice(key2, key2 + 1, 1) |
775 | | - # dim1 |
776 | | - start1, stop1, step1 = key1.start, key1.stop, key1.step |
777 | | - if step1 is None: |
778 | | - step1 = 1 |
779 | | - if start1 is None: |
780 | | - start1 = 0 |
781 | | - if stop1 is None: |
782 | | - stop1 = len(self.num) |
783 | | - # dim1 |
784 | | - start2, stop2, step2 = key2.start, key2.stop, key2.step |
785 | | - if step2 is None: |
786 | | - step2 = 1 |
787 | | - if start2 is None: |
788 | | - start2 = 0 |
789 | | - if stop2 is None: |
790 | | - stop2 = len(self.num[0]) |
791 | | - |
| 778 | + # Construct the transfer function for the subsyste |
792 | 779 | num, den = [], [] |
793 | | - for i in range(start1, stop1, step1): |
| 780 | + for i in outdx: |
794 | 781 | num_i = [] |
795 | 782 | den_i = [] |
796 | | - for j in range(start2, stop2, step2): |
| 783 | + for j in inpdx: |
797 | 784 | num_i.append(self.num[i][j]) |
798 | 785 | den_i.append(self.den[i][j]) |
799 | 786 | num.append(num_i) |
800 | 787 | den.append(den_i) |
801 | 788 |
|
802 | | - # Save the label names |
803 | | - outputs = [self.output_labels[i] for i in range(start1, stop1, step1)] |
804 | | - inputs = [self.input_labels[j] for j in range(start2, stop2, step2)] |
805 | | - |
806 | 789 | # Create the system name |
807 | 790 | sysname = config.defaults['iosys.indexed_system_name_prefix'] + \ |
808 | 791 | self.name + config.defaults['iosys.indexed_system_name_suffix'] |
|
0 commit comments