Skip to content
Snippets Groups Projects
Commit 02eb4648 authored by Mirar (Pontus Hagland)'s avatar Mirar (Pontus Hagland)
Browse files

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)
parent 6a33fa08
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
# $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@
......
# $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()
......
#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,
......
/*
* $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 *****************************************/
......
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment