LCOV - code coverage report
Current view: top level - functionals - PseudoHuber.cpp (source / functions) Hit Total Coverage
Test: test_coverage.info.cleaned Lines: 0 43 0.0 %
Date: 2022-08-04 03:43:28 Functions: 0 14 0.0 %

          Line data    Source code
       1             : #include "PseudoHuber.h"
       2             : #include "Scaling.h"
       3             : #include "TypeCasts.hpp"
       4             : 
       5             : #include <cmath>
       6             : #include <stdexcept>
       7             : 
       8             : namespace elsa
       9             : {
      10             :     template <typename data_t>
      11           0 :     PseudoHuber<data_t>::PseudoHuber(const DataDescriptor& domainDescriptor, real_t delta)
      12           0 :         : Functional<data_t>(domainDescriptor), _delta{delta}
      13             :     {
      14             :         // sanity check delta
      15           0 :         if (delta <= static_cast<real_t>(0.0))
      16           0 :             throw InvalidArgumentError("PseudoHuber: delta has to be positive.");
      17           0 :     }
      18             : 
      19             :     template <typename data_t>
      20           0 :     PseudoHuber<data_t>::PseudoHuber(const Residual<data_t>& residual, real_t delta)
      21           0 :         : Functional<data_t>(residual), _delta{delta}
      22             :     {
      23             :         // sanity check delta
      24           0 :         if (delta <= static_cast<real_t>(0.0))
      25           0 :             throw InvalidArgumentError("PseudoHuber: delta has to be positive.");
      26           0 :     }
      27             : 
      28             :     template <typename data_t>
      29           0 :     data_t PseudoHuber<data_t>::evaluateImpl(const DataContainer<data_t>& Rx)
      30             :     {
      31             :         // note: this is currently not a reduction in DataContainer, but implemented here "manually"
      32             : 
      33           0 :         auto result = static_cast<data_t>(0.0);
      34             : 
      35           0 :         for (index_t i = 0; i < Rx.getSize(); ++i) {
      36           0 :             data_t temp = Rx[i] / _delta;
      37           0 :             result += _delta * _delta
      38           0 :                       * (sqrt(static_cast<data_t>(1.0) + temp * temp) - static_cast<data_t>(1.0));
      39             :         }
      40             : 
      41           0 :         return result;
      42             :     }
      43             : 
      44             :     template <typename data_t>
      45           0 :     void PseudoHuber<data_t>::getGradientInPlaceImpl(DataContainer<data_t>& Rx)
      46             :     {
      47           0 :         for (index_t i = 0; i < Rx.getSize(); ++i) {
      48           0 :             data_t temp = Rx[i] / _delta;
      49           0 :             Rx[i] = Rx[i] / sqrt(static_cast<data_t>(1.0) + temp * temp);
      50             :         }
      51           0 :     }
      52             : 
      53             :     template <typename data_t>
      54           0 :     LinearOperator<data_t> PseudoHuber<data_t>::getHessianImpl(const DataContainer<data_t>& Rx)
      55             :     {
      56           0 :         DataContainer<data_t> scaleFactors(Rx.getDataDescriptor());
      57           0 :         for (index_t i = 0; i < Rx.getSize(); ++i) {
      58           0 :             data_t temp = Rx[i] / _delta;
      59           0 :             data_t tempSq = temp * temp;
      60           0 :             data_t sqrtOnePTempSq = sqrt(static_cast<data_t>(1.0) + tempSq);
      61           0 :             scaleFactors[i] =
      62           0 :                 (sqrtOnePTempSq - tempSq / sqrtOnePTempSq) / (static_cast<data_t>(1.0) + tempSq);
      63             :         }
      64             : 
      65           0 :         return leaf(Scaling<data_t>(Rx.getDataDescriptor(), scaleFactors));
      66           0 :     }
      67             : 
      68             :     template <typename data_t>
      69           0 :     PseudoHuber<data_t>* PseudoHuber<data_t>::cloneImpl() const
      70             :     {
      71           0 :         return new PseudoHuber(this->getResidual(), _delta);
      72             :     }
      73             : 
      74             :     template <typename data_t>
      75           0 :     bool PseudoHuber<data_t>::isEqual(const Functional<data_t>& other) const
      76             :     {
      77           0 :         if (!Functional<data_t>::isEqual(other))
      78           0 :             return false;
      79             : 
      80           0 :         auto otherPHuber = downcast_safe<PseudoHuber>(&other);
      81           0 :         if (!otherPHuber)
      82           0 :             return false;
      83             : 
      84           0 :         if (_delta != otherPHuber->_delta)
      85           0 :             return false;
      86             : 
      87           0 :         return true;
      88             :     }
      89             : 
      90             :     // ------------------------------------------
      91             :     // explicit template instantiation
      92             :     template class PseudoHuber<float>;
      93             :     template class PseudoHuber<double>;
      94             :     // no complex-number instantiations for PseudoHuber! (they would not really be useful)
      95             : 
      96             : } // namespace elsa

Generated by: LCOV version 1.14