|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""sysnorm.py |
| 3 | +
|
| 4 | +Functions for computing system norms. |
| 5 | +
|
| 6 | +Routine in this module: |
| 7 | +
|
| 8 | +norm |
| 9 | +
|
| 10 | +Created on Thu Dec 21 08:06:12 2023 |
| 11 | +Author: Henrik Sandberg |
| 12 | +""" |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import scipy as sp |
| 16 | +import numpy.linalg as la |
| 17 | +import warnings |
| 18 | + |
| 19 | +import control as ct |
| 20 | + |
| 21 | +__all__ = ['norm'] |
| 22 | + |
| 23 | +#------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +def _h2norm_slycot(sys, print_warning=True): |
| 26 | + """H2 norm of a linear system. For internal use. Requires Slycot. |
| 27 | +
|
| 28 | + See also |
| 29 | + -------- |
| 30 | + ``slycot.ab13bd`` : the Slycot routine that does the calculation |
| 31 | + https://github.com/python-control/Slycot/issues/199 : Post on issue with ``ab13bf`` |
| 32 | + """ |
| 33 | + |
| 34 | + try: |
| 35 | + from slycot import ab13bd |
| 36 | + except ImportError: |
| 37 | + ct.ControlSlycot("Can't find slycot module ``ab13bd``!") |
| 38 | + |
| 39 | + try: |
| 40 | + from slycot.exceptions import SlycotArithmeticError |
| 41 | + except ImportError: |
| 42 | + raise ct.ControlSlycot("Can't find slycot class ``SlycotArithmeticError``!") |
| 43 | + |
| 44 | + A, B, C, D = ct.ssdata(ct.ss(sys)) |
| 45 | + |
| 46 | + n = A.shape[0] |
| 47 | + m = B.shape[1] |
| 48 | + p = C.shape[0] |
| 49 | + |
| 50 | + dico = 'C' if sys.isctime() else 'D' # Continuous or discrete time |
| 51 | + jobn = 'H' # H2 (and not L2 norm) |
| 52 | + |
| 53 | + if n == 0: |
| 54 | + # ab13bd does not accept empty A, B, C |
| 55 | + if dico == 'C': |
| 56 | + if any(D.flat != 0): |
| 57 | + if print_warning: |
| 58 | + warnings.warn("System has a direct feedthrough term!", UserWarning) |
| 59 | + return float("inf") |
| 60 | + else: |
| 61 | + return 0.0 |
| 62 | + elif dico == 'D': |
| 63 | + return np.sqrt(D@D.T) |
| 64 | + |
| 65 | + try: |
| 66 | + norm = ab13bd(dico, jobn, n, m, p, A, B, C, D) |
| 67 | + except SlycotArithmeticError as e: |
| 68 | + if e.info == 3: |
| 69 | + if print_warning: |
| 70 | + warnings.warn("System has pole(s) on the stability boundary!", UserWarning) |
| 71 | + return float("inf") |
| 72 | + elif e.info == 5: |
| 73 | + if print_warning: |
| 74 | + warnings.warn("System has a direct feedthrough term!", UserWarning) |
| 75 | + return float("inf") |
| 76 | + elif e.info == 6: |
| 77 | + if print_warning: |
| 78 | + warnings.warn("System is unstable!", UserWarning) |
| 79 | + return float("inf") |
| 80 | + else: |
| 81 | + raise e |
| 82 | + return norm |
| 83 | + |
| 84 | +#------------------------------------------------------------------------------ |
| 85 | + |
| 86 | +def norm(system, p=2, tol=1e-6, print_warning=True, method=None): |
| 87 | + """Computes norm of system. |
| 88 | + |
| 89 | + Parameters |
| 90 | + ---------- |
| 91 | + system : LTI (:class:`StateSpace` or :class:`TransferFunction`) |
| 92 | + System in continuous or discrete time for which the norm should be computed. |
| 93 | + p : int or str |
| 94 | + Type of norm to be computed. ``p=2`` gives the H2 norm, and ``p='inf'`` gives the L-infinity norm. |
| 95 | + tol : float |
| 96 | + Relative tolerance for accuracy of L-infinity norm computation. Ignored |
| 97 | + unless p='inf'. |
| 98 | + print_warning : bool |
| 99 | + Print warning message in case norm value may be uncertain. |
| 100 | + method : str, optional |
| 101 | + Set the method used for computing the result. Current methods are |
| 102 | + 'slycot' and 'scipy'. If set to None (default), try 'slycot' first |
| 103 | + and then 'scipy'. |
| 104 | + |
| 105 | + Returns |
| 106 | + ------- |
| 107 | + norm_value : float |
| 108 | + Norm value of system. |
| 109 | + |
| 110 | + Notes |
| 111 | + ----- |
| 112 | + Does not yet compute the L-infinity norm for discrete time systems with pole(s) in z=0 unless Slycot is used. |
| 113 | + |
| 114 | + Examples |
| 115 | + -------- |
| 116 | + >>> Gc = ct.tf([1], [1, 2, 1]) |
| 117 | + >>> ct.norm(Gc, 2) |
| 118 | + 0.5000000000000001 |
| 119 | + >>> ct.norm(Gc, 'inf', tol=1e-11, method='scipy') |
| 120 | + 1.000000000007276 |
| 121 | + """ |
| 122 | + |
| 123 | + if not isinstance(system, (ct.StateSpace, ct.TransferFunction)): |
| 124 | + raise TypeError('Parameter ``system``: must be a ``StateSpace`` or ``TransferFunction``') |
| 125 | + |
| 126 | + G = ct.ss(system) |
| 127 | + A = G.A |
| 128 | + B = G.B |
| 129 | + C = G.C |
| 130 | + D = G.D |
| 131 | + |
| 132 | + # Decide what method to use |
| 133 | + method = ct.mateqn._slycot_or_scipy(method) |
| 134 | + |
| 135 | + # ------------------- |
| 136 | + # H2 norm computation |
| 137 | + # ------------------- |
| 138 | + if p == 2: |
| 139 | + # -------------------- |
| 140 | + # Continuous time case |
| 141 | + # -------------------- |
| 142 | + if G.isctime(): |
| 143 | + |
| 144 | + # Check for cases with infinite norm |
| 145 | + poles_real_part = G.poles().real |
| 146 | + if any(np.isclose(poles_real_part, 0.0)): # Poles on imaginary axis |
| 147 | + if print_warning: |
| 148 | + warnings.warn("Poles close to, or on, the imaginary axis. Norm value may be uncertain.", UserWarning) |
| 149 | + return float('inf') |
| 150 | + elif any(poles_real_part > 0.0): # System unstable |
| 151 | + if print_warning: |
| 152 | + warnings.warn("System is unstable!", UserWarning) |
| 153 | + return float('inf') |
| 154 | + elif any(D.flat != 0): # System has direct feedthrough |
| 155 | + if print_warning: |
| 156 | + warnings.warn("System has a direct feedthrough term!", UserWarning) |
| 157 | + return float('inf') |
| 158 | + |
| 159 | + else: |
| 160 | + # Use slycot, if available, to compute (finite) norm |
| 161 | + if method == 'slycot': |
| 162 | + return _h2norm_slycot(G, print_warning) |
| 163 | + |
| 164 | + # Else use scipy |
| 165 | + else: |
| 166 | + P = ct.lyap(A, B@B.T, method=method) # Solve for controllability Gramian |
| 167 | + |
| 168 | + # System is stable to reach this point, and P should be positive semi-definite. |
| 169 | + # Test next is a precaution in case the Lyapunov equation is ill conditioned. |
| 170 | + if any(la.eigvals(P).real < 0.0): |
| 171 | + if print_warning: |
| 172 | + warnings.warn("There appears to be poles close to the imaginary axis. Norm value may be uncertain.", UserWarning) |
| 173 | + return float('inf') |
| 174 | + else: |
| 175 | + norm_value = np.sqrt(np.trace(C@P@C.T)) # Argument in sqrt should be non-negative |
| 176 | + if np.isnan(norm_value): |
| 177 | + raise ct.ControlArgument("Norm computation resulted in NaN.") |
| 178 | + else: |
| 179 | + return norm_value |
| 180 | + |
| 181 | + # ------------------ |
| 182 | + # Discrete time case |
| 183 | + # ------------------ |
| 184 | + elif G.isdtime(): |
| 185 | + |
| 186 | + # Check for cases with infinite norm |
| 187 | + poles_abs = abs(G.poles()) |
| 188 | + if any(np.isclose(poles_abs, 1.0)): # Poles on imaginary axis |
| 189 | + if print_warning: |
| 190 | + warnings.warn("Poles close to, or on, the complex unit circle. Norm value may be uncertain.", UserWarning) |
| 191 | + return float('inf') |
| 192 | + elif any(poles_abs > 1.0): # System unstable |
| 193 | + if print_warning: |
| 194 | + warnings.warn("System is unstable!", UserWarning) |
| 195 | + return float('inf') |
| 196 | + |
| 197 | + else: |
| 198 | + # Use slycot, if available, to compute (finite) norm |
| 199 | + if method == 'slycot': |
| 200 | + return _h2norm_slycot(G, print_warning) |
| 201 | + |
| 202 | + # Else use scipy |
| 203 | + else: |
| 204 | + P = ct.dlyap(A, B@B.T, method=method) |
| 205 | + |
| 206 | + # System is stable to reach this point, and P should be positive semi-definite. |
| 207 | + # Test next is a precaution in case the Lyapunov equation is ill conditioned. |
| 208 | + if any(la.eigvals(P).real < 0.0): |
| 209 | + if print_warning: |
| 210 | + warnings.warn("Warning: There appears to be poles close to the complex unit circle. Norm value may be uncertain.", UserWarning) |
| 211 | + return float('inf') |
| 212 | + else: |
| 213 | + norm_value = np.sqrt(np.trace(C@P@C.T + D@D.T)) # Argument in sqrt should be non-negative |
| 214 | + if np.isnan(norm_value): |
| 215 | + raise ct.ControlArgument("Norm computation resulted in NaN.") |
| 216 | + else: |
| 217 | + return norm_value |
| 218 | + |
| 219 | + # --------------------------- |
| 220 | + # L-infinity norm computation |
| 221 | + # --------------------------- |
| 222 | + elif p == "inf": |
| 223 | + |
| 224 | + # Check for cases with infinite norm |
| 225 | + poles = G.poles() |
| 226 | + if G.isdtime(): # Discrete time |
| 227 | + if any(np.isclose(abs(poles), 1.0)): # Poles on unit circle |
| 228 | + if print_warning: |
| 229 | + warnings.warn("Poles close to, or on, the complex unit circle. Norm value may be uncertain.", UserWarning) |
| 230 | + return float('inf') |
| 231 | + else: # Continuous time |
| 232 | + if any(np.isclose(poles.real, 0.0)): # Poles on imaginary axis |
| 233 | + if print_warning: |
| 234 | + warnings.warn("Poles close to, or on, the imaginary axis. Norm value may be uncertain.", UserWarning) |
| 235 | + return float('inf') |
| 236 | + |
| 237 | + # Use slycot, if available, to compute (finite) norm |
| 238 | + if method == 'slycot': |
| 239 | + return ct.linfnorm(G, tol)[0] |
| 240 | + |
| 241 | + # Else use scipy |
| 242 | + else: |
| 243 | + |
| 244 | + # ------------------ |
| 245 | + # Discrete time case |
| 246 | + # ------------------ |
| 247 | + # Use inverse bilinear transformation of discrete time system to s-plane if no poles on |z|=1 or z=0. |
| 248 | + # Allows us to use test for continuous time systems next. |
| 249 | + if G.isdtime(): |
| 250 | + Ad = A |
| 251 | + Bd = B |
| 252 | + Cd = C |
| 253 | + Dd = D |
| 254 | + if any(np.isclose(la.eigvals(Ad), 0.0)): |
| 255 | + raise ct.ControlArgument("L-infinity norm computation for discrete time system with pole(s) in z=0 currently not supported unless Slycot installed.") |
| 256 | + |
| 257 | + # Inverse bilinear transformation |
| 258 | + In = np.eye(len(Ad)) |
| 259 | + Adinv = la.inv(Ad+In) |
| 260 | + A = 2*(Ad-In)@Adinv |
| 261 | + B = 2*Adinv@Bd |
| 262 | + C = 2*Cd@Adinv |
| 263 | + D = Dd - Cd@Adinv@Bd |
| 264 | + |
| 265 | + # -------------------- |
| 266 | + # Continuous time case |
| 267 | + # -------------------- |
| 268 | + def _Hamilton_matrix(gamma): |
| 269 | +
10000
"""Constructs Hamiltonian matrix. For internal use.""" |
| 270 | + R = Ip*gamma**2 - D.T@D |
| 271 | + invR = la.inv(R) |
| 272 | + 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]]) |
| 273 | + |
| 274 | + gaml = la.norm(D,ord=2) # Lower bound |
| 275 | + gamu = max(1.0, 2.0*gaml) # Candidate upper bound |
| 276 | + Ip = np.eye(len(D)) |
| 277 | + |
| 278 | + while any(np.isclose(la.eigvals(_Hamilton_matrix(gamu)).real, 0.0)): # Find actual upper bound |
| 279 | + gamu *= 2.0 |
| 280 | + |
| 281 | + while (gamu-gaml)/gamu > tol: |
| 282 | + gam = (gamu+gaml)/2.0 |
| 283 | + if any(np.isclose(la.eigvals(_Hamilton_matrix(gam)).real, 0.0)): |
| 284 | + gaml = gam |
| 285 | + else: |
| 286 | + gamu = gam |
| 287 | + return gam |
| 288 | + |
| 289 | + # ---------------------- |
| 290 | + # Other norm computation |
| 291 | + # ---------------------- |
| 292 | + else: |
| 293 | + raise ct.ControlArgument(f"Norm computation for p={p} currently not supported.") |
| 294 | + |
0 commit comments