@@ -92,38 +92,47 @@ def _ssmatrix(data, axis=1):
92
92
93
93
Returns
94
94
-------
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
96
96
97
97
"""
98
- # Convert the data into an array or matrix, as configured
98
+
99
99
# 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
100
110
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 )
104
112
else :
105
- arr = np .array (data , dtype = float )
113
+ arr = np .asarray (arr , dtype = arr_ndtype )
114
+
106
115
ndim = arr .ndim
107
116
shape = arr .shape
108
117
109
118
# Change the shape of the array into a 2D array
110
119
if (ndim > 2 ):
111
120
raise ValueError ("state-space matrix must be 2-dimensional" )
112
121
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)
116
125
shape = (0 , 0 )
117
126
118
127
elif ndim == 1 :
119
- # Passed a row or column vector
128
+ # Passed a non-empty row or column vector
120
129
shape = (1 , shape [0 ]) if axis == 1 else (shape [0 ], 1 )
121
130
122
131
elif ndim == 0 :
123
132
# Passed a constant; turn into a matrix
124
133
shape = (1 , 1 )
125
134
126
- # Create the actual object used to store the result
135
+ # create the correct view of the data
127
136
return arr .reshape (shape )
128
137
129
138
0 commit comments