LCOV - code coverage report
Current view: top level - source - DistanceToBall2.cpp (source / functions) Hit Total Coverage
Test: LibForBES Unit Tests Lines: 45 47 95.7 %
Date: 2016-04-18 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* 
       2             :  * File:   DistanceToBall2.cpp
       3             :  * Author: chung
       4             :  * 
       5             :  * Created on January 14, 2016, 2:02 AM
       6             :  */
       7             : 
       8             : #include <complex>
       9             : 
      10             : #include "DistanceToBall2.h"
      11             : 
      12           1 : DistanceToBall2::DistanceToBall2() {
      13           1 :     m_center = NULL;
      14           1 :     m_rho = 1;
      15           1 :     m_w = 1;
      16           1 : }
      17             : 
      18           3 : DistanceToBall2::DistanceToBall2(double w)
      19           3 : : Function(), m_w(w) {
      20           3 :     m_rho = 1;
      21           3 :     m_center = NULL;
      22           3 : }
      23             : 
      24           1 : DistanceToBall2::DistanceToBall2(double w, double rho)
      25           1 : : Function(), m_rho(rho), m_w(w) {
      26           1 :     m_center = NULL;
      27           1 : }
      28             : 
      29           3 : DistanceToBall2::DistanceToBall2(double w, double rho, Matrix& center)
      30           3 : : Function(), m_rho(rho), m_w(w), m_center(&center) {
      31             : 
      32           3 : }
      33             : 
      34          16 : DistanceToBall2::~DistanceToBall2() {
      35          16 : }
      36             : 
      37         352 : int DistanceToBall2::call(Matrix& x, double& f, Matrix& grad) {
      38             :     double norm_x_minus_c;
      39         352 :     f = 0.0;
      40         352 :     grad = x;
      41         352 :     if (m_center != NULL) {
      42           1 :         grad -= *m_center;
      43             :     }
      44         352 :     norm_x_minus_c = std::sqrt((grad * grad)[0]);
      45         352 :     if (norm_x_minus_c > m_rho) {
      46         350 :         f = 0.5 * m_w * std::pow(norm_x_minus_c - m_rho, 2);
      47         350 :         grad *= (1-m_rho/norm_x_minus_c);
      48             :     } else {
      49           6 :         for (size_t i = 0; i < grad.getNrows(); i++){
      50           4 :             grad[i] = 0.0;
      51             :         }
      52             :     }    
      53         352 :     if (std::abs(m_w - 1.0) > 1e-14) {
      54         352 :         grad *= m_w;
      55             :     }
      56         352 :     return ForBESUtils::STATUS_OK;
      57             : }
      58             : 
      59           6 : int DistanceToBall2::call(Matrix& x, double& f) {
      60           6 :     double norm_x_minus_c = 0.0;
      61           6 :     f = 0.0;
      62           6 :     if (m_center != NULL) {
      63          22 :         for (size_t i = 0; i < x.getNrows(); i++) {
      64          20 :             norm_x_minus_c += std::pow(x[i] - m_center->get(i), 2);
      65             :         }
      66           2 :         norm_x_minus_c = std::sqrt(norm_x_minus_c);
      67             :     } else {
      68           4 :         norm_x_minus_c = std::sqrt((x * x)[0]);
      69             :     }
      70           6 :     if (norm_x_minus_c > m_rho) {
      71           5 :         f = 0.5 * m_w * std::pow(norm_x_minus_c - m_rho, 2);
      72             :     }
      73           6 :     return ForBESUtils::STATUS_OK;
      74             : }
      75             : 
      76           0 : FunctionOntologicalClass DistanceToBall2::category() {
      77           0 :     return FunctionOntologyRegistry::indicator();
      78           6 : }
      79             : 
      80             : 
      81             : 
      82             : 
      83             : 

Generated by: LCOV version 1.10