e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
6f7fdc1999-10-26Mirar (Pontus Hagland) 
c5e7291999-03-31Mirar (Pontus Hagland) #include "global.h" #include "config.h"
02eb461999-03-31Mirar (Pontus Hagland) #include <math.h>
c32c932001-05-02Henrik Grubbström (Grubba) #include "pike_error.h"
c5e7291999-03-31Mirar (Pontus Hagland) #include "interpret.h" #include "svalue.h" #include "operators.h" #include "builtin_functions.h"
d2a0bd1999-10-25Mirar (Pontus Hagland) #include "module_support.h"
68ec3f2014-08-18Martin Nilsson #include "pike_types.h"
c5e7291999-03-31Mirar (Pontus Hagland) 
02eb461999-03-31Mirar (Pontus Hagland) #include "math_module.h"
c186d22001-07-15Per Hedbor #include "bignum.h"
6dc2772000-07-28Fredrik Hübinette (Hubbe) 
b442372018-01-19Henrik Grubbström (Grubba) #ifdef HAVE_MPI_H
e9169b2012-05-17Tobias S. Josefowitz #include <mpi.h>
b442372018-01-19Henrik Grubbström (Grubba) #endif
e9169b2012-05-17Tobias S. Josefowitz 
b7b40e2004-03-02Martin Nilsson /*! @module Math */ /*! @class Matrix *! Matrix representation with double precision floating point values. */
02eb461999-03-31Mirar (Pontus Hagland) 
6a33fa1999-03-31Mirar (Pontus Hagland) static struct pike_string *s__clr; static struct pike_string *s_identity;
d2a0bd1999-10-25Mirar (Pontus Hagland) static struct pike_string *s_rotate;
c5e7291999-03-31Mirar (Pontus Hagland) 
c186d22001-07-15Per Hedbor extern struct program *math_matrix_program; extern struct program *math_smatrix_program; extern struct program *math_imatrix_program; extern struct program *math_fmatrix_program; extern struct program *math_lmatrix_program;
c5e7291999-03-31Mirar (Pontus Hagland) 
2f917c2012-11-04Tobias S. Josefowitz #ifdef HAS_MPI struct size_info { int x, y; }; struct op_info { struct object *o; void **data; }; extern PMOD_EXPORT struct object *mpi_ex_op(struct op_info (*create)(void *), struct svalue *ufun, int commute, void *info, int shift, int len); #endif
e2ec1f2007-12-19Martin Nilsson #define PNAME "Matrix"
c186d22001-07-15Per Hedbor #define FTYPE double
09959a2005-01-20Martin Nilsson #define PTYPE tFloat
c186d22001-07-15Per Hedbor #define matrixX(X) PIKE_CONCAT(matrix,X) #define Xmatrix(X) PIKE_CONCAT(X,matrix) #define XmatrixY(X,Y) PIKE_CONCAT3(X,matrix,Y) #define PUSH_ELEM( X ) push_float( (FLOAT_TYPE)(X) )
e9169b2012-05-17Tobias S. Josefowitz #define MATRIX_MPI_TYPE MPI_DOUBLE #define MATRIX_MPI_SHIFT (17<<3)
c186d22001-07-15Per Hedbor #include <matrix_code.h>
e9169b2012-05-17Tobias S. Josefowitz #undef MATRIX_MPI_SHIFT #undef MATRIX_MPI_TYPE
c186d22001-07-15Per Hedbor #undef PUSH_ELEM #undef matrixX #undef Xmatrix #undef XmatrixY #undef FTYPE
99a1c32001-09-07Fredrik Hübinette (Hubbe) #undef PTYPE
e2ec1f2007-12-19Martin Nilsson #undef PNAME
c186d22001-07-15Per Hedbor 
e2ec1f2007-12-19Martin Nilsson #define PNAME "IMatrix"
c186d22001-07-15Per Hedbor #define FTYPE int
09959a2005-01-20Martin Nilsson #define PTYPE tInt
c186d22001-07-15Per Hedbor #define matrixX(X) PIKE_CONCAT(imatrix,X) #define Xmatrix(X) PIKE_CONCAT(X,imatrix) #define XmatrixY(X,Y) PIKE_CONCAT3(X,imatrix,Y) #define PUSH_ELEM( X ) push_int( (INT_TYPE)(X) )
e9169b2012-05-17Tobias S. Josefowitz #define MATRIX_MPI_TYPE MPI_INT #define MATRIX_MPI_SHIFT (18<<3)
c186d22001-07-15Per Hedbor #include <matrix_code.h>
e9169b2012-05-17Tobias S. Josefowitz #undef MATRIX_MPI_SHIFT #undef MATRIX_MPI_TYPE
c186d22001-07-15Per Hedbor #undef PUSH_ELEM #undef Xmatrix #undef matrixX #undef XmatrixY #undef FTYPE
99a1c32001-09-07Fredrik Hübinette (Hubbe) #undef PTYPE
e2ec1f2007-12-19Martin Nilsson #undef PNAME
c186d22001-07-15Per Hedbor 
e2ec1f2007-12-19Martin Nilsson #define PNAME "LMatrix"
a8b9f42001-07-16Henrik Grubbström (Grubba) #define FTYPE INT64
09959a2005-01-20Martin Nilsson #define PTYPE tInt
c186d22001-07-15Per Hedbor #define matrixX(X) PIKE_CONCAT(lmatrix,X) #define Xmatrix(X) PIKE_CONCAT(X,lmatrix) #define XmatrixY(X,Y) PIKE_CONCAT3(X,lmatrix,Y)
99a1c32001-09-07Fredrik Hübinette (Hubbe) #define PUSH_ELEM( X ) push_int64( (INT64)(X) )
e9169b2012-05-17Tobias S. Josefowitz #define MATRIX_MPI_TYPE MPI_LONG_LONG_INT #define MATRIX_MPI_SHIFT (19<<3)
c186d22001-07-15Per Hedbor #include <matrix_code.h>
e9169b2012-05-17Tobias S. Josefowitz #undef MATRIX_MPI_SHIFT #undef MATRIX_MPI_TYPE
c186d22001-07-15Per Hedbor #undef PUSH_ELEM #undef Xmatrix #undef matrixX #undef XmatrixY #undef FTYPE
99a1c32001-09-07Fredrik Hübinette (Hubbe) #undef PTYPE
e2ec1f2007-12-19Martin Nilsson #undef PNAME
c186d22001-07-15Per Hedbor 
e2ec1f2007-12-19Martin Nilsson #define PNAME "FMatrix"
c186d22001-07-15Per Hedbor #define FTYPE float
09959a2005-01-20Martin Nilsson #define PTYPE tFloat
c186d22001-07-15Per Hedbor #define matrixX(X) PIKE_CONCAT(fmatrix,X) #define Xmatrix(X) PIKE_CONCAT(X,fmatrix) #define XmatrixY(X,Y) PIKE_CONCAT3(X,fmatrix,Y) #define PUSH_ELEM( X ) push_float( (FLOAT_TYPE)(X) )
e9169b2012-05-17Tobias S. Josefowitz #define MATRIX_MPI_TYPE MPI_FLOAT #define MATRIX_MPI_SHIFT (20<<3)
c186d22001-07-15Per Hedbor #include <matrix_code.h>
e9169b2012-05-17Tobias S. Josefowitz #undef MATRIX_MPI_SHIFT #undef MATRIX_MPI_TYPE
c186d22001-07-15Per Hedbor #undef PUSH_ELEM #undef Xmatrix #undef matrixX #undef XmatrixY #undef FTYPE
99a1c32001-09-07Fredrik Hübinette (Hubbe) #undef PTYPE
e2ec1f2007-12-19Martin Nilsson #undef PNAME
c186d22001-07-15Per Hedbor 
e2ec1f2007-12-19Martin Nilsson #define PNAME "SMatrix"
c186d22001-07-15Per Hedbor #define FTYPE short
09959a2005-01-20Martin Nilsson #define PTYPE tInt
c186d22001-07-15Per Hedbor #define matrixX(X) PIKE_CONCAT(smatrix,X) #define Xmatrix(X) PIKE_CONCAT(X,smatrix) #define XmatrixY(X,Y) PIKE_CONCAT3(X,smatrix,Y) #define PUSH_ELEM( X ) push_int( (INT_TYPE)(X) )
e9169b2012-05-17Tobias S. Josefowitz #define MATRIX_MPI_TYPE MPI_SHORT_INT #define MATRIX_MPI_SHIFT (21<<3)
c186d22001-07-15Per Hedbor #include <matrix_code.h>
e9169b2012-05-17Tobias S. Josefowitz #undef MATRIX_MPI_SHIFT #undef MATRIX_MPI_TYPE
c186d22001-07-15Per Hedbor #undef PUSH_ELEM #undef Xmatrix #undef matrixX #undef XmatrixY #undef FTYPE
99a1c32001-09-07Fredrik Hübinette (Hubbe) #undef PTYPE
e2ec1f2007-12-19Martin Nilsson #undef PNAME
c5e7291999-03-31Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl void create(array(array(int|float)) matrix_2d) *! @decl void create(array(int|float) matrix_1d) *! Initializes the matrix as a 1D or 2D matrix, e.g. *! @expr{Math.Matrix( ({({1,2}),({3,4})}) )@}. */ /*! @decl void create(int n,int m) *! @decl void create(int n,int m,string type) *! @decl void create(int n,int m,float|int init) *! Initializes the matrix as to be a @[n]*@[m] matrix with @[init] in *! every value. If no third argument is given, or the third argument *! is @expr{"identity"@}, the matrix will be initialized with all *! zeroes except for the diagonal which will be @expr{1@}. */ /*! @decl void create(string type,int size) *! When @[type] is @expr{"identity"@} the matrix is initializes as a *! square identity matrix. */ /*! @decl void create(string type,int size,float rads,Matrix axis) *! @decl void create(string type,int size,float rads,float x,float y,float z) *! *! When @[type] is @expr{"rotate"@} the matrix is initialized as a *! rotation matrix. */
02eb461999-03-31Mirar (Pontus Hagland) 
c5e7291999-03-31Mirar (Pontus Hagland) /* ---------------------------------------------------------------- */
7a74cc2014-08-26Per Hedbor /*! @decl array(array(float)) cast(string to_what) *! @decl array(array(float)) cast(string to_what)
b7b40e2004-03-02Martin Nilsson  *! *! It is possible to cast the matrix to an array and get back a *! double array of floats with the matrix values. *! @seealso *! @[vect] */
02eb461999-03-31Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl array vect() *! Return all the elements of the matrix as an array of numbers */
d2a0bd1999-10-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix transpose() *! Returns the transpose of the matrix as a new object. */
d2a0bd1999-10-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl float norm() *! @decl float norm2() *! @decl Matrix normv() *! 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@}@sup{2@} + *! A@sub{1@}@sup{2@} + ... + A@sub{n@}@sup{2@} ). *! *! It is only usable with 1xn or nx1 matrices. *! *! @expr{m->normv()@} is equal to @expr{m*(1.0/m->norm())@}, *! with the exception that the zero vector will still be *! the zero vector (no error). */
d2a0bd1999-10-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix `+(object with) *! @decl Matrix ``+(object with) *! @decl Matrix add(object with) *! Add this matrix to another matrix. A new matrix is returned. *! The matrices must have the same size. *! *! @decl Matrix `-() *! @decl Matrix `-(object with) *! @decl Matrix ``-(object with) *! @decl Matrix sub(object with) *! Subtracts this matrix from another. A new matrix is returned. *! @expr{-m@} is equal to @expr{-1*m@}. */
02eb461999-03-31Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix sum() *! Produces the sum of all the elements in the matrix. */
6a33fa1999-03-31Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix max() *! @decl Matrix min() *! Produces the maximum or minimum value of all the elements in the *! matrix. */
02eb461999-03-31Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix `*(object with) *! @decl Matrix ``*(object with) *! @decl Matrix mult(object with) *! Matrix multiplication. */
d2a0bd1999-10-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix cross(object with) *! Matrix cross-multiplication. */
02eb461999-03-31Mirar (Pontus Hagland) 
6a33fa1999-03-31Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl float dot_product(object with) *! Matrix dot product. */
4e64922001-04-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @decl Matrix convolve(object with) *! Convolve called matrix with the argument. */
4e64922001-04-25Mirar (Pontus Hagland) 
e2ec1f2007-12-19Martin Nilsson /*! @decl int xsize() *! Returns the width of the matrix. */ /*! @decl int ysize() *! Returns the height of the matrix. */
b7b40e2004-03-02Martin Nilsson /*! @endclass */
d2a0bd1999-10-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @class FMatrix *! @inherit Matrix *! Matrix representation with single precision floating point values. *! @endclass */
d2a0bd1999-10-25Mirar (Pontus Hagland) 
b7b40e2004-03-02Martin Nilsson /*! @class LMatrix *! @inherit Matrix *! Matrix representation with 64 bit integer values. *! @endclass */
2978d52001-04-17Johan Schön 
b7b40e2004-03-02Martin Nilsson /*! @class IMatrix *! @inherit Matrix *! Matrix representation with 32 bit integer values. *! @endclass */ /*! @class SMatrix *! @inherit Matrix *! Matrix representation with 16 bit integer values. *! @endclass */ /*! @endmodule */