コード例 #1
0
ファイル: multiply_log_test.cpp プロジェクト: javaosos/stan
TEST(AgradFwdMultiplyLog,Fvar) {
  using stan::agrad::fvar;
  using std::isnan;
  using std::log;
  using stan::math::multiply_log;

  fvar<double> x(0.5,1.0);
  fvar<double> y(1.2,2.0);
  fvar<double> z(-0.4,3.0);

  double w = 0.0;
  double v = 1.3;

  fvar<double> a = multiply_log(x, y);
  EXPECT_FLOAT_EQ(multiply_log(0.5, 1.2), a.val_);
  EXPECT_FLOAT_EQ(1.0 * log(1.2) + 0.5 * 2.0 / 1.2, a.d_);

  fvar<double> b = multiply_log(x,z);
  isnan(b.val_);
  isnan(b.d_);

  fvar<double> c = multiply_log(x, v);
  EXPECT_FLOAT_EQ(multiply_log(0.5, 1.3), c.val_);
  EXPECT_FLOAT_EQ(log(1.3), c.d_);

  fvar<double> d = multiply_log(v, x);
  EXPECT_FLOAT_EQ(multiply_log(1.3, 0.5), d.val_);
  EXPECT_FLOAT_EQ(1.3 * 1.0 / 0.5, d.d_);

  fvar<double> e = multiply_log(x, w);
  isnan(e.val_);
  isnan(e.d_);
}
コード例 #2
0
ファイル: hypot_test.cpp プロジェクト: javaosos/stan
TEST(AgradFwdHypot,Fvar) {
  using stan::agrad::fvar;
  using boost::math::hypot;
  using std::isnan;

  fvar<double> x(0.5,1.0);
  fvar<double> y(2.3,2.0);

  fvar<double> a = hypot(x, y);
  EXPECT_FLOAT_EQ(hypot(0.5, 2.3), a.val_);
  EXPECT_FLOAT_EQ((0.5 * 1.0 + 2.3 * 2.0) / hypot(0.5, 2.3), a.d_);

  fvar<double> z(0.0,1.0);
  fvar<double> w(-2.3,2.0);
  fvar<double> b = hypot(x, z);

  EXPECT_FLOAT_EQ(0.5, b.val_);
  EXPECT_FLOAT_EQ(1.0, b.d_);

  fvar<double> c = hypot(x, w);
  isnan(c.val_);
  isnan(c.d_);

  fvar<double> d = hypot(z, x);
  EXPECT_FLOAT_EQ(0.5, d.val_);
  EXPECT_FLOAT_EQ(1.0, d.d_);
}
コード例 #3
0
TEST(AgradFwdLog,Fvar) {
    using stan::agrad::fvar;
    using std::log;
    using std::isnan;

    fvar<double> x(0.5,1.0);

    fvar<double> a = log(x);
    EXPECT_FLOAT_EQ(log(0.5), a.val_);
    EXPECT_FLOAT_EQ(1 / 0.5, a.d_);

    fvar<double> b = 2 * log(x) + 4;
    EXPECT_FLOAT_EQ(2 * log(0.5) + 4, b.val_);
    EXPECT_FLOAT_EQ(2 / 0.5, b.d_);

    fvar<double> c = -log(x) + 5;
    EXPECT_FLOAT_EQ(-log(0.5) + 5, c.val_);
    EXPECT_FLOAT_EQ(-1 / 0.5, c.d_);

    fvar<double> d = -3 * log(x) + 5 * x;
    EXPECT_FLOAT_EQ(-3 * log(0.5) + 5 * 0.5, d.val_);
    EXPECT_FLOAT_EQ(-3 / 0.5 + 5, d.d_);

    fvar<double> y(-0.5,1.0);
    fvar<double> e = log(y);
    isnan(e.val_);
    isnan(e.d_);

    fvar<double> z(0.0,1.0);
    fvar<double> f = log(z);
    isnan(f.val_);
    isnan(f.d_);
}
コード例 #4
0
ファイル: acosh_test.cpp プロジェクト: danstowell/stan
TEST(AgradFvar, acosh) {
  using stan::agrad::fvar;
  using boost::math::acosh;
  using std::sqrt;
  using std::isnan;

  fvar<double> x(1.5);
  x.d_ = 1.0;

  fvar<double> a = acosh(x);
  EXPECT_FLOAT_EQ(acosh(1.5), a.val_);
  EXPECT_FLOAT_EQ(1 / sqrt(-1 + (1.5) * (1.5)), a.d_);

  fvar<double> y(-1.2);
  y.d_ = 1.0;

  fvar<double> b = acosh(y);
  isnan(b.val_);
  isnan(b.d_);

  fvar<double> z(0.5);
  z.d_ = 1.0;

  fvar<double> c = acosh(z);
  isnan(c.val_);
  isnan(c.d_);
}
コード例 #5
0
ファイル: pow_test.cpp プロジェクト: danstowell/stan
TEST(AgradFvar, pow) {
  using stan::agrad::fvar;
  using std::pow;
  using std::log;
  using std::isnan;

  fvar<double> x(0.5);
  x.d_ = 1.0;
  double y = 5.0;

  fvar<double> a = pow(x, y);
  EXPECT_FLOAT_EQ(pow(0.5, 5.0), a.val_);
  EXPECT_FLOAT_EQ(5.0 * pow(0.5, 5.0 - 1.0), a.d_);

  fvar<double> b = pow(y, x);
  EXPECT_FLOAT_EQ(pow(5.0, 0.5), b.val_);
  EXPECT_FLOAT_EQ(log(5.0) * pow(5.0, 0.5), b.d_);

  fvar<double> z(1.2);
  z.d_ = 2.0;
  fvar<double> c = pow(x, z);
  EXPECT_FLOAT_EQ(pow(0.5, 1.2), c.val_);
  EXPECT_FLOAT_EQ((2.0 * log(0.5) + 1.2 * 1.0 / 0.5) * pow(0.5, 1.2), c.d_);

  fvar<double> w(-0.4);
  w.d_ = 1.0;
  fvar<double> d = pow(w, x);
  isnan(d.val_);
  isnan(d.d_);
}
コード例 #6
0
ファイル: qrReader.cpp プロジェクト: witwall/qrcode-reader
bool qrReader::handlePossibleCenter(int stateCount[], int i, int j) {
    int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4];
    float centerJ = centerFromEnd(stateCount, j);
    float centerI = crossCheckVertical(i, (int)centerJ, stateCount[2], stateCountTotal);

    if(!isnan(centerI)) {
        // Cross check against the horizontal
        centerJ = crossCheckHorizontal((int)centerJ, (int)centerI, stateCount[2], stateCountTotal);

        // Do we have a center?
        if(!isnan(centerJ)) {
            float estimatedModuleSize = (float)stateCountTotal/7.0f;
            bool found = false;
            for(unsigned int index=0;index<this->possibleCenters.size();index++) {
                FinderPattern *center = this->possibleCenters[index];
                if(center->aboutEquals(estimatedModuleSize, centerI, centerJ)) {
                    this->possibleCenters[index] = center->combineEstimate(centerI, centerJ, estimatedModuleSize);
                    found = true;
                    break;
                }
            }

            if(!found) {
                FinderPattern *newCenter = new FinderPattern(centerJ, centerI, estimatedModuleSize);
                printf("Created new center: (%f, %f)\n", newCenter->getX(), newCenter->getY());
                possibleCenters.push_back(newCenter);
            }

            return true;
        }
    }
    printf("Returning false\n");
    return false;
}
コード例 #7
0
ファイル: log10_test.cpp プロジェクト: aseyboldt/math
TEST(AgradFwdLog10,Fvar) {
  using stan::math::fvar;
  using std::log;
  using std::isnan;
  using std::log10;

  fvar<double> x(0.5,1.0);
  
  fvar<double> a = log10(x);
  EXPECT_FLOAT_EQ(log10(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / (0.5 * log(10)), a.d_);

  fvar<double> b = 2 * log10(x) + 4;
  EXPECT_FLOAT_EQ(2 * log10(0.5) + 4, b.val_);
  EXPECT_FLOAT_EQ(2 / (0.5 * log(10)), b.d_);

  fvar<double> c = -log10(x) + 5;
  EXPECT_FLOAT_EQ(-log10(0.5) + 5, c.val_);
  EXPECT_FLOAT_EQ(-1 / (0.5 * log(10)), c.d_);

  fvar<double> d = -3 * log10(x) + 5 * x;
  EXPECT_FLOAT_EQ(-3 * log10(0.5) + 5 * 0.5, d.val_);
  EXPECT_FLOAT_EQ(-3 / (0.5 * log(10)) + 5, d.d_);

  fvar<double> y(-0.5,1.0);
  fvar<double> e = log10(y);
  isnan(e.val_);
  isnan(e.d_);

  fvar<double> z(0.0,1.0);
  fvar<double> f = log10(z);
  isnan(f.val_);
  isnan(f.d_);
}
コード例 #8
0
TEST(AgradFwdOperatorDivision, Fvar) {
  using stan::agrad::fvar;
  using std::isnan;

  fvar<double> x1(0.5,1.0);
  fvar<double> x2(0.4,2.0);
  fvar<double> a = x1 / x2;

  EXPECT_FLOAT_EQ(0.5 / 0.4, a.val_);
  EXPECT_FLOAT_EQ((1.0 * 0.4 - 2.0 * 0.5) / (0.4 * 0.4), a.d_);

  fvar<double> b = -x1 / x2;
  EXPECT_FLOAT_EQ(-0.5 / 0.4, b.val_);
  EXPECT_FLOAT_EQ((-1 * 0.4 + 2.0 * 0.5) / (0.4 * 0.4), b.d_);

  fvar<double> c = -3 * x1 / x2;
  EXPECT_FLOAT_EQ(-3 * 0.5 / 0.4, c.val_);
  EXPECT_FLOAT_EQ(3 * (-1 * 0.4 + 2.0 * 0.5) / (0.4 * 0.4), c.d_);

  fvar<double> x3(0.5,1.0);
  double x4 = 2.0;

  fvar<double> e = x4 / x3;
  EXPECT_FLOAT_EQ(2 / 0.5, e.val_);
  EXPECT_FLOAT_EQ(-2 * 1.0 / (0.5 * 0.5), e.d_);

  fvar<double> f = x3 / -2;
  EXPECT_FLOAT_EQ(0.5 / -2, f.val_);
  EXPECT_FLOAT_EQ(1.0 / -2, f.d_);

  fvar<double> x5(0.0,1.0);
  fvar<double> g = x3/x5;
  isnan(g.val_);
  isnan(g.d_);
}
コード例 #9
0
TEST(AgradFwdCbrt,Fvar) {
  using stan::agrad::fvar;
  using boost::math::cbrt;
  using std::isnan;

  fvar<double> x(0.5,1.0);
  fvar<double> a = cbrt(x);

  EXPECT_FLOAT_EQ(cbrt(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / (3 * pow(0.5, 2.0 / 3.0)), a.d_);

  fvar<double> b = 3 * cbrt(x) + x;
  EXPECT_FLOAT_EQ(3 * cbrt(0.5) + 0.5, b.val_);
  EXPECT_FLOAT_EQ(3 / (3 * pow(0.5, 2.0 / 3.0)) + 1, b.d_);

  fvar<double> c = -cbrt(x) + 5;
  EXPECT_FLOAT_EQ(-cbrt(0.5) + 5, c.val_);
  EXPECT_FLOAT_EQ(-1 / (3 * pow(0.5, 2.0 / 3.0)), c.d_);

  fvar<double> d = -3 * cbrt(x) + 5 * x;
  EXPECT_FLOAT_EQ(-3 * cbrt(0.5) + 5 * 0.5, d.val_);
  EXPECT_FLOAT_EQ(-3 / (3 * pow(0.5, 2.0 / 3.0)) + 5, d.d_);

  fvar<double> e = -3 * cbrt(-x) + 5 * x;
  EXPECT_FLOAT_EQ(-3 * cbrt(-0.5) + 5 * 0.5, e.val_);
  EXPECT_FLOAT_EQ(3 / (3 * cbrt(-0.5) * cbrt(-0.5)) + 5, e.d_);

  fvar<double> y(0.0,1.0);
  fvar<double> f = cbrt(y);
  EXPECT_FLOAT_EQ(cbrt(0.0), f.val_);
  isnan(f.d_);
}
コード例 #10
0
ファイル: lvm.cpp プロジェクト: achoum/spring
int luaV_tostring (lua_State *L, StkId obj) {
  if (!ttisnumber(obj))
    return 0;
  else {
    char s[LUAI_MAXNUMBER2STR];
    lua_Number n = nvalue(obj);
    // SPRING -- synced safety change
    //        -- need a custom number formatter?
    if (isfinite(n)) {
      lua_number2str(s, n);
    }
    else {
      if (isnan(n)) {
        strcpy(s, "nan");
      }
      else {
        const int inf_type = isinf(n);
        if (inf_type == 1) {
          strcpy(s, "+inf");
        } else if (inf_type == -1) {
          strcpy(s, "-inf");
        } else {
          strcpy(s, "weird_number");
        }
      }
    } 
    setsvalue2s(L, obj, luaS_new(L, s));
    return 1;
  }
}
コード例 #11
0
TEST(AgradFwdFmin,Fvar) {
  using stan::agrad::fvar;
  using stan::agrad::fmin;
  using std::isnan;

  fvar<double> x(2.0,1.0);
  fvar<double> y(-3.0,2.0);

  fvar<double> a = fmin(x, y);
  EXPECT_FLOAT_EQ(-3.0, a.val_);
  EXPECT_FLOAT_EQ(2.0, a.d_);

  fvar<double> b = fmin(2 * x, y);
  EXPECT_FLOAT_EQ(-3.0, b.val_);
  EXPECT_FLOAT_EQ(2.0, b.d_);

  fvar<double> c = fmin(y, x);
  EXPECT_FLOAT_EQ(-3.0, c.val_);
  EXPECT_FLOAT_EQ(2.0, c.d_);

  fvar<double> d = fmin(x, x);
  EXPECT_FLOAT_EQ(2.0, d.val_);
  isnan(d.d_);

  double z = 1.0;

  fvar<double> e = fmin(x, z);
  EXPECT_FLOAT_EQ(1.0, e.val_);
  EXPECT_FLOAT_EQ(0.0, e.d_);

  fvar<double> f = fmin(z, x);
  EXPECT_FLOAT_EQ(1.0, f.val_);
  EXPECT_FLOAT_EQ(0.0, f.d_);
 }
コード例 #12
0
ファイル: fmax_test.cpp プロジェクト: danstowell/stan
TEST(AgradFvar, fmax) {
  using stan::agrad::fvar;
  using std::isnan;

  fvar<double> x(2.0);
  fvar<double> y(-3.0);
  x.d_ = 1.0;
  y.d_ = 2.0;

  fvar<double> a = fmax(x, y);
  EXPECT_FLOAT_EQ(2.0, a.val_);
  EXPECT_FLOAT_EQ(1.0, a.d_);

  fvar<double> b = fmax(2 * x, y);
  EXPECT_FLOAT_EQ(4.0, b.val_);
  EXPECT_FLOAT_EQ(2 * 1.0, b.d_);

  fvar<double> c = fmax(y, x);
  EXPECT_FLOAT_EQ(2.0, c.val_);
  EXPECT_FLOAT_EQ(1.0, c.d_);

  fvar<double> d = fmax(x, x);
  EXPECT_FLOAT_EQ(2.0, d.val_);
  isnan(d.d_);

  double z = 1.0;

  fvar<double> e = fmax(x, z);
  EXPECT_FLOAT_EQ(2.0, e.val_);
  EXPECT_FLOAT_EQ(1.0, e.d_);

  fvar<double> f = fmax(z, x);
  EXPECT_FLOAT_EQ(2.0, f.val_);
  EXPECT_FLOAT_EQ(1.0, f.d_);
 }
コード例 #13
0
ファイル: acos_test.cpp プロジェクト: aseyboldt/math
TEST(AgradFwdAcos,Fvar) {
  using stan::math::fvar;
  using std::acos;
  using std::sqrt;
  using std::isnan;
  using stan::math::NEGATIVE_INFTY;

  fvar<double> x(0.5,1.0);
  
  fvar<double> a = acos(x);
  EXPECT_FLOAT_EQ(acos(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / -sqrt(1 - 0.5 * 0.5), a.d_);

  fvar<double> b = 2 * acos(x) + 4;
  EXPECT_FLOAT_EQ(2 * acos(0.5) + 4, b.val_);
  EXPECT_FLOAT_EQ(2 / -sqrt(1 - 0.5 * 0.5), b.d_);

  fvar<double> c = -acos(x) + 5;
  EXPECT_FLOAT_EQ(-acos(0.5) + 5, c.val_);
  EXPECT_FLOAT_EQ(-1 / -sqrt(1 - 0.5 * 0.5), c.d_);

  fvar<double> d = -3 * acos(x) + 5 * x;
  EXPECT_FLOAT_EQ(-3 * acos(0.5) + 5 * 0.5, d.val_);
  EXPECT_FLOAT_EQ(-3 / -sqrt(1 - 0.5 * 0.5) + 5, d.d_);

  fvar<double> y(3.4);
  y.d_ = 1.0;
  fvar<double> e = acos(y);
  isnan(e.val_);
  isnan(e.d_);

  fvar<double> z(1.0);
  z.d_ = 1.0;
  fvar<double> f = acos(z);
  EXPECT_FLOAT_EQ(acos(1.0), f.val_);
  EXPECT_FLOAT_EQ(NEGATIVE_INFTY, f.d_);

  fvar<double> z2(1.0+stan::math::EPSILON,1.0);
  fvar<double> f2 = acos(z2);
  EXPECT_TRUE(boost::math::isnan(f2.val_));
  EXPECT_TRUE(boost::math::isnan(f2.d_));

  fvar<double> z3(-1.0-stan::math::EPSILON,1.0);
  fvar<double> f3 = acos(z3);
  EXPECT_TRUE(boost::math::isnan(f3.val_));
  EXPECT_TRUE(boost::math::isnan(f3.d_));
}
コード例 #14
0
/* solve a multi contact problem by using a global successive overrelaxation method
 * with a local nonlinear solver
 */
prox_result reference_sequential_g_sor_prox::solve_multi_contact_problem_sor() {
  bool converged          = false;
  bool diverged           = false;
  unsigned int iteration  = 0;
  while(!converged && !diverged && iteration < m_max_global_iterations) {
    converged = true;
    
    for(index_t i = 0; i < m_contacts.size(); ++i) {
      contact & ci = m_contacts[i];
      vec3 rhs = ci.c;
      index_t cbegin = m_gij_rows[i];
      index_t cend   = m_gij_rows[i + 1];
      
      //step 0. get contributions from all the other contacts (gij off-diagonal terms)
      for(index_t j = cbegin; j < cend; ++j) {
        index_t cj = m_gij_columns[j];
        rhs = rhs + m_gij_blocks[j] * m_percussions[cj];
      }
      
      vec3 pold = m_percussions[i];
      //step 1. solve a single contact under the assumption, that all others are known
      vec3 pnew = solve_one_contact_problem_alart_curnier(ci, pold, rhs, m_tol_rel, m_tol_abs);
      
      using std::abs;
      //step 2. check for global convergence
      converged &= 
      abs(pnew[0] - pold[0]) <= m_tol_rel * abs(pnew[0]) + m_tol_abs
      &&  abs(pnew[1] - pold[1]) <= m_tol_rel * abs(pnew[1]) + m_tol_abs
      &&  abs(pnew[2] - pold[2]) <= m_tol_rel * abs(pnew[2]) + m_tol_abs;
      //and check whether a force became infinite or NaN
      using std::isinf; using std::isnan;
      diverged 
      |= isinf(pnew[0]) || isnan(pnew[0])
      || isinf(pnew[1]) || isnan(pnew[1])
      || isinf(pnew[2]) || isnan(pnew[2]);
      m_percussions[i] = pnew;
    }
    ++iteration;
  }
  std::cout << "done\n    # iterations = " << iteration << std::endl;
  return
  converged ? CONVERGED
  : (diverged ? DIVERGED
     : (!(iteration < m_max_global_iterations) ? ITERATION_LIMIT_REACHED
        /*: (time_limit_reached ? TIME_LIMIT_REACHED */: OOPS/*)*/));
}
コード例 #15
0
ファイル: fdim_test.cpp プロジェクト: b11z/math
TEST(AgradFwdFdim,FvarVar_FvarVar_2ndDeriv) {
  using stan::math::fvar;
  using stan::math::var;
  using stan::math::fdim;
  using std::floor;
  using std::isnan;

  fvar<var> x(2.5,1.3);
  fvar<var> z(1.5,1.0);
  fvar<var> a = fdim(x,z);

  AVEC y = createAVEC(x.val_,z.val_);
  VEC g;
  a.d_.grad(y,g);
  isnan(g[0]);
  isnan(g[1]);
}
コード例 #16
0
 std::pair<bool, bool> multicolor_parallel_g_sor_prox::work_function(
                                            sub_problem const & sub,
                                            index_t & l_i,
                                            index_t & g_i,
                                            index_t l_end,
                                            std::vector<vec3> & percussions,
                                            real tol_rel, real tol_abs,
                                            index_t max_local_iterations
                                            ) {
   bool diverged   = false;
   bool converged  = true;
   
   for(; l_i < l_end; ++l_i, ++g_i) {
     contact const & ci = sub.contacts[l_i];
     vec3 rhs = ci.c;
     index_t cbegin = sub.gij_rows[l_i];
     index_t cend   = sub.gij_rows[l_i + 1];
     
     //step 0. get contributions from all the other contacts (gij off-diagonal terms)
     for(index_t j = cbegin; j < cend; ++j) {
       index_t g_j = sub.gij_columns[j];
       rhs = rhs + sub.gij_blocks[j] * percussions[g_j];
     }
     
     vec3 pold = percussions[g_i];
     //step 1. solve a single contact under the assumption, that all others are known
     vec3 pnew = solve_one_contact_problem_alart_curnier(ci, pold, rhs, tol_rel, tol_abs, max_local_iterations);
     
     using std::abs;
     //step 2. check for global convergence
     converged &= 
     abs(pnew[0] - pold[0]) <= tol_rel * abs(pnew[0]) + tol_abs
     && abs(pnew[1] - pold[1]) <= tol_rel * abs(pnew[1]) + tol_abs
     && abs(pnew[2] - pold[2]) <= tol_rel * abs(pnew[2]) + tol_abs;
     //and check whether a force became infinite or NaN
     using std::isinf; using std::isnan;
     diverged 
     |= isinf(pnew[0]) || isnan(pnew[0])
     || isinf(pnew[1]) || isnan(pnew[1])
     || isinf(pnew[2]) || isnan(pnew[2]);
     percussions[g_i] = pnew;
     
   }
   return std::make_pair(converged, diverged);
 }
コード例 #17
0
ファイル: fdim_test.cpp プロジェクト: b11z/math
TEST(AgradFwdFdim,Double_FvarVar_1stDeriv) {
  using stan::math::fvar;
  using stan::math::var;
  using stan::math::fdim;
  using std::floor;
  using std::isnan;

  double x(2.5);
  fvar<var> z(1.5,1.0);
  fvar<var> a = fdim(x,z);

  EXPECT_FLOAT_EQ(fdim(2.5,1.5), a.val_.val());
  isnan(a.d_.val());

  AVEC y = createAVEC(z.val_);
  VEC g;
  a.val_.grad(y,g);
  isnan(g[0]);
}
コード例 #18
0
ファイル: idiff.cpp プロジェクト: 400notout/oiio
// function that standarize printing NaN and Inf values on
// Windows (where they are in 1.#INF, 1.#NAN format) and all
// others platform
inline void
safe_double_print (double val)
{
    if (isnan (val))
        std::cout << "nan";
    else if (isinf (val))
        std::cout << "inf";
    else
        std::cout << val;
    std::cout << '\n';
}
コード例 #19
0
ファイル: log1p_test.cpp プロジェクト: actuariat/stan
TEST(AgradFwdLog1p,Fvar) {
  using stan::agrad::fvar;
  using stan::math::log1p;
  using std::isnan;

  fvar<double> x(0.5,1.0);
  fvar<double> y(-1.0,2.0);
  fvar<double> z(-2.0,3.0);

  fvar<double> a = log1p(x);
  EXPECT_FLOAT_EQ(log1p(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / (1 + 0.5), a.d_);

  fvar<double> b = log1p(y);
  isnan(b.val_);
  isnan(b.d_);

  fvar<double> c = log1p(z);
  isnan(c.val_);
  isnan(c.d_);
}
コード例 #20
0
ファイル: dp-matrix.C プロジェクト: sibonli/BAli-Phy
void DPmatrix::compute_Pr_sum_all_paths()
{
  const int I = size1()-1;
  const int J = size2()-1;

  double total = 0.0;
  for(int state1=0;state1<nstates();state1++)
    total += (*this)(I,J,state1)*GQ(state1,endstate());

  Pr_total = pow(efloat_t(2.0),scale(I,J)) * total;
  assert(not isnan(log(Pr_total)) and isfinite(log(Pr_total)));
}
コード例 #21
0
ファイル: params.cpp プロジェクト: Strongc/szarp
void Params::param_value_changed( iterator itr , double value , ProbeType pt )
{
	if( itr.itr == params.end() ) {
		sz_log(1, "Value changed of undefined param %s", itr->c_str() );
		return;
	}

	using std::isnan;

	auto pvalue = itr.itr->second->get_value( pt );
	if( pt.get_type() == ProbeType::Type::LIVE
	 && (value == pvalue || (isnan(value) && isnan(pvalue))) )
		/**
		 * With LIVE probes if values are equal even
		 * if both are NaNs do nothing. For other probe
		 * types always send update.
		 */
		return;

	itr.itr->second->set_value( value , pt );
	emit_value_changed( itr.itr->second , value , pt );
}
コード例 #22
0
ファイル: asin_test.cpp プロジェクト: danstowell/stan
TEST(AgradFvar, asin) {
  using stan::agrad::fvar;
  using std::asin;
  using std::isnan;
  using std::sqrt;
  using stan::math::INFTY;

  fvar<double> x(0.5);
  x.d_ = 1.0;   // derivatives w.r.t. x
  
  fvar<double> a = asin(x);
  EXPECT_FLOAT_EQ(asin(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / sqrt(1 - 0.5 * 0.5), a.d_);

  fvar<double> b = 2 * asin(x) + 4;
  EXPECT_FLOAT_EQ(2 * asin(0.5) + 4, b.val_);
  EXPECT_FLOAT_EQ(2 / sqrt(1 - 0.5 * 0.5), b.d_);

  fvar<double> c = -asin(x) + 5;
  EXPECT_FLOAT_EQ(-asin(0.5) + 5, c.val_);
  EXPECT_FLOAT_EQ(-1 / sqrt(1 - 0.5 * 0.5), c.d_);

  fvar<double> d = -3 * asin(x) + 5 * x;
  EXPECT_FLOAT_EQ(-3 * asin(0.5) + 5 * 0.5, d.val_);
  EXPECT_FLOAT_EQ(-3 / sqrt(1 - 0.5 * 0.5) + 5, d.d_);

  fvar<double> y(3.4);
  y.d_ = 1.0;
  fvar<double> e = asin(y);
  isnan(e.val_);
  isnan(e.d_);

  fvar<double> z(1.0);
  z.d_ = 1.0;
  fvar<double> f = asin(z);
  EXPECT_FLOAT_EQ(asin(1.0), f.val_);
  EXPECT_FLOAT_EQ(INFTY, f.d_);
}
コード例 #23
0
ファイル: dp-matrix.C プロジェクト: sibonli/BAli-Phy
void DPmatrixConstrained::compute_Pr_sum_all_paths()
{
  const int I = size1()-1;
  const int J = size2()-1;

  double total = 0.0;
  for(int s1=0;s1<states(J).size();s1++) {
    int S1 = states(J)[s1];
    total += (*this)(I,J,S1)*GQ(S1,endstate());
  }

  Pr_total = pow(efloat_t(2.0),scale(I,J)) * total;
  assert(not isnan(log(Pr_total)) and isfinite(log(Pr_total)));
}
コード例 #24
0
ファイル: logit_test.cpp プロジェクト: javaosos/stan
TEST_F(AgradFwdLogit,Fvar) {
  using stan::agrad::fvar;
  using stan::math::logit;
  using std::isnan;

  fvar<double> x(0.5,1.0);

  fvar<double> a = logit(x);
  EXPECT_FLOAT_EQ(logit(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / (0.5 - 0.5 * 0.5), a.d_);

  fvar<double> y(-1.2,1.0);

  fvar<double> b = logit(y);
  isnan(b.val_);
  isnan(b.d_);

  fvar<double> z(1.5,1.0);

  fvar<double> c = logit(z);
  isnan(c.val_);
  isnan(c.d_);
}
コード例 #25
0
ファイル: sqrt_test.cpp プロジェクト: javaosos/stan
TEST(AgradFwdSqrt, Fvar) {
  using stan::agrad::fvar;
  using std::sqrt;
  using std::isnan;

  fvar<double> x(0.5,1.0);
  fvar<double> a = sqrt(x);

  EXPECT_FLOAT_EQ(sqrt(0.5), a.val_);
  EXPECT_FLOAT_EQ(1 / (2 * sqrt(0.5)), a.d_);

  fvar<double> b = 3 * sqrt(x) + x;
  EXPECT_FLOAT_EQ(3 * sqrt(0.5) + 0.5, b.val_);
  EXPECT_FLOAT_EQ(3 / (2 * sqrt(0.5)) + 1, b.d_);

  fvar<double> c = -sqrt(x) + 5;
  EXPECT_FLOAT_EQ(-sqrt(0.5) + 5, c.val_);
  EXPECT_FLOAT_EQ(-1 / (2 * sqrt(0.5)), c.d_);

  fvar<double> d = -3 * sqrt(x) + 5 * x;
  EXPECT_FLOAT_EQ(-3 * sqrt(0.5) + 5 * 0.5, d.val_);
  EXPECT_FLOAT_EQ(-3 / (2 * sqrt(0.5)) + 5, d.d_);

  fvar<double> y(-0.5,1.0);
  fvar<double> e = sqrt(-y);
  EXPECT_FLOAT_EQ(sqrt(0.5), e.val_);
  EXPECT_FLOAT_EQ(-1 / (2 * sqrt(0.5)), e.d_); 

  fvar<double> f = sqrt(y);
  isnan(f.val_);
  isnan(f.d_);

  fvar<double> z(0.0,1.0);
  fvar<double> g = sqrt(z);
  EXPECT_FLOAT_EQ(sqrt(0.0), g.val_);
  isnan(g.d_); 
}
コード例 #26
0
ファイル: String.cpp プロジェクト: scottphilbrook84/libsylph
String::String(const double d) {
    if (isinf(d)) {
        if (copysign(1.0, d) == -1.0) {
            fromAscii("-Infinity");
        } else {
            fromAscii("Infinity");
        }
    } else if (isnan(d)) {
        fromAscii("NaN");
    } else {
        char * buf = static_cast<char*>(GC_MALLOC_ATOMIC(20));
        sprintf(buf,"%.16g",d);
        fromAscii(buf);
    }
}
コード例 #27
0
ファイル: String.cpp プロジェクト: scottphilbrook84/libsylph
String::String(const float f) {
    if (isinf(f)) {
        if (copysign(1.0, f) == -1.0) {
            fromAscii("-Infinity");
        } else {
            fromAscii("Infinity");
        }
    } else if (isnan(f)) {
        fromAscii("NaN");
    } else {
        char * buf = static_cast<char*>(GC_MALLOC_ATOMIC(20));
        sprintf(buf,"%.7g",f);
        fromAscii(buf);
    }
}
コード例 #28
0
void
KneserNeySmoothing::_Estimate(ProbVector &probs, ProbVector &bows) {
    const IndexVector &hists(_pLM->hists(_order));
    const IndexVector &backoffs(_pLM->backoffs(_order));
    const ProbVector & boProbs(_pLM->probs(_order - 1));

    // Compute discounts.
    ProbVector discounts(probs.length(), 0.0);  // Reuse probs vector for discounts.
    discounts = _discParams[min(_effCounts, _discOrder)];

    // Compute backoff weights.
    bows.set(0);
    BinWeight(hists, discounts, bows);
//     if (_order == 4) std::cerr << bows[hists[8]] << std::endl;
//     bows = CondExpr(_invHistCounts == 0, 1, bows * _invHistCounts);
    bows = bows * _invHistCounts;
    assert(allTrue(isfinite(_invHistCounts)));
    assert(allTrue(isfinite(bows)));
    
    // Compute interpolated probabilities.
    if (_order == 1 && !_pLM->vocab().IsFixedVocab())
        probs = CondExpr(!_effCounts, 0,
                         (_effCounts - discounts) * _invHistCounts[hists]
                         + boProbs[backoffs] * bows[hists]);
    else
        probs = CondExpr(!_effCounts, 0,
                         (_effCounts - discounts) * _invHistCounts[hists])
                     + boProbs[backoffs] * bows[hists]
        ;
//     if (_order == 4) std::cerr << probs[8] 
//       << "\t" << _effCounts[8]
//       << "\t" << discounts[8]
//       << "\t" << (1/_invHistCounts[hists[8]])
//       << "\t" << backoffs[8]
//       << "\t" << boProbs[backoffs[8]]
//       << "\t" << bows[hists[8]]
//       << std::endl;
//     if (_order == 4) std::cerr << probs[9] 
//       << "\t" << _effCounts[9]
//       << "\t" << discounts[9]
//       << "\t" << (1/_invHistCounts[hists[9]])
//       << "\t" << backoffs[9]
//       << "\t" << boProbs[backoffs[9]]
//       << "\t" << bows[hists[9]]
//       << std::endl;
//     if (_order == 4) std::cerr << _discParams << std::endl;
    assert(!anyTrue(isnan(probs)));
}
コード例 #29
0
ファイル: fdim_test.cpp プロジェクト: b11z/math
TEST(AgradFwdFdim,Double_FvarVar_2nd_Deriv) {
  using stan::math::fvar;
  using stan::math::var;
  using stan::math::fdim;
  using std::floor;
  using std::isnan;

  double x(2.5);
  fvar<var> z(1.5,1.0);
  fvar<var> a = fdim(x,z);

  AVEC y = createAVEC(z.val_);
  VEC g;
  a.d_.grad(y,g);
  isnan(g[0]);
}
コード例 #30
0
ファイル: ColorLookup.cpp プロジェクト: lidas123/openfx-misc
    // and do some processing
    void multiThreadProcessImages(OfxRectI procWindow)
    {
        assert(nComponents == 1 || nComponents == 3 || nComponents == 4);
        assert(_dstImg);
        float tmpPix[nComponents];
        for (int y = procWindow.y1; y < procWindow.y2; y++) {
            if (_effect.abort()) {
                break;
            }

            PIX *dstPix = (PIX *) _dstImg->getPixelAddress(procWindow.x1, y);

            for (int x = procWindow.x1; x < procWindow.x2; x++)  {
                const PIX *srcPix = (const PIX *)  (_srcImg ? _srcImg->getPixelAddress(x, y) : 0);
                if (nComponents == 1 || nComponents == 3) {
                    // RGB and Alpha: don't premult/unpremult, just apply curves
                    // normalize/denormalize properly
                    for (int c = 0; c < nComponents; ++c) {
                        tmpPix[c] = (float)interpolate(c, srcPix ? (srcPix[c] / (float)maxValue) : 0.f) * maxValue;
                        assert((!srcPix || (!isnan(srcPix[c]) && !isnan(srcPix[c]))) &&
                               !isnan(tmpPix[c]) && !isnan(tmpPix[c]));
                    }
                    // ofxsMaskMix expects denormalized input
                    ofxsMaskMixPix<PIX, nComponents, maxValue, true>(tmpPix, x, y, srcPix, _doMasking, _maskImg, (float)_mix, _maskInvert, dstPix);
                } else {
                    //assert(nComponents == 4);
                    float unpPix[nComponents];
                    ofxsUnPremult<PIX, nComponents, maxValue>(srcPix, unpPix, _premult, _premultChannel);
                    // ofxsUnPremult outputs normalized data
                    for (int c = 0; c < nComponents; ++c) {
                        tmpPix[c] = interpolate(c, unpPix[c]);
                        assert(!isnan(unpPix[c]) && !isnan(unpPix[c]) &&
                               !isnan(tmpPix[c]) && !isnan(tmpPix[c]));
                    }
                    // ofxsPremultMaskMixPix expects normalized input
                    ofxsPremultMaskMixPix<PIX, nComponents, maxValue, true>(tmpPix, _premult, _premultChannel, x, y, srcPix, _doMasking, _maskImg, (float)_mix, _maskInvert, dstPix);
                }
                // increment the dst pixel
                dstPix += nComponents;
            }
        }
    }