home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / src / f-quad.cc < prev    next >
C/C++ Source or Header  |  1995-01-03  |  8KB  |  370 lines

  1. // f-quad.cc                                           -*- C++ -*-
  2. /*
  3.  
  4. Copyright (C) 1993, 1994, 1995 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27.  
  28. #include <strstream.h>
  29.  
  30. #include "Quad.h"
  31.  
  32. #include "tree-const.h"
  33. #include "variables.h"
  34. #include "mappers.h"
  35. #include "gripes.h"
  36. #include "error.h"
  37. #include "utils.h"
  38. #include "pager.h"
  39. #include "help.h"
  40. #include "defun-dld.h"
  41.  
  42. // Global pointer for user defined function required by quadrature functions.
  43. static tree_fvc *quad_fcn;
  44.  
  45. static Quad_options quad_opts;
  46.  
  47. double
  48. quad_user_function (double x)
  49. {
  50.   double retval = 0.0;
  51.  
  52.   Octave_object args;
  53.   args(0) = x;
  54.  
  55.   if (quad_fcn)
  56.     {
  57.       Octave_object tmp = quad_fcn->eval (0, 1, args);
  58.  
  59.       if (error_state)
  60.     {
  61.       quad_integration_error = 1;  // XXX FIXME XXX
  62.       gripe_user_supplied_eval ("quad");
  63.       return retval;
  64.     }
  65.  
  66.       if (tmp.length () && tmp(0).is_defined ())
  67.     {
  68.       retval = tmp(0).double_value ();
  69.  
  70.       if (error_state)
  71.         {
  72.           quad_integration_error = 1;  // XXX FIXME XXX
  73.           gripe_user_supplied_eval ("quad");
  74.         }
  75.     }
  76.       else
  77.     {
  78.       quad_integration_error = 1;  // XXX FIXME XXX
  79.       gripe_user_supplied_eval ("quad");
  80.     }
  81.     }
  82.  
  83.   return retval;
  84. }
  85.  
  86. DEFUN_DLD_BUILTIN ("quad", Fquad, Squad, 5, 3,
  87.   "[V, IER, NFUN] = quad (F, A, B [, TOL] [, SING])\n\
  88. \n\
  89. Where the first argument is the name of the  function to call to\n\
  90. compute the value of the integrand.  It must have the form\n\
  91. \n\
  92.   y = f (x)
  93. \n\
  94. where y and x are scalars.\n\
  95. \n\
  96. The second and third arguments are limits of integration.  Either or\n\
  97. both may be infinite.  The optional argument TOL specifies the desired\n\
  98. accuracy of the result.  The optional argument SING is a vector of\n\
  99. at which the integrand is singular.")
  100. {
  101.   Octave_object retval;
  102.  
  103.   int nargin = args.length ();
  104.  
  105.   if (nargin < 3 || nargin > 5 || nargout > 4)
  106.     {
  107.       print_usage ("quad");
  108.       return retval;
  109.     }
  110.  
  111.   quad_fcn = is_valid_function (args(0), "quad", 1);
  112.   if (! quad_fcn || takes_correct_nargs (quad_fcn, 1, "quad", 1) != 1)
  113.     return retval;
  114.  
  115.   double a = args(1).double_value ();
  116.  
  117.   if (error_state)
  118.     {
  119.       error ("quad: expecting second argument to be a scalar");
  120.       return retval;
  121.     }
  122.  
  123.   double b = args(2).double_value ();
  124.  
  125.   if (error_state)
  126.     {
  127.       error ("quad: expecting third argument to be a scalar");
  128.       return retval;
  129.     }
  130.  
  131.   int indefinite = 0;
  132.   IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite;
  133.   double bound = 0.0;
  134.   if ((int) xisinf (a) && (int) xisinf (b))
  135.     {
  136.       indefinite = 1;
  137.       indef_type = IndefQuad::doubly_infinite;
  138.     }
  139.   else if ((int) xisinf (a))
  140.     {
  141.       indefinite = 1;
  142.       bound = b;
  143.       indef_type = IndefQuad::neg_inf_to_bound;
  144.     }
  145.   else if ((int) xisinf (b))
  146.     {
  147.       indefinite = 1;
  148.       bound = a;
  149.       indef_type = IndefQuad::bound_to_inf;
  150.     }
  151.  
  152.   int ier = 0;
  153.   int nfun = 0;
  154.   double abserr = 0.0;
  155.   double val = 0.0;
  156.   double abstol = 1e-6;
  157.   double reltol = 1e-6;
  158.   Vector tol (2);
  159.   Vector sing;
  160.   int have_sing = 0;
  161.   switch (nargin)
  162.     {
  163.     case 5:
  164.       if (indefinite)
  165.     {
  166.       error("quad: singularities not allowed on infinite intervals");
  167.       return retval;
  168.     }
  169.  
  170.       have_sing = 1;
  171.  
  172.       sing = args(4).vector_value ();
  173.  
  174.       if (error_state)
  175.     {
  176.       error ("quad: expecting vector of singularities as fourth argument");
  177.       return retval;
  178.     }
  179.  
  180.     case 4:
  181.       tol = args(3).vector_value ();
  182.  
  183.       if (error_state)
  184.     {
  185.       error ("quad: expecting vector of tolerances as fifth argument");
  186.       return retval;
  187.     }
  188.  
  189.       switch (tol.capacity ())
  190.     {
  191.     case 2:
  192.       reltol = tol.elem (1);
  193.  
  194.     case 1:
  195.       abstol = tol.elem (0);
  196.       break;
  197.  
  198.     default:
  199.       error ("quad: expecting tol to contain no more than two values");
  200.       return retval;
  201.     }
  202.  
  203.     case 3:
  204.       if (indefinite)
  205.     {
  206.       IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol);
  207.       iq.copy (quad_opts);
  208.       val = iq.integrate (ier, nfun, abserr);
  209.     }
  210.       else
  211.     {
  212.       if (have_sing)
  213.         {
  214.           DefQuad dq (quad_user_function, a, b, sing, abstol, reltol);
  215.           dq.copy (quad_opts);
  216.           val = dq.integrate (ier, nfun, abserr);
  217.         }
  218.       else
  219.         {
  220.           DefQuad dq (quad_user_function, a, b, abstol, reltol);
  221.           dq.copy (quad_opts);
  222.           val = dq.integrate (ier, nfun, abserr);
  223.         }
  224.     }
  225.       break;
  226.  
  227.     default:
  228.       panic_impossible ();
  229.       break;
  230.     }
  231.  
  232.   retval(3) = abserr;
  233.   retval(2) = nfun;
  234.   retval(1) = ier;
  235.   retval(0) = val;
  236.  
  237.   return retval;
  238. }
  239.  
  240. typedef void (Quad_options::*d_set_opt_mf) (double);
  241. typedef double (Quad_options::*d_get_opt_mf) (void);
  242.  
  243. #define MAX_TOKENS 2
  244.  
  245. struct QUAD_OPTIONS
  246. {
  247.   const char *keyword;
  248.   const char *kw_tok[MAX_TOKENS + 1];
  249.   int min_len[MAX_TOKENS + 1];
  250.   int min_toks_to_match;
  251.   d_set_opt_mf d_set_fcn;
  252.   d_get_opt_mf d_get_fcn;
  253. };
  254.  
  255. static QUAD_OPTIONS quad_option_table [] =
  256. {
  257.   { "absolute tolerance",
  258.     { "absolute", "tolerance", 0, },
  259.     { 1, 0, 0, }, 1,
  260.     Quad_options::set_absolute_tolerance,
  261.     Quad_options::absolute_tolerance, },
  262.  
  263.   { "relative tolerance",
  264.     { "relative", "tolerance", 0, },
  265.     { 1, 0, 0, }, 1,
  266.     Quad_options::set_relative_tolerance,
  267.     Quad_options::relative_tolerance, },
  268.  
  269.   { 0,
  270.     { 0, 0, 0, },
  271.     { 0, 0, 0, }, 0,
  272.     0, 0, },
  273. };
  274.  
  275. static void
  276. print_quad_option_list (void)
  277. {
  278.   ostrstream output_buf;
  279.  
  280.   print_usage ("quad_options", 1);
  281.  
  282.   output_buf << "\n"
  283.          << "Options for quad include:\n\n"
  284.          << "  keyword                                  value\n"
  285.          << "  -------                                  -----\n\n";
  286.  
  287.   QUAD_OPTIONS *list = quad_option_table;
  288.  
  289.   const char *keyword;
  290.   while ((keyword = list->keyword) != 0)
  291.     {
  292.       output_buf.form ("  %-40s ", keyword);
  293.  
  294.       double val = (quad_opts.*list->d_get_fcn) ();
  295.       if (val < 0.0)
  296.     output_buf << "computed automatically";
  297.       else
  298.     output_buf << val;
  299.  
  300.       output_buf << "\n";
  301.       list++;
  302.     }
  303.  
  304.   output_buf << "\n" << ends;
  305.   maybe_page_output (output_buf);
  306. }
  307.  
  308. static void
  309. do_quad_option (char *keyword, double val)
  310. {
  311.   QUAD_OPTIONS *list = quad_option_table;
  312.  
  313.   while (list->keyword != 0)
  314.     {
  315.       if (keyword_almost_match (list->kw_tok, list->min_len, keyword,
  316.                 list->min_toks_to_match, MAX_TOKENS))
  317.     {
  318.       (quad_opts.*list->d_set_fcn) (val);
  319.  
  320.       return;
  321.     }
  322.       list++;
  323.     }
  324.  
  325.   warning ("quad_options: no match for `%s'", keyword);
  326. }
  327.  
  328. DEFUN_DLD_BUILTIN ("quad_options", Fquad_options, Squad_options, -1, 1,
  329.   "quad_options (KEYWORD, VALUE)\n\
  330. \n\
  331. Set or show options for quad.  Keywords may be abbreviated\n\
  332. to the shortest match.")
  333. {
  334.   Octave_object retval;
  335.  
  336.   int nargin = args.length ();
  337.  
  338.   if (nargin == 0)
  339.     {
  340.       print_quad_option_list ();
  341.       return retval;
  342.     }
  343.   else if (nargin == 2)
  344.     {
  345.       char *keyword = args(0).string_value ();
  346.  
  347.       if (! error_state)
  348.     {
  349.       double val = args(1).double_value ();
  350.  
  351.       if (! error_state)
  352.         {
  353.           do_quad_option (keyword, val);
  354.           return retval;
  355.         }
  356.     }
  357.     }
  358.  
  359.   print_usage ("quad_options");
  360.  
  361.   return retval;
  362. }
  363.  
  364. /*
  365. ;;; Local Variables: ***
  366. ;;; mode: C++ ***
  367. ;;; page-delimiter: "^/\\*" ***
  368. ;;; End: ***
  369. */
  370.