#include <stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define fileName 100
#define bufLen 80920

typedef struct node_t{
	short index;
	float weight;
}node;

int read_problem(FILE *fp, double **prob, double **coef, int nr_class){
	int index, i = 0;
	double t;
	
	while(1){
begin:
		for(int j = 0; j < nr_class - 1; ++j){
                    if (fscanf(fp,"%lf",&coef[i][j]) == EOF)
			goto end;
		}
		while(1){
			int c;
			do{
				c = fgetc(fp);
				if (c == '\n'){
					++i;
					goto begin;
				}
			}while(isspace(c));
			ungetc(c,fp);
			fscanf(fp,"%d:%lf",&index,&t);
			prob[i][index] = t;
		}
	}

end:
	return i;
}

void prompt(){
    printf("usage: svm-weight -f #features model_file\n");
    printf("-f #features: number of features in the data\n");
    printf("model_file: the *.model file generated by svm-train\n");
}
int main(int argc, char **argv){
	int l, dim, datanum,odim, nr_class;
        int *nr_sv;
        FILE *fp, *fp_model, *fp_tgt, *fp1;
	char model[fileName], tgt[fileName], buf[bufLen];
        double **prob, **coef, ***w_list;	

	if(argc != 4 || strcmp("-f", argv[1]) != 0){
                prompt();
		return 0;
	}
		
	dim = atoi(argv[2]) + 1;
	strcpy(model, argv[3]);
	
	if((fp_model = fopen(model, "r")) == NULL){
            printf("can't open model file: %s\n", model);
            return 0;
        }
        
	while(fgets(buf, bufLen, fp_model) != NULL){
		if(strncmp("nr_class", buf, 8) == 0)
			break;
	}
        char *s = strchr(buf, ' '); 
	nr_class = atoi(s);
        nr_sv = (int *)calloc(nr_class, sizeof(int));
	while(fgets(buf, bufLen, fp_model) != NULL){
		if(strncmp("nr_sv", buf, 5) == 0)
			break;
	}
        s = &buf[0];
        datanum = 0;
        for(int i = 0; i < nr_class; ++i, ++s){
            s = strchr(s, ' ');
            nr_sv[i] = atoi(s);
            datanum += nr_sv[i];
        }

        
        prob = (double **)calloc(datanum, sizeof(double *));
	for(int i = 0; i < datanum; ++i)
		prob[i] = (double *)calloc(dim, sizeof(double));
	
        coef = (double **)calloc(datanum, sizeof(double *));
	for(int i = 0; i < datanum; ++i)
		coef[i] = (double *)calloc(nr_class-1, sizeof(double));

        w_list = (double ***)calloc(nr_class, sizeof(double **));
        for(int i = 0; i < nr_class; ++i){
            w_list[i] = (double **)calloc(nr_class - 1, sizeof(double *));
            for(int j = 0; j < nr_class-1; ++j){
                w_list[i][j] = (double *)calloc(dim, sizeof(double));
            }
        }
	
	while(fgets(buf, bufLen, fp_model) != NULL){
		if(strncmp("SV", buf, 2) == 0)
			break;
	}
        
	
	l = read_problem(fp_model,prob, coef, nr_class);	
	
	for(int i = 1; i < dim; ++i){
	    for(int j = 0; j < nr_class - 1; ++j){
                int index = 0;
                int end = 0; 
                double acc;
                for(int k = 0; k < nr_class; ++k){
                    acc = 0.0;
                    index += (k == 0) ? 0 : nr_sv[k - 1];
                    end = index + nr_sv[k]; 
                    for(int m = index; m < end; ++m){
                        acc += coef[m][j] * prob[m][i];
                    }
                    w_list[k][j][i] = acc;
	        }
            }  
	}

        for(int i = 0; i < nr_class - 1; ++i){
            for(int j = i + 1, k = i; j < nr_class; ++j, ++k){
                for(int m = 1; m < dim; ++m){
                     printf("%d:%lf ", m, w_list[i][k][m] + w_list[j][i][m]);
                }   
                printf("\n");                
            }
        }
        
	return 0;
}
