test_SS_algorithms¶
wield.control.algorithms.statespace.dense.test.test_SS_algorithms
This is a pytest module needing documentation
Functions
|
This is a pytest needing documentation |
|
This is a pytest needing documentation |
|
This is a pytest needing documentation |
Details
- test_2x2_ABCDE_c2r(plot, tpath)[source][github]¶
This is a pytest needing documentation
code
1def test_2x2_ABCDE_c2r(plot, tpath): 2 F_Hz = logspaced(0.1, 100, 100) 3 4 A = np.array([[1, 10], [0, 10]]) 5 B = np.array([[1], [1]]) 6 C = np.array( 7 [ 8 [1, 1], 9 ] 10 ) 11 D = np.array( 12 [ 13 [0], 14 ] 15 ) 16 E = np.array([[1j, 0], [0, 1j]]) 17 A2, B2, C2, D2, E2 = SS.DSS_c2r(A, B, C, D, E, with_imag=True) 18 # TODO make this an actual test 19 h1 = SS.xfer_algorithms.ss2xfer( 20 A, 21 B, 22 C, 23 D, 24 E=E, 25 F_Hz=F_Hz, 26 ) 27 h2 = SS.xfer_algorithms.ss2xfer( 28 A2, 29 B2, 30 C2, 31 D2, 32 E=E2, 33 F_Hz=F_Hz, 34 ) 35 np.testing.assert_almost_equal(h1, h2) 36 if plot: 37 axB = mplfigB(Nrows=1) 38 axB.ax0.loglog(F_Hz, abs(h1)) 39 axB.ax0.loglog(F_Hz, abs(h2)) 40 axB.save(path.join(tpath, "plot.pdf")) 41 return
pytest information
This code is wrapped in a pytest function using conventions detailed in Pytest Conventions. The full name of this test, as known by the documentation, is:
wield.control.algorithms.statespace.dense.test.test_SS_algorithms.test_2x2_ABCDE_c2rThe full name is useful when building documentation, to link a reference to this page using
:func:`name`, or directly include it with an autofunction directive. The collapse nodes below show every instance of the test run. There may only be one, but if the test was run multiple times through pytest parametrizations, the list can be longer.test_2x2_ABCDE_c2r
output
status: skipped duration: 0.001s captured errors: plot = False tpath = '/builds/wield-control/docs/maketests/test_results/test_SS_algorithms.py/test_2x2_ABCDE_c2r' def test_2x2_ABCDE_c2r(plot, tpath): F_Hz = logspaced(0.1, 100, 100) A = np.array([[1, 10], [0, 10]]) B = np.array([[1], [1]]) C = np.array( [ [1, 1], ] ) D = np.array( [ [0], ] ) E = np.array([[1j, 0], [0, 1j]]) A2, B2, C2, D2, E2 = SS.DSS_c2r(A, B, C, D, E, with_imag=True) # TODO make this an actual test > h1 = SS.xfer_algorithms.ss2xfer( A, B, C, D, E=E, F_Hz=F_Hz, ) ../../src/wield/control/algorithms/statespace/dense/test/test_SS_algorithms.py:120: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ A = array([[ 1, 10], [ 0, 10]]), B = array([[1], [1]]) C = array([[1, 1]]), D = array([[0]]) E = array([[0.+1.j, 0.+0.j], [0.+0.j, 0.+1.j]]) F_Hz = array([1.00000000e-01, 1.07226722e-01, 1.14975700e-01, 1.23284674e-01, 1.32194115e-01, 1.41747416e-01, 1.519911...6.57933225e+01, 7.05480231e+01, 7.56463328e+01, 8.11130831e+01, 8.69749003e+01, 9.32603347e+01, 1.00000000e+02]) idx_in = None, idx_out = None def ss2xfer(A, B, C, D, E=None, F_Hz=None, idx_in=None, idx_out=None): > return ss2response_siso( A, B, C, D, E, s = 2j * np.pi * F_Hz, idx_in=idx_in, idx_out=idx_out ) E TypeError: ss2response_siso() got an unexpected keyword argument 's' ../../src/wield/control/algorithms/statespace/dense/xfer_algorithms.py:20: TypeError
- test_ABCDE(tpath)[source][github]¶
This is a pytest needing documentation
code
1def test_ABCDE(tpath): 2 print() 3 F_Hz = logspaced(0.1, 100, 1000) 4 5 z, p, k = ((-1, -5), (-1 + 5j, -1 - 5j, -3), 200) 6 z = np.asarray(z) 7 p = np.asarray(p) 8 9 A, B, C, D, E = SS.zpk2cDSS(z, p, k, mode="CCF", rescale=3j) 10 A, B, C, D, E = SS.DSS_c2r(A, B, C, D, E) 11 print("A", A) 12 print("B", B) 13 print("C", C) 14 print("D", D) 15 print("E", E) 16 reduced = True 17 while reduced: 18 print("REDUCE") 19 A, B, C, D, E, reduced = SS.reduce_modal(A, B, C, D, E, mode="O") 20 # if not reduced: 21 # break 22 # A, B, C, D, E, reduced = reduce_modal(A, B, C, D, E, mode = 'C') 23 24 w, vr = scipy.linalg.eig(A, E, left=False, right=True) 25 print("EIGS", w) 26 print("A", A) 27 print("E", E) 28 print("B", B) 29 30 axB = mplfigB(Nrows=2) 31 w, h = scipy.signal.freqs_zpk(z * 2 * np.pi, p * 2 * np.pi, k, F_Hz * 2 * np.pi) 32 axB.ax0.loglog(F_Hz, abs(h)) 33 axB.ax0.loglog( 34 F_Hz, 35 abs( 36 SS.xfer_algorithms.ss2xfer( 37 A, 38 B, 39 C, 40 D, 41 E=E, 42 F_Hz=F_Hz, 43 ) 44 ), 45 ) 46 ratio = h / SS.xfer_algorithms.ss2xfer( 47 A, 48 B, 49 C, 50 D, 51 E=E, 52 F_Hz=F_Hz, 53 ) 54 axB.ax1.semilogx(F_Hz, abs(ratio)) 55 axB.ax1.semilogx(F_Hz, np.angle(ratio)) 56 axB.save(path.join(tpath, "plot.pdf")) 57 return
pytest information
This code is wrapped in a pytest function using conventions detailed in Pytest Conventions. The full name of this test, as known by the documentation, is:
wield.control.algorithms.statespace.dense.test.test_SS_algorithms.test_ABCDEThe full name is useful when building documentation, to link a reference to this page using
:func:`name`, or directly include it with an autofunction directive. The collapse nodes below show every instance of the test run. There may only be one, but if the test was run multiple times through pytest parametrizations, the list can be longer.test_ABCDE
output
A [[ 0.00000000e+00 0.00000000e+00 6.03603475e+03 -0.00000000e+00 -2.12132034e+00 -0.00000000e+00] [ 0.00000000e+00 0.00000000e+00 -2.13162821e-14 -2.12132034e+00 -0.00000000e+00 -8.39206242e+02] [ 0.00000000e+00 0.00000000e+00 -3.14159265e+01 -0.00000000e+00 -1.50000000e+00 -0.00000000e+00] [ 0.00000000e+00 2.12132034e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 6.03603475e+03] [ 2.12132034e+00 0.00000000e+00 8.39206242e+02 0.00000000e+00 0.00000000e+00 -2.13162821e-14] [ 0.00000000e+00 1.50000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 -3.14159265e+01]] B [[-12124.02697577] [ 0. ] [ 200. ] [ -0. ] [ -5026.54824574] [ 0. ]] C [[ 0. 0. 1. -0. -0. -0.]] D [[0.]] E [[ 1. 0. 0. -0. -0. -0.] [ 0. 1. 0. -0. -0. -0.] [ 0. 0. 1. -0. -0. -0.] [ 0. 0. 0. 1. 0. 0.] [ 0. 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 0. 1.]] REDUCE captured errors: tpath = '/builds/wield-control/docs/maketests/test_results/test_SS_algorithms.py/test_ABCDE' def test_ABCDE(tpath): print() F_Hz = logspaced(0.1, 100, 1000) z, p, k = ((-1, -5), (-1 + 5j, -1 - 5j, -3), 200) z = np.asarray(z) p = np.asarray(p) A, B, C, D, E = SS.zpk2cDSS(z, p, k, mode="CCF", rescale=3j) A, B, C, D, E = SS.DSS_c2r(A, B, C, D, E) print("A", A) print("B", B) print("C", C) print("D", D) print("E", E) reduced = True while reduced: print("REDUCE") > A, B, C, D, E, reduced = SS.reduce_modal(A, B, C, D, E, mode="O") ../../src/wield/control/algorithms/statespace/dense/test/test_SS_algorithms.py:61: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ../../src/wield/control/algorithms/statespace/dense/ss_algorithms.py:59: in reduce_modal v_pairs = eigspaces_right_real(A, E, tol=tol) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ A = array([[ 0.00000000e+00, 0.00000000e+00, 6.03603475e+03, -0.00000000e+00, -2.12132034e+00, -0.00000000e+00],... [ 0.00000000e+00, 1.50000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, -3.14159265e+01]]) B = array([[ 1., 0., 0., -0., -0., -0.], [ 0., 1., 0., -0., -0., -0.], [ 0., 0., 1., -0., -0., -0.], [ 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 0., 1.]]) tol = 1e-07 def eigspaces_right_real(A, B=None, tol=1e-9): v_pairs = eigspaces_right(A, B=B, tol=tol) v_pairs_re = [] v_pairs_im = [] w_im = [] for eigs, evects in v_pairs: eigv = np.mean(eigs) if abs(eigv.imag) < tol: # remove the imaginary part to the eigenvectors assert np.all(np.sum(evects.imag ** 2, axis=1) < tol) v_pairs_re.append((eigs, evects.real)) continue v_pairs_im.append((eigs, evects)) w_im.append(eigv) # this finds conjugate pairs w_near = TFmath.nearest_idx(w_im) v_pairs_im2 = [] for idx_fr, idx_to in enumerate(w_near): if idx_to is None or w_near[idx_to] is None: continue if idx_fr is None or w_near[idx_fr] is None: continue if w_near[idx_to] != idx_fr: continue # unique conjugate pair w_near[idx_to] = None w_near[idx_fr] = None eigs1, eigv1 = v_pairs_im[idx_to] eigs2, eigv2 = v_pairs_im[idx_fr] if w_im[idx_to].imag > 0: eigs_use = eigs1 else: eigs_use = eigs2 v_pairs_im2.append((eigs_use, np.hstack([eigv1, eigv2]))) v_pairs_im3 = [] for eigs, evects in v_pairs_im2: # TODO, it may be the the SVD should be used here # this may rely on r being rank-revealing q, r = scipy.linalg.qr(evects.imag.T) # check that the imaginary space is actually reduced idx_cut = r.shape[0] // 2 assert np.all(np.sum(r[idx_cut:] ** 2, axis=1) < tol) # same check as above (redundant) > assert np.all(np.sum((evects.imag @ q[idx_cut:].T) ** 2, axis=1) < tol) E AssertionError ../../src/wield/control/algorithms/statespace/dense/eig_algorithms.py:106: AssertionError
- test_eig_snip(tpath)[source][github]¶
This is a pytest needing documentation
code
1def test_eig_snip(tpath): 2 print() 3 F_Hz = logspaced(0.1, 100, 100) 4 5 Z = np.array([[0, 0], [0, 0]]) 6 A = np.array([[1, -1], [1, 1]]) 7 E = np.array([[1, 0], [0, 1]]) 8 A = np.block([[A, A], [-A, A]]) 9 E = np.block([[Z, E], [-E, Z]]) 10 11 tol = 1e-9 12 v_pairs = SS.eigspaces_right(A, E, tol=tol) 13 A_projections = [] 14 E_projections = [] 15 print([eig for eig, ev in v_pairs]) 16 for eigs, evects in v_pairs[:1]: 17 # may need pivoting on to work correctly for projections 18 print("eigs", eigs) 19 A_project = evects[:, :1] 20 # A_project = A_project / np.sum(A_project**2 , axis = 1) 21 A_projections.append(A_project) 22 23 A_projections = np.hstack(A_projections) 24 Aq, Ar = scipy.linalg.qr(A_projections, mode="economic") 25 idx_split = Aq.shape[0] - Ar.shape[0] 26 A_SS_projection = np.diag(np.ones(A.shape[0])) - Aq @ Aq.T.conjugate() 27 Aq, Ar, Ap = scipy.linalg.qr(A_SS_projection, mode="economic", pivoting=True) 28 for idx in range(Aq.shape[0]): 29 if np.sum(Ar[-1 - idx] ** 2) < tol: 30 continue 31 else: 32 break 33 idx_split = Aq.shape[0] - idx 34 p_project_imU = Aq[:, :idx_split] 35 p_project_kerU = Aq[:, idx_split:] 36 37 E_projections = E @ p_project_kerU 38 Eq, Er = scipy.linalg.qr(E_projections, mode="economic") 39 40 E_SS_projection = np.diag(np.ones(A.shape[0])) - Eq @ Eq.T.conjugate() 41 Eq, Er, Ep = scipy.linalg.qr(E_SS_projection, mode="economic", pivoting=True) 42 p_project_im = Eq[:idx_split] 43 p_project_ker = Eq[idx_split:] 44 45 A = p_project_im @ A @ p_project_imU 46 E = p_project_im @ E @ p_project_imU 47 48 w, vr = scipy.linalg.eig(A, E, left=False, right=True) 49 print("EIGS", w) 50 print("EIGV") 51 print(vr) 52 53 return
pytest information
This code is wrapped in a pytest function using conventions detailed in Pytest Conventions. The full name of this test, as known by the documentation, is:
wield.control.algorithms.statespace.dense.test.test_SS_algorithms.test_eig_snipThe full name is useful when building documentation, to link a reference to this page using
:func:`name`, or directly include it with an autofunction directive. The collapse nodes below show every instance of the test run. There may only be one, but if the test was run multiple times through pytest parametrizations, the list can be longer.test_eig_snip
output
[[(2.0000000000000004+0j), (2+0j)], [(1.1102230246251568e-16+2j)], [(1.1102230246251568e-16-2j)]] eigs [(2.0000000000000004+0j), (2+0j)] EIGS [ 1.92296269e-16+2.00000000e+00j 2.00000000e+00-3.33066907e-16j -4.07921987e-16-2.00000000e+00j] EIGV [[ 1.96323178e-01+4.59844767e-01j -4.75452190e-02+7.05506522e-01j 1.64959332e-01-4.72004681e-01j] [ 1.96323178e-01+4.59844767e-01j 4.75452190e-02-7.05506522e-01j 1.64959332e-01-4.72004681e-01j] [-6.50318706e-01+2.77642901e-01j -6.22630177e-17+5.93697117e-17j -6.67515421e-01-2.33287725e-01j]]