Changeset 10015
- Timestamp:
- Nov 16, 2006, 8:08:30 AM (19 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/math/tap_psSparse.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/math/tap_psSparse.c
r10010 r10015 8 8 int main (void) 9 9 { 10 plan_tests( 47);10 plan_tests(23); 11 11 12 12 diag("psSparse() tests"); … … 85 85 } 86 86 87 // test psSparseBorderSolve for a simple example matrix 87 /* sample matrix equation: 88 89 1.0 0.1 0.1 | 1.0 | |1.15| 90 0.1 1.0 0.2 | 1.0 | = |1.20| 91 0.1 0.2 0.5 | 0.5 | |0.55| 92 93 lower product : 1.0*0.1 + 1.0*0.2 = 0.3 94 upper product : 0.1*0.5 = 0.05 95 : 0.2*0.5 = 0.10 96 */ 97 98 // test psSparseBorderMultiply 88 99 { 89 100 psMemId id = psMemGetId(); … … 100 111 // Ax = b for x using psSparseSolve. compare with the input values for x. 101 112 102 int Nrows = 10;113 int Nrows = 2; 103 114 int Nborder = 1; 104 115 … … 108 119 skip_start(matrix == NULL, 5, "Skipping tests because psSparseAlloc() failed"); 109 120 110 for (int i = 0; i < Nrows; i++) 111 { 112 psSparseMatrixElement (matrix, i, i, 1.0); 113 if (i + 1 < Nrows) { 114 psSparseMatrixElement (matrix, i + 1, i, 0.1); 115 } 116 } 121 psSparseMatrixElement (matrix, 0, 0, 1.0); 122 psSparseMatrixElement (matrix, 1, 1, 1.0); 123 psSparseMatrixElement (matrix, 1, 0, 0.1); 117 124 psSparseResort (matrix); 118 125 … … 121 128 122 129 // construct the B component: 123 for (int i = 0; i < Nrows; i++) 124 { 125 for (int j = 0; j < Nborder; j++) { 126 psSparseBorderElementB (border, i, j, 0.1); 127 } 128 } 129 130 // construct the Q component: 131 for (int i = 0; i < Nborder; i++) 132 { 133 for (int j = 0; j < Nborder; j++) { 134 psSparseBorderElementT (border, i, j, i+j); 135 } 136 } 130 psSparseBorderElementB (border, 0, 0, 0.1); 131 psSparseBorderElementB (border, 1, 0, 0.2); 132 133 // construct the T component: 134 psSparseBorderElementT (border, 0, 0, 0.5); 137 135 138 136 // construct the X and Y vectors: 139 137 psVector *xRef = psVectorAlloc (Nrows, PS_TYPE_F32); 140 for (int i = 0; i < Nrows; i++) 141 { 142 xRef->data.F32[i] = 1.0; 143 } 138 xRef->data.F32[0] = 1.0; 139 xRef->data.F32[1] = 1.0; 144 140 psVector *yRef = psVectorAlloc (Nborder, PS_TYPE_F32); 145 for (int i = 0; i < Nborder; i++) 146 { 147 yRef->data.F32[i] = 1.0; 148 } 141 yRef->data.F32[0] = 0.5; 149 142 150 143 // construct the f and g vectors by multiplication: 151 psVector *fVec = NULL; 144 psVector *fVec; 145 146 // test the support functions: LowerProduct 147 fVec = psSparseBorderLowerProduct (NULL, border, xRef); 148 ok_float (fVec->n, 1.0, "f dimen: %d", fVec->n); 149 ok_float (fVec->data.F32[0], 0.3, "f(0): %f", fVec->data.F32[0]); 150 psFree (fVec); 151 152 // test the support functions: Upper Product 153 fVec = psSparseBorderUpperProduct (NULL, border, yRef); 154 ok_float (fVec->n, 2.0, "f dimen: %d", fVec->n); 155 ok_float (fVec->data.F32[0], 0.05, "f(0): %f", fVec->data.F32[0]); 156 ok_float (fVec->data.F32[1], 0.10, "f(0): %f", fVec->data.F32[1]); 157 psFree (fVec); 158 159 // test the support functions: Square Product 160 fVec = psSparseBorderSquareProduct (NULL, border, yRef); 161 ok_float (fVec->n, 1.0, "f dimen: %d", fVec->n); 162 ok_float (fVec->data.F32[0], 0.25, "f(0): %f", fVec->data.F32[0]); 163 psFree (fVec); 164 165 fVec = NULL; 152 166 psVector *gVec = NULL; 153 167 psSparseBorderMultiply (&fVec, &gVec, border, xRef, yRef); 168 ok_float (fVec->data.F32[0], 1.15, "f(0): %f", fVec->data.F32[0]); 169 ok_float (fVec->data.F32[1], 1.20, "f(1): %f", fVec->data.F32[1]); 170 ok_float (gVec->data.F32[0], 0.55, "g(0): %f", gVec->data.F32[0]); 154 171 155 172 // supply the fVec and gVec data to the border 156 173 for (int i = 0; i < Nrows; i++) 157 174 { 158 psSparseVectorElement (border->sparse, i, gVec->data.F32[i]);175 psSparseVectorElement (border->sparse, i, fVec->data.F32[i]); 159 176 } 160 177 for (int i = 0; i < Nborder; i++) … … 173 190 psVector *yFit = NULL; 174 191 psSparseBorderSolve (&xFit, &yFit, constraint, border, 4); 192 ok_float_tol (xFit->data.F32[0], 1.0, 1e-4, "f(0): %f", xFit->data.F32[0]); 193 ok_float_tol (xFit->data.F32[1], 1.0, 1e-4, "f(1): %f", xFit->data.F32[1]); 194 ok_float_tol (yFit->data.F32[0], 0.5, 1e-4, "g(0): %f", yFit->data.F32[0]); 195 196 psFree (xFit); 197 psFree (yFit); 198 psFree (fVec); 199 psFree (gVec); 200 psFree (xRef); 201 psFree (yRef); 202 psFree (border); 203 psFree (matrix); 204 skip_end(); 205 ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks"); 206 } 207 208 // test psSparseBorderSolve for a simple example matrix 209 { 210 psMemId id = psMemGetId(); 211 212 diag ("solve a simple, small matrix equation "); 213 214 // the basic equation (Ax = b) is: 215 // |S B'||x| = |f| 216 // |B Q ||y| = |g| 217 218 // construct a matrix S with diagonals of 1 and a small number of off diagonal elements. 219 // construct a border with finite values and a corresponding Q matrix 220 // construct a vector x, construct the corresponding vector b by multiplication. solve 221 // Ax = b for x using psSparseSolve. compare with the input values for x. 222 223 int Nrows = 10; 224 int Nborder = 1; 225 226 // construct the sparse matrix 227 psSparse *matrix = psSparseAlloc (Nrows, Nrows); 228 ok(matrix != NULL, "psSparse successfully allocated"); 229 skip_start(matrix == NULL, 5, "Skipping tests because psSparseAlloc() failed"); 230 231 for (int i = 0; i < Nrows; i++) 232 { 233 psSparseMatrixElement (matrix, i, i, 1.0); 234 if (i + 1 < Nrows) { 235 psSparseMatrixElement (matrix, i + 1, i, 0.1); 236 } 237 } 238 psSparseResort (matrix); 239 240 // border region has a width of 1: 241 psSparseBorder *border = psSparseBorderAlloc (matrix, Nborder); 242 243 // construct the B component: 244 for (int i = 0; i < Nrows; i++) 245 { 246 for (int j = 0; j < Nborder; j++) { 247 psSparseBorderElementB (border, i, j, 0.1); 248 } 249 } 250 ok_float(border->Bij->data.F32[0][0], 0.1, "set matrix element %d,%d", 0, 0); 251 252 // construct the Q component: 253 for (int i = 0; i < Nborder; i++) 254 { 255 for (int j = 0; j < Nborder; j++) { 256 psSparseBorderElementT (border, i, j, i+j+2); 257 } 258 } 259 260 // construct the X and Y vectors: 261 psVector *xRef = psVectorAlloc (Nrows, PS_TYPE_F32); 262 for (int i = 0; i < Nrows; i++) 263 { 264 xRef->data.F32[i] = 1.0; 265 } 266 psVector *yRef = psVectorAlloc (Nborder, PS_TYPE_F32); 267 for (int i = 0; i < Nborder; i++) 268 { 269 yRef->data.F32[i] = 1.0; 270 } 271 272 // construct the f and g vectors by multiplication: 273 psVector *fVec = NULL; 274 psVector *gVec = NULL; 275 psSparseBorderMultiply (&fVec, &gVec, border, xRef, yRef); 276 277 // supply the fVec and gVec data to the border 278 for (int i = 0; i < Nrows; i++) 279 { 280 psSparseVectorElement (border->sparse, i, fVec->data.F32[i]); 281 } 282 for (int i = 0; i < Nborder; i++) 283 { 284 psSparseBorderElementG (border, i, gVec->data.F32[i]); 285 } 286 287 // solve the border equation 288 psSparseConstraint constraint; 289 constraint.paramMin = 0.0; 290 constraint.paramMax = 1e8; 291 constraint.paramDelta = 1e8; 292 293 // solve for normalization terms (need include local sky?) 294 psVector *xFit = NULL; 295 psVector *yFit = NULL; 296 psSparseBorderSolve (&xFit, &yFit, constraint, border, 4); 175 297 176 298 // measure stdev between xFit and xRef … … 180 302 { 181 303 float dX = xRef->data.F32[i] - xFit->data.F32[i]; 182 fprintf (stderr, "%5.3f %5.3f %5.3f %5.3f\n", fVec->data.F32[i], xRef->data.F32[i], xFit->data.F32[i], dX);304 // fprintf (stderr, "%5.3f %5.3f %5.3f %5.3f\n", fVec->data.F32[i], xRef->data.F32[i], xFit->data.F32[i], dX); 183 305 dS2 += PS_SQR(dX); 184 306 dS += dX; … … 190 312 // measure stdev between yFit and yRef 191 313 float dY = yRef->data.F32[0] - yFit->data.F32[0]; 192 fprintf (stderr, "%5.3f %5.3f %5.3f %5.3f\n", gVec->data.F32[0], yRef->data.F32[0], yFit->data.F32[0], dS); 193 ok_float_tol (dY, 0.0, 1e-4, "scatter: %.20f", dY); 194 195 psFree (matrix); 314 // fprintf (stderr, "%5.3f %5.3f %5.3f %5.3f\n", gVec->data.F32[0], yRef->data.F32[0], yFit->data.F32[0], dS); 315 ok_float_tol (dY, 0.0, 2e-4, "scatter: %.20f", dY); 316 196 317 psFree (xRef); 197 318 psFree (yRef); … … 200 321 psFree (fVec); 201 322 psFree (gVec); 323 psFree (matrix); 324 psFree (border); 202 325 203 326 skip_end();
Note:
See TracChangeset
for help on using the changeset viewer.
