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