This is the mail archive of the
gsl-discuss@sources.redhat.com
mailing list for the GSL project.
gsl_vector_fprintf
- To: gsl-discuss at sourceware dot cygnus dot com
- Subject: gsl_vector_fprintf
- From: "E. Robert Tisdale" <edwin at netwood dot net>
- Date: Wed, 19 Jul 2000 01:13:51 +0000
I am suggesting that the GSL vector fprintf function be changed from
int gsl_vector_fprintf(FILE *stream, const gsl_vector* v,
const char* format);
to
int gsl_vector_fprintf(FILE *stream, const gsl_vector* v,
int width, unsigned int precision, unsigned int columns);
This permits the numerical application programmer
to display columns elements of a row vector at a time
on a line with a space between each element on a line
like this:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
instead of like this:
0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
for a vector of 10 elements, for example.
A similar function for type gsl_matrix
simply prints the row vectors in order
so that it appears as a matrix on the display
if all of the columns of a row fit on one line.
int gsl_fprintf(FILE *stream, gsl_double x,
int width, unsigned int precision) {
char format[64];
int characters = sprintf(format, "%%%d.%ue", width, precision);
if (0 <= characters) {
return fprintf(stream, format, (gsl_double)x); } else { return -1; }
}
int gsl_vector_fprintf(FILE *stream, const gsl_vector* v,
int width, unsigned int precision, unsigned int columns) {
int modulus = (0 < columns)? columns: 4;
int characters;
int total = 0;
gsl_extent n = gsl_vector_extent(v);
gsl_offset j = 0;
for (j = 0; j < n; j++) {
characters = (0 == j%modulus)? fprintf(stream, "\n"): fprintf(stream, " ");
if (0 <= characters) { total += characters; } else { return -1; }
characters = gsl_fprintf(stream, gsl_vector_get(v, j), width, precision);
if (0 <= characters) { total += characters; } else { return -1; }
}
characters = fprintf(stream, "\n");
if (0 <= characters) { total += characters; } else { return -1; }
return total;
}