/[ascend]/trunk/base/generic/compiler/relation_util.c
ViewVC logotype

Diff of /trunk/base/generic/compiler/relation_util.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 978 by johnpye, Fri Oct 27 07:18:21 2006 UTC revision 979 by johnpye, Wed Dec 20 14:34:16 2006 UTC
# Line 710  RelationEvaluatePostfixBranch(CONST stru Line 710  RelationEvaluatePostfixBranch(CONST stru
710      (*pos)--;      (*pos)--;
711      return FuncEval(funcptr, RelationEvaluatePostfixBranch(r, pos, lhs));      return FuncEval(funcptr, RelationEvaluatePostfixBranch(r, pos, lhs));
712    default:    default:
713      Asc_Panic(2, NULL,      ASC_PANIC(
714                "Don't know this type of relation type\n"                "Don't know this type of relation type\n"
715                "in function RelationEvaluatePostfixBranch\n");                "in function RelationEvaluatePostfixBranch\n");
716    
# Line 765  RelationEvaluatePostfixBranchSafe(CONST Line 765  RelationEvaluatePostfixBranchSafe(CONST
765    case e_func:    case e_func:
766      (*pos)--; return FuncEvalSafe(TermFunc(term),RECURSE(r,pos,lhs,serr),serr);      (*pos)--; return FuncEvalSafe(TermFunc(term),RECURSE(r,pos,lhs,serr),serr);
767    default:    default:
768      Asc_Panic(2, __FUNCTION__,"Unknown relation term type");      ASC_PANIC("Unknown relation term type");
769    }    }
770  }  }
771  #undef RECURSE  #undef RECURSE
# Line 859  RelationEvaluateResidualPostfix(CONST st Line 859  RelationEvaluateResidualPostfix(CONST st
859        res_stack[s] = FuncEval(funcptr, res_stack[s]);        res_stack[s] = FuncEval(funcptr, res_stack[s]);
860        break;        break;
861      default:      default:
862        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
863      }      }
864    }    }
865  }  }
# Line 869  RelationEvaluateResidualPostfix(CONST st Line 869  RelationEvaluateResidualPostfix(CONST st
869  */  */
870    
871  /**  /**
872      This function evaluates the residual and the gradient for the relation      Compute residuals and gradients for a relation (compiler-side routine)
873      r.  The calling function must provide a pointer to a double for the  
874      residual and an array of doubles for the gradients.  This function      @param r relation for which residual and gradients are to be calculated.
875      assumes r exists and that the pointers to the residual and gradients      @param residual pointer to a double in which the residual shall be stored (must have already been allocated)
876      are not NULL.  This function returns 0 is everythings goes o.k., and      @param gradient pointer to an array of doubles where the gradients can be stored (must already have been allocated)
877      1 otherwise (out of memory).  The function computes the gradients by      
878      maintaining a n stacks, where n = (number-of-variables-in-r + 1)      @return 0 on success, 1 on out-of-memeory.
879    
880        Computes the gradients by maintaining n stacks, where
881            n = (number-of-variables-in-r + 1)
882      The +1 is for the residual.  The stacks come from a single array which      The +1 is for the residual.  The stacks come from a single array which
883      this function gets by calling tmpalloc_array.  Two macros are defined      this function gets by calling tmpalloc_array.  Two macros are defined
884      to make referencing this array easier.      to make referencing this array easier.
# Line 902  RelationEvaluateResidualGradient(CONST s Line 905  RelationEvaluateResidualGradient(CONST s
905    length_rhs = RelationLength(r, 0);    length_rhs = RelationLength(r, 0);
906    if( (length_lhs + length_rhs) == 0 ) {    if( (length_lhs + length_rhs) == 0 ) {
907      for( v = 0; v < num_var; v++ ) gradient[v] = 0.0;      for( v = 0; v < num_var; v++ ) gradient[v] = 0.0;
908        ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Relation with no LHS and no RHS: returning residual 0");
909      *residual = 0.0;      *residual = 0.0;
910      return 0;      return 0;
911    }    }
# Line 1068  RelationEvaluateResidualGradient(CONST s Line 1072  RelationEvaluateResidualGradient(CONST s
1072        res_stack(s) = FuncEval( fxnptr, res_stack(s) );        res_stack(s) = FuncEval( fxnptr, res_stack(s) );
1073        break;        break;
1074      default:      default:
1075        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
1076        break;        break;
1077      }      }
1078    }    }
# Line 1268  RelationEvaluateResidualGradientSafe(CON Line 1272  RelationEvaluateResidualGradientSafe(CON
1272        res_stack(s) = FuncEvalSafe( fxnptr, res_stack(s), serr);        res_stack(s) = FuncEvalSafe( fxnptr, res_stack(s), serr);
1273        break;        break;
1274      default:      default:
1275        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
1276      }      }
1277    }    }
1278  #undef grad_stack  #undef grad_stack
# Line 1430  RelationEvaluateDerivative(CONST struct Line 1434  RelationEvaluateDerivative(CONST struct
1434        res_stack(s) = FuncEval( fxnptr, res_stack(s) );        res_stack(s) = FuncEval( fxnptr, res_stack(s) );
1435        break;        break;
1436      default:      default:
1437        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
1438        break;        break;
1439      }      }
1440    }    }
# Line 1589  RelationEvaluateDerivativeSafe(CONST str Line 1593  RelationEvaluateDerivativeSafe(CONST str
1593        res_stack(s) = FuncEvalSafe( fxnptr, res_stack(s), serr);        res_stack(s) = FuncEvalSafe( fxnptr, res_stack(s), serr);
1594        break;        break;
1595      default:      default:
1596        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
1597      }      }
1598    }    }
1599  #undef grad_stack  #undef grad_stack
# Line 1896  static void CalcDepth(CONST struct relat Line 1900  static void CalcDepth(CONST struct relat
1900        (*depth)--;        (*depth)--;
1901        break;        break;
1902      default:      default:
1903        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
1904      }      }
1905    }    }
1906  }  }
# Line 1920  unsigned long RelationDepth(CONST struct Line 1924  unsigned long RelationDepth(CONST struct
1924      assert(depth == 1);      assert(depth == 1);
1925      break;      break;
1926    default:    default:
1927        Asc_Panic(2, __FUNCTION__,"Unknown relation term type");        ASC_PANIC("Unknown relation term type");
1928    }    }
1929    return maxdepth;    return maxdepth;
1930  }  }
# Line 2156  RelationCalcResidualPostfixSafe(struct I Line 2160  RelationCalcResidualPostfixSafe(struct I
2160              if(reltype >= TOK_REL_TYPE_LOW && reltype <= TOK_REL_TYPE_HIGH){              if(reltype >= TOK_REL_TYPE_LOW && reltype <= TOK_REL_TYPE_HIGH){
2161                  status = safe_problem;                  status = safe_problem;
2162              }else{              }else{
2163                  Asc_Panic(2, __FUNCTION__, "reached end of routine!");                  ASC_PANIC( "reached end of routine!");
2164              }              }
2165      }      }
2166      return status;      return status;
# Line 2215  RelationCalcResidualPostfix(struct Insta Line 2219  RelationCalcResidualPostfix(struct Insta
2219    
2220      return 1;      return 1;
2221    }else{    }else{
2222      Asc_Panic(2, __FUNCTION__,"reached end of routine");      ASC_PANIC("reached end of routine");
2223    }    }
2224  }  }
2225    
# Line 2273  int RelationCalcExceptionsInfix(struct I Line 2277  int RelationCalcExceptionsInfix(struct I
2277      glob_rel = NULL;      glob_rel = NULL;
2278      return -1;      return -1;
2279    }else{    }else{
2280      Asc_Panic(2, __FUNCTION__,"reached end of routine");      ASC_PANIC("reached end of routine");
2281    }    }
2282  }  }
2283    
# Line 2305  int RelationCalcResidualInfix(struct Ins Line 2309  int RelationCalcResidualInfix(struct Ins
2309      glob_rel = NULL;      glob_rel = NULL;
2310      return 1;      return 1;
2311    }else{    }else{
2312      Asc_Panic(2, __FUNCTION__,"reached end of routine");      ASC_PANIC("reached end of routine");
2313    }    }
2314  }  }
2315    
# Line 2333  RelationCalcResidualPostfix2(struct Inst Line 2337  RelationCalcResidualPostfix2(struct Inst
2337      ERROR_REPORTER_HERE(ASC_PROG_ERR,"reltype not implemented (%s)",__FUNCTION__);      ERROR_REPORTER_HERE(ASC_PROG_ERR,"reltype not implemented (%s)",__FUNCTION__);
2338      return 1;      return 1;
2339    }else{    }else{
2340      Asc_Panic(2, __FUNCTION__,"reached end of routine");      ASC_PANIC("reached end of routine");
2341    }    }
2342  }  }
2343    
# Line 2351  RelationCalcGradient(struct Instance *r, Line 2355  RelationCalcGradient(struct Instance *r,
2355  /*  /*
2356      simply call the version that calculates the gradient and the residual,      simply call the version that calculates the gradient and the residual,
2357      then ignore the residual      then ignore the residual
2358        
2359        return 0 on success (as 'safe_ok' enum)
2360  */  */
2361  enum safe_err  enum safe_err
2362  RelationCalcGradientSafe(struct Instance *r, double *grad){  RelationCalcGradientSafe(struct Instance *r, double *grad){
# Line 2359  RelationCalcGradientSafe(struct Instance Line 2365  RelationCalcGradientSafe(struct Instance
2365    return RelationCalcResidGradSafe(r, &residual, grad);    return RelationCalcResidGradSafe(r, &residual, grad);
2366  }  }
2367    
2368    /* return 0 on success, 1 on error */
2369  int  int
2370  RelationCalcResidGrad(struct Instance *i, double *residual, double *gradient){  RelationCalcResidGrad(struct Instance *i, double *residual, double *gradient){
2371    struct relation *r;    struct relation *r;
# Line 2382  RelationCalcResidGrad(struct Instance *i Line 2388  RelationCalcResidGrad(struct Instance *i
2388      return 1;      return 1;
2389    
2390    }else{    }else{
2391      Asc_Panic(2, __FUNCTION__, "reached end of routine");      ASC_PANIC( "reached end of routine");
2392    }    }
2393  }  }
2394    
# Line 2441  RelationCalcResidGradSafe(struct Instanc Line 2447  RelationCalcResidGradSafe(struct Instanc
2447      return not_safe;      return not_safe;
2448    }    }
2449    else {    else {
2450      Asc_Panic(2, __FUNCTION__, "reached end of routine");      ASC_PANIC( "reached end of routine");
2451    }    }
2452  }  }
2453    
# Line 2481  RelationCalcDerivative(struct Instance * Line 2487  RelationCalcDerivative(struct Instance *
2487      return 1;      return 1;
2488    }    }
2489    else {    else {
2490      Asc_Panic(2, __FUNCTION__, "reached end of routine");      ASC_PANIC( "reached end of routine");
2491    }    }
2492  }  }
2493    
# Line 2522  RelationCalcDerivativeSafe(struct Instan Line 2528  RelationCalcDerivativeSafe(struct Instan
2528      return not_safe;      return not_safe;
2529    }    }
2530    else {    else {
2531      Asc_Panic(2, __FUNCTION__, "reached end of routine");      ASC_PANIC( "reached end of routine");
2532    
2533    }    }
2534  }  }
# Line 3028  static int RelationTmpCopySide(union Rel Line 3034  static int RelationTmpCopySide(union Rel
3034      case e_equal: case e_notequal: case e_less:      case e_equal: case e_notequal: case e_less:
3035      case e_greater: case e_lesseq: case e_greatereq:      case e_greater: case e_lesseq: case e_greatereq:
3036      default:      default:
3037        Asc_Panic(2, NULL, "Unknown term type in RelationSide\n");        ASC_PANIC("Unknown term type in RelationSide");
3038        break;        break;
3039      }      }
3040    }    }
# Line 3230  static int SearchEval_Branch(struct rela Line 3236  static int SearchEval_Branch(struct rela
3236      }      }
3237      return 1;      return 1;
3238   default:   default:
3239     Asc_Panic(2, NULL,     ASC_PANIC(
3240               "error in SearchEval_Branch routine\n"               "error in SearchEval_Branch routine\n"
3241               "relation term type not recognized\n");               "relation term type not recognized\n");
3242      return 1;      return 1;
# Line 3621  int RelationInvertToken(struct relation_ Line 3627  int RelationInvertToken(struct relation_
3627      case e_real:      case e_real:
3628      case e_zero:      case e_zero:
3629      case e_int:      case e_int:
3630        Asc_Panic(2,__FUNCTION__,"Unexpected error with real/zero/int type");        ASC_PANIC("Unexpected error with real/zero/int type");
3631        break;        break;
3632      case e_var:      case e_var:
3633        ++glob_done;        ++glob_done;
# Line 3633  int RelationInvertToken(struct relation_ Line 3639  int RelationInvertToken(struct relation_
3639      case e_equal: case e_notequal: case e_less:      case e_equal: case e_notequal: case e_less:
3640      case e_greater: case e_lesseq: case e_greatereq:      case e_greater: case e_lesseq: case e_greatereq:
3641      default:      default:
3642        Asc_Panic(2, NULL, "Unknown term type in RelationInvertToken\n");        ASC_PANIC("Unknown term type in RelationInvertToken\n");
3643        break;        break;
3644      }      }
3645    }    }
# Line 3974  CollectTokenRelationsWithUniqueBINlessSh Line 3980  CollectTokenRelationsWithUniqueBINlessSh
3980  static int  relutil_check_inst_and_res(struct Instance *i, double *res){  static int  relutil_check_inst_and_res(struct Instance *i, double *res){
3981  # ifdef RELUTIL_CHECK_ABORT  # ifdef RELUTIL_CHECK_ABORT
3982      if(i==NULL){      if(i==NULL){
3983          Asc_Panic(2,__FUNCTION__,"NULL instance");          ASC_PANIC("NULL instance");
3984      }else if (res==NULL){      }else if (res==NULL){
3985          Asc_Panic(2,__FUNCTION__,"NULL residual pointer");          ASC_PANIC("NULL residual pointer");
3986      }else if(InstanceKind(i)!=REL_INST){      }else if(InstanceKind(i)!=REL_INST){
3987          Asc_Panic(2,__FUNCTION__,"Not a relation");          ASC_PANIC("Not a relation");
3988      }      }
3989  # else  # else
3990    if( i == NULL ) {    if( i == NULL ) {

Legend:
Removed from v.978  
changed lines
  Added in v.979

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22