~m-a-uchida/maus/RKdEdxDevel

« back to all changes in this revision

Viewing changes to tests/cpp_unit/Maths/PolynomialMapTest.cc

  • Committer: Durga Rajaram
  • Date: 2014-01-14 07:07:02 UTC
  • mfrom: (659.1.80 relcand)
  • Revision ID: durga@fnal.gov-20140114070702-2l1fuj1w6rraw7xe
Tags: MAUS-v0.7.6
MAUS-v0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
229
229
  try {
230
230
    pvec.GetAvgChi2OfDifference(in, out);
231
231
    testpass = false;
232
 
  } catch (Squeal squeal) {}
 
232
  } catch (MAUS::Exception exc) {}
233
233
  ASSERT_TRUE(testpass);
234
234
 
235
235
  for (int i = 0; i < 10; i++) {
255
255
  try {
256
256
    pvec.GetAvgChi2OfDifference(in, out);
257
257
    testpass = false;
258
 
  } catch (Squeal squeal) {}
 
258
  } catch (MAUS::Exception exc) {}
259
259
  ASSERT_TRUE(testpass);
260
260
 
261
261
  // in[i] size != point dimension
265
265
  try {
266
266
    pvec.GetAvgChi2OfDifference(in, out);
267
267
    testpass = false;
268
 
  } catch (Squeal squeal) {}
 
268
  } catch (MAUS::Exception exc) {}
269
269
  ASSERT_TRUE(testpass);
270
270
 
271
271
  // out[i] size != value dimension
276
276
  try {
277
277
    pvec.GetAvgChi2OfDifference(in, out);
278
278
    testpass = false;
279
 
  } catch (Squeal squeal) {}
 
279
  } catch (MAUS::Exception exc) {}
280
280
  ASSERT_TRUE(testpass);
281
281
}
282
282
 
378
378
        points, values, 1, weightFunction);
379
379
 
380
380
  // polynomial order should be 1
381
 
  EXPECT_EQ(pVec->PolynomialOrder(), 1);
 
381
  EXPECT_EQ(pVec->PolynomialOrder(), static_cast<size_t>(1));
382
382
 
383
383
  // with polynomial order 1, the number of poly. coeff. is just the number of
384
384
  // variables plus one for the constant term
385
 
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), 4);
 
385
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), static_cast<size_t>(4));
386
386
 
387
387
  CLHEP::HepMatrix recCoeff
388
388
    = MAUS::CLHEP::HepMatrix(pVec->GetCoefficientsAsMatrix());
406
406
                                                     1, weights);
407
407
 
408
408
  // polynomial order should be 1
409
 
  EXPECT_EQ(pVec->PolynomialOrder(), 1);
 
409
  EXPECT_EQ(pVec->PolynomialOrder(), static_cast<size_t>(1));
410
410
 
411
411
  // with polynomial order 1, the number of poly. coeff. is just the number of
412
412
  // variables plus one for the constant term
413
 
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), 4);
 
413
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), static_cast<size_t>(4));
414
414
 
415
415
  recCoeff = MAUS::CLHEP::HepMatrix(pVec->GetCoefficientsAsMatrix());
416
416
  Squeak::mout(Squeak::debug) << "Weighted Input" << mat << "Weighted Output"
431
431
                                                     1, weightFunction);
432
432
 
433
433
  // polynomial order should be 1
434
 
  EXPECT_EQ(pVec->PolynomialOrder(), 1);
 
434
  EXPECT_EQ(pVec->PolynomialOrder(), static_cast<size_t>(1));
435
435
 
436
436
  // with polynomial order 1, the number of poly. coeff. is just the number of
437
437
  // variables plus one for the constant term
438
 
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), 4);
 
438
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), static_cast<size_t>(4));
439
439
 
440
440
  recCoeff = MAUS::CLHEP::HepMatrix(pVec->GetCoefficientsAsMatrix());
441
441
  for (int i = 0; i < recCoeff.num_row(); i++)
459
459
    points, values, 1, constraintPVec->GetCoefficientsAsVector(), weights);
460
460
 
461
461
  // polynomial order should be 1
462
 
  EXPECT_EQ(pVec->PolynomialOrder(), 1);
 
462
  EXPECT_EQ(pVec->PolynomialOrder(), static_cast<size_t>(1));
463
463
 
464
464
  // with polynomial order 1, the number of poly. coeff. is just the number of
465
465
  // variables plus one for the constant term
466
 
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), 4);
 
466
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), static_cast<size_t>(4));
467
467
 
468
468
  recCoeff = MAUS::CLHEP::HepMatrix(pVec->GetCoefficientsAsMatrix());
469
469
  Squeak::mout(Squeak::debug) << "Constrained Input\n" << *constraintPVec
482
482
      bad_points, values,
483
483
      1, constraintPVec->GetCoefficientsAsVector(), weights);
484
484
    testpass = false;
485
 
  } catch (Squeal squeal) {}
 
485
  } catch (MAUS::Exception exc) {}
486
486
  ASSERT_TRUE(testpass);
487
487
 
488
488
  // bad values size
493
493
      points, bad_values,
494
494
      1, constraintPVec->GetCoefficientsAsVector(), weights);
495
495
    testpass = false;
496
 
  } catch (Squeal squeal) {}
 
496
  } catch (MAUS::Exception exc) {}
497
497
  ASSERT_TRUE(testpass);
498
498
 
499
499
  testpass = true;
552
552
                                << std::endl;
553
553
  } else {
554
554
    // polynomial order should be 1
555
 
    EXPECT_EQ(pVec->PolynomialOrder(), 1);
 
555
    EXPECT_EQ(pVec->PolynomialOrder(), static_cast<size_t>(1));
556
556
 
557
557
    // with polynomial order 1, the number of poly. coeff. is just the number of
558
558
    // variables plus one for the constant term
559
 
    EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), 4);
 
559
    EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), static_cast<size_t>(4));
560
560
 
561
561
    Matrix<double> o1 = pVec ->GetCoefficientsAsMatrix();
562
562
    Matrix<double> o2 = testF->GetCoefficientsAsMatrix();
610
610
    1e-20, delta, 10., 100, deltaMax);
611
611
 
612
612
  // polynomial order should be 2
613
 
  EXPECT_EQ(pVec->PolynomialOrder(), 2);
 
613
  EXPECT_EQ(pVec->PolynomialOrder(), static_cast<size_t>(2));
614
614
 
615
615
  // with point size 3 and polynomial order 2,
616
616
  // the number of poly. coeff. is (3+2)!/(3!2!) = 10
617
 
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), 10);
 
617
  EXPECT_EQ(pVec->NumberOfPolynomialCoefficients(), static_cast<size_t>(10));
618
618
 
619
619
  Squeak::mout(Squeak::debug) << "delta variable walls: ";
620
620
  for (size_t i = 0; i < delta.size(); i++) {
640
640
    testpass = false;
641
641
  } else {
642
642
    // polynomial order should be 2
643
 
    EXPECT_EQ(pVec2->PolynomialOrder(), 2);
 
643
    EXPECT_EQ(pVec2->PolynomialOrder(), static_cast<size_t>(2));
644
644
 
645
645
    // with point size 3 and polynomial order 2,
646
646
    // the number of poly. coeff. is (3+2)!/(3!2!) = 10
647
 
    EXPECT_EQ(pVec2->NumberOfPolynomialCoefficients(), 10);
 
647
    EXPECT_EQ(pVec2->NumberOfPolynomialCoefficients(), static_cast<size_t>(10));
648
648
 
649
649
    Squeak::mout(Squeak::debug) << "Input should be same as output\nInput\n"
650
650
                                << *testF2
716
716
  try {
717
717
    coefficient.SpaceTransform(space_in, space_out);
718
718
    testpass = false;
719
 
  } catch (Squeal squeal) {}
 
719
  } catch (MAUS::Exception exc) {}
720
720
  ASSERT_TRUE(testpass);
721
721
 
722
722
  // out variable not in space mapping
726
726
  try {
727
727
    coefficient.SpaceTransform(space_in, space_out);
728
728
    testpass = false;
729
 
  } catch (Squeal squeal) {}
 
729
  } catch (MAUS::Exception exc) {}
 
730
  ASSERT_TRUE(testpass);
 
731
}
 
732
 
 
733
/*
 
734
TEST_F(PolynomialMapTest, MakePolynomialVector) {
 
735
  const double point_data[2] = {1.0, 2.0};
 
736
  Vector<double> point(point_data, 2);
 
737
  Vector<double> polynomial_vector;
 
738
  test.polynomial_map_->MakePolynomialVector(point, polynomial_vector);
 
739
  for (size_t index = 0; index < 2; ++index) {
 
740
    EXPECT_NEAR(point[index], point_data[index], 1.e-6);
 
741
  }
 
742
}
 
743
*/
 
744
 
 
745
TEST_F(PolynomialMapTest, SetCoefficients) {
 
746
  Matrix<double> coefficient_matrix(3, 6, 0.);  // initialize all elements to zero
 
747
  PolynomialMap test_map(2, coefficient_matrix);
 
748
  const double coefficients[18] = {
 
749
    1., 2., 3., 4., 5., 6.,
 
750
    0., 1., 4., 9., 16., 25.,
 
751
    -1., -2., -3., -4., -5., -6.,
 
752
  };
 
753
  coefficient_matrix = Matrix<double>(3, 6, coefficients);
 
754
  test_map.SetCoefficients(coefficient_matrix);
 
755
  const Matrix<double> test_coefficient_matrix
 
756
    = test_map.GetCoefficientsAsMatrix();
 
757
  const Matrix<double> master_coefficient_matrix
 
758
    = polynomial_map_->GetCoefficientsAsMatrix();
 
759
  for (size_t row = 0; row < 3; ++row) {
 
760
    for (size_t column = 0; column < 6; ++column) {
 
761
      EXPECT_NEAR(test_coefficient_matrix(row+1, column+1),
 
762
                  master_coefficient_matrix(row+1, column+1),
 
763
                  1.e-6);
 
764
    }
 
765
  }
 
766
}
 
767
 
 
768
TEST_F(PolynomialMapTest, UnmakePolynomialVector) {
 
769
  const double point_data[2] = {1.0, 2.0};
 
770
  Vector<double> point(point_data, 2);
 
771
  Vector<double> polynomial_vector;
 
772
  polynomial_map_->MakePolynomialVector(point, polynomial_vector);
 
773
  polynomial_map_->UnmakePolynomialVector(polynomial_vector, point);
 
774
  for (size_t index = 0; index < 2; ++index) {
 
775
    EXPECT_NEAR(point[index], point_data[index], 1.e-6);
 
776
  }
 
777
 
 
778
  Vector<double> empty_point;
 
779
  polynomial_map_->UnmakePolynomialVector(polynomial_vector, empty_point);
 
780
  for (size_t index = 0; index < 2; ++index) {
 
781
    EXPECT_NEAR(empty_point[index], point_data[index], 1.e-6);
 
782
  }
 
783
 
 
784
  Vector<double> wrong_size_point(4);
 
785
  bool testpass = true;
 
786
  try {
 
787
    polynomial_map_->UnmakePolynomialVector(polynomial_vector, wrong_size_point);
 
788
    testpass = false;
 
789
  } catch (MAUS::Exception exc) {}
 
790
  ASSERT_TRUE(testpass);
 
791
 
 
792
  double polynomial_vector_data[polynomial_vector.size()];
 
793
  polynomial_map_->MakePolynomialVector(point_data, polynomial_vector_data);
 
794
  double point_test[2];
 
795
  polynomial_map_->UnmakePolynomialVector(polynomial_vector_data, point_test);
 
796
  for (size_t index = 0; index < 2; ++index) {
 
797
    EXPECT_NEAR(point_test[index], point_data[index], 1.e-6);
 
798
  }
 
799
}
 
800
 
 
801
TEST_F(PolynomialMapTest, generate_polynomial_2) {
 
802
  const double y0[3] = {1., 0., -1.};
 
803
  const double y1[3] = {7., 10., -7.};
 
804
  for (size_t value_var_index = 0; value_var_index < 3; ++value_var_index) {
 
805
    Vector<double> polynomial = generate_polynomial_2D(*polynomial_map_,
 
806
                                                      0,    // point var index
 
807
                                                      value_var_index,
 
808
                                                      0.,   // point var min
 
809
                                                      1.,   // point var max
 
810
                                                      1.);  // point var delta
 
811
    const double y[2] = {y0[value_var_index], y1[value_var_index]};
 
812
    for (size_t index = 0; index < 2; ++index) {
 
813
      EXPECT_NEAR(y[index], polynomial[index], 1.e-6);
 
814
    }
 
815
  }
 
816
}
 
817
 
 
818
TEST_F(PolynomialMapTest, RecentredNotSupported) {
 
819
  bool testpass = true;
 
820
  try {
 
821
    double * point = NULL;
 
822
    polynomial_map_->Recentred(point);
 
823
    testpass = false;
 
824
  } catch (MAUS::Exception exc) {}
 
825
  ASSERT_TRUE(testpass);
 
826
}
 
827
 
 
828
TEST_F(PolynomialMapTest, InverseNotSupported) {
 
829
  bool testpass = true;
 
830
  try {
 
831
    polynomial_map_->Inverse();
 
832
    testpass = false;
 
833
  } catch (MAUS::Exception exc) {}
730
834
  ASSERT_TRUE(testpass);
731
835
}