📄 matrixio.c
字号:
if ( (*line == 'f' || *line == 'F') && i < dim-1 )
{ i++; dynamic = FALSE; goto redo; }
#if REAL == DOUBLE
} while ( *line=='\0' || sscanf(line,"%lf",&vec->ve[i]) < 1 );
#elif REAL == FLOAT
} while ( *line=='\0' || sscanf(line,"%f",&vec->ve[i]) < 1 );
#endif
return (vec);
}
/* bfin_vec -- batch-file input of vector */
#ifndef ANSI_C
VEC *bfin_vec(fp,vec)
FILE *fp;
VEC *vec;
#else
VEC *bfin_vec(FILE *fp,VEC *vec)
#endif
{
unsigned int i,dim;
int io_code;
/* get dimension */
skipjunk(fp);
if ((io_code=fscanf(fp," Vector: dim:%u",&dim)) < 1 ||
dim>MAXDIM )
error(io_code==EOF ? 7 : 6,"bfin_vec");
/* allocate memory if necessary */
if ( vec==(VEC *)NULL )
vec = v_resize(vec,dim);
/* get entries */
skipjunk(fp);
for ( i=0; i<dim; i++ )
#if REAL == DOUBLE
if ((io_code=fscanf(fp,"%lf",&vec->ve[i])) < 1 )
#elif REAL == FLOAT
if ((io_code=fscanf(fp,"%f",&vec->ve[i])) < 1 )
#endif
error(io_code==EOF ? 7 : 6,"bfin_vec");
return (vec);
}
/**************************************************************************
Output routines
**************************************************************************/
static const char *format = "%14.9g ";
/* setformat -- sets the printf format string for the Meschach I/O operations
-- returns the previous format string */
#ifndef ANSI_C
char *setformat(f_string)
char *f_string;
#else
const char *setformat(const char *f_string)
#endif
{
const char *old_f_string;
old_f_string = format;
if ( f_string != (char *)NULL && *f_string != '\0' )
format = f_string;
return old_f_string;
}
/* m_foutput -- prints a representation of the matrix a onto file/stream fp */
#ifndef ANSI_C
void m_foutput(fp,a)
FILE *fp;
MAT *a;
#else
void m_foutput(FILE *fp, const MAT *a)
#endif
{
unsigned int i, j, tmp;
if ( a == (MAT *)NULL )
{ fprintf(fp,"Matrix: NULL\n"); return; }
fprintf(fp,"Matrix: %d by %d\n",a->m,a->n);
if ( a->me == (Real **)NULL )
{ fprintf(fp,"NULL\n"); return; }
for ( i=0; i<a->m; i++ ) /* for each row... */
{
fprintf(fp,"row %u: ",i);
for ( j=0, tmp=2; j<a->n; j++, tmp++ )
{ /* for each col in row... */
fprintf(fp,format,a->me[i][j]);
if ( ! (tmp % 5) ) putc('\n',fp);
}
if ( tmp % 5 != 1 ) putc('\n',fp);
}
}
/* px_foutput -- prints a representation of px onto file/stream fp */
#ifndef ANSI_C
void px_foutput(fp,px)
FILE *fp;
PERM *px;
#else
void px_foutput(FILE *fp, const PERM *px)
#endif
{
unsigned int i;
if ( px == (PERM *)NULL )
{ fprintf(fp,"Permutation: NULL\n"); return; }
fprintf(fp,"Permutation: size: %u\n",px->size);
if ( px->pe == (unsigned int *)NULL )
{ fprintf(fp,"NULL\n"); return; }
for ( i=0; i<px->size; i++ )
if ( ! (i % 8) && i != 0 )
fprintf(fp,"\n %u->%u ",i,px->pe[i]);
else
fprintf(fp,"%u->%u ",i,px->pe[i]);
fprintf(fp,"\n");
}
/* v_foutput -- prints a representation of x onto file/stream fp */
#ifndef ANSI_C
void v_foutput(fp,x)
FILE *fp;
VEC *x;
#else
void v_foutput(FILE *fp, const VEC *x)
#endif
{
unsigned int i, tmp;
if ( x == (VEC *)NULL )
{ fprintf(fp,"Vector: NULL\n"); return; }
fprintf(fp,"Vector: dim: %d\n",x->dim);
if ( x->ve == (Real *)NULL )
{ fprintf(fp,"NULL\n"); return; }
for ( i=0, tmp=0; i<x->dim; i++, tmp++ )
{
fprintf(fp,format,x->ve[i]);
if ( tmp % 5 == 4 ) putc('\n',fp);
}
if ( tmp % 5 != 0 ) putc('\n',fp);
}
/* m_dump -- prints a dump of all pointers and data in a onto fp
-- suitable for low-level debugging */
#ifndef ANSI_C
void m_dump(fp,a)
FILE *fp;
MAT *a;
#else
void m_dump(FILE *fp, const MAT *a)
#endif
{
unsigned int i, j, tmp;
if ( a == (MAT *)NULL )
{ fprintf(fp,"Matrix: NULL\n"); return; }
fprintf(fp,"Matrix: %d by %d @ 0x%lx\n",a->m,a->n,(long)a);
fprintf(fp,"\tmax_m = %d, max_n = %d, max_size = %d\n",
a->max_m, a->max_n, a->max_size);
if ( a->me == (Real **)NULL )
{ fprintf(fp,"NULL\n"); return; }
fprintf(fp,"a->me @ 0x%lx\n",(long)(a->me));
fprintf(fp,"a->base @ 0x%lx\n",(long)(a->base));
for ( i=0; i<a->m; i++ ) /* for each row... */
{
fprintf(fp,"row %u: @ 0x%lx ",i,(long)(a->me[i]));
for ( j=0, tmp=2; j<a->n; j++, tmp++ )
{ /* for each col in row... */
fprintf(fp,format,a->me[i][j]);
if ( ! (tmp % 5) ) putc('\n',fp);
}
if ( tmp % 5 != 1 ) putc('\n',fp);
}
}
/* px_dump -- prints a dump of all pointers and data in a onto fp
-- suitable for low-level debugging */
#ifndef ANSI_C
void px_dump(fp,px)
FILE *fp;
PERM *px;
#else
void px_dump(FILE *fp, const PERM *px)
#endif
{
unsigned int i;
if ( ! px )
{ fprintf(fp,"Permutation: NULL\n"); return; }
fprintf(fp,"Permutation: size: %u @ 0x%lx\n",px->size,(long)(px));
if ( ! px->pe )
{ fprintf(fp,"NULL\n"); return; }
fprintf(fp,"px->pe @ 0x%lx\n",(long)(px->pe));
for ( i=0; i<px->size; i++ )
fprintf(fp,"%u->%u ",i,px->pe[i]);
fprintf(fp,"\n");
}
/* v_dump -- prints a dump of all pointers and data in a onto fp
-- suitable for low-level debugging */
#ifndef ANSI_C
void v_dump(fp,x)
FILE *fp;
VEC *x;
#else
void v_dump(FILE *fp, const VEC *x)
#endif
{
unsigned int i, tmp;
if ( ! x )
{ fprintf(fp,"Vector: NULL\n"); return; }
fprintf(fp,"Vector: dim: %d @ 0x%lx\n",x->dim,(long)(x));
if ( ! x->ve )
{ fprintf(fp,"NULL\n"); return; }
fprintf(fp,"x->ve @ 0x%lx\n",(long)(x->ve));
for ( i=0, tmp=0; i<x->dim; i++, tmp++ )
{
fprintf(fp,format,x->ve[i]);
if ( tmp % 5 == 4 ) putc('\n',fp);
}
if ( tmp % 5 != 0 ) putc('\n',fp);
}
/* iv_foutput -- print a representation of iv on stream fp */
#ifndef ANSI_C
void iv_foutput(fp,iv)
FILE *fp;
IVEC *iv;
#else
void iv_foutput(FILE *fp, const IVEC *iv)
#endif
{
int i;
fprintf(fp,"IntVector: ");
if ( iv == IVNULL )
{
fprintf(fp,"**** NULL ****\n");
return;
}
fprintf(fp,"dim: %d\n",iv->dim);
for ( i = 0; i < iv->dim; i++ )
{
if ( (i+1) % 8 )
fprintf(fp,"%8d ",iv->ive[i]);
else
fprintf(fp,"%8d\n",iv->ive[i]);
}
if ( i % 8 )
fprintf(fp,"\n");
}
/* iv_finput -- input integer vector from stream fp
-- input from a terminal is handled interactively
-- batch/file input has the same format as produced by
iv_foutput except that whitespace and comments ("#...\n")
are skipped */
#ifndef ANSI_C
IVEC *iv_finput(fp,x)
FILE *fp;
IVEC *x;
#else
IVEC *iv_finput(FILE *fp, IVEC *x)
#endif
{
IVEC *iiv_finput(),*biv_finput();
if ( isatty(fileno(fp)) )
return iiv_finput(fp,x);
else
return biv_finput(fp,x);
}
/* iiv_finput -- interactive input of IVEC iv */
#ifndef ANSI_C
IVEC *iiv_finput(fp,iv)
FILE *fp;
IVEC *iv;
#else
IVEC *iiv_finput(FILE *fp, IVEC *iv)
#endif
{
unsigned int i,dim,dynamic; /* dynamic set if memory allocated here */
/* get dimension */
if ( iv != (IVEC *)NULL && iv->dim<MAXDIM )
{ dim = iv->dim; dynamic = FALSE; }
else
{
dynamic = TRUE;
do
{
fprintf(stderr,"IntVector: dim: ");
if ( fgets(line,MAXLINE,fp)==NULL )
error(E_INPUT,"iiv_finput");
} while ( sscanf(line,"%u",&dim)<1 || dim>MAXDIM );
iv = iv_get(dim);
}
/* input elements */
for ( i=0; i<dim; i++ )
do
{
redo:
fprintf(stderr,"entry %u: ",i);
if ( !dynamic )
fprintf(stderr,"old: %-9d new: ",iv->ive[i]);
if ( fgets(line,MAXLINE,fp)==NULL )
error(E_INPUT,"iiv_finput");
if ( (*line == 'b' || *line == 'B') && i > 0 )
{ i--; dynamic = FALSE; goto redo; }
if ( (*line == 'f' || *line == 'F') && i < dim-1 )
{ i++; dynamic = FALSE; goto redo; }
} while ( *line=='\0' || sscanf(line,"%d",&iv->ive[i]) < 1 );
return (iv);
}
/* biv_finput -- batch-file input of IVEC iv */
#ifndef ANSI_C
IVEC *biv_finput(fp,iv)
FILE *fp;
IVEC *iv;
#else
IVEC *biv_finput(FILE *fp, IVEC *iv)
#endif
{
unsigned int i,dim;
int io_code;
/* get dimension */
skipjunk(fp);
if ((io_code=fscanf(fp," IntVector: dim:%u",&dim)) < 1 ||
dim>MAXDIM )
error(io_code==EOF ? 7 : 6,"biv_finput");
/* allocate memory if necessary */
if ( iv==(IVEC *)NULL || iv->dim<dim )
iv = iv_resize(iv,dim);
/* get entries */
skipjunk(fp);
for ( i=0; i<dim; i++ )
if ((io_code=fscanf(fp,"%d",&iv->ive[i])) < 1 )
error(io_code==EOF ? 7 : 6,"biv_finput");
return (iv);
}
/* iv_dump -- dumps all the contents of IVEC iv onto stream fp */
#ifndef ANSI_C
void iv_dump(fp,iv)
FILE*fp;
IVEC*iv;
#else
void iv_dump(FILE *fp, const IVEC *iv)
#endif
{
int i;
fprintf(fp,"IntVector: ");
if ( ! iv )
{
fprintf(fp,"**** NULL ****\n");
return;
}
fprintf(fp,"dim: %d, max_dim: %d\n",iv->dim,iv->max_dim);
fprintf(fp,"ive @ 0x%lx\n",(long)(iv->ive));
for ( i = 0; i < iv->max_dim; i++ )
{
if ( (i+1) % 8 )
fprintf(fp,"%8d ",iv->ive[i]);
else
fprintf(fp,"%8d\n",iv->ive[i]);
}
if ( i % 8 )
fprintf(fp,"\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -