Skip to content

Check for symmetric matrices with machine precision #348

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

Merged
merged 4 commits into from
Dec 30, 2019
Merged
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
36 changes: 23 additions & 13 deletions control/mateqn.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
Author: Bjorn Olofsson
"""

from numpy import shape, size, array, asarray, copy, zeros, eye, dot
from numpy import shape, size, asarray, copy, zeros, eye, dot, \
finfo, inexact, atleast_2d
from scipy.linalg import eigvals, solve_discrete_are, solve
from .exception import ControlSlycot, ControlArgument
from .statesp import _ssmatrix
Expand Down Expand Up @@ -122,7 +123,7 @@ def lyap(A, Q, C=None, E=None):
if size(Q) > 1 and shape(Q)[0] != shape(Q)[1]:
raise ControlArgument("Q must be a quadratic matrix.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

# Solve the Lyapunov equation by calling Slycot function sb03md
Expand Down Expand Up @@ -188,7 +189,7 @@ def lyap(A, Q, C=None, E=None):
raise ControlArgument("E must be a square matrix with the same \
dimension as A.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

# Make sure we have access to the write slicot routine
Expand Down Expand Up @@ -309,7 +310,7 @@ def dlyap(A,Q,C=None,E=None):
if size(Q) > 1 and shape(Q)[0] != shape(Q)[1]:
raise ControlArgument("Q must be a quadratic matrix.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

# Solve the Lyapunov equation by calling the Slycot function sb03md
Expand Down Expand Up @@ -371,7 +372,7 @@ def dlyap(A,Q,C=None,E=None):
raise ControlArgument("E must be a square matrix with the same \
dimension as A.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

# Solve the generalized Lyapunov equation by calling Slycot
Expand Down Expand Up @@ -500,10 +501,10 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True):
size(B) == 1 and n > 1:
raise ControlArgument("Incompatible dimensions of B matrix.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

if not (asarray(R) == asarray(R).T).all():
if not _is_symmetric(R):
raise ControlArgument("R must be a symmetric matrix.")

# Create back-up of arrays needed for later computations
Expand Down Expand Up @@ -603,10 +604,10 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True):
size(S) == 1 and m > 1:
raise ControlArgument("Incompatible dimensions of S matrix.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

if not (asarray(R) == asarray(R).T).all():
if not _is_symmetric(R):
raise ControlArgument("R must be a symmetric matrix.")

# Create back-up of arrays needed for later computations
Expand Down Expand Up @@ -775,10 +776,10 @@ def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
size(B) == 1 and n > 1:
raise ControlArgument("Incompatible dimensions of B matrix.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

if not (asarray(R) == asarray(R).T).all():
if not _is_symmetric(R):
raise ControlArgument("R must be a symmetric matrix.")

# Create back-up of arrays needed for later computations
Expand Down Expand Up @@ -882,10 +883,10 @@ def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
size(S) == 1 and m > 1:
raise ControlArgument("Incompatible dimensions of S matrix.")

if not (asarray(Q) == asarray(Q).T).all():
if not _is_symmetric(Q):
raise ControlArgument("Q must be a symmetric matrix.")

if not (asarray(R) == asarray(R).T).all():
if not _is_symmetric(R):
raise ControlArgument("R must be a symmetric matrix.")

# Create back-up of arrays needed for later computations
Expand Down Expand Up @@ -960,3 +961,12 @@ def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
# Invalid set of input parameters
else:
raise ControlArgument("Invalid set of input parameters.")


def _is_symmetric(M):
M = atleast_2d(M)
if isinstance(M[0, 0], inexact):
eps = finfo(M.dtype).eps
return ((M - M.T) < eps).all()
else:
return (M == M.T).all()
Loading