Main Lemma Repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gaussianquadrature.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author M. Andy Kass
  8. @date 10/07/2010
  9. @version $Id: gaussianquadrature.cpp 87 2013-09-05 22:44:05Z tirons $
  10. **/
  11. #include "gaussianquadrature.h"
  12. namespace Lemma {
  13. // std::ostream &operator<<(std::ostream &stream,
  14. // const gaussianquadrature &ob) {
  15. //
  16. // }
  17. ////////////////////////////////////////////////
  18. gaussianquadrature::gaussianquadrature() {
  19. }
  20. gaussianquadrature::~gaussianquadrature() {
  21. }
  22. gaussianquadrature* gaussianquadrature::New() {
  23. return new gaussianquadrature;
  24. }
  25. void gaussianquadrature::Delete() {
  26. delete this;
  27. }
  28. ////////////////////////////////////////////////
  29. void gaussianquadrature::Integrate() {
  30. this->intres=(this->funcx.array()*this->wgq.array()).sum();
  31. }
  32. ////////////////////////////////////////////////
  33. void gaussianquadrature::CalcAW() {
  34. while ((this->y-this->y0).array().abs().maxCoeff()>
  35. this->machineeps) {
  36. this->L.col(0).setOnes();
  37. this->Ltemp.col(0).setZero();
  38. this->L.col(1)=this->y;
  39. this->Ltemp.col(1).setOnes();
  40. for (int kk=2;kk<this->N2;++kk) {
  41. this->ytemp=this->L.col(kk-1).array()*this->y.array();
  42. this->ytemp=this->ytemp.array()*(2.0*Real(kk)-1.0);
  43. this->L.col(kk)=this->ytemp.array()-((Real(kk)-1.0)*
  44. this->L.col(kk-2).array());
  45. this->L.col(kk)=this->L.col(kk).array()/Real(kk);
  46. }
  47. this->ytemp=this->y.array()*this->L.col(this->N1).array();
  48. this->Lp=this->L.col(this->N1-1)-this->ytemp;
  49. this->Lp=this->Lp.array()/(1.0-(this->y.array()*this->y.array()));
  50. this->Lp=this->Lp.array()*this->N2;
  51. this->y0=this->y;
  52. this->ytemp=this->L.col(this->N1);
  53. this->y=this->y0.array()-(this->ytemp.array()/this->Lp.array());
  54. }
  55. for (int ii=0;ii<this->N1;++ii) {
  56. this->xu(ii)=((this->b-this->a)*((this->y(ii)+1.0)/2.0))+this->a;
  57. this->wgq(ii)=((this->b-this->a)/((1.0-(this->y(ii)*this->y(ii)))*
  58. (this->Lp(ii)*this->Lp(ii))))*((Real(this->N2)/Real(this->N1))*
  59. (Real(this->N2)/Real(this->N1)));
  60. }
  61. }
  62. ////////////////////////////////////////////////
  63. void gaussianquadrature::SetFreqs(const int& nfreq,
  64. const Real& a, const Real& b) {
  65. this->N = nfreq-1;
  66. this->N1 = nfreq;
  67. this->N2 = nfreq+1;
  68. // \TODO Use pointers here instead
  69. this->a = a;
  70. this->b = b;
  71. //Debugging only
  72. //std::cout << N << " " << a << " " << b << std::endl;
  73. this->dx = (2.0)/Real(nfreq);
  74. this->machineeps = std::numeric_limits<Real>::epsilon();
  75. this->ytemp.resize(this->N1);
  76. this->xu.resize(this->N1);
  77. this->y.resize(this->N1);
  78. this->y0.resize(this->N1);
  79. this->Lp.resize(this->N1);
  80. this->wgq.resize(this->N1);
  81. // \TODO Instead of copying the variable, use pointers
  82. this->funcx.resize(nfreq);
  83. this->L.resize(this->N1,this->N2);
  84. this->Ltemp.resize(this->N1,this->N2);
  85. for (int ii=0;ii<this->N1;++ii) {
  86. this->xu(ii)=(dx*Real(ii))-1.0;
  87. this->y(ii)=std::cos((2.0*Real(ii)+1.0)*PI/(2.0*
  88. Real(this->N)+2.0))+(0.27/(Real(this->N1)))*
  89. std::sin(PI*xu(ii)*Real(this->N)/(Real(this->N2)));
  90. this->y0(ii)=2.0;
  91. this->ytemp(ii)=0.0;
  92. }
  93. }
  94. ////////////////////////////////////////////////
  95. void gaussianquadrature::SetFunc(const VectorXr& fx) {
  96. this->funcx = fx;
  97. }
  98. ////////////////////////////////////////////////
  99. VectorXr gaussianquadrature::GetAbscissae() {
  100. return this->xu;
  101. }
  102. ////////////////////////////////////////////////
  103. VectorXr gaussianquadrature::GetWeights() {
  104. return this->wgq;
  105. }
  106. ////////////////////////////////////////////////
  107. Real gaussianquadrature::GetResult() {
  108. return intres;
  109. }
  110. } //end of namespace Lemma