Annotation of OpenXM/src/Ti/matrices.c, Revision 1.1
1.1 ! maekawa 1: /*
! 2: ** matrices.c 4/99
! 3: **
! 4: ** Definition and Minipulation of integer and double matrices and vectors.
! 5: ** vectors are simply C-vectors (with indices starting at zero) and matrices
! 6: ** are stored Numerical Recepies style -- as a vector of pointers to the
! 7: ** rows of the matrix [which are themselves just regular C vectors].
! 8: **
! 9: ** TiGERS, Toric Groebner Basis Enumeration by Reverse Search
! 10: ** copyright (c) 1999 Birk Huber
! 11: **
! 12: */
! 13: #include<stdio.h>
! 14: #include<stdlib.h>
! 15: #include "utils.h"
! 16: #include "matrices.h"
! 17:
! 18: /*
! 19: ** Integer Matrices:
! 20: ** new_imatrix(int r, int c) -- reserve storage for a rxc matrix of integers
! 21: ** free_imatrix(*M) -- free storage allocated with new_imatrix()
! 22: **
! 23: ** integer matrix is given by the integers r,c and a vector M of rows
! 24: **
! 25: ** example r=4, c=3
! 26: ** _____________________________
! 27: ** M= | row0 | row1 | row 2| row 3 | pointers to rows
! 28: ** ---|--------|-------|---- --|
! 29: ** | | \ \
! 30: ** _____________________________________
! 31: ** |e0|e1|e2|e3|e4|e5|e6|e7|e8|e9|e10|e11| vector of elements
! 32: ** -------------------------------------
! 33: **
! 34: */
! 35: int **new_imatrix(int r, int c){
! 36: int **tmp=0;
! 37: int i,m=r,n=c;
! 38: /* get space for pointers to rows*/
! 39: if ((tmp=(int **)malloc(m*sizeof(int *)))==0){
! 40: fprintf(stderr,"memory failure 1 in new_imatrix()\n");
! 41: abort();
! 42: }
! 43: /* get space for all entrees -- store in pointer to first row*/
! 44: if ((tmp[0]=(int *)malloc(n*m*sizeof(int)))==0){
! 45: fprintf(stderr,"memory failure 2 in new_imatrix()\n");
! 46: free((void *)tmp);
! 47: abort();
! 48: }
! 49: /* set remaining row pointers into entry space*/
! 50: for(i=1;i<m;i++) tmp[i]=tmp[i-1]+n;
! 51: return tmp;
! 52: }
! 53:
! 54:
! 55: void free_imatrix(int **M){
! 56: if (M!=0){
! 57: if (M[0]!=0) free((void *)M[0]);
! 58: free((void *)M);
! 59: }
! 60: }
! 61:
! 62: /*
! 63: ** Integer Matrices: IO routines
! 64: **
! 65: ** imatrix_read(FILE *is, int *m, int *n)
! 66: ** -- read in description of imatrix from is, create matrix and fill it.
! 67: ** -- format: { m n : entry_1 .... entry_mn } where m=number of rows
! 68: ** n=number of columns
! 69: ** e.g.
! 70: ** { 2 4 : 1 0 3 5 0 1 8 9 } describes the matrix [ 1, 0, 3, 5]
! 71: ** [ 0, 1, 8, 9]
! 72: ** print_imatrix(FILE *of, char *prefix,int **M, int m, int n)
! 73: ** -- copy prefex string to output file of, then write ascii representation
! 74: ** of matrix with m-rows, n-cols and entrees in M to of.
! 75: **
! 76: */
! 77: int **imatrix_read(FILE *is,int *m, int *n){
! 78: char c;
! 79: int i,j;
! 80: int **M;
! 81:
! 82: /* find openning brace */
! 83: eatwhite(is);
! 84: c=getc(is);
! 85: if (c!='{'){
! 86: fprintf(stderr,"ERROR: expecting '{' in imatrix_read()\n");
! 87: return 0;
! 88: }
! 89: /* read in matrix dimensions and initialize matrix */
! 90: fscanf(is," %d %d :",m,n);
! 91: M=new_imatrix(*m,*n);
! 92:
! 93: /* read in matrix entrees */
! 94: for(i=0;i<*m;i++){
! 95: for(j=0;j<*n;j++){
! 96: fscanf(is," %d",&(IMref(M,i,j)));
! 97: }
! 98: }
! 99:
! 100: /* find closing brace */
! 101: eatwhite(is);
! 102: c=getc(is);
! 103: if (c!='}'){
! 104: fprintf(stderr,"ERROR: expecting '}' in imatrix_read()\n");
! 105: return 0;
! 106: }
! 107:
! 108: return M;
! 109: }
! 110:
! 111: void print_imatrix(FILE *of, char *prefix,int **M, int m, int n){
! 112: int i,j;
! 113: fprintf(of,"{ %d %d:\n",m,n);
! 114: for(i=0;i<m;i++){
! 115: fprintf(of,"%s %d",prefix,IMref(M,i,0));
! 116: for(j=1;j<n;j++) fprintf(of,", %d",IMref(M,i,j));
! 117: fprintf(of,"\n");
! 118: }
! 119: fprintf(of,"%s}\n",prefix);
! 120: }
! 121:
! 122: /*
! 123: ** Integer Vectors:
! 124: ** new_ivector(int n) -- reserve storage for a c-vector of n integers
! 125: ** free_ivector(int *M) -- free storage allocated by new_ivector()
! 126: **
! 127: */
! 128: int *new_ivector(int n){
! 129: int *tmp=0;
! 130: tmp=(int *)malloc(n*sizeof(int));
! 131: if (tmp==0){ fprintf(stderr,"memory failure in new_ivector()\n");
! 132: abort();
! 133: }
! 134: return tmp;
! 135: }
! 136:
! 137: void free_ivector(int *M){ if (M!=0)free((void *)M);}
! 138:
! 139:
! 140: /*
! 141: ** Double Matrices
! 142: ** new_matrix(int r, int c) -- reserve storage for a rxc matrix of doubles
! 143: ** free_matrix(*M) -- free storage allocated with new_matrix()
! 144: **
! 145: ** (aside from the data types of the entries -- double matrices are exactly
! 146: ** as described above for integer matrices)
! 147: **
! 148: */
! 149: double **new_matrix(int r, int c){
! 150: double **tmp=0;
! 151: int i,m=c,n=r;
! 152: if ((tmp=(double **)malloc(m*sizeof(double *)))==0){
! 153: fprintf(stderr,"memory failure 1 in new_matrix()\n");
! 154: abort();
! 155: }
! 156: if ((tmp[0]=(double *)malloc(n*m*sizeof(double)))==0){
! 157: fprintf(stderr,"memory failure 2 in new_matrix()\n");
! 158: free((void *)tmp);
! 159: abort();
! 160: }
! 161: for(i=1;i<m;i++) tmp[i]=tmp[i-1]+n;
! 162: return tmp;
! 163: }
! 164:
! 165: void free_matrix(double **M){
! 166: if (M!=0){
! 167: if (M[0]!=0) free((void *)M[0]);
! 168: free((void *)M);
! 169: }
! 170: }
! 171:
! 172: /*
! 173: ** Double Vectors:
! 174: ** new_vector(int n) -- reserve storage for a c-vector of n doubles
! 175: ** free_vector(int *M) -- free storage allocated by new_vector()
! 176: **
! 177: */
! 178: double *new_vector(int n){
! 179: double *tmp=0;
! 180: tmp=(double *)malloc(n*sizeof(double));
! 181: if (tmp==0){ fprintf(stderr,"memory failure in new_vector\n");
! 182: abort();
! 183: }
! 184: return tmp;
! 185: }
! 186:
! 187: void free_vector(double *M){ if (M!=0)free((void *)M);}
! 188:
! 189:
! 190:
! 191:
! 192:
! 193:
! 194:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>