home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / meschach / !Meschach / c / zsolve < prev    next >
Encoding:
Text File  |  1994-01-12  |  7.4 KB  |  354 lines

  1. TED
  2.    int    i;
  3. #endif
  4.    
  5.    if ( mat==(ZMAT *)NULL || (int)(mat->m) < 0 ||
  6.        (int)(mat->n) < 0 )
  7.      /* don't trust it */
  8.      return (-1);
  9.    
  10. #ifndef SEGMENTED
  11.    if ( mat->base != (complex *)NULL ) {
  12.       if (mem_info_is_on()) {
  13.      mem_bytes(TYPE_ZMAT,mat->max_m*mat->max_n*sizeof(complex),0);
  14.       }       
  15.       free((char *)(mat->base));
  16.    }
  17. #else
  18.    for ( i = 0; i < mat->max_m; i++ )
  19.      if ( mat->me[i] != (complex *)NULL ) {
  20.     if (mem_info_is_on()) {
  21.        mem_bytes(TYPE_ZMAT,mat->max_n*sizeof(complex),0);
  22.     }
  23.     free((char *)(mat->me[i]));
  24.      }
  25. #endif
  26.    if ( mat->me != (complex **)NULL ) {
  27.       if (mem_info_is_on()) {
  28.      mem_bytes(TYPE_ZMAT,mat->max_m*sizeof(complex *),0);
  29.       }       
  30.       free((char *)(mat->me));
  31.    }
  32.    
  33.    if (mem_info_is_on()) {
  34.       mem_bytes(TYPE_ZMAT,sizeof(ZMAT),0);
  35.       mem_numvar(TYPE_ZMAT,-1);
  36.    }
  37.    free((char *)mat);
  38.    
  39.    return (0);
  40. }
  41.  
  42.  
  43. /* zv_free -- returns ZVEC & asoociated memory back to memory heap */
  44. int    zv_free(vec)
  45. ZVEC    *vec;
  46. {
  47.    if ( vec==(ZVEC *)NULL || (int)(vec->dim) < 0 )
  48.      /* don't trust it */
  49.      return (-1);
  50.    
  51.    if ( vec->ve == (complex *)NULL ) {
  52.       if (mem_info_is_on()) {
  53.      mem_bytes(TYPE_ZVEC,sizeof(ZVEC),0);
  54.      mem_numvar(TYPE_ZVEC,-1);
  55.       }
  56.       free((char *)vec);
  57.    }
  58.    else
  59.    {
  60.       if (mem_info_is_on()) {
  61.      mem_bytes(TYPE_ZVEC,vec->max_dim*sizeof(complex)+
  62.               sizeof(ZVEC),0);
  63.      mem_numvar(TYPE_ZVEC,-1);
  64.       }
  65.       
  66.       free((char *)vec->ve);
  67.       free((char *)vec);
  68.    }
  69.    
  70.    return (0);
  71. }
  72.  
  73.  
  74. /* zm_resize -- returns the matrix A of size new_m x new_n; A is zeroed
  75.    -- if A == NULL on entry then the effect is equivalent to m_get() */
  76. ZMAT    *zm_resize(A,new_m,new_n)
  77. ZMAT    *A;
  78. int    new_m, new_n;
  79. {
  80.    u_int    i, new_max_m, new_max_n, new_size, old_m, old_n;
  81.    
  82.    if (new_m < 0 || new_n < 0)
  83.      error(E_NEG,"zm_resize");
  84.  
  85.    if ( ! A )
  86.      return zm_get(new_m,new_n);
  87.    
  88.    if (new_m == A->m && new_n == A->n)
  89.      return A;
  90.  
  91.    old_m = A->m;    old_n = A->n;
  92.    if ( new_m > A->max_m )
  93.    {    /* re-allocate A->me */
  94.       if (mem_info_is_on()) {
  95.      mem_bytes(TYPE_ZMAT,A->max_m*sizeof(complex *),
  96.               new_m*sizeof(complex *));
  97.       }
  98.  
  99.       A->me = RENEW(A->me,new_m,complex *);
  100.       if ( ! A->me )
  101.     error(E_MEM,"zm_resize");
  102.    }
  103.    new_max_m = max(new_m,A->max_m);
  104.    new_max_n = max(new_n,A->max_n);
  105.    
  106. #ifndef SEGMENTED
  107.    new_size = new_max_m*new_max_n;
  108.    if ( new_size > A->max_size )
  109.    {    /* re-allocate A->base */
  110.       if (mem_info_is_on()) {
  111.      mem_bytes(TYPE_ZMAT,A->max_m*A->max_n*sizeof(complex),
  112.         new_size*sizeof(complex));      
  113.       }
  114.  
  115.       A->base = RENEW(A->base,new_size,complex);
  116.       if ( ! A->base )
  117.     error(E_MEM,"zm_resize");
  118.       A->max_size = new_size;
  119.    }
  120.    
  121.    /* now set up A->me[i] */
  122.    for ( i = 0; i < new_m; i++ )
  123.      A->me[i] = &(A->base[i*new_n]);
  124.    
  125.    /* now shift data in matrix */
  126.    if ( old_n > new_n )
  127.    {
  128.       for ( i = 1; i < min(old_m,new_m); i++ )
  129.     MEM_COPY((char *)&(A->base[i*old_n]),
  130.          (char *)&(A->base[i*new_n]),
  131.          sizeof(complex)*new_n);
  132.    }
  133.    else if ( old_n < new_n )
  134.    {
  135.       for ( i = min(old_m,new_m)-1; i > 0; i-- )
  136.       {   /* copy & then zero extra space */
  137.      MEM_COPY((char *)&(A->base[i*old_n]),
  138.           (char *)&(A->base[i*new_n]),
  139.           sizeof(complex)*old_n);
  140.      __zzero__(&(A->base[i*new_n+old_n]),(new_n-old_n));
  141.       }
  142.       __zzero__(&(A->base[old_n]),(new_n-old_n));
  143.       A->max_n = new_n;
  144.    }
  145.    /* zero out the new rows.. */
  146.    for ( i = old_m; i < new_m; i++ )
  147.      __zzero__(&(A->base[i*new_n]),new_n);
  148. #else
  149.    if ( A->max_n < new_n )
  150.    {
  151.       complex    *tmp;
  152.       
  153.       for ( i = 0; i < A->max_m; i++ )
  154.       {
  155.      if (mem_info_is_on()) {
  156.         mem_bytes(TYPE_ZMAT,A->max_n*sizeof(complex),
  157.              new_max_n*sizeof(complex));
  158.      }
  159.  
  160.      if ( (tmp = RENEW(A->me[i],new_max_n,complex)) == NULL )
  161.        error(E_MEM,"zm_resize");
  162.      else {
  163.         A->me[i] = tmp;
  164.      }
  165.       }
  166.       for ( i = A->max_m; i < new_max_m; i++ )
  167.       {
  168.      if ( (tmp = NEW_A(new_max_n,complex)) == NULL )
  169.        error(E_MEM,"zm_resize");
  170.      else {
  171.         A->me[i] = tmp;
  172.         if (mem_info_is_on()) {
  173.            mem_bytes(TYPE_ZMAT,0,new_max_n*sizeof(complex));
  174.         }
  175.      }
  176.       }
  177.    }
  178.    else if ( A->max_m < new_m )
  179.    {
  180.       for ( i = A->max_m; i < new_m; i++ )
  181.     if ( (A->me[i] = NEW_A(new_max_n,complex)) == NULL )
  182.       error(E_MEM,"zm_resize");
  183.     else if (mem_info_is_on()) {
  184.        mem_bytes(TYPE_ZMAT,0,new_max*sizeof(complex));
  185.     }
  186.       
  187.    }
  188.    
  189.    if ( old_n < new_n )
  190.    {
  191.       for ( i = 0; i < old_m; i++ )
  192.     __zzero__(&(A->me[i][old_n]),new_n-old_n);
  193.    }
  194.    
  195.    /* zero out the new rows.. */
  196.    for ( i = old_m; i < new_m; i++ )
  197.      __zzero__(A->me[i],new_n);
  198. #endif
  199.    
  200.    A->max_m = new_max_m;
  201.    A->max_n = new_max_n;
  202.    A->max_size = A->max_m*A->max_n;
  203.    A->m = new_m;    A->n = new_n;
  204.    
  205.    return A;
  206. }
  207.  
  208.  
  209. /* zv_resize -- returns the (complex) vector x with dim new_dim
  210.    -- x is set to the zero vector */
  211. ZVEC    *zv_resize(x,new_dim)
  212. ZVEC    *x;
  213. int    new_dim;
  214. {
  215.    if (new_dim < 0)
  216.      error(E_NEG,"zv_resize");
  217.  
  218.    if ( ! x )
  219.      return zv_get(new_dim);
  220.  
  221.    if (new_dim == x->dim)
  222.      return x;
  223.  
  224.    if ( x->max_dim == 0 )    /* assume that it's from sub_zvec */
  225.      return zv_get(new_dim);
  226.    
  227.    if ( new_dim > x->max_dim )
  228.    {
  229.       if (mem_info_is_on()) { 
  230.      mem_bytes(TYPE_ZVEC,x->max_dim*sizeof(complex),
  231.               new_dim*sizeof(complex));
  232.       }
  233.  
  234.       x->ve = RENEW(x->ve,new_dim,complex);
  235.       if ( ! x->ve )
  236.     error(E_MEM,"zv_resize");
  237.       x->max_dim = new_dim;
  238.    }
  239.    
  240.    if ( new_dim > x->dim )
  241.      __zzero__(&(x->ve[x->dim]),new_dim - x->dim);
  242.    x->dim = new_dim;
  243.    
  244.    return x;
  245. }
  246.  
  247.  
  248. /* varying arguments */
  249.  
  250. #ifdef ANSI_C
  251.  
  252. #include <stdarg.h>
  253.  
  254.  
  255. /* To allocate memory to many arguments. 
  256.    The function should be called:
  257.    zv_get_vars(dim,&x,&y,&z,...,NULL);
  258.    where 
  259.      int dim;
  260.      ZVEC *x, *y, *z,...;
  261.      The last argument should be NULL ! 
  262.      dim is the length of vectors x,y,z,...
  263.      returned value is equal to the number of allocated variables
  264.      Other gec_... functions are similar.
  265. */
  266.  
  267. int zv_get_vars(int dim,...) 
  268. {
  269.    va_list ap;
  270.    int i=0;
  271.    ZVEC **par;
  272.    
  273.    va_start(ap, dim);
  274.    while (par = va_arg(ap,ZVEC **)) {   /* NULL ends the list*/
  275.       *par = zv_get(dim);
  276.       i++;
  277.    } 
  278.  
  279.    va_end(ap);
  280.    return i;
  281. }
  282.  
  283.  
  284.  
  285. int zm_get_vars(int m,int n,...) 
  286. {
  287.    va_list ap;
  288.    int i=0;
  289.    ZMAT **par;
  290.    
  291.    va_start(ap, n);
  292.    while (par = va_arg(ap,ZMAT **)) {   /* NULL ends the list*/
  293.       *par = zm_get(m,n);
  294.       i++;
  295.    } 
  296.  
  297.    va_end(ap);
  298.    return i;
  299. }
  300.  
  301.  
  302.  
  303. /* To resize memory for many arguments. 
  304.    The function should be called:
  305.    v_resize_vars(new_dim,&x,&y,&z,...,NULL);
  306.    where 
  307.      int new_dim;
  308.      ZVEC *x, *y, *z,...;
  309.      The last argument should be NULL ! 
  310.      rdim is the resized length of vectors x,y,z,...
  311.      returned value is equal to the number of allocated variables.
  312.      If one of x,y,z,.. arguments is NULL then memory is allocated to this 
  313.      argument. 
  314.      Other *_resize_list() functions are similar.
  315. */
  316.  
  317. int zv_resize_vars(int new_dim,...)
  318. {
  319.    va_list ap;
  320.    int i=0;
  321.    ZVEC **par;
  322.    
  323.    va_start(ap, new_dim);
  324.    while (par = va_arg(ap,ZVEC **)) {   /* NULL ends the list*/
  325.       *par = zv_resize(*par,new_dim);
  326.       i++;
  327.    } 
  328.  
  329.    va_end(ap);
  330.    return i;
  331. }
  332.  
  333.  
  334.  
  335. int zm_resize_vars(int m,int n,...) 
  336. {
  337.    va_list ap;
  338.    int i=0;
  339.    ZMAT **par;
  340.    
  341.    va_start(ap, n);
  342.    while (par = va_arg(ap,ZMAT **)) {   /* NULL ends the list*/
  343.       *par = zm_resize(*par,m,n);
  344.       i++;
  345.    } 
  346.  
  347.    va_end(ap);
  348.    return i;
  349. }
  350.  
  351.  
  352. /* To deallocate memory for many arguments. 
  353.    The function should be called:
  354.    v_free_v