Exemple #1
0
double PowerSum::kurtosis() const throw()
{
    if (n<4)
        return 0;
    double m2 = moment(2);
    return moment(4)/(m2*m2);
}
 void test_moment(const size_t order, std::ostream &outStream = std::cout) const {
   try {
     const size_t numPts = 10000;
     Real pt = 0., wt = 1./(Real)numPts;
     std::vector<Real> mVec(order,0.);
     for (size_t i = 0; i < numPts; i++) {
       pt = invertCDF((Real)rand()/(Real)RAND_MAX);
       mVec[0] += wt*pt;
       for (size_t q = 1; q < order; q++) {
         mVec[q] += wt*std::pow(pt,q+1);
       }
     }
     outStream << std::scientific << std::setprecision(0);
     outStream << std::right << std::setw(20) << "CHECK DENSITY: Check first " << order
               << " moments against Monte Carlo using " << numPts << " samples"
               << std::endl;
     outStream << std::setw(20) << "Error should be O(" << 1./std::sqrt(numPts) << ")" << std::endl;
     outStream << std::scientific << std::setprecision(11);
     for (size_t q = 0; q < order; q++) {
       outStream << std::setw(20) << "Error in " << q+1 << " moment: "
                 << std::abs(mVec[q]-moment(q+1)) << std::endl;
     }
     outStream << std::endl;
   }
   catch(std::exception &e) {
     outStream << "moment is not implemented!"
               << std::endl << std::endl;
   }
 }
Exemple #3
0
double stat::stddev(const std::string& key) const {
  jubatus::util::data::unordered_map<std::string, stat_val>::const_iterator p =
      stats_.find(key);
  if (p == stats_.end()) {
    throw JUBATUS_EXCEPTION(stat_error("stddev: key " + key + " not found"));
  }
  const stat_val& st = p->second;
  return std::sqrt(moment(key, 2, st.sum_ / st.n_));
}
//http://klas-physics.googlecode.com/svn/trunk/src/general/Integrator.cpp (reference)
void CVX_Voxel::timeStep(float dt)
{
	previousDt = dt;
	if (dt == 0.0f) return;

	if (dofIsAllSet(dofFixed)){ //if this voxel is fixed, no need to change its location
		haltMotion();
		return;
	}

	//Translation
	Vec3D<> curForce = force();
	linMom += curForce*dt;

	if (dofIsSet(dofFixed, X_TRANSLATE)){linMom.x=0;}
	if (dofIsSet(dofFixed, Y_TRANSLATE)){linMom.y=0;}
	if (dofIsSet(dofFixed, Z_TRANSLATE)){linMom.z=0;}

	Vec3D<double> translate(linMom*(dt*mat->_massInverse)); //movement of the voxel this timestep

	if (isFloorEnabled() && floorPenetration() > 0){ //we must catch a slowing voxel here since it all boils down to needing access to the dt of this timestep.
		vfloat work = curForce.x*translate.x + curForce.y*translate.y; //F dot disp
		if(kineticEnergy() + work < 0) setFloorStaticFriction(true); //this checks for a change of direction according to the work-energy principle

		if (isFloorStaticFriction()){ //if we're in a state of static friction, zero out all horizontal motion
			linMom.x = linMom.y = 0;
			translate.x = translate.y = 0;
		}
	}

	pos += translate;

	//Rotation
	Vec3D<> curMoment = moment();
	angMom += curMoment*dt;

	if (dofIsSet(dofFixed, X_ROTATE)){angMom.x=0;}
	if (dofIsSet(dofFixed, Y_ROTATE)){angMom.y=0;}
	if (dofIsSet(dofFixed, Z_ROTATE)){angMom.z=0;}

	orient = CQuat<>(angularVelocity()*dt)*orient; //update the orientation




//	if(pSim->StatToCalc & CALCSTAT_KINE) KineticEnergy = 0.5*Mass*Vel.Length2() + 0.5*Inertia*AngVel.Length2(); //1/2 m v^2
//	if(pSim->StatToCalc & CALCSTAT_PRESSURE) {
////		vfloat VolumetricStrain = GetVoxelStrain(AXIS_X) + GetVoxelStrain(AXIS_Y) + GetVoxelStrain(AXIS_Z);
//		vfloat VolumetricStrain = strain(false).x+strain(false).y+strain(false).z;
//		Pressure = - _pMat->GetElasticMod()*VolumetricStrain/(3*(1-2*_pMat->GetPoissonsRatio())); //http://www.colorado.edu/engineering/CAS/courses.d/Structures.d/IAST.Lect05.d/IAST.Lect05.pdf
//	}
//
//	updatePoissonsstrain();
//
//

}
Exemple #5
0
void PowerSum::dump(std::ostream& str) const throw()
{
    str << "n:" << n;
    for (int i=1; i<=order; i++)
        str << " s" << i << ":" << s[i];
    str << std::endl;

    str << "m1:" << moment(1)
        << " m2:" << moment(2)
        << " m3:" << moment(3)
        << " m4:" << moment(4)
        << std::endl;

    str << "average:" << average()
        << " stddev:" << sqrt(variance())
        << " skew:" << skew()
        << " kurtosis:" << kurtosis()
        << std::endl;
}
Exemple #6
0
TEST_F(MomentTest, constructor) {
  cout << "moment: " << moment().format() << endl;
  EXPECT_EQ(Moment(1u).valueOf(), 1000);
  EXPECT_EQ(Moment(1u).unix(), 1);
  EXPECT_EQ(Moment(1u).format(), "1970-01-01 08:00:01.000");

  EXPECT_EQ(Moment(1ull).valueOf(), 1);
  EXPECT_EQ(Moment(1ull).unix(), 0);
  EXPECT_EQ(Moment(1000ull).unix(), 1);
  EXPECT_EQ(Moment(1ull).format(), "1970-01-01 08:00:00.001");

  EXPECT_EQ(Moment(1u, 1000000u).valueOf(), 1001);
  EXPECT_EQ(Moment(1u, 1000000u).format(), "1970-01-01 08:00:01.001");
}
Exemple #7
0
Foam::dimensionedVector Foam::sixDOFqODE::OmegaDot
(
    const HamiltonRodriguezRot& rotation,
    const dimensionedVector& omega
) const
{
    return 
        inv(momentOfInertia_)
      & (
            E(omega)
            // To relative
          + (rotation.R() & moment())
          + momentRelative()
        );
}
Exemple #8
0
bool path_statistics_3d(Basic_Path_Statistics & bps, unsigned char *** inimg_data3d, V3DLONG inimg_sz0, V3DLONG inimg_sz1, V3DLONG inimg_sz2, vector <Coord3D> & mCoord)
{
	if (!inimg_data3d || inimg_sz0<=0 || inimg_sz1<=0 || inimg_sz2<=0) return false;
	V3DLONG n_segs = mCoord.size()-1;
	double * seg_profile = 0;
	try {seg_profile = new double [n_segs];}
	catch (...) {return false;}
	for (V3DLONG k=0;k<n_segs;k++)
	{
		seg_profile[k] = twopoints_lineprofile_3dimg_sum(inimg_data3d, inimg_sz0, inimg_sz1, inimg_sz2, mCoord.at(k).x, mCoord.at(k).y, mCoord.at(k).z, mCoord.at(k+1).x, mCoord.at(k+1).y, mCoord.at(k+1).z);
	}
	moment(seg_profile, n_segs, bps.ave, bps.adev, bps.sdev, bps.var, bps.skew, bps.curt);
	if (seg_profile) {delete []seg_profile;}
	return true;
}
Exemple #9
0
int main(int argc, const char **argv)
{
	const char *video_path;
	int order;
	const char *fmt;
	int w;
	int h;
	int ret;

	if (argc < 6) {
		usage();
		return 2;
	}

	order	 = atoi(argv[1]);
	fmt		 = argv[2];
	video_path = argv[3];
	w        = atoi(argv[4]);
	h        = atoi(argv[5]);

	if (w <= 0 || h <= 0) {
		usage();
		return 2;
	}

	if (!(order == 1 || order == 2))
	{
		usage();
		return 2;
	}

	ret = moment(video_path, w, h, fmt, order);

	if (ret)
		return ret;

	return 0;
}
Exemple #10
0
int main ( int argc, char * argv[]) {

    int test_number = 0;
    double alpha, beta;
    double delta = 0.3;
    double x[2][3] = {{0.0}};
    double y[2][3] = {{0.0}};
    double moment (int moment_n, double x[][3], double y[][3], double delta);
    double avg= 0.0, avg_sq = 0.0, avg_sq_sw = 0.0;
    double avg_exact= 0.0;

    if ( argc == 2 ) test_number = atoi(argv[1]); 

    switch (test_number) {
	
    case 0:

	x[0][2] = 1;
	y[0][2] = 1;


	avg = moment (1, x, y, delta);
	avg_exact = delta*delta/4*(1.0 - exp (-4.0/(delta*delta) ) );
	printf ("alpha: %8.3lf    beta: %8.3lf   avg:  %8.10lf    exact: %8.10lf  diff: %8.2le\n",
		alpha, beta, avg, avg_exact, fabs(avg-avg_exact)/avg_exact);
	break;
	
    case 1:
    
	alpha = M_PI/3;
	beta  = M_PI/6;

	x[0][2] = 1;
	x[1][2] = cos (beta);
	x[1][0] = sin (beta);
    
	y[0][2] = 1;
	y[1][2] = cos (alpha);
	y[1][0] = sin (alpha);


	avg_sq = moment (2, x, y, delta);
	avg_sq_sw = moment (2, y, x, delta);
	printf ("alpha: %8.3lf    beta: %8.3lf   avg_sq:  %8.10lf   switched: %8.10lf \n",
		alpha, beta, avg_sq, avg_sq_sw);
	break;

    case 2:
    
	alpha = beta  = M_PI/3;

	x[0][2] = 1;
	x[1][2] = cos (beta);
	x[1][0] = sin (beta);
    
	y[0][2] = 1;
	y[1][2] = cos (alpha);
	y[1][0] = sin (alpha);


	avg_sq = moment (2, x, y, delta);
	printf ("alpha: %8.3lf    beta: %8.3lf   avg_sq:  %8.10lf  \n",
		alpha, beta, avg_sq);
	break;

     case 3:
    
	alpha = beta  = M_PI/10;

	x[0][2] = 1;
	x[1][2] = cos (beta);
	x[1][0] = sin (beta);
    
	y[0][2] = 1;
	y[1][2] = -x[1][2];
	y[1][0] = -x[1][0];


	avg_sq = moment (2, x, y, delta);
	printf ("alpha: %8.3lf    beta: %8.3lf   avg_sq:  %8.10lf ( %8.3le)\n",
		alpha, beta, avg_sq, avg_sq);
	break;

    default:
	fprintf ( stderr, "Unrecognized test number: %s\n", argv[1]); 

    }
    
    return 0;
}
Exemple #11
0
double PowerSum::skew() const throw()
{
    if (n<3)
        return 0;
    return moment(3)/pow(moment(2),1.5);
}
Exemple #12
0
double PowerSum::variance() const throw()
{
    if (n<2)
        return 0;
    return moment(2);
}
Exemple #13
0
void toyMCGen(const int nsig, const float wtag, const float purity){
    TFile* file = new TFile("toyMC.root","RECREATE");

//    gROOT->ProcessLine(".L ../PDFs/Faddeeva.hh+");
    gROOT->LoadMacro("../PDFs/libFadeeva.so");
    gROOT->ProcessLine(".L ../PDFs/RooBtoDhFadeeva.cxx++");

    RooRealVar tau("tau","tau",_tau,"ps"); tau.setConstant(kTRUE);
    RooRealVar dm("dm","dm",_dm,"ps^{-1}"); dm.setConstant(kTRUE);
    RooRealVar sin2beta("sin2beta","sin2beta",_sin2beta); sin2beta.setConstant(kTRUE);
    RooRealVar cos2beta("cos2beta","cos2beta",_cos2beta); cos2beta.setConstant(kTRUE);
    RooRealVar dt("dt","dt",-3.,3.,"ps");

    RooRealVar moment("moment","moment",-1.); moment.setConstant(kTRUE);
    RooRealVar parity("parity","parity",0.); parity.setConstant(kTRUE);

    RooRealVar* K[8];
    RooRealVar* Kb[8];
    RooRealVar* C[8];
    RooRealVar* S[8];
    RooRealVar* Sb[8];
    stringstream out;
    for(int i=0; i<8; i++){
        out.str("");
        out << "K" << i;
        K[i] = new RooRealVar(out.str().c_str(),out.str().c_str(),_K[i]); K[i]->setConstant(kTRUE);
        out.str("");
        out << "Kb" << i;
        Kb[i] = new RooRealVar(out.str().c_str(),out.str().c_str(),_Kb[i]); Kb[i]->setConstant(kTRUE);
        out.str("");
        out << "C" << i;
        C[i] = new RooRealVar(out.str().c_str(),out.str().c_str(),_C[i]); C[i]->setConstant(kTRUE);
        out.str("");
        out << "S" << i;
        S[i] = new RooRealVar(out.str().c_str(),out.str().c_str(),_S[i]); S[i]->setConstant(kTRUE);
        out.str("");
        out << "Sb" << i;
        Sb[i] = new RooRealVar(out.str().c_str(),out.str().c_str(),-_S[i]); Sb[i]->setConstant(kTRUE);
    }
    cout << "Hello world!" << endl;

    RooCategory tag("tag","tag");
    tag.defineType("B0",1);
    tag.defineType("anti-B0",-1);

    RooCategory bin("bin","bin");
    bin.defineType("1",1);
    bin.defineType("2",2);
    bin.defineType("3",3);
    bin.defineType("4",4);
    bin.defineType("5",5);
    bin.defineType("6",6);
    bin.defineType("7",7);
    bin.defineType("8",8);
    bin.defineType("-1",-1);
    bin.defineType("-2",-2);
    bin.defineType("-3",-3);
    bin.defineType("-4",-4);
    bin.defineType("-5",-5);
    bin.defineType("-6",-6);
    bin.defineType("-7",-7);
    bin.defineType("-8",-8);

    RooRealVar mean("mean","mean",0.,"ps"); mean.setConstant(kTRUE);
    RooRealVar sigma("sigma","sigma",_tau,"ps"); sigma.setConstant(kTRUE);
//    RooGaussModel rf("rf","rf",dt,mean,sigma);
//    RooTruthModel rf("rf","rf",dt);

    cout << "Raw PDFs..." << endl;

    RooBtoDhFadeeva pdfs[16];
    RooBtoDhFadeeva pdfsb[16];
    for(int i=0; i<8; i++){
        out.str("");
        out << "B0 pdf " << i+1;
        pdfs[i]   = RooBtoDhFadeeva(out.str().c_str(),out.str().c_str(),dt,tau,dm,sin2beta,cos2beta,*K[i],*Kb[i],*C[i],*S[i],moment,parity);
        out.str("");
        out << "B0 pdf " << -(i+1);
        pdfs[i+8] = RooBtoDhFadeeva(out.str().c_str(),out.str().c_str(),dt,tau,dm,sin2beta,cos2beta,*Kb[i],*K[i],*C[i],*Sb[i],moment,parity);
        out.str("");
        out << "anti-B0 pdf " << i+1;
        pdfsb[i]  = RooBtoDhFadeeva(out.str().c_str(),out.str().c_str(),dt,tau,dm,sin2beta,cos2beta,*K[i],*Kb[i],*C[i],*Sb[i],moment,parity);
        out.str("");
        out << "anti-B0 pdf " << -(i+1);
        pdfsb[i+8]= RooBtoDhFadeeva(out.str().c_str(),out.str().c_str(),dt,tau,dm,sin2beta,cos2beta,*Kb[i],*K[i],*C[i],*S[i],moment,parity);
    }

    cout << "Numerical convolution..." << endl;

//    RooFFTConvPdf PDFs[16];
//    RooFFTConvPdf PDFsb[16];
//    for(int i=0; i<8; i++){
//        out.str("");
//        out << "B0 PDF " << i+1;
//        PDFs[i]   = RooFFTConvPdf(out.str().c_str(),out.str().c_str(),dt,pdfs[i],rf);
//        out.str("");
//        out << "B0 PDF " << -(i+1);
//        PDFs[i+8] = RooFFTConvPdf(out.str().c_str(),out.str().c_str(),dt,pdfs[i+8],rf);
//        out.str("");
//        out << "anti-B0 PDF " << i+1;
//        PDFsb[i]  = RooFFTConvPdf(out.str().c_str(),out.str().c_str(),dt,pdfsb[i],rf);
//        out.str("");
//        out << "anti-B0 PDF " << -(i+1);
//        PDFsb[i+8]= RooFFTConvPdf(out.str().c_str(),out.str().c_str(),dt,pdfsb[i+8],rf);
//    }

    cout << "Filling dataset..." << endl;

    RooDataSet d("data","data",RooArgSet(dt,bin,tag));
    RooDataSet *data;
    int N;
    for(int i=0; i<8; i++){
        out.str("");
        out << i+1;
        bin.setLabel(out.str().c_str());
        // Signal with true tag
        N = (int)(nsig*(1.-wtag)*_K[i]*0.5);
        // B0
        data = PDFs[i].generate(RooArgSet(dt),N);
        tag.setLabel("B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // anti-B0
        data = PDFsb[i].generate(RooArgSet(dt),N);
        tag.setLabel("anti-B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // Signal with wrong tag
        N = (int)(nsig*wtag*_K[i]*0.5);
        // B0
        data = PDFsb[i].generate(RooArgSet(dt),N);
        tag.setLabel("B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // anti-B0
        data = PDFs[i].generate(RooArgSet(dt),N);
        tag.setLabel("anti-B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        //Background
        N = (int)(nsig/purity*_K[i]*0.5);
        // B0
        data = rf.generate(RooArgSet(dt),N);
        tag.setLabel("B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // anti-B0
        data = rf.generate(RooArgSet(dt),N);
        tag.setLabel("anti-B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }
        //////////////
        // anti-bin //
        //////////////
        out.str("");
        out << -(i+1);
        bin.setLabel(out.str().c_str());
        // Signal with true tag
        N = (int)(nsig*(1.-wtag)*_Kb[i]*0.5);
        // B0
        data = PDFs[i+8].generate(RooArgSet(dt),N);
        tag.setLabel("B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // anti-B0
        data = PDFsb[i+8].generate(RooArgSet(dt),N);
        tag.setLabel("anti-B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // Signal with wrong tag
        N = (int)(nsig*wtag*_Kb[i]*0.5);
        // B0
        data = PDFsb[i+8].generate(RooArgSet(dt),N);
        tag.setLabel("B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // anti-B0
        data = PDFs[i+8].generate(RooArgSet(dt),N);
        tag.setLabel("anti-B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        //Background
        N = (int)(nsig/purity*_Kb[i]*0.5);
        // B0
        data = rf.generate(RooArgSet(dt),N);
        tag.setLabel("B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }

        // anti-B0
        data = rf.generate(RooArgSet(dt),N);
        tag.setLabel("anti-B0");
        for(int j=0; j<N; j++){
            dt = data->get(j)->find(dt.GetName())->getVal();
            d.add(RooArgSet(dt,bin,tag));
        }
    }

    d.Print();
    d.Write();
    file->Close();

    return;
}
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 carries out a test on a rectangle.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    03 October 2012

  Author:

    John Burkardt
*/
{
  double alpha_exact[6] = {
    1.0, 
    5.0, 4.0, 
    30.66666666666667, 22.0, 18.66666666666666 };
  double alpha_pq;
  int k;
  double mu_exact[6] = {
    1.0, 
    0.0, 0.0, 
    5.666666666666667, 2.0, 2.666666666666667 };
  double mu_pq;
  int n = 4;
  double nu_exact[6] = {
    40.0, 
    200.0, 160.0, 
    1226.66666666666667, 880.0, 746.66666666666666 };
  double nu_pq;
  int p;
  int q;
  int s;
  double x[4] = {
    2.0, 10.0, 8.0, 0.0 };
  double y[4] = {
    0.0,  4.0, 8.0, 4.0 };

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Check normalized moments of a rectangle.\n" );
  printf ( "\n" );
  printf ( "   P   Q             Nu(P,Q)\n" );
  printf ( "            Computed         Exact\n" );
  printf ( "\n" );
  k = 0;
  for ( s = 0; s <= 2; s++ )
  {
    for ( p = s; 0 <= p; p-- )
    {
      q = s - p;
      nu_pq = moment ( n, x, y, p, q );
      printf ( "  %2d  %2d  %14.6g  %14.6g\n", p, q, nu_pq, nu_exact[k] );
      k = k + 1;
    }
  }

  printf ( "\n" );
  printf ( "   P   Q           Alpha(P,Q)\n" );
  printf ( "            Computed         Exact\n" );
  printf ( "\n" );
  k = 0;
  for ( s = 0; s <= 2; s++ )
  {
    for ( p = s; 0 <= p; p-- )
    {
      q = s - p;
      alpha_pq = moment_normalized ( n, x,y, p, q );
      printf ( "  %2d  %2d  %14.6g  %14.6g\n", p, q, alpha_pq, alpha_exact[k] );
      k = k + 1;
    }
  }

  printf ( "\n" );
  printf ( "   P   Q             Mu(P,Q)\n" );
  printf ( "            Computed         Exact\n" );
  printf ( "\n" );
  k = 0;
  for ( s = 0; s <= 2; s++ )
  {
    for ( p = s; 0 <= p; p-- )
    {
      q = s - p;
      mu_pq = moment_central ( n, x, y , p, q );
      printf ( "  %2d  %2d  %14.6g  %14.6g\n", p, q, mu_pq, mu_exact[k] );
      k = k + 1;
    }
  }

  return;
}