| 344 | } |
| 345 | |
| 346 | double firstOrderDerivativeAt(const Matrix &layerData, const Index &index, Dim2D dim, |
| 347 | double resolution) |
| 348 | { |
| 349 | const int numCol = layerData.cols(); |
| 350 | const int numRow = layerData.rows(); |
| 351 | |
| 352 | double left, right; |
| 353 | switch (dim) { |
| 354 | case Dim2D::X: { |
| 355 | left = layerData(bindIndexToRange(index.x() + 1, numRow), index.y()); |
| 356 | right = layerData(bindIndexToRange(index.x() - 1, numRow), index.y()); |
| 357 | break; |
| 358 | } |
| 359 | case Dim2D::Y: { |
| 360 | left = layerData(index.x(), bindIndexToRange(index.y() + 1, numCol)); |
| 361 | right = layerData(index.x(), bindIndexToRange(index.y() - 1, numCol)); |
| 362 | break; |
| 363 | } |
| 364 | default: { |
| 365 | throw std::runtime_error("Unknown derivative direction"); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | const double perturbation = resolution; |
| 370 | // central difference approximation |
| 371 | // we need to multiply with resolution since we are |
| 372 | // operating in scaled coordinates |
| 373 | return (right - left) / (2.0 * perturbation) * resolution; |
| 374 | } |
| 375 | |
| 376 | double mixedSecondOrderDerivativeAt(const Matrix &layerData, const Index &index, double resolution) |
| 377 | { |
no test coverage detected