home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / liboctave / Quad.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  4KB  |  171 lines

  1. /*
  2.  
  3. Copyright (C) 1996, 1997 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "Quad.h"
  32. #include "f77-fcn.h"
  33. #include "lo-error.h"
  34. #include "sun-utils.h"
  35.  
  36. static integrand_fcn user_fcn;
  37.  
  38. // XXX FIXME XXX -- would be nice to not have to have this global
  39. // variable.
  40. // Nonzero means an error occurred in the calculation of the integrand
  41. // function, and the user wants us to quit.
  42. int quad_integration_error = 0;
  43.  
  44. extern "C"
  45. {
  46.   int F77_FCN (dqagp, DQAGP) (int (*)(double*, int&, double*),
  47.                   const double&, const double&,
  48.                   const int&, const double*,
  49.                   const double&, const double&, double&,
  50.                   double&, int&, int&, const int&,
  51.                   const int&, int&, int*, double*);
  52.  
  53.   int F77_FCN (dqagi, DQAGI) (int (*)(double*, int&, double*),
  54.                   const double&, const int&,
  55.                   const double&, const double&, double&,
  56.                   double&, int&, int&, const int&,
  57.                   const int&, int&, int*, double*); 
  58. }
  59.  
  60. static int
  61. user_function (double *x, int& ierr, double *result)
  62. {
  63. #if defined (sun) && defined (__GNUC__)
  64.   double xx = access_double (x);
  65. #else
  66.   double xx = *x;
  67. #endif
  68.  
  69.   quad_integration_error = 0;
  70.  
  71.   double xresult = (*user_fcn) (xx);
  72.  
  73. #if defined (sun) && defined (__GNUC__)
  74.   assign_double (result, xresult);
  75. #else
  76.   *result = xresult;
  77. #endif
  78.  
  79.   if (quad_integration_error)
  80.     ierr = -1;
  81.  
  82.   return 0;
  83. }
  84.  
  85. double
  86. DefQuad::integrate (int& ier, int& neval, double& abserr)
  87. {
  88.   int npts = singularities.capacity () + 2;
  89.   double *points = singularities.fortran_vec ();
  90.   double result = 0.0;
  91.  
  92.   int leniw = 183*npts - 122;
  93.   Array<int> iwork (leniw);
  94.   int *piwork = iwork.fortran_vec ();
  95.  
  96.   int lenw = 2*leniw - npts;
  97.   Array<double> work (lenw);
  98.   double *pwork = work.fortran_vec ();
  99.  
  100.   user_fcn = f;
  101.   int last;
  102.  
  103.   double abs_tol = absolute_tolerance ();
  104.   double rel_tol = relative_tolerance ();
  105.  
  106.   F77_XFCN (dqagp, DQAGP, (user_function, lower_limit, upper_limit,
  107.                npts, points, abs_tol, rel_tol, result,
  108.                abserr, neval, ier, leniw, lenw, last,
  109.                piwork, pwork));
  110.  
  111.   if (f77_exception_encountered)
  112.     (*current_liboctave_error_handler) ("unrecoverable error in dqagp");
  113.  
  114.   return result;
  115. }
  116.  
  117. double
  118. IndefQuad::integrate (int& ier, int& neval, double& abserr)
  119. {
  120.   double result = 0.0;
  121.  
  122.   int leniw = 128;
  123.   Array<int> iwork (leniw);
  124.   int *piwork = iwork.fortran_vec ();
  125.  
  126.   int lenw = 8*leniw;
  127.   Array<double> work (lenw);
  128.   double *pwork = work.fortran_vec ();
  129.  
  130.   user_fcn = f;
  131.   int last;
  132.  
  133.   int inf;
  134.   switch (type)
  135.     {
  136.     case bound_to_inf:
  137.       inf = 1;
  138.       break;
  139.  
  140.     case neg_inf_to_bound:
  141.       inf = -1;
  142.       break;
  143.  
  144.     case doubly_infinite:
  145.       inf = 2;
  146.       break;
  147.  
  148.     default:
  149.       assert (0);
  150.       break;
  151.     }
  152.  
  153.   double abs_tol = absolute_tolerance ();
  154.   double rel_tol = relative_tolerance ();
  155.  
  156.   F77_XFCN (dqagi, DQAGI, (user_function, bound, inf, abs_tol, rel_tol,
  157.                result, abserr, neval, ier, leniw, lenw,
  158.                last, piwork, pwork));
  159.  
  160.   if (f77_exception_encountered)
  161.     (*current_liboctave_error_handler) ("unrecoverable error in dqagi");
  162.  
  163.   return result;
  164. }
  165.  
  166. /*
  167. ;;; Local Variables: ***
  168. ;;; mode: C++ ***
  169. ;;; End: ***
  170. */
  171.