home *** CD-ROM | disk | FTP | other *** search
- uld be 2)\n",k);
- }
-
- for ( k = 0 ; k < 20; k++ )
- {
- i = sA->m - 1 - ((rand() >> 8) % 10);
- j = sA->n - 1 - ((rand() >> 8) % 10);
- s1 = rand()/((Real)MAX_RAND);
- sp_set_val(sA,i,j,s1);
- if ( fabs(s1 - sp_get_val(sA,i,j)) >= MACHEPS )
- break;
- }
- if ( k < 20 )
- errmesg("sp_resize()");
- sp_col_access(sA);
- if ( ! chk_col_access(sA) )
- {
- errmesg("sp_col_access()");
- }
- sp_diag_access(sA);
- for ( i = 0; i < sA->m; i++ )
- {
- r = &(sA->row[i]);
- if ( r->diag != sprow_idx(r,i) )
- break;
- }
- if ( i < sA->m )
- {
- errmesg("sp_diag_access()");
- }
-
- k = sp_free_vars(&sA,&sB,NULL);
- if (k != 2)
- errmesg("sp_free_vars");
- #endif /* SPARSE */
-
-
- #ifdef COMPLEX
- /* complex stuff */
-
- ONE = zmake(1.0,0.0);
- printf("# ONE = "); z_output(ONE);
- printf("# Check: MACHEPS = %g\n",MACHEPS);
- /* allocate, initialise, copy and resize operations */
- /* ZVEC */
- notice("vector initialise, copy & resize");
- zv_get_vars(12,&zx,&zy,&zz,NULL);
-
- zv_rand(zx);
- zv_rand(zy);
- zz = zv_copy(zx,zz);
- if ( zv_norm2(zv_sub(zx,zz,zz)) >= MACHEPS )
- errmesg("ZVEC copy");
- zv_copy(zx,zy);
-
- zv_resize_vars(10,&zx,&zy,NULL);
- if ( zv_norm2(zv_sub(zx,zy,zz)) >= MACHEPS )
- errmesg("ZVEC copy/resize");
-
- zv_resize_vars(20,&zx,&zy,NULL);
- if ( zv_norm2(zv_sub(zx,zy,zz)) >= MACHEPS )
- errmesg("VZEC resize");
- zv_free_vars(&zx,&zy,&zz,NULL);
-
-
- /* ZMAT */
- notice("matrix initialise, copy & resize");
- zm_get_vars(8,5,&zA,&zB,&zC,NULL);
-
- zm_rand(zA);
- zm_rand(zB);
- zC = zm_copy(zA,zC);
- if ( zm_norm_inf(zm_sub(zA,zC,zC)) >= MACHEPS )
- errmesg("ZMAT copy");
-
- zm_copy(zA,zB);
- zm_resize_vars(3,5,&zA,&zB,&zC,NULL);
-
- if ( zm_norm_inf(zm_sub(zA,zB,zC)) >= MACHEPS )
- errmesg("ZMAT copy/resize");
- zm_resize_vars(20,20,&zA,&zB,&zC,NULL);
-
- if ( zm_norm_inf(zm_sub(zA,zB,zC)) >= MACHEPS )
- errmesg("ZMAT resize");
-
- zm_free_vars(&zA,&zB,&zC,NULL);
- #endif /* COMPLEX */
-
- #endif /* if defined(ANSI_C) || defined(VARARGS) */
-
- printf("# test of mem_info_bytes and mem_info_numvar\n");
- printf(" TYPE VEC: %ld bytes allocated, %d variables allocated\n",
- mem_info_bytes(TYPE_VEC,0),mem_info_numvar(TYPE_VEC,0));
-
- notice("static memory test");
- mem_info_on(TRUE);
- mem_stat_mark(1);
- for (i=0; i < 100; i++)
- stat_test1(i);
- mem_stat_free(1);
-
- mem_stat_mark(1);
- for (i=0; i < 100; i++) {
- stat_test1(i);
- #ifdef COMPLEX
- stat_test4(i);
- #endif
- }
-
- mem_stat_mark(2);
- for (i=0; i < 100; i++)
- stat_test2(i);
-
- mem_stat_mark(3);
- #ifdef SPARSE
- for (i=0; i < 100; i++)
- stat_test3(i);
- #endif
-
- mem_info();
- mem_dump_list(stdout,0);
-
- mem_stat_free(1);
- mem_stat_free(3);
- mem_stat_mark(4);
-
- for (i=0; i < 100; i++) {
- stat_test1(i);
- #ifdef COMPLEX
- stat_test4(i);
- #endif
- }
-
- mem_stat_dump(stdout,0);
- if (mem_stat_show_mark() != 4) {
- errmesg("not 4 in mem_stat_show_mark()");
- }
-
- mem_stat_free(2);
- mem_stat_free(4);
-
- if (mem_stat_show_mark() != 0) {
- errmesg("not 0 in mem_stat_show_mark()");
- }
-
- /* add new list of types */
-
- mem_attach_list(FOO_LIST,FOO_NUM_TYPES,foo_type_name,
- foo_free_func,foo_info_sum);
- if (!mem_is_list_attached(FOO_LIST))
- errmesg("list FOO_LIST is not attached");
-
- mem_dump_list(stdout,FOO_LIST);
- foo_1 = foo_1_get(6);
- foo_2 = foo_2_get(3);
- for (i=0; i < foo_1->dim; i++)
- for (j=0; j < foo_1->fix_dim; j++)
- foo_1->a[i][j] = i+j;
- for (i=0; i < foo_2->dim; i++)
- for (j=0; j < foo_2->fix_dim; j++)
- foo_2->a[i][j] = i+j;
- printf(" foo_1->a[%d][%d] = %g\n",5,9,foo_1->a[5][9]);
- printf(" foo_2->a[%d][%d] = %g\n",2,1,foo_2->a[2][1]);
-
- mem_stat_mark(5);
- mem_stat_reg_list((void **)&foo_1,TYPE_FOO_1,FOO_LIST);
- mem_stat_reg_list((void **)&foo_2,TYPE_FOO_2,FOO_LIST);
- mem_stat_dump(stdout,FOO_LIST);
- mem_info_file(stdout,FOO_LIST);
- mem_stat_free_list(5,FOO_LIST);
- mem_stat_dump(stdout,FOO_LIST);
- if ( foo_1 != NULL )
- errmesg(" foo_1 is not released");
- if ( foo_2 != NULL )
- errmesg(" foo_2 is not released");
- mem_dump_list(stdout,FOO_LIST);
- mem_info_file(stdout,FOO_LIST);
-
- mem_free_vars(FOO_LIST);
- if ( mem_is_list_attached(FOO_LIST) )
- errmesg("list FOO_LIST is not detached");
-
- mem_info();
-
- #if REAL == FLOAT
- printf("# SINGLE PRECISION was used\n");
- #elif REAL == DOUBLE
- printf("# DOUBLE PRECISION was used\n");
- #endif
-
- #define ANSI_OR_VAR
-
- #ifndef ANSI_C
- #ifndef VARARGS
- #undef ANSI_OR_VAR
- #endif
- #endif
-
- #ifdef ANSI_OR_VAR
-
- printf("# you should get: \n");
- #if (REAL == FLOAT)
- printf("# type VEC: 276 bytes allocated, 3 variables allocated\n");
- #elif (REAL == DOUBLE)
- printf("# type VEC: 516 bytes allocated, 3 variables allocated\n");
- #endif
- printf("# and other types are zeros\n");
-
- #endif /*#if defined(ANSI_C) || defined(VARAGS) */
-
- printf("# Finished memory torture test\n");
- return;
- }
- FileDataŵmfunc :# Eÿÿÿüß? ^
- /**************************************************************************
- **
- ** 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.
- **
- ***************************************************************************/
-
-
- /*
- This file contains routines for computing functions of matrices
- especially polynomials and exponential functions
- Copyright (C) Teresa Leyk and David Stewart, 1993
- */
-
- #include <stdio.h>
- #include <math.h>
- #include "matrix.h"
- #include "matrix2.h"
-
- static char rcsid[] = "$Id: mfunc.c,v 1.1 1994/01/13 05:33:58 des Exp $";
-
-
-
- /* _m_pow -- computes integer powers of a square matrix A, A^p
- -- uses tmp as temporary workspace */
- MAT *_m_pow(A, p, tmp, out)
- MAT *A, *tmp, *out;
- int p;
- {
- int it_cnt, k, max_bit;
-
- /*
- File containing routines for evaluating matrix functions
- esp. the exponential function
- */
-
- #define Z(k) (((k) & 1) ? tmp : out)
-
- if ( ! A )
- error(E_NULL,"_m_pow");
- if ( A->m != A->n )
- error(E_SQUARE,"_m_pow");
- if ( p < 0 )
- error(E_NEG,"_m_pow");
- out = m_resize(out,A->m,A->n);
- tmp = m_resize(tmp,A->m,A->n);
-
- if ( p == 0 )
- m_ident(out);
- else if ( p > 0 )
- {
- it_cnt = 1;
- for ( max_bit = 0; ; max_bit++ )
- if ( (p >> (max_bit+1)) == 0 )
- break;
- tmp = m_copy(A,tmp);
-
- for ( k = 0; k < max_bit; k++ )
- {
- m_mlt(Z(it_cnt),Z(it_cnt),Z(it_cnt+1));
- it_cnt++;
- if ( p & (1 << (max_bit-1)) )
- {
- m_mlt(A,Z(it_cnt),Z(it_cnt+1));
- /* m_copy(Z(it_cnt),out); */
- it_cnt++;
- }
- p <<= 1;
- }
- if (it_cnt & 1)
- out = m_copy(Z(it_cnt),out);
- }
-
- return out;
-
- #undef Z
- }
-
- /* m_pow -- computes integer powers of a square matrix A, A^p */
- MAT *m_pow(A, p, out)
- MAT *A, *out;
- int p;
- {
- static MAT *wkspace = MNULL;
-
- if ( ! A )
- error(E_NULL,"m_pow");
- if ( A->m != A->n )
- error(E_SQUARE,"m_pow");
-
- wkspace = m_resize(wkspace,A->m,A->n);
- MEM_STAT_REG(wkspace,TYPE_MAT);
-
- return _m_pow(A, p, wkspace, out);
-
- }
-
- /**************************************************/
-
- /* _m_exp -- compute matrix exponential of A and save it in out
- -- uses Pade approximation followed by repeated squaring
- -- eps is the tolerance used for the Pnteractive input of vector */
- VEC *ifin_vec(fp,vec)
- FILE *fp;
- VEC *vec;
- {
- u_int i,dim,dynamic; /* dynamic set if memory allocated here */
-
- /* get vector dimension */
- if ( vec != (VEC *)NULL && vec->dim<MAXDIM )
- { dim = vec->dim; dynamic = FALSE; }
- else
- {
- dynamic = TRUE;
- do
- {
- fprintf(stderr,"Vector: dim: ");
- if ( fgets(line,MAXLINE,fp)==NULL )
- error(E_INPUT,"ifin_vec");
- } while ( sscanf(line,"%u",&dim)<1 || dim>MAXDIM );
- vec = v_get(dim);
- }
-
- /* input elements */
- for ( i=0; i<dim; i++ )
- do
- {
- redo:
- fprintf(stderr,"entry %u: ",i);
- if ( !dynamic )
- fprintf(stderr,"old %14.9g new: ",vec->ve[i]);
- if ( fgets(line,MAXLINE,fp)==NULL )
- error(E_INPUT,"ifin_vec");
- if ( (*line == 'b' || *line == 'B') && i > 0 )
- { i--; dynamic = FALSE; goto redo; }
- if ( (*line == 'f' || *line == 'F') && i < dim-1 )
- { i++; dynamic = FALSE; goto redo; }
- #if REAL == DOUBLE
- } while ( *line=='\0' || sscanf(line,"%lf",&vec->ve[i]) < 1 );
- #elif REAL == FLOAT
- } while ( *line=='\0' || sscanf(line,"%f",&vec->ve[i]) < 1 );
- #endif
-
- return (vec);
- }
-
- /* bfin_vec -- batch-file input of vector */
- VEC *bfin_vec(fp,vec)
- FILE *fp;
- VEC *vec;
- {
- u_int i,dim;
- int io_code;
-
- /* get dimension */
- skipjunk(fp);
- if ((io_code=fscanf(fp," Vector: dim:%u",&dim)) < 1 ||
- dim>MAXDIM )
- error(io_code==EOF ? 7 : 6,"bfin_vec");
-
- /* allocate memory if necessary */
- if ( vec==(VEC *)NULL )
- vec = v_resize(vec,dim);
-
- /* get entries */
- skipjunk(fp);
- for ( i=0; i<dim; i++ )
- #if REAL == DOUBLE
- if ((io_code=fscanf(fp,"%lf",&vec->ve[i])) < 1 )
- #elif REAL == FLOAT
- if ((io_code=fscanf(fp,"%f",&vec->ve[i])) < 1 )
- #endif
- error(io_code==EOF ? 7 : 6,"bfin_vec");
-
- return (vec);
- }
-
- /**************************************************************************
- Output routines
- **************************************************************************/
- static char *format = "%14.9g ";
-
- char *setformat(f_string)
- char *f_string;
- {
- char *old_f_string;
- old_f_string = format;
- if ( f_string != (char *)NULL && *f_string != '\0' )
- format = f_string;
-
- return old_f_string;
- }
-
- void m_foutput(fp,a)
- FILE *fp;
- MAT *a;
- {
- u_int i, j, tmp;
-
- if ( a == (MAT *)NULL )
- { fprintf(fp,"Matrix: NULL\n"); return; }
- fprintf(fp,"Matrix: %d by %d\n",a->m,a->n);
- if ( a->me == (Real **)NULL )
- { fprintf(fp,"NULL\n"); return; }
- for ( i=0; i<a->m; i++ ) /* for each row... */
- {
- fprintf(fp,"row %u: ",i);
- for ( j=0, tmp=2; j<a->n; j++, tmp++ )
- { /* for each col in row... */
- fprintf(fp,format,a->me[i][j]);
- if ( ! (tmp % 5) ) putc('\n',fp);
- }
- if ( tmp % 5 != 1 ) putc('\n',fp);
- }
- }
-
- void px_foutput(fp,px)
- FILE *fp;
- PERM *px;
- {
- u_int i;
-
- if ( px == (PERM *)NULL )
- { fprintf(fp,"Permutation: NULL\n"); return; }
- fprintf(fp,"Permutation: size: %u\n",px->size);
- if ( px->pe == (u_int *)NULL )
- { fprintf(fp,"NULL\n"); return; }
- for ( i=0; i<px->size; i++ )
- if ( ! (i % 8) && i != 0 )
- fprintf(fp,"\n %u->%u ",i,px->pe[i]);
- else
- fprintf(fp,"%u->%u ",i,px->pe[i]);
- fprintf(fp,"\n");
- }
-
- void v_foutput(fp,x)
- FILE *fp;
- VECles is non-negative */
- if ( num < 0 ) {
-
- if (mlist->info_sum[type].numvar < 0)
- {
- fprintf(stderr,
- "\n WARNING !! memory info: allocated # of variables is less than 0\n");
- fprintf(stderr,"\t TYPE %s \n\n", mlist->type_names[type]);
- if ( !isatty(fileno(stdout)) ) {
- fprintf(stdout,
- "\n WARNING !! memory info: allocated # of variables is less than 0\n");
- fprintf(stdout,"\t TYPE %s \n\n", mlist->type_names[type]);
- }
- }
- }
- }
-
- FileDataŵmemory ÎM Eýÿÿ.ÿô UŒ
- /**************************************************************************
- **
- ** 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.
- **
- ***************************************************************************/
-
-
- /* memory.c 1.3 11/25/87 */
-
- #include "matrix.h"
-
-
- static char rcsid[] = "$Id: memory.c,v 1.13 1994/04/05 02:10:37 des Exp $";
-
- /* m_get -- gets an mxn matrix (in MAT form) by dynamic memory allocation */
- MAT *m_get(m,n)
- int m,n;
- {
- MAT *matrix;
- int i;
-
- if (m < 0 || n < 0)
- error(E_NEG,"m_get");
-
- if ((matrix=NEW(MAT)) == (MAT *)NULL )
- error(E_MEM,"m_get");
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,0,sizeof(MAT));
- mem_numvar(TYPE_MAT,1);
- }
-
- matrix->m = m; matrix->n = matrix->max_n = n;
- matrix->max_m = m; matrix->max_size = m*n;
- #ifndef SEGMENTED
- if ((matrix->base = NEW_A(m*n,Real)) == (Real *)NULL )
- {
- free(matrix);
- error(E_MEM,"m_get");
- }
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,0,m*n*sizeof(Real));
- }
- #else
- matrix->base = (Real *)NULL;
- #endif
- if ((matrix->me = (Real **)calloc(m,sizeof(Real *))) ==
- (Real **)NULL )
- { free(matrix->base); free(matrix);
- error(E_MEM,"m_get");
- }
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,0,m*sizeof(Real *));
- }
-
- #ifndef SEGMENTED
- /* set up pointers */
- for ( i=0; i<m; i++ )
- matrix->me[i] = &(matrix->base[i*n]);
- #else
- for ( i = 0; i < m; i++ )
- if ( (matrix->me[i]=NEW_A(n,Real)) == (Real *)NULL )
- error(E_MEM,"m_get");
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,0,n*sizeof(Real));
- }
- #endif
-
- return (matrix);
- }
-
-
- /* px_get -- gets a PERM of given 'size' by dynamic memory allocation
- -- Note: initialized to the identity permutation */
- PERM *px_get(size)
- int size;
- {
- PERM *permute;
- int i;
-
- if (size < 0)
- error(E_NEG,"px_get");
-
- if ((permute=NEW(PERM)) == (PERM *)NULL )
- error(E_MEM,"px_get");
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_PERM,0,sizeof(PERM));
- mem_numvar(TYPE_PERM,1);
- }
-
- permute->size = permute->max_size = size;
- if ((permute->pe = NEW_A(size,u_int)) == (u_int *)NULL )
- error(E_MEM,"px_get");
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_PERM,0,size*sizeof(u_int));
- }
-
- for ( i=0; i<size; i++ )
- permute->pe[i] = i;
-
- return (permute);
- }
-
- /* v_get -- gets a VEC of dimension 'dim'
- -- Note: initialized to zero */
- VEC *v_get(size)
- int size;
- {
- VEC *vector;
-
- if (size < 0)
- error(E_NEG,"v_get");
-
- if ((vector=NEW(VEC)) == (VEC *)NULL )
- error(E_MEM,"v_get");
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_VEC,0,sizeof(VEC));
- mem_numvar(TYPE_VEC,1);
- }
-
- vector->dim = vector->max_dim = size;
- if ((vector->ve=NEW_A(size,Real)) == (Real *)NULL )
- {
- free(vector);
- error(E_MEM,"v_get");
- }
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_VEC,0,size*sizeof(Real));
- }
-
- return (vector);
- }
-
- /* m_free -- returns MAT & asoociated memory back to memory heap */
- int m_free(mat)
- MAT *mat;
- {
- #ifdef SEGMENTED
- int i;
- #endif
-
- if ( mat==(MAT *)NULL || (int)(mat->m) < 0 ||
- (int)(mat->n) < 0 )
- /* don't trust it */
- return (-1);
-
- #ifndef SEGMENTED
- if ( mat->base != (Real *)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,mat->max_m*mat->max_n*sizeof(Real),0);
- }
- free((char *)(mat->base));
- }
- #else
- for ( i = 0; i < mat->max_m; i++ )
- if ( mat->me[i] != (Real *)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,mat->max_n*sizeof(Real),0);
- }
- free((char *)(mat->me[i]));
- }
- #endif
- if ( mat->me != (Real **)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,mat->max_m*sizeof(Real *),0);
- }
- free((char *)(mat->me));
- }
-
- if (mem_info_is_on()) {
- mem_bytes(TYPE_MAT,sizeof(MAT),0);
- mem_numvar(TYPE_MAT,-1);
- }
- free((char *)mat);
-
- return (0);
- }
-
-
-
- /* px_free -- returns PERM & asoociated memory back to memory heap */
- int px_free(px)
- PERM *px;
- {
- if ( px==(PERM *)NULL || (int)(px->siz