double integ(VEC &X,VEC &Y,int n){ // composite nth order Newton-Cotes integral
    int iter = (X.len()-1)/n;
    //printf("iter : %d\n",iter);
    double space = X[X.len()] - X[0];
    double h = space/(X.len()-1);
    double sum = 0.0;
    VEC w(n+1);
    if(n == 1)
	w[0] = w[1] = 0.5;
    else if (n == 2)
	w[0] = w[2] =  1./3. , w[1] = 4./3.;
    else if (n == 3)
	w[0] = w[3] =  3./8. , w[1] = w[2] = 9./8.;
    else if (n == 4)
	w[0] = w[4] =  14./45. , w[1] = w[3] = 64./45. , w[2] = 24./45.;
    else if (n == 5)
	w[0] = w[5] =  95./288. , w[1] = w[4] = 375./288. , w[2] = w[3] = 250./288.;
    else if (n == 6)
	w[0] = w[6] =  41./140. , w[1] = w[5] = 216./140. , w[2] = w[4] = 27./140. , w[3] = 272./140.;
    /*for(int i = 0; i<=n;i++)
	printf("w%d = %f\n",i,w[i]);
	printf("space = %f\n h = %f\n",space,h);*/
    for(int i = 0; i< iter; i++){
	for(int j = 0; j<= n; j++){
	    sum += (w[j] * Y[ n*i + j ]);
	    //printf("%d\n",n*i+j);
	}
    }
    return sum * h;
  
} 
Exemple #2
0
//writes file in format:
//x_1   y_m     z_1_m
//x_2   y_m     z_1_m
//x_3   y_m     z_1_m
//      .
//      .
//x_n   y_m     z_1_(m-1)
//x_1   y_(m_1) z_1_(m-1)
//      .
//      .
//x_n   y_1     z_n_1
bool XYZ_Writer::writeToFile(const VEC &x,
                             const VEC &y, 
                             const vector<VEC> &z)
{
    ofstream fhandle(m_fileName.c_str(), ios::out);

    //get size from vectors
    size_t n = x.size();
    size_t m = y.size();

    double xVal, yVal, zVal;
    size_t xIdx, yIdx;
    for(size_t i = 0; i != m*n; ++i)
    {
        //write x value
        xIdx = i % n;
        xVal = x[xIdx];
        fhandle << xVal << " ";

        //write y value
        yIdx = m - 1 - (i - (i%n))/n;
        yVal = y[yIdx];
        fhandle << yVal << " ";

        //write z value
        zVal = isnan(z[yIdx][xIdx]) ? 0.0 : z[yIdx][xIdx];


        fhandle << zVal << endl;
    }

    return true;
}
Exemple #3
0
void test_log_diff_exp_2_vv(double a_val, 
                           double b_val) {
  using std::exp;
  using std::log;
  using stan::math::log_diff_exp;

  AVAR a(a_val);
  AVAR b(b_val);

  AVEC x = createAVEC(a,b);
  AVAR f = log_diff_exp(a,b);
  VEC g;
  f.grad(x,g);
  
  double f_val = f.val();

  stan::agrad::var a2(a_val);
  stan::agrad::var b2(b_val);
  AVEC x2 = createAVEC(a2,b2);
  AVAR f2 = log(exp(a2) - exp(b2));
  VEC g2;
  f2.grad(x2,g2);

  EXPECT_FLOAT_EQ(f2.val(), f_val);
  EXPECT_EQ(2U,g.size());
  EXPECT_EQ(2U,g2.size());
  EXPECT_FLOAT_EQ(g2[0],g[0]);
  EXPECT_FLOAT_EQ(g2[1],g[1]);
}
Exemple #4
0
void test_log1m_exp(double val) {
  using stan::math::log1m_exp;
  using stan::agrad::log1m_exp;
  using stan::agrad::exp;
  using std::exp;

  AVAR a(val);   
  AVEC x = createAVEC(a);
  AVAR f = log1m_exp(a);
  EXPECT_FLOAT_EQ(log1m_exp(val), f.val());
  VEC g;
  f.grad(x,g);
  double f_val = f.val();
  
  AVAR a2(val);
  AVEC x2 = createAVEC(a2);
  AVAR f2 = log(1.0 - exp(a2));
  VEC g2;
  f2.grad(x2,g2);

  EXPECT_EQ(1U,g.size());
  EXPECT_EQ(1U,g2.size());
  EXPECT_FLOAT_EQ(g2[0],g[0]);
  EXPECT_FLOAT_EQ(g2[0],-1/boost::math::expm1(-val)); // analytic deriv
  EXPECT_FLOAT_EQ(f2.val(),f_val);
}
Exemple #5
0
vector<HTML *> HTMLGraph(T * parent) {
  vector<HTML*> result;
  bool border = true;
  typedef vector<T*> VEC;
  VEC V;
  parent->GetGraphChildren(V);
  VEC::const_iterator w = V.begin(), e = V.end();
  HTML * child;
  HTML * aroot;
  while(w!=e) {
    child = *w;
    table = new HTMLTable(border);
    result.push_back(table); 
    table->add(p->HTMLGraphNode(),1,1);
    vector<HTML *> L;
    child->GetGraphChildren(L);
    vector<HTML*>::const_iterator ww = L.begin(), ee = L.end();
    if(ww!=ee) {
      HTMLTable * subtable = new HTMLTable(border);
      table->add(subtable,1,2);
      int i=1;
      while(ww!=ee) {
        subtable->add(*ww,1,i);
        ++ww;++i;
      };
    };
    ++w;
  };
};
Exemple #6
0
MTRX generateMatrix(Mnd mnd) {
  MTRX output;
  int mn = mnd.m * mnd.n;
  int m = mnd.m;
  
  for (int mn1 = 0; mn1 < mn; mn1++) {
    int m1 = mn1 / m;
    int n1 = mn1 % m;
    VEC v;
    for (int mn2 = 0; mn2 < mn; mn2++) {
      int m2 = mn2 / m;
      int n2 = mn2 % m;
      if (mn1 == mn2 || abs(m1 - m2) + abs(n1 - n2) == mnd.d) {
        v.push_back(1);
      } else {
        v.push_back(0);
      }
    }
    for (int mn3 = 0; mn3 < mn; mn3++) {
      v.push_back(mn1 == mn3 ? 1 : 0);
    }
    output.push_back(v);
  }
  return output;
}
Exemple #7
0
void test_log_diff_exp_2_dv(double a,
                           double b_val) {
  using std::exp;
  using std::log;
  using stan::math::log_diff_exp;

  AVAR b(b_val);
  AVEC x = createAVEC(b);
  AVAR f = log_diff_exp(a,b);
  VEC g;
  f.grad(x,g);
  
  double f_val = f.val();

  AVAR b2(b_val);
  AVEC x2 = createAVEC(b2);
  AVAR f2 = log(exp(a) - exp(b2));
  VEC g2;
  f2.grad(x2,g2);

  EXPECT_FLOAT_EQ(f2.val(), f_val);
  EXPECT_EQ(1U,g.size());
  EXPECT_EQ(1U,g2.size());
  EXPECT_FLOAT_EQ(g2[0],g[0]);
  
}
Exemple #8
0
 static std::size_t choose_next(VEC const& weights, std::size_t present, RNG& rng){
   double sum = *(std::max_element(weights.begin(), weights.end()));
   sum -= weights[present] * rng();
   for (int i = 0; i < weights.size(); ++i) {
     int j = (present + i + 1) % weights.size();
     if (sum <= weights[j]) return j;
     sum -= weights[j];
   }
   return present;
 }
void TestSslowlog(){
    CRedisClient redis;
    redis.connect( "127.0.0.1", 6379 );
    cout << "------test slowlog------" << endl;
    VEC vec;
    vec.push_back("GET");
    CResult res;
    redis.slowlog(vec,res);
    cout<<res<<endl;
}
Exemple #10
0
TEST(AgradRev,asin_out_of_bounds2) {
  AVAR a = -1.0 - stan::math::EPSILON;
  AVAR f = asin(a);
  AVEC x = createAVEC(a);
  VEC g;
  f.grad(x,g);
  EXPECT_TRUE(std::isnan(asin(a)));
  EXPECT_TRUE(g.size() == 1);
  EXPECT_TRUE(std::isnan(g[0]));
}
Exemple #11
0
TEST(AgradRev,abs_var_3) {
  AVAR a = 0.0;
  AVAR f = abs(a);
  EXPECT_FLOAT_EQ(0.0, f.val());

  AVEC x = createAVEC(a);
  VEC g;
  f.grad(x,g);
  EXPECT_EQ(1,g.size());
  EXPECT_FLOAT_EQ(0.0, g[0]);
}
void TestServerPrint(const string& cmd,VEC& vec)
{
    cout<<cmd<<":"<<endl;
    VEC::const_iterator it = vec.begin();
    VEC::const_iterator end = vec.end();
    for ( ; it != end; ++it )
    {
        cout<<*it<<endl;
    }
    cout<<endl;
}
Exemple #13
0
VEC multiply(const MTRX &matrix, const VEC &row) {
  VEC output;
  for (int i = 0; i < matrix.size(); i++) {
    int sum = 0;
    for (int j = 0; j < matrix[i].size(); j++) {
      sum += matrix[i][j] * row[j];
    }
    output.push_back(sum % 2);
  }
  return output;
}
Exemple #14
0
/** @brief rotate in the given direction
 * @tparam VEC point type including POINT::Scalar as coord type
 * @param _direction vector which points in the desired direction
 */
template<class VEC> void glRotate(const VEC& _direction)
{
  // get coordinate type from vector type
  typedef typename VEC::scalar_type Scalar;
  // pre-calculate vector length
  Scalar length = _direction.length();
  // calculate phi and theta (@link http://de.wikipedia.org/wiki/Kugelkoordinaten#.C3.9Cbliche_Konvention)
  Scalar phi = tomo::rad2deg( atan2(_direction.y(), _direction.x()) );
  Scalar theta = (0.0 != length) ? tomo::rad2deg(acos( (_direction.z() / length) )) : 0.0;
  // rotate GL world
  glRotate(phi,theta);
}
Exemple #15
0
TEST(AgradRev,abs_NaN) {
  AVAR a = std::numeric_limits<double>::quiet_NaN();
  AVAR f = abs(a);

  AVEC x = createAVEC(a);
  VEC g;
  f.grad(x,g);
  
  EXPECT_TRUE(boost::math::isnan(f.val()));
  ASSERT_EQ(1,g.size());
  EXPECT_TRUE(boost::math::isnan(g[0]));
}
Exemple #16
0
void BoundingBox<VEC>::fusion(const BoundingBox<VEC>& bb)
{
	assert(m_initialized || !"Bounding box not initialized");
	VEC bbmin = bb.min() ;
	VEC bbmax = bb.max() ;
	for(unsigned int i = 0; i < bbmin.dimension(); ++i)
	{
		if(bbmin[i] < m_pMin[i])
			m_pMin[i] = bbmin[i] ;
		if(bbmax[i] > m_pMax[i])
			m_pMax[i] = bbmax[i] ;
	}
}
Exemple #17
0
MTRX preSolve(Mnd mnd) {
  MTRX matrix = generateMatrix(mnd);
  int rank = gaussianElimination(matrix);
  MTRX output;
  for (int i = rank; i < matrix.size(); i++) {
    VEC v;
    for (int j = matrix[0].size()/2; j < matrix[0].size(); j++) {
      v.push_back(matrix[i][j]);
    }
    output.push_back(v);
  }
  return output;
}
static void findLocalMinima ( const VEC& intData, vector<mz_uint>& minIndexes) {
    for (size_t i = 0; i < intData.size(); ++i) {
        if ( i == 0) {
            if (intData[i + 1] > intData[i])
                minIndexes.push_back(i);
        } else if ( i == intData.size() - 1) {
            if ( intData[i - 1] > intData[i] )
                minIndexes.push_back(i);
        } else {
            if (intData[i - 1] > intData[i] && intData[i + 1] > intData[i])
                minIndexes.push_back(i);
        }
    }
}
static void findLocalMaxima ( const VEC& intData, vector<mz_uint>& maxIndexes, float threshold) {
    for (size_t i = 0; i < intData.size(); ++i) {
        if ( i == 0) {
            if (intData[i + 1] < intData[i] && intData[i] > threshold)
                maxIndexes.push_back(i);
        } else if ( i == intData.size() - 1 && intData[i] > threshold) {
            if ( intData[i - 1] < intData[i] )
                maxIndexes.push_back(i);
        } else {
            if (intData[i - 1] < intData[i] && intData[i + 1] < intData[i] && intData[i] > threshold)
                maxIndexes.push_back(i);
        }
    }
}
Exemple #20
0
bool BoundingBox<VEC>::intersects(const BoundingBox<VEC>& bb)
{
	assert(m_initialized || !"Bounding box not initialized");
	VEC bbmin = bb.min() ;
	VEC bbmax = bb.max() ;
	for(unsigned int i = 0; i < bbmin.dimension(); ++i)
	{
		if(m_pMax[i] < bbmin[i])
			return false ;
		if(m_pMin[i] > bbmax[i])
			return false ;
	}
	return true ;
}
VEC polyRoots(double x,VEC &A,int maxiter, double eps){
	int n = A.leng()-1;
	double err,f,df,B_i,C_i;
	VEC Z(n);
	int k;
	VEC B(n+1);
	VEC C(n+1);
	while(n>=1){
		err = 1+eps;
		k = 0;
		while((err>=eps)&&(k<maxiter)){
			B[n-1] = A[n];
			C[n-1] = B[n-1];
			for(int j=n-2;j>=0;j--)
				B[j] = A[j+1]+x*B[j+1];
			for(int j=n-3;j>=0;j--)
				C[j] = B[j+1]+x*C[j+1];
			B_i = A[0]+x*B[0];
			C_i = B[0]+x*C[0];
			f = B_i;
			df = C_i;
			x = x - f/df;
			err = fabs(f);
			k++;
		}
		Z[n-1] = x;
		for(int j=0;j<n;j++)
			A[j] = B[j];
		x = Z[n-1];
		n--;
	}
	return Z;
}
Exemple #22
0
TEST(AgradRevMatrix, dot_product_dv_vec) {
  VEC a;
  AVEC b;
  AVAR c;
  for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3)
    a.push_back(i);
    b.push_back(i + 2);
  }
  c = dot_product(a, b);
  EXPECT_EQ(2, c);
  VEC grad;
  c.grad(b, grad);
  EXPECT_EQ(grad[0], -1);
  EXPECT_EQ(grad[1], 0);
  EXPECT_EQ(grad[2], 1);
}
Exemple #23
0
TEST(AgradRevMatrix, dot_product_vd) {
  AVEC a;
  VEC b;
  AVAR c;
  for (int i = -1; i < 2; i++) { // a = (-1, 0, 1), b = (1, 2, 3)
    a.push_back(i);
    b.push_back(i + 2);
  }
  c = dot_product(&a[0], &b[0], 3);
  EXPECT_EQ(2, c);
  VEC grad;
  c.grad(a, grad);
  EXPECT_EQ(grad[0], 1);
  EXPECT_EQ(grad[1], 2);
  EXPECT_EQ(grad[2], 3);
}
Exemple #24
0
//----------------------------------------------------------------------
EEMD::EEMD( const VEC& input_array ) {

    signal_length = input_array.size();
    input_signal = input_array;

    tools = new Tools();
}   
Exemple #25
0
TEST(AgradRevMatrix, meanStdVector) {
  using stan::math::mean; // should use arg-dep lookup
  AVEC x(0);
  EXPECT_THROW(mean(x), std::domain_error);
  x.push_back(1.0);
  EXPECT_FLOAT_EQ(1.0, mean(x).val());
  x.push_back(2.0);
  EXPECT_FLOAT_EQ(1.5, mean(x).val());

  AVEC y = createAVEC(1.0,2.0);
  AVAR f = mean(y);
  VEC grad = cgrad(f, y[0], y[1]);
  EXPECT_FLOAT_EQ(0.5, grad[0]);
  EXPECT_FLOAT_EQ(0.5, grad[1]);
  EXPECT_EQ(2U, grad.size());
}
double l1norm(VEC x){
    double sum = 0.0;
    for (int i = 0; i < x.len(); i++){
	sum += std::abs(x[i]);
    }
    return sum;
}
TEST(AgradRevMatrix,mdivide_left_ldlt_finite_diff_vv) {
  using stan::math::matrix_d;
  using stan::agrad::matrix_v;
  using stan::math::multiply;
  using stan::math::mdivide_left_spd;

  matrix_d Ad(2,2), Ad_tmp(2,2);
  matrix_d Bd(2,2), Bd_tmp(2,2);
  matrix_d Cd(2,2);
  
  Ad << 2.0, 3.0, 
  3.0, 7.0;
  Bd << 12.0, 13.0, 
  15.0, 17.0;

  stan::math::LDLT_factor<double,-1,-1> ldlt_Ad;
  ldlt_Ad.compute(Ad);
  ASSERT_TRUE(ldlt_Ad.success());
  
  for (size_type i = 0; i < Bd.rows(); i++) {
    for (size_type j = 0; j < Bd.cols(); j++) {
      // compute derivatives
      matrix_v A(2,2), B(2,2), C;
      for (size_t k = 0; k < 4; k++) {
        A(k) = Ad(k);
        B(k) = Bd(k);
      }
      A(0,1) = A(1,0);
      stan::math::LDLT_factor<stan::agrad::var,-1,-1> ldlt_A;
      ldlt_A.compute(A);
      ASSERT_TRUE(ldlt_A.success());
      
      C = mdivide_left_ldlt(ldlt_A,B);
      AVEC x = createAVEC(A(0,0),A(0,1),A(0,1),A(1,1),
                          B(0,0),B(1,0),B(0,1),B(1,1));
      VEC gradient;
      C(i,j).grad(x,gradient);

      // compute finite differences
      VEC finite_diffs = finite_differences(i, j, Ad, true, Bd, true);
      
      ASSERT_EQ(gradient.size(), finite_diffs.size());
      for (size_t k = 0; k < gradient.size(); k++)
        EXPECT_NEAR(finite_diffs[k], gradient[k], 1e-4);
    }
  }
}
Exemple #28
0
//----------------------------------------------------------------------
VEC Tools::getRands(int n) {
if(VERBOSE) printf("\nVEC Tools::getRands(int n=%d)\n", n);
    VEC rands;
    rands.set_size(n);
    arma::Col<float>::iterator elem = rands.begin();
	float f;
    seedRandom();

	for (int i=0; i < n; i++) {
		f = (float)rand() / RAND_MAX;
		f = 2.0*f - 1.0;
		*elem = f;
        elem++;
	}

	return rands;
}
double l2norm(VEC x){
    double sum = 0.0;
    for(int i = 0; i< x.len(); i++){
	sum += (x[i]*x[i]);
    }
    
    return sqrt(sum);
}
Exemple #30
0
vector<HTML *> HTMLShallowGraph(T * parent) {
  vector<HTML*> result;
  bool border = true;
  typedef vector<T*> VEC;
  VEC V;
  parent->GetGraphChildren(V);
  VEC::const_iterator w = V.begin(), e = V.end();
  HTML * child;
  HTML * aroot;
  while(w!=e) {
    child = *w;
    table = new HTMLTable(border);
    result.push_back(table); 
    table->add(HTMLGraphNode(p),1,1);
    ++w;
  };
};