Skip to content

fixed StateSpace for complex inputs #468

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions control/statesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

# Define module default parameter values
_statesp_defaults = {
'statesp.use_numpy_matrix': True,
'statesp.use_numpy_matrix': False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the default setting here. We have a road map in mind when this should happen and it should be independent of the support for complex types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, actually #438 takes complex inputs into account already!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will change this back

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, actually #438 takes complex inputs into account already!

Oh, I see, should I close my PR?

'statesp.default_dt': None,
'statesp.remove_useless_states': True,
}
Expand Down Expand Up @@ -98,11 +98,11 @@ def _ssmatrix(data, axis=1):
# Convert the data into an array or matrix, as configured
# If data is passed as a string, use (deprecated?) matrix constructor
if config.defaults['statesp.use_numpy_matrix']:
arr = np.matrix(data, dtype=float)
arr = np.matrix(data, dtype=complex)
elif isinstance(data, str):
arr = np.array(np.matrix(data, dtype=float))
arr = np.array(np.matrix(data, dtype=complex))
else:
arr = np.array(data, dtype=float)
arr = np.array(data, dtype=complex)
ndim = arr.ndim
shape = arr.shape

Expand Down
42 changes: 42 additions & 0 deletions examples/diagonalize_space_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from matplotlib import pyplot as plt

import numpy as np
from scipy.linalg import eig
from control import step_response, StateSpace
# Example of diagonalization of StateSpace representation
# In this example the diagonalized system has complex values on the matrices

def diagonalize(A, B, C, D):
eigenvectors_matrix = eig(A)[1]
A = np.matmul(np.matmul(np.linalg.inv(eigenvectors_matrix), A), eigenvectors_matrix)
B = np.matmul(np.linalg.inv(eigenvectors_matrix), B)
C = np.matmul(C, eigenvectors_matrix)
print(A)
print(B)
print(C)
print(D)
return A, B, C, D


def main():
A = np.array([[0, 0, 1000], [0.1, 0, 0], [0, 0.5, 0.5]])
B = np.array([[0], [0], [1]])
C = np.array([[0, 0, 1]])
D = np.array([[0]])

Aw, Bw, Cw, Dw = diagonalize(A, B, C, D)

system = StateSpace(A, B, C, D, 1)
system_w = StateSpace(Aw, Bw, Cw, Dw, 1)

length = 20
plt.figure(figsize=(14, 9), dpi=100)
response_w = step_response(system_w, T=np.arange(length))
response = step_response(system, T=np.arange(length))
plt.plot(response[0], response[1], color="red", label="System")
plt.plot(response_w[0], response_w[1], color="blue", label="System W")
plt.xticks(np.arange(length))
plt.legend()
plt.savefig("diagonalization.png")

main()