|
9 | 9 | from numpy.testing import assert_allclose |
10 | 10 | from scipy.linalg import schur |
11 | 11 |
|
12 | | -from slycot import math, mb03rd, mb03vd, mb03vy, mb03wd, mb05md, mb05nd |
13 | | -from slycot.exceptions import SlycotArithmeticError, SlycotResultWarning |
| 12 | +from slycot import math, mb02ed, mb03rd, mb03vd, mb03vy, mb03wd, mb05md, mb05nd |
| 13 | +from slycot.exceptions import SlycotArithmeticError, SlycotResultWarning, SlycotParameterError |
14 | 14 |
|
15 | 15 | from .test_exceptions import assert_docstring_parse |
16 | 16 |
|
17 | 17 |
|
| 18 | +def test_mb02ed_ex(): |
| 19 | + """Test MB02ED using the example given in the MB02ED SLICOT Documentation""" |
| 20 | + n = 3 |
| 21 | + k = 3 |
| 22 | + nrhs = 2 |
| 23 | + TYPET = "C" |
| 24 | + T = np.array( |
| 25 | + [ |
| 26 | + [3.0000, 1.0000, 0.2000], |
| 27 | + [1.0000, 4.0000, 0.4000], |
| 28 | + [0.2000, 0.4000, 5.0000], |
| 29 | + [0.1000, 0.1000, 0.2000], |
| 30 | + [0.2000, 0.0400, 0.0300], |
| 31 | + [0.0500, 0.2000, 0.1000], |
| 32 | + [0.1000, 0.0300, 0.1000], |
| 33 | + [0.0400, 0.0200, 0.2000], |
| 34 | + [0.0100, 0.0300, 0.0200], |
| 35 | + ] |
| 36 | + ) |
| 37 | + B = np.array( |
| 38 | + [ |
| 39 | + [1.0000, 2.0000], |
| 40 | + [1.0000, 2.0000], |
| 41 | + [1.0000, 2.0000], |
| 42 | + [1.0000, 2.0000], |
| 43 | + [1.0000, 2.0000], |
| 44 | + [1.0000, 2.0000], |
| 45 | + [1.0000, 2.0000], |
| 46 | + [1.0000, 2.0000], |
| 47 | + [1.0000, 2.0000], |
| 48 | + ] |
| 49 | + ) |
| 50 | + X = np.array( |
| 51 | + [ |
| 52 | + [0.2408, 0.4816], |
| 53 | + [0.1558, 0.3116], |
| 54 | + [0.1534, 0.3068], |
| 55 | + [0.2302, 0.4603], |
| 56 | + [0.1467, 0.2934], |
| 57 | + [0.1537, 0.3075], |
| 58 | + [0.2349, 0.4698], |
| 59 | + [0.1498, 0.2995], |
| 60 | + [0.1653, 0.3307], |
| 61 | + ] |
| 62 | + ) |
| 63 | + |
| 64 | + result,_ = mb02ed(T=T, B=B, n=n, k=k, typet=TYPET, nrhs=nrhs) |
| 65 | + np.testing.assert_almost_equal(result, X, decimal=4) |
| 66 | + |
| 67 | +def test_mb02ed_parameter_errors(): |
| 68 | + """Test for errors in the input parameters of MB02ED""" |
| 69 | + n = 3 |
| 70 | + k = 3 |
| 71 | + nrhs = 2 |
| 72 | + TYPET = "C" |
| 73 | + T = np.array( |
| 74 | + [ |
| 75 | + [3.0000, 1.0000, 0.2000], |
| 76 | + [1.0000, 4.0000, 0.4000], |
| 77 | + [0.2000, 0.4000, 5.0000], |
| 78 | + [0.1000, 0.1000, 0.2000], |
| 79 | + [0.2000, 0.0400, 0.0300], |
| 80 | + [0.0500, 0.2000, 0.1000], |
| 81 | + [0.1000, 0.0300, 0.1000], |
| 82 | + [0.0400, 0.0200, 0.2000], |
| 83 | + [0.0100, 0.0300, 0.0200], |
| 84 | + ] |
| 85 | + ) |
| 86 | + B = np.array( |
| 87 | + [ |
| 88 | + [1.0000, 2.0000], |
| 89 | + [1.0000, 2.0000], |
| 90 | + [1.0000, 2.0000], |
| 91 | + [1.0000, 2.0000], |
| 92 | + [1.0000, 2.0000], |
| 93 | + [1.0000, 2.0000], |
| 94 | + [1.0000, 2.0000], |
| 95 | + [1.0000, 2.0000], |
| 96 | + [1.0000, 2.0000], |
| 97 | + ] |
| 98 | + ) |
| 99 | + X = np.array( |
| 100 | + [ |
| 101 | + [0.2408, 0.4816], |
| 102 | + [0.1558, 0.3116], |
| 103 | + [0.1534, 0.3068], |
| 104 | + [0.2302, 0.4603], |
| 105 | + [0.1467, 0.2934], |
| 106 | + [0.1537, 0.3075], |
| 107 | + [0.2349, 0.4698], |
| 108 | + [0.1498, 0.2995], |
| 109 | + [0.1653, 0.3307], |
| 110 | + ] |
| 111 | + ) |
| 112 | + |
| 113 | + # Test for wrong parameter typet |
| 114 | + with pytest.raises(expected_exception=SlycotParameterError, match='typet must be either "R" or "C"') as cm: |
| 115 | + mb02ed(T=T, B=B, n=n, k=k, typet='U', nrhs=nrhs) |
| 116 | + assert cm.value.info == -1 |
| 117 | + #Test for negative number of columns |
| 118 | + with pytest.raises(expected_exception=SlycotParameterError, match="k must be >= 0") as cm: |
| 119 | + mb02ed(T=T, B=B, n=n, k=-1, typet=TYPET, nrhs=nrhs) |
| 120 | + assert cm.value.info == -2 |
| 121 | + #Test for negative number of blocks |
| 122 | + with pytest.raises(expected_exception=SlycotParameterError, match="n must be >= 0") as cm: |
| 123 | + mb02ed(T=T, B=B, n=-1, k=k, typet=TYPET, nrhs=nrhs) |
| 124 | + assert cm.value.info == -3 |
| 125 | + #Test for negative number of right hand sides |
| 126 | + with pytest.raises(expected_exception=SlycotParameterError, match="nrhs must be >= 0") as cm: |
| 127 | + mb02ed(T=T, B=B, n=n, k=k, typet=TYPET, nrhs=-1) |
| 128 | + assert cm.value.info == -4 |
| 129 | + |
| 130 | + |
| 131 | +def test_mb02ed_matrix_error(): |
| 132 | + """Test for a negative definite input matrix in MB02ED""" |
| 133 | + n = 3 |
| 134 | + k = 3 |
| 135 | + nrhs = 2 |
| 136 | + TYPET = "C" |
| 137 | + T = np.array( |
| 138 | + [ |
| 139 | + [3.0000, 1.0000, 0.2000], |
| 140 | + [1.0000, 4.0000, 0.4000], |
| 141 | + [0.2000, 0.4000, 5.0000], |
| 142 | + [0.1000, 0.1000, 0.2000], |
| 143 | + [0.2000, 0.0400, 0.0300], |
| 144 | + [0.0500, 0.2000, 0.1000], |
| 145 | + [0.1000, 0.0300, 0.1000], |
| 146 | + [0.0400, 0.0200, 0.2000], |
| 147 | + [0.0100, 0.0300, 0.0200], |
| 148 | + ] |
| 149 | + ) |
| 150 | + # Create a negative definite matrix |
| 151 | + T = -1 * T |
| 152 | + B = np.array( |
| 153 | + [ |
| 154 | + [1.0000, 2.0000], |
| 155 | + [1.0000, 2.0000], |
| 156 | + [1.0000, 2.0000], |
| 157 | + [1.0000, 2.0000], |
| 158 | + [1.0000, 2.0000], |
| 159 | + [1.0000, 2.0000], |
| 160 | + [1.0000, 2.0000], |
| 161 | + [1.0000, 2.0000], |
| 162 | + [1.0000, 2.0000], |
| 163 | + ] |
| 164 | + ) |
| 165 | + X = np.array( |
| 166 | + [ |
| 167 | + [0.2408, 0.4816], |
| 168 | + [0.1558, 0.3116], |
| 169 | + [0.1534, 0.3068], |
| 170 | + [0.2302, 0.4603], |
| 171 | + [0.1467, 0.2934], |
| 172 | + [0.1537, 0.3075], |
| 173 | + [0.2349, 0.4698], |
| 174 | + [0.1498, 0.2995], |
| 175 | + [0.1653, 0.3307], |
| 176 | + ] |
| 177 | + ) |
| 178 | + |
| 179 | + with pytest.raises(SlycotArithmeticError, |
| 180 | + match = "The reduction algorithm failed. " |
| 181 | + "The Toeplitz matrix associated\nwith T " |
| 182 | + r"is not numerically positive definite.") as cm: |
| 183 | + mb02ed(T=T, B=B, n=n, k=k, typet=TYPET, nrhs=nrhs) |
| 184 | + assert cm.value.info == 1 |
| 185 | + |
| 186 | + |
18 | 187 | def test_mb03rd(): |
19 | 188 | """ Test for Schur form reduction. |
20 | 189 |
|
|
0 commit comments