From c5e729f93b6cd6b8fe78f11e120754ae8b22c6d9 Mon Sep 17 00:00:00 2001
From: "Mirar (Pontus Hagland)" <pike@sort.mirar.org>
Date: Wed, 31 Mar 1999 15:12:38 +0200
Subject: [PATCH] initial checkin of Math classes library

Rev: src/modules/Math/.cvsignore:1.1
Rev: src/modules/Math/Makefile.in:1.1
Rev: src/modules/Math/configure.in:1.1
Rev: src/modules/Math/math.c:1.1
Rev: src/modules/Math/math.h:1.1
Rev: src/modules/Math/matrix.c:1.1
Rev: src/modules/Math/testsuite.in:1.1
Rev: src/modules/_math/.cvsignore:1.5
Rev: src/modules/_math/Makefile.in:1.5
Rev: src/modules/_math/Makefile.src:1.6(DEAD)
Rev: src/modules/_math/configure:1.2(DEAD)
Rev: src/modules/_math/configure.in:1.7
Rev: src/modules/_math/doc/acos:1.1.1.1
Rev: src/modules/_math/doc/asin:1.1.1.1
Rev: src/modules/_math/doc/atan:1.1.1.1
Rev: src/modules/_math/doc/ceil:1.1.1.1
Rev: src/modules/_math/doc/cos:1.1.1.1
Rev: src/modules/_math/doc/exp:1.1.1.1
Rev: src/modules/_math/doc/floor:1.1.1.1
Rev: src/modules/_math/doc/log:1.1.1.1
Rev: src/modules/_math/doc/pow:1.1.1.1
Rev: src/modules/_math/doc/sin:1.1.1.1
Rev: src/modules/_math/doc/sqrt:1.1.1.1
Rev: src/modules/_math/doc/tan:1.1.1.1
Rev: src/modules/_math/math.c:1.17
Rev: src/modules/_math/testsuite.in:1.2
Rev: src/modules/math/.cvsignore:1.5(DEAD)
Rev: src/modules/math/Makefile.in:1.5(DEAD)
Rev: src/modules/math/Makefile.src:1.6(DEAD)
Rev: src/modules/math/configure:1.2(DEAD)
Rev: src/modules/math/configure.in:1.7(DEAD)
Rev: src/modules/math/doc/acos:1.1.1.1(DEAD)
Rev: src/modules/math/doc/asin:1.1.1.1(DEAD)
Rev: src/modules/math/doc/atan:1.1.1.1(DEAD)
Rev: src/modules/math/doc/ceil:1.1.1.1(DEAD)
Rev: src/modules/math/doc/cos:1.1.1.1(DEAD)
Rev: src/modules/math/doc/exp:1.1.1.1(DEAD)
Rev: src/modules/math/doc/floor:1.1.1.1(DEAD)
Rev: src/modules/math/doc/log:1.1.1.1(DEAD)
Rev: src/modules/math/doc/pow:1.1.1.1(DEAD)
Rev: src/modules/math/doc/sin:1.1.1.1(DEAD)
Rev: src/modules/math/doc/sqrt:1.1.1.1(DEAD)
Rev: src/modules/math/doc/tan:1.1.1.1(DEAD)
Rev: src/modules/math/math.c:1.17(DEAD)
Rev: src/modules/math/testsuite.in:1.2(DEAD)
---
 .gitattributes                           |   6 +
 src/modules/Math/.cvsignore              |  13 ++
 src/modules/Math/.gitignore              |  13 ++
 src/modules/Math/Makefile.in             |   8 +
 src/modules/Math/configure.in            |   9 +
 src/modules/Math/math.c                  |  42 +++++
 src/modules/Math/math.h                  |   2 +
 src/modules/Math/matrix.c                | 210 +++++++++++++++++++++++
 src/modules/Math/testsuite.in            |   1 +
 src/modules/{math => _math}/.cvsignore   |   0
 src/modules/{math => _math}/.gitignore   |   0
 src/modules/{math => _math}/Makefile.in  |   2 +-
 src/modules/{math => _math}/configure.in |   2 +-
 src/modules/{math => _math}/doc/acos     |   0
 src/modules/{math => _math}/doc/asin     |   0
 src/modules/{math => _math}/doc/atan     |   0
 src/modules/{math => _math}/doc/ceil     |   0
 src/modules/{math => _math}/doc/cos      |   0
 src/modules/{math => _math}/doc/exp      |   0
 src/modules/{math => _math}/doc/floor    |   0
 src/modules/{math => _math}/doc/log      |   0
 src/modules/{math => _math}/doc/pow      |   0
 src/modules/{math => _math}/doc/sin      |   0
 src/modules/{math => _math}/doc/sqrt     |   0
 src/modules/{math => _math}/doc/tan      |   0
 src/modules/{math => _math}/math.c       |   2 +-
 src/modules/{math => _math}/testsuite.in |   0
 27 files changed, 307 insertions(+), 3 deletions(-)
 create mode 100644 src/modules/Math/.cvsignore
 create mode 100644 src/modules/Math/.gitignore
 create mode 100644 src/modules/Math/Makefile.in
 create mode 100644 src/modules/Math/configure.in
 create mode 100644 src/modules/Math/math.c
 create mode 100644 src/modules/Math/math.h
 create mode 100644 src/modules/Math/matrix.c
 create mode 100644 src/modules/Math/testsuite.in
 rename src/modules/{math => _math}/.cvsignore (100%)
 rename src/modules/{math => _math}/.gitignore (100%)
 rename src/modules/{math => _math}/Makefile.in (68%)
 rename src/modules/{math => _math}/configure.in (84%)
 rename src/modules/{math => _math}/doc/acos (100%)
 rename src/modules/{math => _math}/doc/asin (100%)
 rename src/modules/{math => _math}/doc/atan (100%)
 rename src/modules/{math => _math}/doc/ceil (100%)
 rename src/modules/{math => _math}/doc/cos (100%)
 rename src/modules/{math => _math}/doc/exp (100%)
 rename src/modules/{math => _math}/doc/floor (100%)
 rename src/modules/{math => _math}/doc/log (100%)
 rename src/modules/{math => _math}/doc/pow (100%)
 rename src/modules/{math => _math}/doc/sin (100%)
 rename src/modules/{math => _math}/doc/sqrt (100%)
 rename src/modules/{math => _math}/doc/tan (100%)
 rename src/modules/{math => _math}/math.c (99%)
 rename src/modules/{math => _math}/testsuite.in (100%)

diff --git a/.gitattributes b/.gitattributes
index 0ebdcedaa6..c737b3410f 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -210,6 +210,9 @@ testfont binary
 /src/modules/MIME/module.pmod.in foreign_ident
 /src/modules/MIME/testsuite.in foreign_ident
 /src/modules/Makefile.in foreign_ident
+/src/modules/Math/Makefile.in foreign_ident
+/src/modules/Math/configure.in foreign_ident
+/src/modules/Math/math.c foreign_ident
 /src/modules/Msql/Makefile.in foreign_ident
 /src/modules/Msql/configure.in foreign_ident
 /src/modules/Msql/msql_config.h.in foreign_ident
@@ -338,6 +341,9 @@ testfont binary
 /src/modules/_Image_XFace/configure.in foreign_ident
 /src/modules/_Image_XFace/image_xface.c foreign_ident
 /src/modules/_Image_XFace/testsuite.in foreign_ident
+/src/modules/_math/Makefile.in foreign_ident
+/src/modules/_math/configure.in foreign_ident
+/src/modules/_math/math.c foreign_ident
 /src/modules/call_out/Makefile.in foreign_ident
 /src/modules/call_out/call_out.c foreign_ident
 /src/modules/call_out/configure.in foreign_ident
diff --git a/src/modules/Math/.cvsignore b/src/modules/Math/.cvsignore
new file mode 100644
index 0000000000..ac7a4afa05
--- /dev/null
+++ b/src/modules/Math/.cvsignore
@@ -0,0 +1,13 @@
+Makefile
+config.h
+config.h.in
+config.log
+config.status
+configure
+dependencies
+linker_options
+modlist_headers
+modlist_segment
+module_testsuite
+stamp-h
+stamp-h.in
diff --git a/src/modules/Math/.gitignore b/src/modules/Math/.gitignore
new file mode 100644
index 0000000000..4e3fcaefb4
--- /dev/null
+++ b/src/modules/Math/.gitignore
@@ -0,0 +1,13 @@
+/Makefile
+/config.h
+/config.h.in
+/config.log
+/config.status
+/configure
+/dependencies
+/linker_options
+/modlist_headers
+/modlist_segment
+/module_testsuite
+/stamp-h
+/stamp-h.in
diff --git a/src/modules/Math/Makefile.in b/src/modules/Math/Makefile.in
new file mode 100644
index 0000000000..c3f84a0742
--- /dev/null
+++ b/src/modules/Math/Makefile.in
@@ -0,0 +1,8 @@
+# $Id: Makefile.in,v 1.1 1999/03/31 13:12:36 mirar Exp $
+SRCDIR=@srcdir@
+VPATH=@srcdir@:@srcdir@/../..:../..
+OBJS=math.o matrix.o
+MODULE_LDFLAGS=@LDFLAGS@ @LIBS@
+
+@dynamic_module_makefile@
+@dependencies@
diff --git a/src/modules/Math/configure.in b/src/modules/Math/configure.in
new file mode 100644
index 0000000000..b5ec6c9a67
--- /dev/null
+++ b/src/modules/Math/configure.in
@@ -0,0 +1,9 @@
+# $Id: configure.in,v 1.1 1999/03/31 13:12:36 mirar Exp $
+AC_INIT(math.c)
+AC_CONFIG_HEADER(config.h)
+
+AC_MODULE_INIT()
+
+AC_C_CHAR_UNSIGNED
+
+AC_OUTPUT(Makefile,echo FOO >stamp-h )
diff --git a/src/modules/Math/math.c b/src/modules/Math/math.c
new file mode 100644
index 0000000000..97490acc0c
--- /dev/null
+++ b/src/modules/Math/math.c
@@ -0,0 +1,42 @@
+/*
+ * $Id: math.c,v 1.1 1999/03/31 13:12:37 mirar Exp $
+ */
+
+#include "global.h"
+#include "config.h"
+
+#include "program.h"
+
+#include "math.h"
+
+/*** module init & exit & stuff *****************************************/
+
+/* add other parsers here */
+
+static struct math_class
+{
+   char *name;
+   void (*func)(void);
+} sub[] = {
+   {"Matrix",init_math_matrix},
+};
+
+void pike_module_exit(void)
+{
+}
+
+void pike_module_init(void)
+{
+   int i;
+   
+   for (i=0; i<(int)(sizeof(sub)/sizeof(sub[0])); i++)
+   {
+      struct program *p;
+
+      start_new_program();
+      sub[i].func();
+      p=end_program();
+      add_program_constant(sub[i].name,p,0);
+      free_program(p);
+   }
+}
diff --git a/src/modules/Math/math.h b/src/modules/Math/math.h
new file mode 100644
index 0000000000..c68f65cae7
--- /dev/null
+++ b/src/modules/Math/math.h
@@ -0,0 +1,2 @@
+void init_math_matrix();
+
diff --git a/src/modules/Math/matrix.c b/src/modules/Math/matrix.c
new file mode 100644
index 0000000000..a5d69de747
--- /dev/null
+++ b/src/modules/Math/matrix.c
@@ -0,0 +1,210 @@
+#include "global.h"
+#include "config.h"
+
+#include "pike_macros.h"
+#include "../../error.h"
+#include "object.h"
+#include "constants.h"
+#include "interpret.h"
+#include "svalue.h"
+#include "threads.h"
+#include "array.h"
+#include "operators.h"
+#include "builtin_functions.h"
+#include "mapping.h"
+
+/* ---------------------------------------------------------------- */
+
+#define FTYPE double
+
+struct matrix_storage
+{
+   int xsize,ysize;
+   FTYPE *m;
+};
+
+#ifdef THIS
+#undef THIS /* Needed for NT */
+#endif
+
+#define THIS ((struct matrix_storage*)(fp->current_storage))
+#define THISOBJ (fp->current_object)
+
+static struct pike_string *s_array;
+static struct pike_string *s_base;
+
+/* ---------------------------------------------------------------- */
+
+static void init_matrix(struct object *o)
+{
+   THIS->xsize=THIS->ysize=0;
+   THIS->m=NULL;
+}
+
+static void exit_matrix(struct object *o)
+{
+   if (THIS->m) free(THIS->m);
+}
+
+/* ---------------------------------------------------------------- */
+
+static void matrix_create(INT32 args)
+{
+   int ys=0,xs=0;
+   int i,j;
+   FTYPE *m=NULL;
+
+   if (!args)
+      SIMPLE_TOO_FEW_ARGS_ERROR("matrix",1);
+
+   if (THIS->m)
+      SIMPLE_BAD_ARG_ERROR("matrix",1,"not to be called again");
+   
+   if (sp[-args].type==T_ARRAY)
+   {
+      ys=THIS->ysize=sp[-args].u.array->size;
+
+      if (ys<1)
+	 SIMPLE_BAD_ARG_ERROR("matrix",1,"non-empty array");
+      for (i=0; i<ys; i++)
+      {
+	 if (sp[-args].u.array->item[i].type!=T_ARRAY)
+	    SIMPLE_BAD_ARG_ERROR("matrix",1,"array(array)");
+	 if (i==0) 
+	 {
+	    xs=sp[-args].u.array->item[i].u.array->size;
+	    THIS->m=m=malloc(sizeof(FTYPE)*xs*ys);
+	    if (!m)
+	       SIMPLE_OUT_OF_MEMORY_ERROR("matrix",
+					  sizeof(FTYPE)*xs*ys);
+	 }
+	 else
+	    if (xs!=sp[-args].u.array->item[i].u.array->size)
+	       SIMPLE_BAD_ARG_ERROR("matrix",1,
+				    "array of equal sized arrays");
+	 for (j=0; j<xs; j++)
+	    switch (sp[-args].u.array->item[i].u.array->item[j].type)
+	    {
+	       case T_INT:
+		  *(m++)=(FTYPE)
+		     sp[-args].u.array->item[i].u.array->item[j].u.integer;
+		  break;
+	       case T_FLOAT:
+		  *(m++)=(FTYPE)
+		     sp[-args].u.array->item[i].u.array->item[j].u.float_number;
+		  break;
+	       default:
+	       SIMPLE_BAD_ARG_ERROR("matrix",1,
+				    "array(array(int|float))");
+	    }
+      }
+      THIS->xsize=xs;
+   }
+   else if (sp[-args].type==T_INT)
+   {
+      FTYPE z=0.0;
+
+      if (args<2)
+	 SIMPLE_TOO_FEW_ARGS_ERROR("matrix",2);
+      if (sp[1-args].type!=T_INT)
+	 SIMPLE_BAD_ARG_ERROR("matrix",2,"int");
+
+      if ((THIS->xsize=xs=sp[-args].u.integer)<=0)
+	 SIMPLE_BAD_ARG_ERROR("matrix",1,"int > 0");
+      if ((THIS->ysize=ys=sp[1-args].u.integer)<=0)
+	 SIMPLE_BAD_ARG_ERROR("matrix",2,"int > 0");
+
+      THIS->m=m=malloc(sizeof(FTYPE)*xs*ys);
+      if (!m)
+	 SIMPLE_OUT_OF_MEMORY_ERROR("matrix",
+				    sizeof(FTYPE)*xs*ys);
+      
+      if (args>2)
+	 if (sp[2-args].type==T_INT)
+	    z=(FTYPE)sp[2-args].u.integer;
+	 else if (sp[2-args].type==T_FLOAT)
+	    z=(FTYPE)sp[2-args].u.integer;
+	 else if (sp[2-args].type!=T_STRING)
+	 {
+	    if (sp[2-args].u.string==s_base)
+	    {
+	       pop_n_elems(args-2); /* same as nothing */
+	       args=2;
+	    }
+	    else
+	       SIMPLE_BAD_ARG_ERROR("matrix",3,"\"base\"|int|float");
+	    /* insert other base matrices here */
+	 }
+	 else
+	    SIMPLE_BAD_ARG_ERROR("matrix",3,"int|float|string");
+      
+      xs*=ys;
+      while (xs--) *(m++)=z;
+
+      if (args==2) /* fill base */
+      {
+	 xs=THIS->xsize;
+	 for (i=0; i<xs && i<ys; i++)
+	    THIS->m[i*(xs+1)]=1.0;
+      }
+
+done_made:
+   }
+   else
+      SIMPLE_BAD_ARG_ERROR("matrix",1,"array|int");
+
+   pop_n_elems(args);
+   push_int(0);
+}
+
+/* ---------------------------------------------------------------- */
+
+void matrix_cast(INT32 args)
+{
+   if (!THIS->m)
+   {
+      pop_n_elems(args);
+      push_int(0);
+   }
+
+   if (args)
+      if (sp[-1].type==T_STRING)
+	 if (sp[-1].u.string==s_array)
+	 {
+	    int i,j;
+	    int xs=THIS->xsize,ys=THIS->ysize;
+	    FTYPE *m=THIS->m;
+	    check_stack(xs+ys);
+	    pop_n_elems(args);
+	    for (i=0; i<ys; i++)
+	    {
+	       for (j=0; j<xs; j++)
+		  push_float((FLOAT_TYPE)*(m++));
+	       f_aggregate(xs);
+	    }
+	    f_aggregate(ys);
+	    return;
+	 }
+   SIMPLE_BAD_ARG_ERROR("matrix->cast",1,"string");
+}
+
+/* ---------------------------------------------------------------- */
+
+void init_math_matrix()
+{
+   MAKE_CONSTANT_SHARED_STRING(s_array,"array");
+   MAKE_CONSTANT_SHARED_STRING(s_base,"base");
+
+   ADD_STORAGE(struct matrix_storage);
+   
+   set_init_callback(init_matrix);
+   set_exit_callback(exit_matrix);
+
+   add_function("create",matrix_create,
+		"function(array(array(int|float)):object)|"
+		"function(int(1..),int(1..),int|float|string|void:object)",
+		0);
+   
+   add_function("cast",matrix_cast,
+		"function(string:array(array(float)))",0);
+}
diff --git a/src/modules/Math/testsuite.in b/src/modules/Math/testsuite.in
new file mode 100644
index 0000000000..8b13789179
--- /dev/null
+++ b/src/modules/Math/testsuite.in
@@ -0,0 +1 @@
+
diff --git a/src/modules/math/.cvsignore b/src/modules/_math/.cvsignore
similarity index 100%
rename from src/modules/math/.cvsignore
rename to src/modules/_math/.cvsignore
diff --git a/src/modules/math/.gitignore b/src/modules/_math/.gitignore
similarity index 100%
rename from src/modules/math/.gitignore
rename to src/modules/_math/.gitignore
diff --git a/src/modules/math/Makefile.in b/src/modules/_math/Makefile.in
similarity index 68%
rename from src/modules/math/Makefile.in
rename to src/modules/_math/Makefile.in
index 97e4314459..3a0ee390fe 100644
--- a/src/modules/math/Makefile.in
+++ b/src/modules/_math/Makefile.in
@@ -1,4 +1,4 @@
-# $Id: Makefile.in,v 1.5 1998/03/28 13:57:26 grubba Exp $
+# $Id: Makefile.in,v 1.5 1999/03/31 13:12:37 mirar fake $
 SRCDIR=@srcdir@
 VPATH=@srcdir@:@srcdir@/../..:../..
 MODULE_LDFLAGS=@LIBS@
diff --git a/src/modules/math/configure.in b/src/modules/_math/configure.in
similarity index 84%
rename from src/modules/math/configure.in
rename to src/modules/_math/configure.in
index da42d743c1..58ee3c83f7 100644
--- a/src/modules/math/configure.in
+++ b/src/modules/_math/configure.in
@@ -1,4 +1,4 @@
-# $Id: configure.in,v 1.7 1998/09/20 08:33:34 hubbe Exp $
+# $Id: configure.in,v 1.7 1999/03/31 13:12:37 mirar fake $
 AC_INIT(math.c)
 
 
diff --git a/src/modules/math/doc/acos b/src/modules/_math/doc/acos
similarity index 100%
rename from src/modules/math/doc/acos
rename to src/modules/_math/doc/acos
diff --git a/src/modules/math/doc/asin b/src/modules/_math/doc/asin
similarity index 100%
rename from src/modules/math/doc/asin
rename to src/modules/_math/doc/asin
diff --git a/src/modules/math/doc/atan b/src/modules/_math/doc/atan
similarity index 100%
rename from src/modules/math/doc/atan
rename to src/modules/_math/doc/atan
diff --git a/src/modules/math/doc/ceil b/src/modules/_math/doc/ceil
similarity index 100%
rename from src/modules/math/doc/ceil
rename to src/modules/_math/doc/ceil
diff --git a/src/modules/math/doc/cos b/src/modules/_math/doc/cos
similarity index 100%
rename from src/modules/math/doc/cos
rename to src/modules/_math/doc/cos
diff --git a/src/modules/math/doc/exp b/src/modules/_math/doc/exp
similarity index 100%
rename from src/modules/math/doc/exp
rename to src/modules/_math/doc/exp
diff --git a/src/modules/math/doc/floor b/src/modules/_math/doc/floor
similarity index 100%
rename from src/modules/math/doc/floor
rename to src/modules/_math/doc/floor
diff --git a/src/modules/math/doc/log b/src/modules/_math/doc/log
similarity index 100%
rename from src/modules/math/doc/log
rename to src/modules/_math/doc/log
diff --git a/src/modules/math/doc/pow b/src/modules/_math/doc/pow
similarity index 100%
rename from src/modules/math/doc/pow
rename to src/modules/_math/doc/pow
diff --git a/src/modules/math/doc/sin b/src/modules/_math/doc/sin
similarity index 100%
rename from src/modules/math/doc/sin
rename to src/modules/_math/doc/sin
diff --git a/src/modules/math/doc/sqrt b/src/modules/_math/doc/sqrt
similarity index 100%
rename from src/modules/math/doc/sqrt
rename to src/modules/_math/doc/sqrt
diff --git a/src/modules/math/doc/tan b/src/modules/_math/doc/tan
similarity index 100%
rename from src/modules/math/doc/tan
rename to src/modules/_math/doc/tan
diff --git a/src/modules/math/math.c b/src/modules/_math/math.c
similarity index 99%
rename from src/modules/math/math.c
rename to src/modules/_math/math.c
index 08fc98c38f..2bb3972a55 100644
--- a/src/modules/math/math.c
+++ b/src/modules/_math/math.c
@@ -24,7 +24,7 @@
 #include <floatingpoint.h>
 #endif
 
-RCSID("$Id: math.c,v 1.17 1999/02/10 21:53:47 hubbe Exp $");
+RCSID("$Id: math.c,v 1.17 1999/03/31 13:12:37 mirar fake $");
 
 #ifndef M_PI
 #define M_PI 3.1415926535897932384626433832795080
diff --git a/src/modules/math/testsuite.in b/src/modules/_math/testsuite.in
similarity index 100%
rename from src/modules/math/testsuite.in
rename to src/modules/_math/testsuite.in
-- 
GitLab