GSL(Gnu Scientific Library)을 이용중 bug 해결을 위한 질문입니다.

aeronova의 이미지

안녕하세요,
GSL manual을 참고하면서 이용중 이유를 알 수 없는 버그를 만나서 질문을 올리게 되었습니다.
제가 하려고 하는 것은, 파일을 읽어서 gsl_matrix로 저장하고 거기서 gsl_vector 형식으로 column을 뽑아서 그 column의 mean을 구하는 것입니다.
array로 초벌로 짜두고, 일관성을 위해 gsl의 data type인 matrix와 vector로 고치는 중 mean값이 1.81이 되어야 하는데 쓰레기값이 자꾸 나옵니다.
제 input은 다음과 같구요,

sample.dat

2.5     2.4
0.5     0.7
2.2     2.9
1.9     2.2
3.1     3.0
2.3     2.7
2       1.6
1       1.1
1.5     1.6
1.1     0.9

코드는 다음과 같습니다.

#include <stdio.h >
#include <string.h>
#include <gsl/gsl_matrix.h>
 
#define MAX_LINE_LENGTH 80
 
int get_num_column(FILE* file_ptr) {
    char line[MAX_LINE_LENGTH];
    char delimiters[] = " \f\n\r\t\v"; // standard whitespace characters
    char* ch_ptr;
    int i=0;  //counter
 
    fgets(line, sizeof(line), file_ptr);
    //printf("%s\n",line);
    ch_ptr = strtok(line, delimiters);
    while(ch_ptr != NULL) {
        //printf("%s\n",pch);
        ch_ptr = strtok(NULL, delimiters);
        i++;
    }
    //printf("%d\n",i);
    rewind(file_ptr); /* reset the file pointer's position */
    return i;
}
 
int get_num_row(FILE* file_ptr) {
    char line[MAX_LINE_LENGTH];
    int i=0;
    while (fgets(line, sizeof(line), file_ptr) != NULL) {
        //printf("%s\n",line);
        i++;
    }
    rewind(file_ptr); /* reset the file pointer's position */
    return i;
}
 
int main(void) {
    FILE* file_ptr;
    file_ptr=fopen("./sample.dat","r");
 
    if(file_ptr==NULL) {
        printf("Error: cannot open a file.\n");
        /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
        return 1;
    }
 
    //***** get the size of data
    const int num_column = get_num_column(file_ptr);
    const int num_row    = get_num_row(file_ptr);  
 
    printf("Num of Columns = %d\n",get_num_column(file_ptr));
    printf("Num of Rows    = %d\n",get_num_row(file_ptr));   
 
    gsl_matrix* m = gsl_matrix_calloc(num_row, num_column);
    gsl_matrix_fscanf(file_ptr, m);
 
    // check out loaded data
    int i,j;
    for (j = 0; j < num_column; j++) {
        printf("%i th column:\n", j);
        for (i = 0; i < num_row; i++) {
            printf("%g\n", gsl_matrix_get(m, i, j));
        }
        printf("\n");
    }
 
    //***** read data   
    //get_data(file_ptr,m);
 
 
    //**** mean
    gsl_vector* column = gsl_vector_calloc(num_row);
    gsl_matrix_get_col(column, m, 0);
    // check out loaded data
    for (i = 0; i < num_row; i++) {
        printf("%g\n", gsl_vector_get(column, i));
    }
    printf("\n");
    for (i = 0; i < num_row; i++) {
        printf("%g\n", column->data[i]);
    }
    //double gsl_stats_mean (const double data[], size_t stride, size_t n)
    double mean = gsl_stats_mean(column->data, 1, num_row);
    printf("mean = %g\n", mean);
 
    gsl_vector_free(column);
    gsl_matrix_free(m);
    return 0;
}
 
뭔가 함수 인자의 data type에서 문제가 생기지 않았나 싶은데, GSL manual을 대조해 보아도 특별히 잘못된 점을 찾지 못하였습니다.
뭔가 조언이라도 주시면 감사하겠습니다.
aeronova의 이미지

#include 을 빼먹었더군요. :(
헤더가 없었는데 아무 불평없었다는 것이 더 신기합니다.

It's better to burn out than to fade away. -- Kurt Cobain.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.