From 02eb46487c2c8179c04119b91ae46b66faf1ffa8 Mon Sep 17 00:00:00 2001 From: "Mirar (Pontus Hagland)" <pike@sort.mirar.org> Date: Wed, 31 Mar 1999 21:27:19 +0200 Subject: [PATCH] fixes & doc Rev: src/modules/Math/Makefile.in:1.2 Rev: src/modules/Math/configure.in:1.2 Rev: src/modules/Math/math.c:1.3(DEAD) Rev: src/modules/Math/math.h:1.2(DEAD) Rev: src/modules/Math/math_matrix.c:1.1 Rev: src/modules/Math/math_module.c:1.1 Rev: src/modules/Math/math_module.h:1.1 Rev: src/modules/Math/matrix.c:1.3(DEAD) --- .gitattributes | 2 +- src/modules/Math/Makefile.in | 4 +- src/modules/Math/configure.in | 4 +- src/modules/Math/{matrix.c => math_matrix.c} | 132 +++++++++++++++++++ src/modules/Math/{math.c => math_module.c} | 4 +- src/modules/Math/{math.h => math_module.h} | 0 6 files changed, 139 insertions(+), 7 deletions(-) rename src/modules/Math/{matrix.c => math_matrix.c} (74%) rename src/modules/Math/{math.c => math_module.c} (90%) rename src/modules/Math/{math.h => math_module.h} (100%) diff --git a/.gitattributes b/.gitattributes index c737b3410f..06b1fc6578 100644 --- a/.gitattributes +++ b/.gitattributes @@ -212,7 +212,7 @@ testfont binary /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/Math/math_module.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 diff --git a/src/modules/Math/Makefile.in b/src/modules/Math/Makefile.in index c3f84a0742..14d933d23e 100644 --- a/src/modules/Math/Makefile.in +++ b/src/modules/Math/Makefile.in @@ -1,7 +1,7 @@ -# $Id: Makefile.in,v 1.1 1999/03/31 13:12:36 mirar Exp $ +# $Id: Makefile.in,v 1.2 1999/03/31 19:27:11 mirar Exp $ SRCDIR=@srcdir@ VPATH=@srcdir@:@srcdir@/../..:../.. -OBJS=math.o matrix.o +OBJS=math_module.o math_matrix.o MODULE_LDFLAGS=@LDFLAGS@ @LIBS@ @dynamic_module_makefile@ diff --git a/src/modules/Math/configure.in b/src/modules/Math/configure.in index b5ec6c9a67..3aa9c56089 100644 --- a/src/modules/Math/configure.in +++ b/src/modules/Math/configure.in @@ -1,5 +1,5 @@ -# $Id: configure.in,v 1.1 1999/03/31 13:12:36 mirar Exp $ -AC_INIT(math.c) +# $Id: configure.in,v 1.2 1999/03/31 19:27:13 mirar Exp $ +AC_INIT(math_module.c) AC_CONFIG_HEADER(config.h) AC_MODULE_INIT() diff --git a/src/modules/Math/matrix.c b/src/modules/Math/math_matrix.c similarity index 74% rename from src/modules/Math/matrix.c rename to src/modules/Math/math_matrix.c index e3fdc4fa68..ff39ccb32e 100644 --- a/src/modules/Math/matrix.c +++ b/src/modules/Math/math_matrix.c @@ -1,6 +1,8 @@ #include "global.h" #include "config.h" +#include <math.h> + #include "pike_macros.h" #include "../../error.h" #include "object.h" @@ -13,6 +15,19 @@ #include "builtin_functions.h" #include "mapping.h" +#include "math_module.h" + +/* +**! module Math +**! class Matrix +**! +**! This class hold Matrix capabilites, +**! and support a range of matrix operations. +**! +**! It can only - for speed and simplicity - +**! hold floating point numbers and are only in 2 dimensions. +*/ + /* ---------------------------------------------------------------- */ extern struct program *math_matrix_program; @@ -51,6 +66,31 @@ static void exit_matrix(struct object *o) /* ---------------------------------------------------------------- */ +/* +**! method void create(array(array(int|float))) +**! method void create(int n,int m) +**! method void create(int n,int m,string type) +**! method void create(int n,int m,float|int init) +**! +**! This method initializes the matrix. +**! It is illegal to create and hold an empty matrix. +**! +**! The normal operation is to create the matrix object +**! with a double array, like +**! <tt>Math.Matrix( ({({1,2}),({3,4})}) )</tt>. +**! +**! Another use is to create a special type of matrix, +**! and this is told as third argument. +**! +**! Currently there are only the "identity" type, +**! which gives a matrix of zeroes except in the diagonal, +**! where the number one (1.0) is. This is the default, +**! too. +**! +**! The third use is to give all indices in the +**! matrix the same value, for instance zero or 42. +*/ + static void matrix_create(INT32 args) { int ys=0,xs=0; @@ -169,6 +209,12 @@ done_made: /* ---------------------------------------------------------------- */ +/* +**! method array(array(float)) cast(string to_what) +**! This is to be able to get the matrix values. +**! This gives back a double array of floats. +*/ + void matrix_cast(INT32 args) { if (!THIS->m) @@ -213,6 +259,11 @@ static INLINE struct matrix_storage * _push_new_matrix(int xsize,int ysize) /* --- real math stuff --------------------------------------------- */ +/* +**! method object transpose() +**! Transpose of the matrix as a new object. +*/ + static void matrix_transpose(INT32 args) { struct matrix_storage *mx; @@ -237,6 +288,74 @@ static void matrix_transpose(INT32 args) } } +/* +**! method float norm() +**! method float norm2() +**! Norm of the matrix, and the square of the norm +**! of the matrix. (The later method is because you +**! may skip a square root sometimes.) +**! +**! This equals |A| or sqrt( A<sub>0</sub><sup>2</sup> + +**! A<sub>1</sub><sup>2</sup> + ... + A<sub>n</sub><sup>2</sup> ). +**! +**! It is only usable with 1xn or nx1 matrices. +*/ + +static void matrix_norm(INT32 args) +{ + struct matrix_storage *mx; + FTYPE z,*s; + int n=THIS->xsize*THIS->ysize; + + pop_n_elems(args); + + if (!(THIS->xsize==1 || THIS->ysize==1)) + math_error("Matrix->norm",sp-args,args,0, + "Cannot compute norm of non 1xn or nx1 matrices"); + + z=0.0; + s=THIS->m; + while (n--) + z+=*s**s,s++; + + push_float(sqrt(z)); +} + +static void matrix_norm2(INT32 args) +{ + struct matrix_storage *mx; + FTYPE z,*s; + int n=THIS->xsize*THIS->ysize; + + pop_n_elems(args); + + if (!(THIS->xsize==1 || THIS->ysize==1)) + math_error("Matrix->norm",sp-args,args,0, + "Cannot compute norm of non 1xn or nx1 matrices"); + + z=0.0; + s=THIS->m; + while (n--) + z+=*s**s,s++; + + push_float(z); +} + +/* +**! method object `+(object with) +**! method object ``+(object with) +**! method object add(object with) +**! Add this matrix to another matrix. A new matrix is returned. +**! The matrices must have the same size. +**! +**! method object `-() +**! method object `-(object with) +**! method object ``-(object with) +**! method object sub(object with) +**! Subtracts this matrix from another. A new matrix is returned. +**! -<i>m</i> is equal to -1*<i>m</i>. +*/ + static void matrix_add(INT32 args) { struct matrix_storage *mx=NULL; @@ -314,6 +433,13 @@ static void matrix_sub(INT32 args) *(d++)=-*(s1++); } +/* +**! method object `*(object with) +**! method object ``*(object with) +**! method object mult(object with) +**! Matrix multiplication. +*/ + static void matrix_mult(INT32 args) { struct matrix_storage *mx=NULL; @@ -382,6 +508,7 @@ scalar_mult: pop_stack(); } + /* ---------------------------------------------------------------- */ void init_math_matrix() @@ -407,6 +534,11 @@ void init_math_matrix() add_function("transpose",matrix_transpose, "function(:object)",0); + add_function("norm",matrix_norm, + "function(:float)",0); + add_function("norm2",matrix_norm2, + "function(:float)",0); + add_function("add",matrix_add, "function(object:object)",0); add_function("`+",matrix_add, diff --git a/src/modules/Math/math.c b/src/modules/Math/math_module.c similarity index 90% rename from src/modules/Math/math.c rename to src/modules/Math/math_module.c index 80159f6dd0..767ba3a2ef 100644 --- a/src/modules/Math/math.c +++ b/src/modules/Math/math_module.c @@ -1,5 +1,5 @@ /* - * $Id: math.c,v 1.2 1999/03/31 18:36:52 mirar Exp $ + * $Id: math_module.c,v 1.1 1999/03/31 19:27:18 mirar Exp $ */ #include "global.h" @@ -7,7 +7,7 @@ #include "program.h" -#include "math.h" +#include "math_module.h" /*** module init & exit & stuff *****************************************/ diff --git a/src/modules/Math/math.h b/src/modules/Math/math_module.h similarity index 100% rename from src/modules/Math/math.h rename to src/modules/Math/math_module.h -- GitLab