[37] | 1 | % Test code for the quaternion svd function. |
---|
| 2 | |
---|
| 3 | % Copyright © 2006 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
| 4 | % See the file : Copyright.m for further details. |
---|
| 5 | |
---|
| 6 | T = 1e-10; |
---|
| 7 | |
---|
| 8 | % This is quite a long script because of the need to test the |
---|
| 9 | % function with square, wide and tall arrays, and with 1, 2 or |
---|
| 10 | % 3 output parameters, and with normal and economy decompositions. |
---|
| 11 | |
---|
| 12 | % Square array. |
---|
| 13 | |
---|
| 14 | display('svd on real quaternions ...') |
---|
| 15 | display('svd on square array ...') |
---|
| 16 | |
---|
| 17 | A = quaternion(randn(5,5),randn(5,5),randn(5,5),randn(5,5)); |
---|
| 18 | |
---|
| 19 | % Normal decomposition. |
---|
| 20 | |
---|
| 21 | [U, B, V] = svd(A); |
---|
| 22 | compare(U*B*V', A, T, 'quaternion/svd failed test 1A') |
---|
| 23 | |
---|
| 24 | [U, B] = svd(A); |
---|
| 25 | compare(U*B*V', A, T, 'quaternion/svd failed test 1B') |
---|
| 26 | |
---|
| 27 | S = svd(A); |
---|
| 28 | compare(S, diag(B), T, 'quaternion/svd failed test 1C') |
---|
| 29 | |
---|
| 30 | % Economy (0) decomposition. |
---|
| 31 | |
---|
| 32 | [U, B, V] = svd(A,0); |
---|
| 33 | compare(U*B*V', A, T, 'quaternion/svd failed test 2A') |
---|
| 34 | |
---|
| 35 | [U, B] = svd(A,0); |
---|
| 36 | compare(U*B*V', A, T, 'quaternion/svd failed test 2B') |
---|
| 37 | |
---|
| 38 | S = svd(A,0); |
---|
| 39 | compare(S, diag(B), T, 'quaternion/svd failed test 2C') |
---|
| 40 | |
---|
| 41 | % Economy (''econ'') decomposition. |
---|
| 42 | |
---|
| 43 | [U, B, V] = svd(A,'econ'); |
---|
| 44 | compare(U*B*V', A, T, 'quaternion/svd failed test 3A') |
---|
| 45 | |
---|
| 46 | [U, B] = svd(A,'econ'); |
---|
| 47 | compare(U*B*V', A, T, 'quaternion/svd failed test 3B') |
---|
| 48 | |
---|
| 49 | S = svd(A,'econ'); |
---|
| 50 | compare(S, diag(B), T, 'quaternion/svd failed test 3C') |
---|
| 51 | |
---|
| 52 | % Wide array. |
---|
| 53 | |
---|
| 54 | display('svd on wide array ...') |
---|
| 55 | |
---|
| 56 | A = quaternion(randn(5,7),randn(5,7),randn(5,7),randn(5,7)); |
---|
| 57 | |
---|
| 58 | % Normal decomposition. |
---|
| 59 | |
---|
| 60 | [U, B, V] = svd(A); |
---|
| 61 | compare(U*B*V', A, T, 'quaternion/svd failed test 4A') |
---|
| 62 | |
---|
| 63 | [U, B] = svd(A); |
---|
| 64 | compare(U*B*V', A, T, 'quaternion/svd failed test 4B') |
---|
| 65 | |
---|
| 66 | S = svd(A); |
---|
| 67 | compare(S, diag(B), T, 'quaternion/svd failed test 4C') |
---|
| 68 | |
---|
| 69 | % Economy (0) decomposition. |
---|
| 70 | |
---|
| 71 | [U, B, V] = svd(A,0); |
---|
| 72 | compare(U*B*V', A, T, 'quaternion/svd failed test 5A') |
---|
| 73 | |
---|
| 74 | [U, B] = svd(A,0); |
---|
| 75 | compare(U*B*V', A, T, 'quaternion/svd failed test 5B') |
---|
| 76 | |
---|
| 77 | S = svd(A,0); |
---|
| 78 | compare(S, diag(B), T, 'quaternion/svd failed test 5C') |
---|
| 79 | |
---|
| 80 | % Economy (''econ'') decomposition. |
---|
| 81 | |
---|
| 82 | [U, B, V] = svd(A,'econ'); |
---|
| 83 | compare(U*B*V', A, T, 'quaternion/svd failed test 6A') |
---|
| 84 | |
---|
| 85 | [U, B] = svd(A,'econ'); |
---|
| 86 | compare(U*B*V', A, T, 'quaternion/svd failed test 6B') |
---|
| 87 | |
---|
| 88 | S = svd(A,'econ'); |
---|
| 89 | compare(S, diag(B), T, 'quaternion/svd failed test 6C') |
---|
| 90 | |
---|
| 91 | % Tall array. |
---|
| 92 | |
---|
| 93 | display('svd on tall array ...') |
---|
| 94 | |
---|
| 95 | A = A.'; |
---|
| 96 | |
---|
| 97 | % Normal decomposition. |
---|
| 98 | |
---|
| 99 | [U, B, V] = svd(A); |
---|
| 100 | compare(U*B*V', A, T, 'quaternion/svd failed test 7A') |
---|
| 101 | |
---|
| 102 | [U, B] = svd(A); |
---|
| 103 | compare(U*B*V', A, T, 'quaternion/svd failed test 7B') |
---|
| 104 | |
---|
| 105 | S = svd(A); |
---|
| 106 | compare(S, diag(B), T, 'quaternion/svd failed test 7C') |
---|
| 107 | |
---|
| 108 | % Economy (0) decomposition. |
---|
| 109 | |
---|
| 110 | [U, B, V] = svd(A,0); |
---|
| 111 | compare(U*B*V', A, T, 'quaternion/svd failed test 8A') |
---|
| 112 | |
---|
| 113 | [U, B] = svd(A,0); |
---|
| 114 | compare(U*B*V', A, T, 'quaternion/svd failed test 8B') |
---|
| 115 | |
---|
| 116 | S = svd(A,0); |
---|
| 117 | compare(S, diag(B), T, 'quaternion/svd failed test 8C') |
---|
| 118 | |
---|
| 119 | % Economy (''econ'') decomposition. |
---|
| 120 | |
---|
| 121 | [U, B, V] = svd(A,'econ'); |
---|
| 122 | compare(U*B*V', A, T, 'quaternion/svd failed test 9A') |
---|
| 123 | |
---|
| 124 | [U, B] = svd(A,'econ'); |
---|
| 125 | compare(U*B*V', A, T, 'quaternion/svd failed test 9B') |
---|
| 126 | |
---|
| 127 | S = svd(A,'econ'); |
---|
| 128 | compare(S, diag(B), T, 'quaternion/svd failed test 9C') |
---|
| 129 | |
---|
| 130 | % Now repeat the whole lot with complex data ........ |
---|
| 131 | |
---|
| 132 | T = 1e-8; % The error margin for complex data is much more |
---|
| 133 | % relaxed than for real data, because the results |
---|
| 134 | % are not so accurate. |
---|
| 135 | |
---|
| 136 | % Square array. |
---|
| 137 | |
---|
| 138 | display('svd on complex quaternions ...') |
---|
| 139 | display('svd on square array ...') |
---|
| 140 | |
---|
| 141 | A = quaternion(complex(randn(5,5),randn(5,5)),... |
---|
| 142 | complex(randn(5,5),randn(5,5)),... |
---|
| 143 | complex(randn(5,5),randn(5,5)),... |
---|
| 144 | complex(randn(5,5),randn(5,5))); |
---|
| 145 | |
---|
| 146 | % Normal decomposition. |
---|
| 147 | |
---|
| 148 | [U, B, V] = svd(A); |
---|
| 149 | compare(U*B*V', A, T, 'quaternion/svd failed test 10A') |
---|
| 150 | |
---|
| 151 | [U, B] = svd(A); |
---|
| 152 | compare(U*B*V', A, T, 'quaternion/svd failed test 10B') |
---|
| 153 | |
---|
| 154 | S = svd(A); |
---|
| 155 | compare(S, diag(B), T, 'quaternion/svd failed test 10C') |
---|
| 156 | |
---|
| 157 | % Economy (0) decomposition. |
---|
| 158 | |
---|
| 159 | [U, B, V] = svd(A,0); |
---|
| 160 | compare(U*B*V', A, T, 'quaternion/svd failed test 11A') |
---|
| 161 | |
---|
| 162 | [U, B] = svd(A,0); |
---|
| 163 | compare(U*B*V', A, T, 'quaternion/svd failed test 11B') |
---|
| 164 | |
---|
| 165 | S = svd(A,0); |
---|
| 166 | compare(S, diag(B), T, 'quaternion/svd failed test 11C') |
---|
| 167 | |
---|
| 168 | % Economy (''econ'') decomposition. |
---|
| 169 | |
---|
| 170 | [U, B, V] = svd(A,'econ'); |
---|
| 171 | compare(U*B*V', A, T, 'quaternion/svd failed test 12A') |
---|
| 172 | |
---|
| 173 | [U, B] = svd(A,'econ'); |
---|
| 174 | compare(U*B*V', A, T, 'quaternion/svd failed test 12B') |
---|
| 175 | |
---|
| 176 | S = svd(A,'econ'); |
---|
| 177 | compare(S, diag(B), T, 'quaternion/svd failed test 12C') |
---|
| 178 | |
---|
| 179 | % Wide array. |
---|
| 180 | |
---|
| 181 | display('svd on wide array ...') |
---|
| 182 | |
---|
| 183 | A = quaternion(complex(randn(5,7),randn(5,7)),... |
---|
| 184 | complex(randn(5,7),randn(5,7)),... |
---|
| 185 | complex(randn(5,7),randn(5,7)),... |
---|
| 186 | complex(randn(5,7),randn(5,7))); |
---|
| 187 | |
---|
| 188 | % Normal decomposition. |
---|
| 189 | |
---|
| 190 | [U, B, V] = svd(A); |
---|
| 191 | compare(U*B*V', A, T, 'quaternion/svd failed test 13A') |
---|
| 192 | |
---|
| 193 | [U, B] = svd(A); |
---|
| 194 | compare(U*B*V', A, T, 'quaternion/svd failed test 13B') |
---|
| 195 | |
---|
| 196 | S = svd(A); |
---|
| 197 | compare(S, diag(B), T, 'quaternion/svd failed test 13C') |
---|
| 198 | |
---|
| 199 | % Economy (0) decomposition. |
---|
| 200 | |
---|
| 201 | [U, B, V] = svd(A,0); |
---|
| 202 | compare(U*B*V', A, T, 'quaternion/svd failed test 14A') |
---|
| 203 | |
---|
| 204 | [U, B] = svd(A,0); |
---|
| 205 | compare(U*B*V', A, T, 'quaternion/svd failed test 14B') |
---|
| 206 | |
---|
| 207 | S = svd(A,0); |
---|
| 208 | compare(S, diag(B), T, 'quaternion/svd failed test 14C') |
---|
| 209 | |
---|
| 210 | % Economy (''econ'') decomposition. |
---|
| 211 | |
---|
| 212 | [U, B, V] = svd(A,'econ'); |
---|
| 213 | compare(U*B*V', A, T, 'quaternion/svd failed test 15A') |
---|
| 214 | |
---|
| 215 | [U, B] = svd(A,'econ'); |
---|
| 216 | compare(U*B*V', A, T, 'quaternion/svd failed test 15B') |
---|
| 217 | |
---|
| 218 | S = svd(A,'econ'); |
---|
| 219 | compare(S, diag(B), T, 'quaternion/svd failed test 15C') |
---|
| 220 | |
---|
| 221 | % Tall array. |
---|
| 222 | |
---|
| 223 | display('svd on tall array ...') |
---|
| 224 | |
---|
| 225 | A = A.'; |
---|
| 226 | |
---|
| 227 | % Normal decomposition. |
---|
| 228 | |
---|
| 229 | [U, B, V] = svd(A); |
---|
| 230 | compare(U*B*V', A, T, 'quaternion/svd failed test 16A') |
---|
| 231 | |
---|
| 232 | [U, B] = svd(A); |
---|
| 233 | compare(U*B*V', A, T, 'quaternion/svd failed test 16B') |
---|
| 234 | |
---|
| 235 | S = svd(A); |
---|
| 236 | compare(S, diag(B), T, 'quaternion/svd failed test 16C') |
---|
| 237 | |
---|
| 238 | % Economy (0) decomposition. |
---|
| 239 | |
---|
| 240 | [U, B, V] = svd(A,0); |
---|
| 241 | compare(U*B*V', A, T, 'quaternion/svd failed test 17A') |
---|
| 242 | |
---|
| 243 | [U, B] = svd(A,0); |
---|
| 244 | compare(U*B*V', A, T, 'quaternion/svd failed test 17B') |
---|
| 245 | |
---|
| 246 | S = svd(A,0); |
---|
| 247 | compare(S, diag(B), T, 'quaternion/svd failed test 17C') |
---|
| 248 | |
---|
| 249 | % Economy (''econ'') decomposition. |
---|
| 250 | |
---|
| 251 | [U, B, V] = svd(A,'econ'); |
---|
| 252 | compare(U*B*V', A, T, 'quaternion/svd failed test 18A') |
---|
| 253 | |
---|
| 254 | [U, B] = svd(A,'econ'); |
---|
| 255 | compare(U*B*V', A, T, 'quaternion/svd failed test 18B') |
---|
| 256 | |
---|
| 257 | S = svd(A,'econ'); |
---|
| 258 | compare(S, diag(B), T, 'quaternion/svd failed test 18C') |
---|
| 259 | |
---|
| 260 | clear A U B V S |
---|