📄 matrix.h
字号:
/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
** Meschach Library
**
** This Meschach Library is provided "as is" without any express
** or implied warranty of any kind with respect to this software.
** In particular the authors shall not be liable for any direct,
** indirect, special, incidental or consequential damages arising
** in any way from use of the software.
**
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
** 1. All copies contain this copyright notice.
** 2. All modified copies shall carry a notice stating who
** made the last modification and the date of such modification.
** 3. No charge is made for this software or works derived from it.
** This clause shall not be construed as constraining other software
** distributed on the same medium as this software, nor is a
** distribution fee considered a charge.
**
***************************************************************************/
/*
Type definitions for general purpose maths package
*/
#ifndef MATRIXH
/* RCS id: $Id: matrix.h,v 1.18 1994/04/16 00:33:37 des Exp $ */
#define MATRIXH
#include "machine.h"
#include "err.h"
#include "meminfo.h"
/* unsigned integer type */
/************************************************************
#ifndef U_INT_DEF
typedef unsigned int u_int;
#define U_INT_DEF
#endif
************************************************************/
/* vector definition */
typedef struct {
unsigned int dim, max_dim;
Real *ve;
} VEC;
/* matrix definition */
typedef struct {
unsigned int m, n;
unsigned int max_m, max_n, max_size;
Real **me,*base; /* base is base of alloc'd mem */
} MAT;
/* band matrix definition */
typedef struct {
MAT *mat; /* matrix */
int lb,ub; /* lower and upper bandwidth */
} BAND;
/* permutation definition */
typedef struct {
unsigned int size, max_size, *pe;
} PERM;
/* integer vector definition */
typedef struct {
unsigned int dim, max_dim;
int *ive;
} IVEC;
#ifndef MALLOCDECL
#ifndef ANSI_C
extern char *malloc(), *calloc(), *realloc();
#else
extern void *malloc(size_t),
*calloc(size_t,size_t),
*realloc(void *,size_t);
#endif
#endif /* MALLOCDECL */
/* For creating MEX files (for use with Matlab) using Meschach
See also: mexmesch.h */
#ifdef MEX
#include "mex.h"
#define malloc(len) mxMalloc(len)
#define calloc(n,len) mxCalloc(n,len)
#define realloc(ptr,len) mxRealloc(ptr,len)
#define free(ptr) mxFree(ptr)
#define printf mexPrintf
#ifndef THREADSAFE /* for use as a shared library */
#define THREADSAFE 1
#endif
#endif /* MEX */
#ifdef THREADSAFE
#define STATIC
#else
#define STATIC static
#endif /* THREADSAFE */
#ifndef ANSI_C
extern void m_version();
#else
void m_version( void );
#endif
#ifndef ANSI_C
/* allocate one object of given type */
#define NEW(type) ((type *)calloc((size_t)1,sizeof(type)))
/* allocate num objects of given type */
#define NEW_A(num,type) ((type *)calloc((size_t)(num),sizeof(type)))
/* re-allocate arry to have num objects of the given type */
#define RENEW(var,num,type) \
((var)=(type *)((var) ? \
realloc((char *)(var),(size_t)(num)*sizeof(type)) : \
calloc((size_t)(num),sizeof(type))))
#define MEMCOPY(from,to,n_items,type) \
MEM_COPY((char *)(from),(char *)(to),(size_t)(n_items)*sizeof(type))
#else
/* allocate one object of given type */
#define NEW(type) ((type *)calloc((size_t)1,(size_t)sizeof(type)))
/* allocate num objects of given type */
#define NEW_A(num,type) ((type *)calloc((size_t)(num),(size_t)sizeof(type)))
/* re-allocate arry to have num objects of the given type */
#define RENEW(var,num,type) \
((var)=(type *)((var) ? \
realloc((char *)(var),(size_t)((num)*sizeof(type))) : \
calloc((size_t)(num),(size_t)sizeof(type))))
#define MEMCOPY(from,to,n_items,type) \
MEM_COPY((char *)(from),(char *)(to),(unsigned)(n_items)*sizeof(type))
#endif /* ANSI_C */
/* type independent min and max operations */
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif /* max */
#ifndef min
#define min(a,b) ((a) > (b) ? (b) : (a))
#endif /* min */
#undef TRUE
#define TRUE 1
#undef FALSE
#define FALSE 0
/* for input routines */
#define MAXLINE 81
/* Dynamic memory allocation */
/* Should use M_FREE/V_FREE/PX_FREE in programs instead of m/v/px_free()
as this is considerably safer -- also provides a simple type check ! */
#ifndef ANSI_C
extern VEC *v_get(), *v_resize();
extern MAT *m_get(), *m_resize();
extern PERM *px_get(), *px_resize();
extern IVEC *iv_get(), *iv_resize();
extern int m_free(),v_free();
extern int px_free();
extern int iv_free();
extern BAND *bd_get(), *bd_resize();
extern int bd_free();
#else
/* get/resize vector to given dimension */
extern VEC *v_get(int), *v_resize(VEC *,int);
/* get/resize matrix to be m x n */
extern MAT *m_get(int,int), *m_resize(MAT *,int,int);
/* get/resize permutation to have the given size */
extern PERM *px_get(int), *px_resize(PERM *,int);
/* get/resize an integer vector to given dimension */
extern IVEC *iv_get(int), *iv_resize(IVEC *,int);
/* get/resize a band matrix to given dimension */
extern BAND *bd_get(int,int,int), *bd_resize(BAND *,int,int,int);
/* free (de-allocate) (band) matrices, vectors, permutations and
integer vectors */
extern int iv_free(IVEC *);
extern m_free(MAT *),v_free(VEC *),px_free(PERM *);
extern int bd_free(BAND *);
#endif /* ANSI_C */
/* MACROS */
/* macros that also check types and sets pointers to NULL */
#define M_FREE(mat) ( m_free(mat), (mat)=(MAT *)NULL )
#define V_FREE(vec) ( v_free(vec), (vec)=(VEC *)NULL )
#define PX_FREE(px) ( px_free(px), (px)=(PERM *)NULL )
#define IV_FREE(iv) ( iv_free(iv), (iv)=(IVEC *)NULL )
#define MAXDIM 10000001
/* Entry level access to data structures */
/* routines to check indexes */
#define m_chk_idx(A,i,j) ((i)>=0 && (i)<(A)->m && (j)>=0 && (j)<=(A)->n)
#define v_chk_idx(x,i) ((i)>=0 && (i)<(x)->dim)
#define bd_chk_idx(A,i,j) ((i)>=max(0,(j)-(A)->ub) && \
(j)>=max(0,(i)-(A)->lb) && (i)<(A)->mat->n && (j)<(A)->mat->n)
#define m_entry(A,i,j) m_get_val(A,i,j)
#define v_entry(x,i) v_get_val(x,i)
#define bd_entry(A,i,j) bd_get_val(A,i,j)
#ifdef DEBUG
#define m_set_val(A,i,j,val) ( m_chk_idx(A,i,j) ? \
(A)->me[(i)][(j)] = (val) : (error(E_BOUNDS,"m_set_val"), 0.0))
#define m_add_val(A,i,j,val) ( m_chk_idx(A,i,j) ? \
(A)->me[(i)][(j)] += (val) : (error(E_BOUNDS,"m_add_val"), 0.0))
#define m_sub_val(A,i,j,val) ( m_chk_idx(A,i,j) ? \
(A)->me[(i)][(j)] -= (val) : (error(E_BOUNDS,"m_sub_val"), 0.0))
#define m_get_val(A,i,j) ( m_chk_idx(A,i,j) ? \
(A)->me[(i)][(j)] : (error(E_BOUNDS,"m_get_val"), 0.0))
#define v_set_val(x,i,val) ( v_chk_idx(x,i) ? (x)->ve[(i)] = (val) : \
(error(E_BOUNDS,"v_set_val"), 0.0))
#define v_add_val(x,i,val) ( v_chk_idx(x,i) ? (x)->ve[(i)] += (val) : \
(error(E_BOUNDS,"v_set_val"), 0.0))
#define v_sub_val(x,i,val) ( v_chk_idx(x,i) ? (x)->ve[(i)] -= (val) : \
(error(E_BOUNDS,"v_set_val"), 0.0))
#define v_get_val(x,i) ( v_chk_idx(x,i) ? (x)->ve[(i)] : \
(error(E_BOUNDS,"v_get_val"), 0.0))
#define bd_set_val(A,i,j,val) ( bd_chk_idx(A,i,j) ? \
(A)->mat->me[(A)->lb+(j)-(i)][(j)] = (val) : \
(error(E_BOUNDS,"bd_set_val"), 0.0))
#define bd_add_val(A,i,j,val) ( bd_chk_idx(A,i,j) ? \
(A)->mat->me[(A)->lb+(j)-(i)][(j)] += (val) : \
(error(E_BOUNDS,"bd_set_val"), 0.0))
#define bd_get_val(A,i,j) ( bd_chk_idx(A,i,j) ? \
(A)->mat->me[(A)->lb+(j)-(i)][(j)] : \
(error(E_BOUNDS,"bd_get_val"), 0.0))
#else /* no DEBUG */
#define m_set_val(A,i,j,val) ((A)->me[(i)][(j)] = (val))
#define m_add_val(A,i,j,val) ((A)->me[(i)][(j)] += (val))
#define m_sub_val(A,i,j,val) ((A)->me[(i)][(j)] -= (val))
#define m_get_val(A,i,j) ((A)->me[(i)][(j)])
#define v_set_val(x,i,val) ((x)->ve[(i)] = (val))
#define v_add_val(x,i,val) ((x)->ve[(i)] += (val))
#define v_sub_val(x,i,val) ((x)->ve[(i)] -= (val))
#define v_get_val(x,i) ((x)->ve[(i)])
#define bd_set_val(A,i,j,val) ((A)->mat->me[(A)->lb+(j)-(i)][(j)] = (val))
#define bd_add_val(A,i,j,val) ((A)->mat->me[(A)->lb+(j)-(i)][(j)] += (val))
#define bd_get_val(A,i,j) ((A)->mat->me[(A)->lb+(j)-(i)][(j)])
#endif /* DEBUG */
/* I/O routines */
#ifndef ANSI_C
extern void v_foutput(),m_foutput(),px_foutput();
extern void iv_foutput();
extern VEC *v_finput();
extern MAT *m_finput();
extern PERM *px_finput();
extern IVEC *iv_finput();
extern int fy_or_n(), fin_int(), yn_dflt(), skipjunk();
extern double fin_double();
#else
/* print x on file fp */
void v_foutput(FILE *fp,const VEC *x),
/* print A on file fp */
m_foutput(FILE *fp,const MAT *A),
/* print px on file fp */
px_foutput(FILE *fp,const PERM *px);
/* print ix on file fp */
void iv_foutput(FILE *fp,const IVEC *ix);
/* Note: if out is NULL, then returned object is newly allocated;
Also: if out is not NULL, then that size is assumed */
/* read in vector from fp */
VEC *v_finput(FILE *fp,VEC *out);
/* read in matrix from fp */
MAT *m_finput(FILE *fp,MAT *out);
/* read in permutation from fp */
PERM *px_finput(FILE *fp,PERM *out);
/* read in int vector from fp */
IVEC *iv_finput(FILE *fp,IVEC *out);
/* fy_or_n -- yes-or-no to question in string s
-- question written to stderr, input from fp
-- if fp is NOT a tty then return y_n_dflt */
int fy_or_n(FILE *fp, const char *s);
/* yn_dflt -- sets the value of y_n_dflt to val */
int yn_dflt(int val);
/* fin_int -- return integer read from file/stream fp
-- prompt s on stderr if fp is a tty
-- check that x lies between low and high: re-prompt if
fp is a tty, error exit otherwise
-- ignore check if low > high */
int fin_int(FILE *fp,const char *s,int low,int high);
/* fin_double -- return double read from file/stream fp
-- prompt s on stderr if fp is a tty
-- check that x lies between low and high: re-prompt if
fp is a tty, error exit otherwise
-- ignore check if low > high */
double fin_double(FILE *fp,const char *s,double low,double high);
/* it skips white spaces and strings of the form #....\n
Here .... is a comment string */
int skipjunk(FILE *fp);
#endif /* ANSI_C */
/* MACROS */
/* macros to use stdout and stdin instead of explicit fp */
#define v_output(vec) v_foutput(stdout,vec)
#define v_input(vec) v_finput(stdin,vec)
#define m_output(mat) m_foutput(stdout,mat)
#define m_input(mat) m_finput(stdin,mat)
#define px_output(px) px_foutput(stdout,px)
#define px_input(px) px_finput(stdin,px)
#define iv_output(iv) iv_foutput(stdout,iv)
#define iv_input(iv) iv_finput(stdin,iv)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -