summaryrefslogtreecommitdiff
path: root/src/math/math_ComputeGaussPointsAndWeights.cxx
blob: dcd0997afd02a7a6c3a9f363abe039aca7e6762e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// File:	math_ComputeGaussPointsAndWeights.cxx
// Created:	Mon Dec 19 16:01:53 2005
// Author:	Julia GERASIMOVA
//		<jgv@clubox>


#include <math_ComputeGaussPointsAndWeights.ixx>

#include <math_EigenValuesSearcher.hxx>
#include <math_Array1OfValueAndWeight.hxx>
#include <math_CompareOfValueAndWeight.hxx>
#include <math_QuickSortOfValueAndWeight.hxx>
#include <Standard_ErrorHandler.hxx>

math_ComputeGaussPointsAndWeights::math_ComputeGaussPointsAndWeights(const Standard_Integer Number)
{
  myIsDone = Standard_False;

  try {
    myPoints  = new TColStd_HArray1OfReal(1, Number);
    myWeights = new TColStd_HArray1OfReal(1, Number);

    Standard_Integer i;

    TColStd_Array1OfReal aDiag(1, Number);
    TColStd_Array1OfReal aSubDiag(1, Number);

    //Initialization of a real symmetric tridiagonal matrix for
    //computation of Gauss quadrature.

    for (i = 1; i <= Number; i++) {
      aDiag(i) = 0.;

      if (i == 1)
	aSubDiag(i) = 0.;
      else {
	Standard_Integer sqrIm1 = (i-1)*(i-1);
	aSubDiag(i) = sqrIm1/(4.*sqrIm1 - 1);
	aSubDiag(i) = Sqrt(aSubDiag(i));
      }
    }

    // Compute eigen values.
    math_EigenValuesSearcher EVsearch(aDiag, aSubDiag); 

    if (EVsearch.IsDone()) {
      math_Array1OfValueAndWeight VWarray(1, Number);
      for (i = 1; i <= Number; i++) {
	math_Vector anEigenVector = EVsearch.EigenVector(i);
	Standard_Real aWeight = anEigenVector(1);
	aWeight = 2. * aWeight * aWeight;
	math_ValueAndWeight EVW( EVsearch.EigenValue(i), aWeight );
	VWarray(i) = EVW;
      }

      math_CompareOfValueAndWeight theComparator;
      math_QuickSortOfValueAndWeight::Sort(VWarray, theComparator);

      for (i = 1; i <= Number; i++) {
	myPoints->ChangeValue(i)  = VWarray(i).Value();
	myWeights->ChangeValue(i) = VWarray(i).Weight();
      }      
      myIsDone = Standard_True;
    }
  } catch (Standard_Failure) {
  }
}

Standard_Boolean math_ComputeGaussPointsAndWeights::IsDone() const
{
  return myIsDone;
}

math_Vector math_ComputeGaussPointsAndWeights::Points() const
{
  Standard_Integer Number = myPoints->Length();
  math_Vector thePoints(1, Number);
  for (Standard_Integer i = 1; i <= Number; i++)
    thePoints(i) = myPoints->Value(i);

  return thePoints;
}

math_Vector math_ComputeGaussPointsAndWeights::Weights() const
{
  Standard_Integer Number = myWeights->Length();
  math_Vector theWeights(1, Number);
  for (Standard_Integer i = 1; i <= Number; i++)
    theWeights(i) = myWeights->Value(i);

  return theWeights;
}