From 0fda8c0a9d3b47d93aae631db416ae40a69cb646 Mon Sep 17 00:00:00 2001 From: bnavigator Date: Wed, 30 Dec 2020 00:14:04 +0100 Subject: [PATCH] allow complex statespace matrices --- control/statesp.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/control/statesp.py b/control/statesp.py index d23fbd7be..81bb30751 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -93,17 +93,26 @@ 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 @@ -111,20 +120,20 @@ def _ssmatrix(data, axis=1): 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) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy