Skip to content
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

Force positive definite Gramiam matrices when balancing #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion nengolib/signal/lyapunov.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import warnings

import numpy as np
from scipy.linalg import (solve_lyapunov, solve_discrete_lyapunov, eig,
cholesky, svd)
cholesky, svd, eigvalsh)
from scipy.optimize import fminbound

from nengolib.signal.discrete import cont2discrete
Expand Down Expand Up @@ -176,6 +178,22 @@ def balanced_transformation(sys):
R = control_gram(sys)
O = observe_gram(sys)

def force_positive_definite(M, neg_tol=1e-9, eps=1e-19):
eig = np.min(eigvalsh(M))
if -neg_tol < eig < 0:
cond = eps - eig
warnings.warn("Conditioning matrix by adding %s to diagonal due "
"to rounding errors" % cond)
M += np.eye(len(M)) * cond
return M

# Mathematically R and O should be positive definite.
# However, due to possible rounding errors in control_gram
# and observe_gram this will not always be the case.
# Also see https://github.com/scikit-learn/scikit-learn/issues/8252
R = force_positive_definite(R)
O = force_positive_definite(O)

LR = cholesky(R, lower=True)
LO = cholesky(O, lower=True)

Expand Down