User:Karthik0112358/Coefficient Calculator

From ASCEND
Jump to navigation Jump to search
#define INTEG_OTHER_VAR -1L
#define INTEG_ALGEBRAIC_VAR 0L
#define INTEG_STATE_VAR 1L
int Coefficient_Calculator(struct Instance *i,double *coefficient){
   struct relation *r;
   enum Expr_enum reltype;
   int dummy_int;
   long index, type;
   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;
         }
      }
      term = NewRelationTerm(r, t++, lhs);
// Here I need something to take data from relation_term structure's term and pass it to Classify_Var() which accepts var_variable structure.
// Let me assume its done. Let me call it info.
      type = Classify_Var(info,&index);
      if(type==INTEG_ALGEBRAIC_VAR){
// It is an algebraic variable (solver variable)
      }else if(type==INTEG_OTHER_VAR){
// It is an independent variable
      }else if(type>=INTEG_STATE_VAR){
         if(type == 1){
// It is one of the states.One of y_i
         }else if(type == 2){ 
// Detects it as first order derivative. Write suitable code for determining coefficient here.        
         }else{
// Error message for higher order derivatives. 
         }   
      }
}