GSL(Gnu Scientific Library)을 이용중 bug 해결을 위한 질문입니다.
글쓴이: aeronova / 작성시간: 일, 2006/06/25 - 5:32오전
안녕하세요,
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을 대조해 보아도 특별히 잘못된 점을 찾지 못하였습니다.
뭔가 조언이라도 주시면 감사하겠습니다.Forums:


해결되었습니다.
#include 을 빼먹었더군요. :(
헤더가 없었는데 아무 불평없었다는 것이 더 신기합니다.
It's better to burn out than to fade away. -- Kurt Cobain.
댓글 달기