ZMAT *zm_get(int m, int n) #endif { ZMAT *matrix; unsigned int i; if (m < 0 || n < 0) error(E_NEG, "zm_get"); if ((matrix = NEW(ZMAT)) == (ZMAT *) NULL) error(E_MEM, "zm_get"); else if (mem_info_is_on()) { mem_bytes(TYPE_ZMAT, 0, sizeof(ZMAT)); mem_numvar(TYPE_ZMAT, 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, complex)) == (complex *) NULL) { free(matrix); error(E_MEM, "zm_get"); } else if (mem_info_is_on()) { mem_bytes(TYPE_ZMAT, 0, m * n * sizeof(complex)); } #else matrix->base = (complex *)NULL; #endif if ((matrix->me = (complex **) calloc(m, sizeof(complex *))) == (complex **) NULL) { free(matrix->base); free(matrix); error(E_MEM, "zm_get"); } else if (mem_info_is_on()) { mem_bytes(TYPE_ZMAT, 0, m * sizeof(complex *)); } #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,complex)) == (complex *)NULL ) error(E_MEM,"zm_get"); else if (mem_info_is_on()) { mem_bytes(TYPE_ZMAT,0,n*sizeof(complex)); } #endif return (matrix); }
/* px_get -- gets a PERM of given 'size' by dynamic memory allocation -- Note: initialized to the identity permutation */ extern PERM *px_get(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 */ extern VEC *v_get(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); }
ZVEC *zv_get(int size) #endif { ZVEC *vector; if (size < 0) error(E_NEG,"zv_get"); if ((vector=NEW(ZVEC)) == (ZVEC *)NULL ) error(E_MEM,"zv_get"); else if (mem_info_is_on()) { mem_bytes(TYPE_ZVEC,0,sizeof(ZVEC)); mem_numvar(TYPE_ZVEC,1); } vector->dim = vector->max_dim = size; if ((vector->ve=NEW_A(size,complex)) == (complex *)NULL ) { free(vector); error(E_MEM,"zv_get"); } else if (mem_info_is_on()) { mem_bytes(TYPE_ZVEC,0,size*sizeof(complex)); } return (vector); }
/* m_get -- gets an mxn matrix (in MAT form) by dynamic memory allocation -- normally ALL matrices should be obtained this way -- if either m or n is negative this will raise an error -- note that 0 x n and m x 0 matrices can be created */ MAT *m_get(int m, int n) { MAT *matrix; int i; if (m < 0 || n < 0) {return NULL;} //error(E_NEG,"m_get"); if ((matrix=NEW(MAT)) == (MAT *)NULL ){return NULL;} //error(E_MEM,"m_get"); matrix->m = m; matrix->n = matrix->max_n = n; matrix->max_m = m; matrix->max_size = m*n; matrix->base = (Real *)NULL; if ((matrix->me = (Real **)calloc(m,sizeof(Real *))) == (Real **)NULL ) { free(matrix->base); free(matrix); return NULL; //error(E_MEM,"m_get"); } for ( i = 0; i < m; i++ ) { if ( (matrix->me[i]=NEW_A(n,Real)) == (Real *)NULL ){return NULL;} //error(E_MEM,"m_get"); } return (matrix); }
/* v_get -- gets a VEC of dimension 'size' -- Note: initialized to zero */ VEC *v_get(int size) { VEC *vector; if (size < 0) {return NULL;}//error(E_NEG,"v_get"); if ((vector=NEW(VEC)) == (VEC *)NULL ) {return NULL;}//error(E_MEM,"v_get"); vector->dim = vector->max_dim = size; if ((vector->ve=NEW_A(size,Real)) == (Real *)NULL ) { free(vector); return NULL; //error(E_MEM,"v_get"); } return (vector); }
/* m_resize -- returns the matrix A of size new_m x new_n; A is zeroed -- if A == NULL on entry then the effect is equivalent to m_get() */ extern MAT *m_resize(MAT *A, int new_m, int new_n) { int i; int new_max_m, new_max_n, new_size, old_m, old_n; if (new_m < 0 || new_n < 0) error(E_NEG,"m_resize"); if ( ! A ) return m_get(new_m,new_n); /* nothing was changed */ if (new_m == A->m && new_n == A->n) return A; old_m = A->m; old_n = A->n; if ( new_m > A->max_m ) { /* re-allocate A->me */ if (mem_info_is_on()) { mem_bytes(TYPE_MAT,A->max_m*sizeof(Real *), new_m*sizeof(Real *)); } A->me = RENEW(A->me,new_m,Real *); if ( ! A->me ) error(E_MEM,"m_resize"); } new_max_m = max(new_m,A->max_m); new_max_n = max(new_n,A->max_n); #ifndef SEGMENTED new_size = new_max_m*new_max_n; if ( new_size > A->max_size ) { /* re-allocate A->base */ if (mem_info_is_on()) { mem_bytes(TYPE_MAT,A->max_m*A->max_n*sizeof(Real), new_size*sizeof(Real)); } A->base = RENEW(A->base,new_size,Real); if ( ! A->base ) error(E_MEM,"m_resize"); A->max_size = new_size; } /* now set up A->me[i] */ for ( i = 0; i < new_m; i++ ) A->me[i] = &(A->base[i*new_n]); /* now shift data in matrix */ if ( old_n > new_n ) { for ( i = 1; i < min(old_m,new_m); i++ ) MEM_COPY((char *)&(A->base[i*old_n]), (char *)&(A->base[i*new_n]), sizeof(Real)*new_n); } else if ( old_n < new_n ) { for ( i = (int)(min(old_m,new_m))-1; i > 0; i-- ) { /* copy & then zero extra space */ MEM_COPY((char *)&(A->base[i*old_n]), (char *)&(A->base[i*new_n]), sizeof(Real)*old_n); __zero__(&(A->base[i*new_n+old_n]),(new_n-old_n)); } __zero__(&(A->base[old_n]),(new_n-old_n)); A->max_n = new_n; } /* zero out the new rows.. */ for ( i = old_m; i < new_m; i++ ) __zero__(&(A->base[i*new_n]),new_n); #else if ( A->max_n < new_n ) { Real *tmp; for ( i = 0; i < A->max_m; i++ ) { if (mem_info_is_on()) { mem_bytes(TYPE_MAT,A->max_n*sizeof(Real), new_max_n*sizeof(Real)); } if ( (tmp = RENEW(A->me[i],new_max_n,Real)) == NULL ) error(E_MEM,"m_resize"); else { A->me[i] = tmp; } } for ( i = A->max_m; i < new_max_m; i++ ) { if ( (tmp = NEW_A(new_max_n,Real)) == NULL ) error(E_MEM,"m_resize"); else { A->me[i] = tmp; if (mem_info_is_on()) { mem_bytes(TYPE_MAT,0,new_max_n*sizeof(Real)); } } } } else if ( A->max_m < new_m ) { for ( i = A->max_m; i < new_m; i++ ) if ( (A->me[i] = NEW_A(new_max_n,Real)) == NULL ) error(E_MEM,"m_resize"); else if (mem_info_is_on()) { mem_bytes(TYPE_MAT,0,new_max_n*sizeof(Real)); } } if ( old_n < new_n ) { for ( i = 0; i < old_m; i++ ) __zero__(&(A->me[i][old_n]),new_n-old_n); } /* zero out the new rows.. */ for ( i = old_m; i < new_m; i++ ) __zero__(A->me[i],new_n); #endif A->max_m = new_max_m; A->max_n = new_max_n; A->max_size = A->max_m*A->max_n; A->m = new_m; A->n = new_n; return A; }