\File{util.c},{02:25},{Jan  4 1998}
\L{\LB{\C{}\/* \$Source: \/mit\/bert\/Cvsroot\/Thesis\/src\/exact\/util.c,v \$}}
\L{\LB{ *}}
\L{\LB{ * random helper functions}}
\L{\LB{ *\/\CE{}}}
\L{\LB{}}
\L{\LB{\K{\#include} \<malloc.h\>}}
\L{\LB{\K{\#include} \<errno.h\>}}
\L{\LB{\K{\#include} \<string.h\>}}
\L{\LB{\K{\#include} \<stdio.h\>}}
\L{\LB{\K{\#include} \S{}\"complex.h\"\SE{}}}
\L{\LB{\K{\#include} \S{}\"util.h\"\SE{}}}
\L{\LB{}}
\L{\LB{\K{static} \K{char} rcsid[] = \S{}\"\$Id: util.c,v 1.9 1998\/01\/04 07:25:41 bert Exp \$\"\SE{};}}
\L{\LB{}}
\L{\LB{\K{char}* progname;  \C{}\/* name we\'re running under *\/\CE{}}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ * allocate memory (and check that we actually got some)}}
\L{\LB{ *   size \-\- number of bytes to allocate.}}
\L{\LB{ *   info \-\- documentation for error message.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{void} *safe\_malloc(size\_t size, \K{char}* info)}}
\L{\LB{\{}}
\L{\LB{  \K{void} *ret = (\K{void}*)malloc(size);}}
\L{\LB{  \K{if} (ret==(\K{void}*)NULL) \{}}
\L{\LB{    fprintf(stderr,\S{}\"\%s: malloc(\%s) failed [\%s]\!n\"\SE{},}}
\L{\LB{}\Tab{8}{    progname, info ? info : \S{}\"\"\SE{}, strerror(errno));}}
\L{\LB{    exit(255);}}
\L{\LB{  \}}}
\L{\LB{  \K{return} ret;}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/*}}
\L{\LB{ * copy an NxN complex array of complex numbers.}}
\L{\LB{ *   A \-\-    source.  (n x n complex array)}}
\L{\LB{ *   B \-\-    destination.  (n x n complex array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{void} copy\_data (\K{complex} *A, \K{complex} *B, \K{long} n)}}
\L{\LB{\{}}
\L{\LB{  memcpy(B, A, n*n*\K{sizeof}(\K{complex}));}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\C{}\/* save real parts from a complex array to a file in ASCII form}}
\L{\LB{ *   A \-\-    data.  (n x n array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *   file \-\- output file name.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{void} dump\_Re(\K{complex} *A, \K{long} n, \K{char}* file)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i,j;}}
\L{\LB{  FILE *ff;}}
\L{\LB{}}
\L{\LB{  ff = fopen (file, \S{}\"w\"\SE{});}}
\L{\LB{  \K{if} (ff==(FILE*)NULL) \{}}
\L{\LB{    fprintf(stderr,\S{}\"\%s: can\'t open \%s for writing [\%s]\!n\"\SE{},}}
\L{\LB{}\Tab{8}{    progname, file, strerror(errno));}}
\L{\LB{    exit(99);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<n; i++) \{}}
\L{\LB{    \K{for} (j=0; j\<n; j++) \{}}
\L{\LB{      fprintf(ff, \S{}\" \%20.16e\"\SE{}, Re(A[i*n+j]));}}
\L{\LB{    \}}}
\L{\LB{    fprintf(ff, \S{}\"\!n\"\SE{});}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  fclose(ff);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\C{}\/* save imaginary parts from a complex array to a file in ASCII form}}
\L{\LB{ *   A \-\-    data.  (n x n array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *   file \-\- output file name.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{void} dump\_Im(\K{complex} *A, \K{long} n, \K{char}* file)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i,j;}}
\L{\LB{  FILE *ff;}}
\L{\LB{}}
\L{\LB{  ff = fopen (file, \S{}\"w\"\SE{});}}
\L{\LB{  \K{if} (ff==(FILE*)NULL) \{}}
\L{\LB{    fprintf(stderr,\S{}\"\%s: can\'t open \%s for writing [\%s]\!n\"\SE{},}}
\L{\LB{}\Tab{8}{    progname, file, strerror(errno));}}
\L{\LB{    exit(99);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<n; i++) \{}}
\L{\LB{    \K{for} (j=0; j\<n; j++) \{}}
\L{\LB{      fprintf(ff, \S{}\" \%20.16e\"\SE{}, Im(A[i*n+j]));}}
\L{\LB{    \}}}
\L{\LB{    fprintf(ff, \S{}\"\!n\"\SE{});}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  fclose(ff);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\C{}\/* save absolute values from a complex array to a file in ASCII form}}
\L{\LB{ *   A \-\-    data.  (n x n array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *   file \-\- output file name.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{void} dump\_absolute(\K{complex} *A, \K{long} n, \K{char}* file)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i,j;}}
\L{\LB{  FILE *ff;}}
\L{\LB{}}
\L{\LB{  ff = fopen (file, \S{}\"w\"\SE{});}}
\L{\LB{  \K{if} (ff==(FILE*)NULL) \{}}
\L{\LB{    fprintf(stderr,\S{}\"\%s: can\'t open \%s for writing [\%s]\!n\"\SE{},}}
\L{\LB{}\Tab{8}{    progname, file, strerror(errno));}}
\L{\LB{    exit(99);}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<n; i++) \{}}
\L{\LB{    \K{for} (j=0; j\<n; j++) \{}}
\L{\LB{      fprintf(ff, \S{}\" \%20.16e\"\SE{}, Cabs(A[i*n+j]));}}
\L{\LB{    \}}}
\L{\LB{    fprintf(ff, \S{}\"\!n\"\SE{});}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  fclose(ff);}}
\L{\LB{\}}}
