8000 Change _ssmatrix conversion: allow complex dtype, (cf. #376) · bnavigator/python-control@a644f59 · GitHub
[go: up one dir, main page]

Skip to content

Commit a644f59

Browse files
committed
Change _ssmatrix conversion: allow complex dtype, (cf. python-control#376)
1 parent dce9fe2 commit a644f59

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

control/statesp.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,38 +93,47 @@ def _ssmatrix(data, axis=1):
9393
9494
Returns
9595
-------
96-
arr : 2D array, with shape (0, 0) if a is empty
96+
arr : real or complex 2D array, with shape (0, 0) if a is empty
9797
9898
"""
99-
# Convert the data into an array or matrix, as configured
99+
100100
# If data is passed as a string, use (deprecated?) matrix constructor
101+
if isinstance(data, str):
102+
arr = np.asarray(np.matrix(data))
103+
else:
104+
# TODO: Is it okay to use a view here (asarray or copy=False)?
105+
arr = np.array(data)
106+
107+
# find out if we need to cast to float or complex
108+
arr_ndtype = np.result_type(np.float64, arr)
109+
110+
# Get a view of the data as 2D array or matrix, as configured
101111
if config.defaults['statesp.use_numpy_matrix']:
102-
arr = np.matrix(data, dtype=float)
103-
elif isinstance(data, str):
104-
arr = np.array(np.matrix(data, dtype=float))
112+
arr = np.asmatrix(arr, dtype=arr_ndtype)
105113
else:
106-
arr = np.array(data, dtype=float)
114+
arr = np.asarray(arr, dtype=arr_ndtype)
115+
107116
ndim = arr.ndim
108117
shape = arr.shape
109118

110119
# Change the shape of the array into a 2D array
111120
if (ndim > 2):
112121
raise ValueError("state-space matrix must be 2-dimensional")
113122

114-
elif (ndim == 2 and shape == (1, 0)) or \
115-
(ndim == 1 and shape == (0, )):
116-
# Passed an empty matrix or empty vector; change shape to (0, 0)
123+
# note: default empty matrix is (1,0)
124+
if shape in [(1, 0), (0, )]:
125+
# Passed an empty matrix, list or array vector; change shape to (0, 0)
117126
shape = (0, 0)
118127

119128
elif ndim == 1:
120-
# Passed a row or column vector
129+
# Passed a non-empty row or column vector
121130
shape = (1, shape[0]) if axis == 1 else (shape[0], 1)
122131

123132
elif ndim == 0:
124133
# Passed a constant; turn into a matrix
125134
shape = (1, 1)
126135

127-
# Create the actual object used to store the result
136+
# create the correct view of the data
128137
return arr.reshape(shape)
129138

130139

0 commit comments

Comments
 (0)
0