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

Skip to content

Commit da7d6c0

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

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
@@ -92,38 +92,47 @@ def _ssmatrix(data, axis=1):
9292
9393
Returns
9494
-------
95-
arr : 2D array, with shape (0, 0) if a is empty
95+
arr : real or complex 2D array, with shape (0, 0) if a is empty
9696
9797
"""
98-
# Convert the data into an array or matrix, as configured
98+
9999
# If data is passed as a string, use (deprecated?) matrix constructor
100+
if isinstance(data, str):
101+
arr = np.asarray(np.matrix(data))
102+
else:
103+
# TODO: Is it okay to use a view here (asarray or copy=False)?
104+
arr = np.array(data)
105+
106+
# find out if we need to cast to float or complex
107+
arr_ndtype = np.result_type(np.float64, arr)
108+
109+
# Get a view of the data as 2D array or matrix, as configured
100110
if config.defaults['statesp.use_numpy_matrix']:
101-
arr = np.matrix(data, dtype=float)
102-
elif isinstance(data, str):
103-
arr = np.array(np.matrix(data, dtype=float))
111+
arr = np.asmatrix(arr, dtype=arr_ndtype)
104112
else:
105-
arr = np.array(data, dtype=float)
113+
arr = np.asarray(arr, dtype=arr_ndtype)
114+
106115
ndim = arr.ndim
107116
shape = arr.shape
108117

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

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

118127
elif ndim == 1:
119-
# Passed a row or column vector
128+
# Passed a non-empty row or column vector
120129
shape = (1, shape[0]) if axis == 1 else (shape[0], 1)
121130

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

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

129138

0 commit comments

Comments
 (0)
0