Repository navigation
System norms #960
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
Closed
System norms #960
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6a0e136
New function for LTI system norm computation
a0fbc80
* Updated documentation of function norm
457c623
Update sysnorm.py
adcb486
Merge branch 'python-control:main' into system-norms
henriks76 fd076c5
New function for LTI system norm computation
0fe2c57
* Updated documentation of function norm
cdf4bab
Update sysnorm.py
34f9537
Do not track changes in VS Code setup.
b419f12
Lowered tolerances in tests.
9ecd594
Added:
14fce67
Merge branch 'system-norms' of https://github.com/henriks76/python-co…
5c38d4f
escape latex labels
bnavigator 99e56f8
Use Numpy API for pi instead of undocumented scipy.pi
bnavigator 2c32913
Update notebooks (no rerun, no output change)
bnavigator 85328d9
Merge pull request #965 from bnavigator/fix-examples
bnavigator 7ace4bc
New function for LTI system norm computation
5103448
* Updated documentation of function norm
4fb252e
Update sysnorm.py
108817c
Do not track changes in VS Code setup.
6683eb3
Lowered tolerances in tests.
32d38bf
Added:
6f810ba
Update sysnorm.py
793f0d6
Added:
12ba677
Merge branch 'system-norms' of https://github.com/henriks76/python-co…
7a5af50
Fixed merge error with __all__.
49f7e5f
Update sysnorm.py
247cff5
Merge branch 'system-norms' of https://github.com/henriks76/python-co…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update sysnorm.py
* Added additional tests and warning messages for systems with poles close to stability boundary * Added __all__ * Added more comments
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,9 @@ | |
|
|
||
| Functions for computing system norms. | ||
|
|
||
| Routines in this module: | ||
| Routine in this module: | ||
|
|
||
| norm() | ||
| norm | ||
|
|
||
| Created on Thu Dec 21 08:06:12 2023 | ||
| Author: Henrik Sandberg | ||
|
|
@@ -16,9 +16,11 @@ | |
|
|
||
| import control as ct | ||
|
|
||
| __all__ = ['norm'] | ||
|
|
||
| #------------------------------------------------------------------------------ | ||
|
|
||
| def norm(system, p=2, tol=1e-6): | ||
| def norm(system, p=2, tol=1e-6, print_warning=True): | ||
| """Computes norm of system. | ||
|
|
||
| Parameters | ||
|
|
@@ -30,11 +32,13 @@ def norm(system, p=2, tol=1e-6): | |
| tol : float | ||
| Relative tolerance for accuracy of L_infinity norm computation. Ignored | ||
| unless p='inf'. | ||
| print_warning : bool | ||
| Print warning message in case norm value may be uncertain. | ||
|
|
||
| Returns | ||
| ------- | ||
| norm : float | ||
| Norm of system | ||
| norm_value : float or NoneType | ||
| Norm value of system (float) or None if computation could not be completed. | ||
|
|
||
| Notes | ||
| ----- | ||
|
|
@@ -54,52 +58,114 @@ def norm(system, p=2, tol=1e-6): | |
| C = G.C | ||
| D = G.D | ||
|
|
||
| if p == 2: # H_2-norm | ||
| # | ||
| # H_2-norm computation | ||
| # | ||
| if p == 2: | ||
| # Continuous time case | ||
| if G.isctime(): | ||
| if (D != 0).any() or any(G.poles().real >= 0): | ||
| poles_real_part = G.poles().real | ||
| if any(np.isclose(poles_real_part, 0.0)): | ||
| if print_warning: | ||
| print("Warning: Poles close to, or on, the imaginary axis. Norm value may be uncertain.") | ||
| return float('inf') | ||
| elif (D != 0).any() or any(poles_real_part > 0.0): # System unstable or has direct feedthrough? | ||
| return float('inf') | ||
| else: | ||
| P = ct.lyap(A, B@B.T) | ||
| return np.sqrt(np.trace(C@P@C.T)) | ||
| try: | ||
| P = ct.lyap(A, B@B.T) | ||
| except Exception as e: | ||
| print(f"An error occurred solving the continuous time Lyapunov equation: {e}") | ||
| return None | ||
|
|
||
| # System is stable to reach this point, and P should be positive semi-definite. | ||
| # Test next is a precaution in case the Lyapunov equation is ill conditioned. | ||
| if any(la.eigvals(P) < 0.0): | ||
| if print_warning: | ||
| print("Warning: There appears to be poles close to the imaginary axis. Norm value may be uncertain.") | ||
| return float('inf') | ||
| else: | ||
| norm_value = np.sqrt(np.trace(C@P@C.T)) # Argument in sqrt should be non-negative | ||
| if np.isnan(norm_value): | ||
| print("Unknown error. Norm computation resulted in NaN.") | ||
| return None | ||
| else: | ||
| return norm_value | ||
|
|
||
| # Discrete time case | ||
| elif G.isdtime(): | ||
| if any(abs(G.poles()) >= 1): | ||
| poles_abs = abs(G.poles()) | ||
| if any(np.isclose(poles_abs, 1.0)): | ||
| if print_warning: | ||
| print("Warning: Poles close to, or on, the complex unit circle. Norm value may be uncertain.") | ||
| return float('inf') | ||
| elif any(poles_abs > 1.0): # System unstable? | ||
| return float('inf') | ||
| else: | ||
| P = ct.dlyap(A, B@B.T) | ||
| return np.sqrt(np.trace(C@P@C.T + D@D.T)) | ||
|
|
||
| elif p == "inf": # L_infinity-norm | ||
| try: | ||
| P = ct.dlyap(A, B@B.T) | ||
| except Exception as e: | ||
| print(f"An error occurred solving the discrete time Lyapunov equation: {e}") | ||
| return None | ||
|
|
||
| # System is stable to reach this point, and P should be positive semi-definite. | ||
| # Test next is a precaution in case the Lyapunov equation is ill conditioned. | ||
| if any(la.eigvals(P) < 0.0): | ||
| if print_warning: | ||
| print("Warning: There appears to be poles close to the complex unit circle. Norm value may be uncertain.") | ||
| return float('inf') | ||
| else: | ||
| norm_value = np.sqrt(np.trace(C@P@C.T + D@D.T)) # Argument in sqrt should be non-negative | ||
| if np.isnan(norm_value): | ||
| print("Unknown error. Norm computation resulted in NaN.") | ||
| return None | ||
| else: | ||
| return norm_value | ||
| # | ||
| # L_infinity-norm computation | ||
| # | ||
| elif p == "inf": | ||
| def _Hamilton_matrix(gamma): | ||
| """Constructs Hamiltonian matrix.""" | ||
| """Constructs Hamiltonian matrix. For internal use.""" | ||
| R = Ip*gamma**2 - D.T@D | ||
| invR = la.inv(R) | ||
| return np.block([[A+B@invR@D.T@C, B@invR@B.T], [-C.T@(Ip+D@invR@D.T)@C, -(A+B@invR@D.T@C).T]]) | ||
|
|
||
| if G.isdtime(): # Bilinear transformation of discrete time system to s-plane if no poles at |z|=1 or z=0 | ||
| # Discrete time case | ||
| # Use inverse bilinear transformation of discrete time system to s-plane if no poles on |z|=1 or z=0. | ||
| # Allows us to use test for continuous time systems next. | ||
| if G.isdtime(): | ||
| Ad = A | ||
| Bd = B | ||
| Cd = C | ||
| Dd = D | ||
| if any(np.isclose(abs(la.eigvals(Ad)), 1.0)): | ||
| if print_warning: | ||
| print("Warning: Poles close to, or on, the complex unit circle. Norm value may be uncertain.") | ||
| return float('inf') | ||
| elif any(np.isclose(la.eigvals(Ad), 0.0)): | ||
| print("L_infinity norm computation for discrete time system with pole(s) at z = 0 currently not supported.") | ||
| print("L_infinity norm computation for discrete time system with pole(s) at z=0 currently not supported.") | ||
| return None | ||
|
|
||
| # Inverse bilinear transformation | ||
| In = np.eye(len(Ad)) | ||
| Adinv = la.inv(Ad+In) | ||
| A = 2*(Ad-In)@Adinv | ||
| B = 2*Adinv@Bd | ||
| C = 2*Cd@Adinv | ||
| D = Dd - Cd@Adinv@Bd | ||
|
|
||
|
|
||
| # Continus time case | ||
| if any(np.isclose(la.eigvals(A).real, 0.0)): | ||
| if print_warning: | ||
| print("Warning: Poles close to, or on, imaginary axis. Norm value may be uncertain.") | ||
| return float('inf') | ||
|
|
||
| gaml = la.norm(D,ord=2) # Lower bound | ||
| gamu = max(1.0, 2.0*gaml) # Candidate upper bound | ||
| gaml = la.norm(D,ord=2) # Lower bound | ||
| gamu = max(1.0, 2.0*gaml) # Candidate upper bound | ||
| Ip = np.eye(len(D)) | ||
|
|
||
| while any(np.isclose(la.eigvals(_Hamilton_matrix(gamu)).real, 0.0)): # Find an upper bound | ||
| while any(np.isclose(la.eigvals(_Hamilton_matrix(gamu)).real, 0.0)): # Find actual upper bound | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered using SLICOT-Routines (already or to be wrapped by Slycot) optimized for certain Hamiltonian matrices instead of the generic scipy eigenvalue solver? (I didn't check if the SLICOT routines are applicable here) |
||
| gamu *= 2.0 | ||
|
|
||
| while (gamu-gaml)/gamu > tol: | ||
|
|
@@ -110,5 +176,5 @@ def _Hamilton_matrix(gamma): | |
| gamu = gam | ||
| return gam | ||
| else: | ||
| print("Norm computation for p =", p, "currently not supported.") | ||
| print(f"Norm computation for p={p} currently not supported.") | ||
| return None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use
printhere and in the error cases below. For warnings usewarnings, if it is an error, please keep raising the exception. And be more specific than justException.