changeset 3254:28d5f556b8cf

[project @ 1999-07-15 16:02:57 by jwe]
author jwe
date Thu, 15 Jul 1999 16:02:57 +0000
parents 07d2d307c43e
children 4d33b1e56bff
files src/DLD-FUNCTIONS/betainc.cc src/DLD-FUNCTIONS/gammainc.cc src/DLD-FUNCTIONS/time.cc
diffstat 3 files changed, 338 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/src/DLD-FUNCTIONS/betainc.cc
@@ -0,0 +1,164 @@
+/*
+
+Copyright (C) 1997 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "lo-specfun.h"
+
+#include "defun-dld.h"
+#include "error.h"
+#include "gripes.h"
+#include "oct-obj.h"
+#include "utils.h"
+
+DEFUN_DLD (betainc, args, ,
+  "betainc (x, a, b)\n\
+\n\
+Compute the incomplete beta function\n\
+\n\
+  betainc(x,a,b) = beta(a,b)^(-1) \\int_0^x t^(a-1) (1-t)^(b-1) dt\n\
+\n\
+The sizes of x, a, and b must agree.")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 3)
+    {
+      octave_value x_arg = args(0);
+      octave_value a_arg = args(1);
+      octave_value b_arg = args(2);
+
+      if (x_arg.is_scalar_type ())
+	{
+	  double x = x_arg.double_value ();
+
+	  if (a_arg.is_scalar_type ())
+	    {
+	      double a = a_arg.double_value ();
+
+	      if (! error_state)
+		{
+		  if (b_arg.is_scalar_type ())
+		    {
+		      double b = b_arg.double_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		  else
+		    {
+		      Matrix b = b_arg.matrix_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		}
+	    }
+	  else
+	    {
+	      Matrix a = a_arg.matrix_value ();
+
+	      if (! error_state)
+		{
+		  if (b_arg.is_scalar_type ())
+		    {
+		      double b = b_arg.double_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		  else
+		    {
+		      Matrix b = b_arg.matrix_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		}
+	    }
+	}
+      else
+	{
+	  Matrix x = x_arg.matrix_value ();
+
+	  if (a_arg.is_scalar_type ())
+	    {
+	      double a = a_arg.double_value ();
+
+	      if (! error_state)
+		{
+		  if (b_arg.is_scalar_type ())
+		    {
+		      double b = b_arg.double_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		  else
+		    {
+		      Matrix b = b_arg.matrix_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		}
+	    }
+	  else
+	    {
+	      Matrix a = a_arg.matrix_value ();
+
+	      if (! error_state)
+		{
+		  if (b_arg.is_scalar_type ())
+		    {
+		      double b = b_arg.double_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		  else
+		    {
+		      Matrix b = b_arg.matrix_value ();
+
+		      if (! error_state)
+			retval = betainc (x, a, b);
+		    }
+		}
+	    }
+	}
+    }
+  else
+    print_usage ("betainc");
+
+  return retval;
+}
+
+/*
+;;; Local Variables: ***
+;;; mode: C++ ***
+;;; End: ***
+*/
+
new file mode 100644
--- /dev/null
+++ b/src/DLD-FUNCTIONS/gammainc.cc
@@ -0,0 +1,113 @@
+/*
+
+Copyright (C) 1997 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+Octave is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "lo-specfun.h"
+
+#include "defun-dld.h"
+#include "error.h"
+#include "gripes.h"
+#include "oct-obj.h"
+#include "utils.h"
+
+DEFUN_DLD (gammainc, args, ,
+  "gammainc (x, a)\n\
+\n\
+Compute the incomplete gamma function\n\
+\n\
+  gammainc(x,a) = (\\int_0^x exp(-t) t^(a-1) dt) / gamma(a).\n\
+\n\
+If a is scalar, then gammainc(x,a) is returned for each element of x\n\
+and vice versa.\n\
+\n\
+If neither a nor x is scalar, the sizes of a and x must agree, and\n\
+gammainc is applied for corresponding elements of x and a.")
+{
+  octave_value retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 2)
+    {
+      octave_value x_arg = args(0);
+      octave_value a_arg = args(1);
+
+      if (x_arg.is_scalar_type ())
+	{
+	  double x = x_arg.double_value ();
+
+	  if (! error_state)
+	    {
+	      if (a_arg.is_scalar_type ())
+		{
+		  double a = a_arg.double_value ();
+
+		  if (! error_state)
+		    retval = gammainc (x, a);
+		}
+	      else
+		{
+		  Matrix a = a_arg.matrix_value ();
+
+		  if (! error_state)
+		    retval = gammainc (x, a);
+		}
+	    }
+	}
+      else
+	{
+	  Matrix x = x_arg.matrix_value ();
+
+	  if (! error_state)
+	    {
+	      if (a_arg.is_scalar_type ())
+		{
+		  double a = a_arg.double_value ();
+
+		  if (! error_state)
+		    retval = gammainc (x, a);
+		}
+	      else
+		{
+		  Matrix a = a_arg.matrix_value ();
+
+		  if (! error_state)
+		    retval = gammainc (x, a);
+		}
+	    }
+	}
+    }
+  else
+    print_usage ("gammainc");
+
+  return retval;
+}
+
+/*
+;;; Local Variables: ***
+;;; mode: C++ ***
+;;; End: ***
+*/
+
--- a/src/DLD-FUNCTIONS/time.cc
+++ b/src/DLD-FUNCTIONS/time.cc
@@ -26,72 +26,53 @@
 
 #include <string>
 
-#include<iostream.h>
-
 #include "defun-dld.h"
 #include "error.h"
 #include "oct-map.h"
-#include "systime.h"
+#include "oct-time.h"
 #include "ov.h"
 #include "oct-obj.h"
-#include "utils.h"
 
 // Date and time functions.
 
 static Octave_map
-mk_tm_map (struct tm *tm, double fraction)
+mk_tm_map (const octave_base_tm& t)
 {
   Octave_map m;
 
-  m ["usec"] = fraction * 1e6;
-  m ["sec"] = static_cast<double> (tm->tm_sec);
-  m ["min"] = static_cast<double> (tm->tm_min);
-  m ["hour"] = static_cast<double> (tm->tm_hour);
-  m ["mday"] = static_cast<double> (tm->tm_mday);
-  m ["mon"] = static_cast<double> (tm->tm_mon);
-  m ["year"] = static_cast<double> (tm->tm_year);
-  m ["wday"] = static_cast<double> (tm->tm_wday);
-  m ["yday"] = static_cast<double> (tm->tm_yday);
-  m ["isdst"] = static_cast<double> (tm->tm_isdst);
-
-#if defined (HAVE_TM_ZONE)
-  m ["zone"]  = tm->tm_zone;
-#elif defined (HAVE_TZNAME)
-  if (tm->tm_isdst == 0 || tm->tm_isdst == 1)
-    m ["zone"] = tzname[tm->tm_isdst];
-#endif
+  m ["usec"] = static_cast<double> (t.usec ());
+  m ["sec"] = static_cast<double> (t.sec ());
+  m ["min"] = static_cast<double> (t.min ());
+  m ["hour"] = static_cast<double> (t.hour ());
+  m ["mday"] = static_cast<double> (t.mday ());
+  m ["mon"] = static_cast<double> (t.mon ());
+  m ["year"] = static_cast<double> (t.year ());
+  m ["wday"] = static_cast<double> (t.wday ());
+  m ["yday"] = static_cast<double> (t.yday ());
+  m ["isdst"] = static_cast<double> (t.isdst ());
+  m ["zone"]  = t.zone ();
 
   return m;
 }
 
-static struct tm*
-extract_tm (Octave_map &m, double& fraction)
+static octave_base_tm
+extract_tm (Octave_map &m)
 {
-  static struct tm tm;
+  octave_base_tm tm;
 
-  fraction = (m ["usec"] . double_value ()) / 1e6;
-  tm.tm_sec = static_cast<int> (m ["sec"] . double_value ());
-  tm.tm_min = static_cast<int> (m ["min"] . double_value ());
-  tm.tm_hour = static_cast<int> (m ["hour"] . double_value ());
-  tm.tm_mday = static_cast<int> (m ["mday"] . double_value ());
-  tm.tm_mon = static_cast<int> (m ["mon"] . double_value ());
-  tm.tm_year = static_cast<int> (m ["year"] . double_value ());
-  tm.tm_wday = static_cast<int> (m ["wday"] . double_value ());
-  tm.tm_yday = static_cast<int> (m ["yday"] . double_value ());
-  tm.tm_isdst = static_cast<int> (m ["isdst"] . double_value ());
+  tm.usec (static_cast<int> (m ["usec"] . double_value ()));
+  tm.sec (static_cast<int> (m ["sec"] . double_value ()));
+  tm.min (static_cast<int> (m ["min"] . double_value ()));
+  tm.hour (static_cast<int> (m ["hour"] . double_value ()));
+  tm.mday (static_cast<int> (m ["mday"] . double_value ()));
+  tm.mon (static_cast<int> (m ["mon"] . double_value ()));
+  tm.year (static_cast<int> (m ["year"] . double_value ()));
+  tm.wday (static_cast<int> (m ["wday"] . double_value ()));
+  tm.yday (static_cast<int> (m ["yday"] . double_value ()));
+  tm.isdst (static_cast<int> (m ["isdst"] . double_value ()));
+  tm.zone (m ["zone"] . string_value ());
 
-#if defined (HAVE_TM_ZONE)
-  static char *tm_zone = 0;
-
-  string tstr = m ["zone"] . string_value ();
-
-  delete [] tm_zone;
-  tm_zone = strsave (tstr.c_str ());
-
-  tm.tm_zone = tm_zone;
-#endif
-
-  return &tm;
+  return tm;
 }
 
 DEFUN_DLD (time, , ,
@@ -100,30 +81,9 @@
 Return current time.  On Unix systems, this is the number of\n\
 seconds since the epoch.")
 {
-  time_t now;
-  double fraction = 0.0;
-
-#if defined (HAVE_GETTIMEOFDAY)
-
-  struct timeval tp;
+  octave_time now;
 
-#if defined  (GETTIMEOFDAY_NO_TZ)
-  gettimeofday (&tp);
-#else
-  gettimeofday (&tp, 0);
-#endif
-
-  now = tp.tv_sec;
-
-  fraction = tp.tv_usec / 1e6;
-
-#else
-
-  now = time (0);
-
-#endif
- 
-  return static_cast<double> (now) + fraction;
+  return now.as_double ();
 }
 
 DEFUN_DLD (gmtime, args, ,
@@ -140,13 +100,7 @@
       double tmp = args(0).double_value ();
 
       if (! error_state)
-	{
-	  time_t timeval = static_cast<int> (tmp);
-	  double ip;
-	  double fraction = modf (tmp, &ip); 
-
-	  retval = octave_value (mk_tm_map (gmtime (&timeval), fraction));
-	}
+	retval = octave_value (mk_tm_map (octave_gmtime (tmp)));
     }
   else
     print_usage ("gmtime");
@@ -169,8 +123,8 @@
   year  : years since 1900\n\
   wday  : days since Sunday (0, 6)\n\
   yday  : days since January 1 (0, 365)\n\
-  isdst : Daylight Savings Time flag\n\
-  zone  : Time zone")
+  isdst : daylight savings time flag\n\
+  zone  : time zone")
 {
   octave_value_list retval;
 
@@ -179,13 +133,7 @@
       double tmp = args(0).double_value ();
 
       if (! error_state)
-	{
-	  time_t timeval = static_cast<int> (tmp);
-	  double ip;
-	  double fraction = modf (tmp, &ip); 
-
-	  retval = octave_value (mk_tm_map (localtime (&timeval), fraction));
-	}
+	retval = octave_value (mk_tm_map (octave_localtime (tmp)));
     }
   else
     print_usage ("localtime");
@@ -198,16 +146,25 @@
 {
   octave_value_list retval;
 
-  if (args.length () == 1 && args(0).is_map ()) 
+  if (args.length () == 1)
     {
       Octave_map map = args(0).map_value ();
 
-      double fraction;
+      if (! error_state)
+	{
+	  octave_base_tm tm = extract_tm (map);
+
+	  if (! error_state)
+	    {
+	      octave_time ot (tm);
 
-      struct tm *tm = extract_tm (map, fraction);
-
-      if (! error_state)
-	retval = static_cast<double> (mktime (tm)) + fraction;
+	      retval = ot.as_double ();
+	    }
+	  else
+	    error ("mktime: invalid TMSTRUCT argument");
+	}
+      else
+	error ("mktime: expecting structure argument");
     }
   else
     print_usage ("mktime");
@@ -215,10 +172,6 @@
   return retval;
 }
 
-#if !defined STRFTIME_BUF_INITIAL_SIZE
-#define STRFTIME_BUF_INITIAL_SIZE 128
-#endif
-
 DEFUN_DLD (strftime, args, ,
   "strftime (FMT, TMSTRUCT)\n\
 \n\
@@ -283,39 +236,28 @@
 {
   octave_value_list retval;
 
-  if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) 
+  if (args.length () == 2)
     {
       string fmt = args(0).string_value ();
 
-      Octave_map map = args(1).map_value ();
-
-      double fraction;
-
-      struct tm *tm = extract_tm (map, fraction);
-
       if (! error_state)
 	{
-	  const char *fmt_str = fmt.c_str ();
+	  Octave_map map = args(1).map_value ();
 
-	  char *buf = 0;
-	  size_t bufsize = STRFTIME_BUF_INITIAL_SIZE;
-	  size_t chars_written = 0;
-
-	  while (chars_written == 0)
+	  if (! error_state)
 	    {
-	      delete [] buf;
-	      buf = new char[bufsize];
-	      buf[0] = '\0';
-
-	      chars_written = strftime (buf, bufsize, fmt_str, tm);
+	      octave_base_tm tm = extract_tm (map);
 
-	      bufsize *= 2;
+	      if (! error_state)
+		retval = tm.format_as_string (fmt);
+	      else
+		error ("strftime: invalid TMSTRUCT argument");
 	    }
-
-	  retval = buf;
-
-	  delete [] buf;
+	  else
+	    error ("strftime: expecting structure as second argument");
 	}
+      else
+	error ("strftime: expecting format string as first argument");
     }
   else
     print_usage ("strftime");