help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] human-readable gsl_matrix_fprintf?


From: Tom Weber
Subject: [Help-gsl] human-readable gsl_matrix_fprintf?
Date: Fri, 31 Oct 2003 16:41:38 +0100
User-agent: KMail/1.5

Hi!
I just found out about gsl, this is the math library I always needed but 
didn't know about!
I started playing with it, and found that gsl_matrix_fprintf() prints one 
element per line, not very readable if you ask me. Also I need to feed 
matrices to gnuplot.
The problem with human-readable matrices is the aligning of columns, you must 
first check the max width of each column.

What about this routine (a little dirty):

int my_gsl_matrix_fprintf(FILE *stream,gsl_matrix *m,char *fmt)
{
        size_t rows=m->size1;
        size_t cols=m->size2;
        size_t row,col,ml;
        int fill;
        char buf[100];
        gsl_vector *maxlen;

        maxlen=gsl_vector_alloc(cols);
        for (col=0;col<cols;++col) {
                ml=0;
                for (row=0;row<rows;++row) {
                        sprintf(buf,fmt,gsl_matrix_get(m,row,col));
                        if (strlen(buf)>ml)
                                ml=strlen(buf);
                }
                gsl_vector_set(maxlen,col,ml);
        }

        for (row=0;row<rows;++row) {
                for (col=0;col<cols;++col) {
                        sprintf(buf,fmt,gsl_matrix_get(m,row,col));
                        fprintf(stream,"%s",buf);
                        fill=gsl_vector_get(maxlen,col)+1-strlen(buf);
                        while (--fill>=0)
                                fprintf(stream," ");
                }
                fprintf(stream,"\n");
        }
        gsl_vector_free(maxlen);
        return 0;
}

which has printed this: (best viewed with monospace font)

0.159893  0.0215342 -2.64864  0.863043  2.52851
0.827413  -0.826668 -0.806819 0.644796  -1.60179
0.0582382 0.703523  -1.18743  -1.32534  -0.793077
0.0307781 -1.05489  0.871883  -0.687079 -1.45294
-0.126298 -0.492506 1.70216   0.933397  0.749687

Why isn't there a routine like this in gsl?

-- 
Cheers,
Tom Weber





reply via email to

[Prev in Thread] Current Thread [Next in Thread]