diff --git a/.gitattributes b/.gitattributes index c737b3410f6de4af6a90a02bd4a91dc5e3417e2b..06b1fc657866a7f011b18d27b66b94401b1af90e 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 c3f84a0742a133b1d5bcdd94926b7062a656c67c..14d933d23e1cbd26610dd2382380d620365dcff9 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 b5ec6c9a67b1b55e4af0c5786fa249db918a2c2a..3aa9c5608946537b80e8733c0bcdc425460b81b6 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 e3fdc4fa68957ececb44125a7befd5c735c087e0..ff39ccb32ecfbb67920803a5322e0783cbf396f0 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 80159f6dd01cb493a3546e869cb930b5148c3193..767ba3a2ef5cec8be3d29c306b17300c858b7d28 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