Compares the jacobian of the approximant to the central difference of the approximant function value */
| 315 | |
| 316 | /* Compares the jacobian of the approximant to the central difference of the approximant function value */ |
| 317 | void compareJacobianValue(TestFunction *exact, |
| 318 | std::function<Function *(const DataTable &table)> approx_gen_func, |
| 319 | size_t numSamplePoints, size_t numEvalPoints, |
| 320 | double one_eps, double two_eps, double inf_eps) |
| 321 | { |
| 322 | auto dim = exact->getNumVariables(); |
| 323 | |
| 324 | auto samplePoints = linspace(dim, -5, 5, std::pow(numSamplePoints, 1.0/dim)); |
| 325 | auto evalPoints = linspace(dim, -4.95, 4.95, std::pow(numEvalPoints, 1.0/dim)); |
| 326 | |
| 327 | DataTable table = sample(exact, samplePoints); |
| 328 | |
| 329 | Function *approx = approx_gen_func(table); |
| 330 | |
| 331 | INFO("Approximant: " << approx->getDescription()); |
| 332 | INFO("Function: " << exact->getFunctionStr()); |
| 333 | |
| 334 | DenseVector oneNormVec(evalPoints.size()); |
| 335 | DenseVector twoNormVec(evalPoints.size()); |
| 336 | DenseVector infNormVec(evalPoints.size()); |
| 337 | |
| 338 | double maxOneNormError = 0.0; |
| 339 | double maxTwoNormError = 0.0; |
| 340 | double maxInfNormError = 0.0; |
| 341 | |
| 342 | DenseVector maxOneNormErrorPoint(dim); |
| 343 | maxOneNormErrorPoint.fill(0.0); |
| 344 | DenseVector maxTwoNormErrorPoint(dim); |
| 345 | maxTwoNormErrorPoint.fill(0.0); |
| 346 | DenseVector maxInfNormErrorPoint(dim); |
| 347 | maxInfNormErrorPoint.fill(0.0); |
| 348 | |
| 349 | int i = 0; |
| 350 | for (auto &point : evalPoints) |
| 351 | { |
| 352 | DenseVector x = vecToDense(point); |
| 353 | |
| 354 | // Compare the central difference to the approximated jacobian |
| 355 | DenseMatrix exactValue = approx->centralDifference(x); |
| 356 | DenseMatrix approxValue = approx->evalJacobian(x); |
| 357 | |
| 358 | DenseVector error = DenseVector::Zero(exactValue.cols()); |
| 359 | for (size_t j = 0; j < (size_t) error.size(); ++j) |
| 360 | { |
| 361 | error(j) = getError(exactValue(j), approxValue(j)); |
| 362 | } |
| 363 | |
| 364 | oneNormVec(i) = getOneNorm(error) / error.size(); // "Average" |
| 365 | twoNormVec(i) = getTwoNorm(error); |
| 366 | infNormVec(i) = getInfNorm(error); |
| 367 | |
| 368 | if (oneNormVec(i) > maxOneNormError) |
| 369 | { |
| 370 | maxOneNormError = oneNormVec(i); |
| 371 | maxOneNormErrorPoint = x; |
| 372 | } |
| 373 | if (twoNormVec(i) > maxTwoNormError) |
| 374 | { |
no test coverage detected