User:Karthik0112358/Coefficient Calculator
Jump to navigation
Jump to search
int Coefficient_Calculator(struct Instance *i,double *coefficient){ struct relation *r; enum Expr_enum reltype; int dummy_int; r = (struct relation *)GetInstanceRelation(i, &reltype); if( r == NULL ) { ERROR_REPORTER_HERE(ASC_PROG_ERR,"null relation\n"); return -1; } // the current term in the relation r unsigned long t; // the number of variables in the relation r unsigned long num_var; // the index of the variable we are looking at unsigned long v; // looking at left(=1) or right(=0) hand side of r int lhs; // the memory for the stacks double *stacks; // height of each stack unsigned long stack_height; // the top position in the stacks long s = -1; unsigned long length_lhs, length_rhs; CONST struct relation_term *term; CONST struct Func *fxnptr; num_var = NumberVariables(r); length_lhs = RelationLength(r, 1); length_rhs = RelationLength(r, 0); if( (length_lhs + length_rhs) == 0 ) { for( v = 0; v < num_var; v++ ) coefficient[v] = 0.0; *residual = 0.0; return 0; } else { stack_height = 1 + MAX(length_lhs,length_rhs); } //creating stacks stacks = tmpalloc_array(((num_var+1)*stack_height),double); if( stacks == NULL ) return 1; #define res_stack(s) stacks[(s)] #define coeff_stack(v,s) stacks[((v)*stack_height)+(s)] lhs = 1; t = 0; while(1) { if( lhs && (t >= length_lhs) ) { if( length_rhs ) { lhs = t = 0; } else { for( v = 1; v <= num_var; v++ ) coefficient[v-1] = coeff_stack(v,s); return 0; } } else if( (!lhs) && (t >= length_rhs) ) { if( length_lhs ) { for( v = 1; v <= num_var; v++ ) { coefficient[v-1] = coeff_stack(v,s-1) - coeff_stack(v,s); } return 0; } else { for( v = 1; v <= num_var; v++ ) { coefficient[v-1] = -coeff_stack(v,s); } return 0; } } //more to write }