Add companion output utilities based on already opened streams.

Functions csrmatrix_write_stream() and vector_write_stream() subsume
the output responsibilities of csrmatrix_write() and vector_write(),
respectively, but do not deal with opening or closing streams.  This
allows, e.g., a call such as

    csrmatrix_write_stream(A, stderr);

for debugging purposes.

Re-implement csrmatrix_write() and vector_write() in terms of
csrmatrix_write_stream() and vector_write_stream(), respectively.
This commit is contained in:
Bård Skaflestad 2011-09-07 23:14:47 +02:00
parent 4e8b35f9ed
commit 70a9e8a7a8
2 changed files with 41 additions and 15 deletions

View File

@ -203,41 +203,60 @@ void
csrmatrix_write(const struct CSRMatrix *A, const char *fn)
/* ---------------------------------------------------------------------- */
{
size_t i, j;
FILE *fp;
FILE *fp;
fp = fopen(fn, "wt");
if (fp != NULL) {
for (i = j = 0; i < A->m; i++) {
for (; j < (size_t) (A->ia[i + 1]); j++) {
fprintf(fp, "%lu %lu %26.18e\n",
(unsigned long) (i + 1),
(unsigned long) (A->ja[j] + 1),
A->sa[j]);
}
}
csrmatrix_write_stream(A, fp);
}
fclose(fp);
}
/* ---------------------------------------------------------------------- */
void
csrmatrix_write_stream(const struct CSRMatrix *A, FILE *fp)
/* ---------------------------------------------------------------------- */
{
size_t i, j;
for (i = j = 0; i < A->m; i++) {
for (; j < (size_t) (A->ia[i + 1]); j++) {
fprintf(fp, "%lu %lu %26.18e\n",
(unsigned long) (i + 1),
(unsigned long) (A->ja[j] + 1),
A->sa[j]);
}
}
}
/* ---------------------------------------------------------------------- */
void
vector_write(size_t n, const double *v, const char *fn)
/* ---------------------------------------------------------------------- */
{
size_t i;
FILE *fp;
FILE *fp;
fp = fopen(fn, "wt");
if (fp != NULL) {
for (i = 0; i < n; i++) {
fprintf(fp, "%26.18e\n", v[i]);
}
vector_write_stream(n, v, fp);
}
fclose(fp);
}
/* ---------------------------------------------------------------------- */
void
vector_write_stream(size_t n, const double *v, FILE *fp)
/* ---------------------------------------------------------------------- */
{
size_t i;
for (i = 0; i < n; i++) {
fprintf(fp, "%26.18e\n", v[i]);
}
}

View File

@ -21,6 +21,7 @@
#define OPM_SPARSE_SYS_HEADER_INCLUDED
#include <stddef.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
@ -70,9 +71,15 @@ vector_zero(size_t n, double *v);
void
csrmatrix_write(const struct CSRMatrix *A, const char *fn);
void
csrmatrix_write_stream(const struct CSRMatrix *A, FILE *fp);
void
vector_write(size_t n, const double *v, const char *fn);
void
vector_write_stream(size_t n, const double *v, FILE *fp);
#ifdef __cplusplus
}
#endif