Skip to content

allow complex statespace matrices #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions control/statesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,47 @@ def _ssmatrix(data, axis=1):

Returns
-------
arr : 2D array, with shape (0, 0) if a is empty
arr : real or complex 2D array, with shape (0, 0) if a is empty

"""
# Convert the data into an array or matrix, as configured

# If data is passed as a string, use (deprecated?) matrix constructor
if isinstance(data, str):
arr = np.asarray(np.matrix(data))
else:
# TODO: Is it okay to use a view here (asarray or copy=False)?
arr = np.array(data)

# find out if we need to cast to float or complex
arr_ndtype = np.result_type(np.float64, arr)

# Get a view of the data as 2D array or matrix, as configured
if config.defaults['statesp.use_numpy_matrix']:
arr = np.matrix(data, dtype=float)
elif isinstance(data, str):
arr = np.array(np.matrix(data, dtype=float))
arr = np.asmatrix(arr, dtype=arr_ndtype)
else:
arr = np.array(data, dtype=float)
arr = np.asarray(arr, dtype=arr_ndtype)

ndim = arr.ndim
shape = arr.shape

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

elif (ndim == 2 and shape == (1, 0)) or \
(ndim == 1 and shape == (0, )):
# Passed an empty matrix or empty vector; change shape to (0, 0)
# note: default empty matrix is (1,0)
if shape in [(1, 0), (0, )]:
# Passed an empty matrix, list or array vector; change shape to (0, 0)
shape = (0, 0)

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

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

# Create the actual object used to store the result
# create the correct view of the data
return arr.reshape(shape)


Expand Down