home *** CD-ROM | disk | FTP | other *** search
- TED
- int i;
- #endif
-
- if ( mat==(ZMAT *)NULL || (int)(mat->m) < 0 ||
- (int)(mat->n) < 0 )
- /* don't trust it */
- return (-1);
-
- #ifndef SEGMENTED
- if ( mat->base != (complex *)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,mat->max_m*mat->max_n*sizeof(complex),0);
- }
- free((char *)(mat->base));
- }
- #else
- for ( i = 0; i < mat->max_m; i++ )
- if ( mat->me[i] != (complex *)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,mat->max_n*sizeof(complex),0);
- }
- free((char *)(mat->me[i]));
- }
- #endif
- if ( mat->me != (complex **)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,mat->max_m*sizeof(complex *),0);
- }
- free((char *)(mat->me));
- }
-
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,sizeof(ZMAT),0);
- mem_numvar(TYPE_ZMAT,-1);
- }
- free((char *)mat);
-
- return (0);
- }
-
-
- /* zv_free -- returns ZVEC & asoociated memory back to memory heap */
- int zv_free(vec)
- ZVEC *vec;
- {
- if ( vec==(ZVEC *)NULL || (int)(vec->dim) < 0 )
- /* don't trust it */
- return (-1);
-
- if ( vec->ve == (complex *)NULL ) {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZVEC,sizeof(ZVEC),0);
- mem_numvar(TYPE_ZVEC,-1);
- }
- free((char *)vec);
- }
- else
- {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZVEC,vec->max_dim*sizeof(complex)+
- sizeof(ZVEC),0);
- mem_numvar(TYPE_ZVEC,-1);
- }
-
- free((char *)vec->ve);
- free((char *)vec);
- }
-
- return (0);
- }
-
-
- /* zm_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() */
- ZMAT *zm_resize(A,new_m,new_n)
- ZMAT *A;
- int new_m, new_n;
- {
- u_int i, new_max_m, new_max_n, new_size, old_m, old_n;
-
- if (new_m < 0 || new_n < 0)
- error(E_NEG,"zm_resize");
-
- if ( ! A )
- return zm_get(new_m,new_n);
-
- 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_ZMAT,A->max_m*sizeof(complex *),
- new_m*sizeof(complex *));
- }
-
- A->me = RENEW(A->me,new_m,complex *);
- if ( ! A->me )
- error(E_MEM,"zm_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_ZMAT,A->max_m*A->max_n*sizeof(complex),
- new_size*sizeof(complex));
- }
-
- A->base = RENEW(A->base,new_size,complex);
- if ( ! A->base )
- error(E_MEM,"zm_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(complex)*new_n);
- }
- else if ( old_n < new_n )
- {
- for ( i = 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(complex)*old_n);
- __zzero__(&(A->base[i*new_n+old_n]),(new_n-old_n));
- }
- __zzero__(&(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++ )
- __zzero__(&(A->base[i*new_n]),new_n);
- #else
- if ( A->max_n < new_n )
- {
- complex *tmp;
-
- for ( i = 0; i < A->max_m; i++ )
- {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,A->max_n*sizeof(complex),
- new_max_n*sizeof(complex));
- }
-
- if ( (tmp = RENEW(A->me[i],new_max_n,complex)) == NULL )
- error(E_MEM,"zm_resize");
- else {
- A->me[i] = tmp;
- }
- }
- for ( i = A->max_m; i < new_max_m; i++ )
- {
- if ( (tmp = NEW_A(new_max_n,complex)) == NULL )
- error(E_MEM,"zm_resize");
- else {
- A->me[i] = tmp;
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,0,new_max_n*sizeof(complex));
- }
- }
- }
- }
- 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,complex)) == NULL )
- error(E_MEM,"zm_resize");
- else if (mem_info_is_on()) {
- mem_bytes(TYPE_ZMAT,0,new_max*sizeof(complex));
- }
-
- }
-
- if ( old_n < new_n )
- {
- for ( i = 0; i < old_m; i++ )
- __zzero__(&(A->me[i][old_n]),new_n-old_n);
- }
-
- /* zero out the new rows.. */
- for ( i = old_m; i < new_m; i++ )
- __zzero__(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;
- }
-
-
- /* zv_resize -- returns the (complex) vector x with dim new_dim
- -- x is set to the zero vector */
- ZVEC *zv_resize(x,new_dim)
- ZVEC *x;
- int new_dim;
- {
- if (new_dim < 0)
- error(E_NEG,"zv_resize");
-
- if ( ! x )
- return zv_get(new_dim);
-
- if (new_dim == x->dim)
- return x;
-
- if ( x->max_dim == 0 ) /* assume that it's from sub_zvec */
- return zv_get(new_dim);
-
- if ( new_dim > x->max_dim )
- {
- if (mem_info_is_on()) {
- mem_bytes(TYPE_ZVEC,x->max_dim*sizeof(complex),
- new_dim*sizeof(complex));
- }
-
- x->ve = RENEW(x->ve,new_dim,complex);
- if ( ! x->ve )
- error(E_MEM,"zv_resize");
- x->max_dim = new_dim;
- }
-
- if ( new_dim > x->dim )
- __zzero__(&(x->ve[x->dim]),new_dim - x->dim);
- x->dim = new_dim;
-
- return x;
- }
-
-
- /* varying arguments */
-
- #ifdef ANSI_C
-
- #include <stdarg.h>
-
-
- /* To allocate memory to many arguments.
- The function should be called:
- zv_get_vars(dim,&x,&y,&z,...,NULL);
- where
- int dim;
- ZVEC *x, *y, *z,...;
- The last argument should be NULL !
- dim is the length of vectors x,y,z,...
- returned value is equal to the number of allocated variables
- Other gec_... functions are similar.
- */
-
- int zv_get_vars(int dim,...)
- {
- va_list ap;
- int i=0;
- ZVEC **par;
-
- va_start(ap, dim);
- while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
- *par = zv_get(dim);
- i++;
- }
-
- va_end(ap);
- return i;
- }
-
-
-
- int zm_get_vars(int m,int n,...)
- {
- va_list ap;
- int i=0;
- ZMAT **par;
-
- va_start(ap, n);
- while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
- *par = zm_get(m,n);
- i++;
- }
-
- va_end(ap);
- return i;
- }
-
-
-
- /* To resize memory for many arguments.
- The function should be called:
- v_resize_vars(new_dim,&x,&y,&z,...,NULL);
- where
- int new_dim;
- ZVEC *x, *y, *z,...;
- The last argument should be NULL !
- rdim is the resized length of vectors x,y,z,...
- returned value is equal to the number of allocated variables.
- If one of x,y,z,.. arguments is NULL then memory is allocated to this
- argument.
- Other *_resize_list() functions are similar.
- */
-
- int zv_resize_vars(int new_dim,...)
- {
- va_list ap;
- int i=0;
- ZVEC **par;
-
- va_start(ap, new_dim);
- while (par = va_arg(ap,ZVEC **)) { /* NULL ends the list*/
- *par = zv_resize(*par,new_dim);
- i++;
- }
-
- va_end(ap);
- return i;
- }
-
-
-
- int zm_resize_vars(int m,int n,...)
- {
- va_list ap;
- int i=0;
- ZMAT **par;
-
- va_start(ap, n);
- while (par = va_arg(ap,ZMAT **)) { /* NULL ends the list*/
- *par = zm_resize(*par,m,n);
- i++;
- }
-
- va_end(ap);
- return i;
- }
-
-
- /* To deallocate memory for many arguments.
- The function should be called:
- v_free_v