home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / misc / math / MCalc17.lha / MCalc / Source / Parser.y < prev    next >
Text File  |  1995-11-29  |  21KB  |  682 lines

  1. %{
  2. /*
  3. Auto:        smake MCalc
  4. */
  5.  
  6. static void    __yy_bcopy(char *from, char *to, int count);
  7.  
  8. #undef        alloca
  9. #define        alloca(x) AllocVecPool(ParsePool, x)
  10. #undef        malloc
  11. #define        malloc(x) AllocVecPool(ParsePool, x)
  12. #undef        free
  13. #define        free(x) FreeVecPool(ParsePool, x)
  14.  
  15. #undef        YYINITDEPTH
  16. #define        YYINITDEPTH 512
  17.  
  18. #undef        _STDC_
  19. #define        _STDC_
  20.  
  21. #undef        error
  22. #define        error
  23.  
  24. extern        APTR        ParsePool;
  25. extern        double        Value;
  26. extern        double        IMem,
  27.                 JMem,
  28.                 KMem,
  29.                 LMem,
  30.                 MMem,
  31.                 NMem,
  32.                 OMem,
  33.                 PMem,
  34.                 QMem,
  35.                 RMem,
  36.                 SMem,
  37.                 TMem,
  38.                 UMem,
  39.                 VMem,
  40.                 WMem,
  41.                 XMem,
  42.                 YMem,
  43.                 ZMem;
  44. extern        struct    List    StandardList;
  45. extern        struct    List    LinearList;
  46.  
  47. #define        outputr(x)    {Value = x;}
  48. #define        outputmem(x, y)    {Value = *(x) = y;}
  49. #define        errout(x)    ;
  50. #define        erroutm(x, y)    ;
  51.  
  52.  
  53. /**********************************************************************/
  54. /*                    Chars read and read-position                    */
  55. /**********************************************************************/
  56. extern    UWORD    PColumn;
  57. extern    UWORD    PCharRead;
  58.  
  59.  
  60.     // This is for the wanted angle
  61.  
  62. extern    UWORD        IntAngle;
  63.  
  64.  
  65. /**********************************************************************/
  66. /*                            Error-Value                             */
  67. /**********************************************************************/
  68. UWORD        PError;
  69. %}
  70.  
  71.  
  72.  
  73. /**********************************************************************/
  74. /*                       Possible Return-Value                        */
  75. /**********************************************************************/
  76. %union
  77. {
  78.     double    Real;
  79.     double    *MemPtr;
  80. }
  81.  
  82.  
  83. /**********************************************************************/
  84. /*                      Token which returns Real                      */
  85. /**********************************************************************/
  86. %token <Real>     INT_CONSTANT
  87.  
  88.  
  89. /**********************************************************************/
  90. /*                       Non-Priority functions                       */
  91. /**********************************************************************/
  92. %token EQU_OP OPEN_OP CLOSE_OP COMMA SEMICOLON
  93.  
  94.  
  95.  
  96. /**********************************************************************/
  97. /*                           Set priorities                           */
  98. /**********************************************************************/
  99. %left AND_OP OR_OP XOR_OP ASL ASR LSL LSR ROL ROR
  100. %left ADD_OP SUB_OP
  101. %left MUL_OP DIV_OP MOD_OP
  102. %left NEG_OP NOT_OP PERCENT CHPERCENT TPERCENT
  103. %left POW
  104. %nonassoc MY_ABS COS SIN TAN ACOS ASIN ATAN EXP LOG LOG10 SQRT SINH COSH TANH FAK COT N_OVER_K YROOT REZ SD_INIT SD_AVERAGE SD_DEVIATION1 SD_DEVIATION2 SD_QSUM SD_SUM SD_NUM LR_INIT LR_XAVERAGE LR_YAVERAGE LR_XDEVIATION1 LR_YDEVIATION1 LR_XDEVIATION2 LR_YDEVIATION2 LR_XQSUM LR_YQSUM LR_XSUM LR_YSUM LR_XNUM LR_YNUM LR_XYSUM LR_ALPHA LR_BETA LR_ASSESSX LR_ASSESSY LR_CORR LR_CRITCORR LR_COVAR EXG_OP
  105. %nonassoc I_MEM J_MEM K_MEM L_MEM M_MEM N_MEM O_MEM P_MEM Q_MEM R_MEM S_MEM T_MEM U_MEM V_MEM W_MEM X_MEM Y_MEM Z_MEM
  106.  
  107.  
  108.  
  109. /**********************************************************************/
  110. /*                    Return-Value of my terminal                     */
  111. /**********************************************************************/
  112. %type <Real>   int_expr
  113. %type <Real>   standard_deviation
  114. %type <Real>   linear_regression
  115. %type <MemPtr> memory_name
  116. %%
  117.  
  118.  
  119.  
  120. /**********************************************************************/
  121. /*                          Main "sentence"                           */
  122. /**********************************************************************/
  123. eingabe
  124.     : int_expr                    {outputr($1);}
  125.     | standard_deviation                {UpdateHistoryWindow(FALSE); outputr(0.0);}
  126.     | linear_regression                {UpdateHistoryWindow(TRUE); outputr(0.0);}
  127.     | memory_name EQU_OP int_expr            {outputmem($1, $3);}
  128.     | error
  129.     ;
  130.  
  131.  
  132.  
  133.  
  134. /**********************************************************************/
  135. /*                           Do the grammar                           */
  136. /**********************************************************************/
  137. int_expr
  138.     : OPEN_OP int_expr CLOSE_OP            {$$ = $2;}
  139.     | int_expr AND_OP int_expr            {$$ = (make_ulong($1)) & (make_ulong($3));}
  140.     | int_expr OR_OP int_expr            {$$ = (make_ulong($1)) | (make_ulong($3));}
  141.     | int_expr XOR_OP int_expr            {$$ = MyXOR(make_ulong($1),make_ulong($3));}
  142.     | int_expr NOT_OP AND_OP int_expr        {$$ = MyNAND(make_ulong($1), make_ulong($4));}
  143.     | int_expr NOT_OP OR_OP int_expr        {$$ = MyNOR(make_ulong($1), make_ulong($4));}
  144.     | int_expr NOT_OP XOR_OP int_expr        {$$ = MyNXOR(make_ulong($1), make_ulong($4));}
  145.     | int_expr ADD_OP int_expr PERCENT        {$$ = ($1 + (($1 * $3) / 100));}
  146.     | int_expr SUB_OP int_expr PERCENT        {$$ = ($1 - (($1 * $3) / 100));}
  147.     | int_expr MUL_OP int_expr PERCENT        {$$ = (($1 * $3) / 100);}
  148.     | int_expr CHPERCENT int_expr            {$$ = ((($3 - $1) * 100) / $1);}
  149.     | int_expr TPERCENT int_expr            {$$ = (($3 * 100) / $1);}
  150.     | int_expr ADD_OP int_expr            {$$ = $1 + $3;}
  151.     | int_expr SUB_OP int_expr            {$$ = $1 - $3;}
  152.     | int_expr MUL_OP int_expr            {$$ = $1 * $3;}
  153.     | int_expr DIV_OP int_expr            {if($3 == 0.0) {PError = ERR_DIVBY0; yyerror(NULL); } else $$ = $1 / $3;}
  154.     | int_expr MOD_OP int_expr            {if($3 == 0.0) {PError = ERR_DIVBY0; yyerror(NULL); } else $$ = fmod($1,$3);}
  155.     | SUB_OP int_expr %prec NEG_OP            {$$ = -$2; }
  156.     | NOT_OP int_expr                {$$ = (~make_ulong($2)); }
  157.     | int_expr ASL int_expr                {$$ = (double)MyASL(make_ulong($1),make_ulong($3)); }
  158.     | int_expr ASR int_expr                {$$ = (double)MyASR(make_ulong($1),make_ulong($3)); }
  159.     | int_expr LSL int_expr                {$$ = (double)MyLSL(make_ulong($1),make_ulong($3)); }
  160.     | int_expr LSR int_expr                {$$ = (double)MyLSR(make_ulong($1),make_ulong($3)); }
  161.     | int_expr ROL int_expr                {$$ = (double)MyROL(make_ulong($1),make_ulong($3)); }
  162.     | int_expr ROR int_expr                {$$ = (double)MyROR(make_ulong($1),make_ulong($3)); }
  163.     | int_expr POW int_expr                {$$ = pow($1,$3); }
  164.     | SIN int_expr                    {$$ = sin(calc_angle($2)); }
  165.     | COS int_expr                    {$$ = cos(calc_angle($2)); }
  166.     | TAN int_expr                    {$$ = tan(calc_angle($2)); }
  167.     | ASIN int_expr                    {$$ = asin(calc_angle($2)); }
  168.     | ACOS int_expr                    {$$ = acos(calc_angle($2)); }
  169.     | ATAN int_expr                    {$$ = atan(calc_angle($2)); }
  170.     | COT int_expr                    {$$ = cot(calc_angle($2)); }
  171.     | SINH int_expr                    {$$ = sinh(calc_angle($2)); }
  172.     | COSH int_expr                    {$$ = cosh(calc_angle($2)); }
  173.     | TANH int_expr                    {$$ = tanh(calc_angle($2)); }
  174.     | EXP int_expr                    {$$ = exp($2); }
  175.     | LOG int_expr                    {$$ = log($2); }
  176.     | LOG10 int_expr                {$$ = log10($2); }
  177.     | MY_ABS int_expr                {$$ = fabs($2); }
  178.     | SQRT int_expr                    {if($2 < 0.0) {PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = sqrt($2);}
  179.     | int_expr N_OVER_K int_expr            {if($1 < 0.0 || ($1 - $3) < 0.0 || $1 > 170.0 || ($1 - $3) > 170.0 || $3 < 0.0 || $3 > 170.0) {PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = (calc_fak($1) / (calc_fak($3) * calc_fak($1 - $3)));}
  180.     | int_expr YROOT int_expr            {if($1 < 0.0) {PError = ERR_DIVBY0; yyerror(NULL);} else $$ = pow($3, (1/$1));}
  181.     | REZ int_expr                    {if($2 < 0.0) {PError = ERR_DIVBY0; yyerror(NULL);} else $$ = (1.0 / $2);}
  182.     | FAK int_expr                    {if($2 > 170.0) { PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = calc_fak($2);}
  183.     | int_expr FAK                    {if($1 > 170.0) { PError = ERR_OVERFLOW; yyerror(NULL); } else $$ = calc_fak($1);}
  184.     | SD_AVERAGE                    {if(numbers_in_list(&StandardList) < 1) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_average(&StandardList, FALSE);}}
  185.     | SD_DEVIATION1                    {if(numbers_in_list(&StandardList) < 1) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_population(&StandardList, FALSE);}}
  186.     | SD_DEVIATION2                    {if(numbers_in_list(&StandardList) < 2) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_sample(&StandardList, FALSE);}}
  187.     | SD_QSUM                    {$$ = calc_powsum(&StandardList, FALSE);}
  188.     | SD_SUM                    {$$ = calc_sum(&StandardList, FALSE);}
  189.     | SD_NUM                    {$$ = (double)numbers_in_list(&StandardList);}
  190.     | LR_XAVERAGE                    {if(numbers_in_list(&LinearList) < 1) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_average(&LinearList, FALSE);}}
  191.     | LR_XDEVIATION1                {if(numbers_in_list(&LinearList) < 1) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_population(&LinearList, FALSE);}}
  192.     | LR_XDEVIATION2                {if(numbers_in_list(&LinearList) < 2) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_sample(&LinearList, FALSE);}}
  193.     | LR_XQSUM                    {$$ = calc_powsum(&LinearList, FALSE);}
  194.     | LR_XSUM                    {$$ = calc_sum(&LinearList, FALSE);}
  195.     | LR_XNUM                    {$$ = (double)numbers_in_list(&LinearList);}
  196.     | LR_YAVERAGE                    {if(numbers_in_list(&LinearList) < 1) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_average(&LinearList, TRUE);}}
  197.     | LR_YDEVIATION1                {if(numbers_in_list(&LinearList) < 1) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_population(&LinearList, TRUE);}}
  198.     | LR_YDEVIATION2                {if(numbers_in_list(&LinearList) < 2) {PError = ERR_UNDERFLOW; yyerror(NULL);} else {$$ = calc_sample(&LinearList, TRUE);}}
  199.     | LR_YQSUM                    {$$ = calc_powsum(&LinearList, TRUE);}
  200.     | LR_YSUM                    {$$ = calc_sum(&LinearList, TRUE);}
  201.     | LR_YNUM                    {$$ = (double)numbers_in_list(&LinearList);}
  202.     | LR_XYSUM                    {$$ = calc_xysum();}
  203.     | LR_ALPHA                    {$$ = calc_lralpha(); if(PError) yyerror(NULL);}
  204.     | LR_BETA                    {$$ = calc_lrbeta(); if(PError) yyerror(NULL);}
  205.     | LR_ASSESSX int_expr                {$$ = calc_lrassessx($2); if(PError) yyerror(NULL);}
  206.     | LR_ASSESSY int_expr                {$$ = calc_lrassessy($2); if(PError) yyerror(NULL);}
  207.     | LR_CORR                    {$$ = calc_lrcorr(); if(PError) yyerror(NULL);}
  208.     | LR_CRITCORR                    {$$ = pow(calc_lrcorr(), 2.0); if(PError) yyerror(NULL);}
  209.     | LR_COVAR                    {$$ = calc_lrcovar(); if(PError) yyerror(NULL);}
  210.     | memory_name                    {$<Real>$ = (*($1));}
  211.     | INT_CONSTANT
  212.     | EXG_OP OPEN_OP memory_name COMMA memory_name CLOSE_OP    {double Spare; $$ = 0.0; Spare = *($3); *($3) = *($5); *($5) = Spare;}
  213.     ;
  214.  
  215.  
  216. standard_deviation
  217.     : SD_INIT OPEN_OP standard_input CLOSE_OP    {$$ = 0.0;}
  218.  
  219. standard_input
  220.     : int_expr                    {if(!add_standard_value($1)) { PError = ERR_NOMEM; yyerror(NULL); }}
  221.     | int_expr COMMA standard_input            {if(!add_standard_value($1)) { PError = ERR_NOMEM; yyerror(NULL); }}
  222.     ;
  223.  
  224.  
  225. linear_regression
  226.     : LR_INIT OPEN_OP linear_input CLOSE_OP            {$$ = 0.0;}
  227.  
  228. linear_input
  229.     : int_expr SEMICOLON int_expr                {if(!add_linear_value($1, $3)) { PError = ERR_NOMEM; yyerror(NULL); }}
  230.     | int_expr SEMICOLON int_expr COMMA linear_input    {if(!add_linear_value($1, $3)) { PError = ERR_NOMEM; yyerror(NULL); }}
  231.     ;
  232.  
  233. memory_name
  234.     : I_MEM                        {$$ = &IMem;}
  235.     | J_MEM                        {$$ = &JMem;}
  236.     | K_MEM                        {$$ = &KMem;}
  237.     | L_MEM                        {$$ = &LMem;}
  238.     | M_MEM                        {$$ = &MMem;}
  239.     | N_MEM                        {$$ = &NMem;}
  240.     | O_MEM                        {$$ = &OMem;}
  241.     | P_MEM                        {$$ = &PMem;}
  242.     | Q_MEM                        {$$ = &QMem;}
  243.     | R_MEM                        {$$ = &RMem;}
  244.     | S_MEM                        {$$ = &SMem;}
  245.     | T_MEM                        {$$ = &TMem;}
  246.     | U_MEM                        {$$ = &UMem;}
  247.     | V_MEM                        {$$ = &VMem;}
  248.     | W_MEM                        {$$ = &WMem;}
  249.     | X_MEM                        {$$ = &XMem;}
  250.     | Y_MEM                        {$$ = &YMem;}
  251.     | Z_MEM                        {$$ = &ZMem;}
  252.     ;
  253.  
  254. %%
  255.  
  256.  
  257.  
  258.  
  259. /**********************************************************************/
  260. /*             Convert a double to a long (the hard way)              */
  261. /**********************************************************************/
  262. ULONG make_ulong(double OldVal)
  263. {
  264.     char    Buffer[26];
  265.     ULONG    Dummy;
  266.  
  267.     sprintf(Buffer, "%f", OldVal);
  268.     stcd_l(Buffer, (LONG *)&Dummy);
  269.     return(Dummy);
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276. /**********************************************************************/
  277. /*                           Calculate Fak                            */
  278. /**********************************************************************/
  279. double calc_fak(double Fak)
  280. {
  281.     double    RetVal = 1.0, i = 1.0;
  282.  
  283.     while(i <= Fak)
  284.     {
  285.         RetVal *= i;
  286.         i++;
  287.     }
  288.  
  289.     return(RetVal);
  290. }
  291.  
  292.  
  293.  
  294.  
  295.  
  296. /**********************************************************************/
  297. /*              Calculate correct angle value (rad, deg, gra)         */
  298. /**********************************************************************/
  299. double calc_angle(double Value)
  300. {
  301.     switch(IntAngle)
  302.     {
  303.         case ID_GRAD :
  304.         {
  305.             Value = (Value * (9.0 / 10.0));
  306.         }
  307.         case ID_DEG :
  308.         {
  309.             Value = (Value / 180.0) * Pi;
  310.             break;
  311.         }
  312.     }
  313.  
  314.     return(Value);
  315. }
  316.  
  317.  
  318.  
  319. /**********************************************************************/
  320. /*                          Error-Routine ;)                          */
  321. /**********************************************************************/
  322. int yyerror(char *s)
  323. {
  324.     return(0);
  325. }
  326.  
  327.  
  328.  
  329.  
  330. /**********************************************************************/
  331. /*            Add a value to the standard regression array            */
  332. /**********************************************************************/
  333. BOOL add_standard_value(double Value)
  334. {
  335.     struct    DoubleNode    *NewNode;
  336.  
  337.     if((NewNode = AllocVec(sizeof(struct DoubleNode), MEMF_CLEAR)))
  338.     {
  339.         AddHead(&StandardList, (struct Node *)NewNode);
  340.  
  341.         NewNode->ValueX        = Value;
  342.  
  343.         return(TRUE);
  344.     }
  345.     else
  346.         return(FALSE);
  347. }
  348.  
  349.  
  350.  
  351.  
  352. /**********************************************************************/
  353. /*               Add a value to linear regression array               */
  354. /**********************************************************************/
  355. BOOL add_linear_value(double ValueX, double ValueY)
  356. {
  357.     struct    DoubleNode    *NewNode;
  358.  
  359.     if((NewNode = AllocVec(sizeof(struct DoubleNode), MEMF_CLEAR)))
  360.     {
  361.         AddHead(&LinearList, (struct Node *)NewNode);
  362.  
  363.         NewNode->ValueX        = ValueX;
  364.         NewNode->ValueY        = ValueY;
  365.  
  366.         return(TRUE);
  367.     }
  368.     else
  369.         return(FALSE);
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376. /**********************************************************************/
  377. /*               Return the number of entries in a list               */
  378. /**********************************************************************/
  379. int numbers_in_list(struct List *Lst)
  380. {
  381.     struct    Node    *CheckNode    = Lst->lh_Head;
  382.     int        Number        = 0;
  383.  
  384.     while(CheckNode->ln_Succ)
  385.     {
  386.         Number++;
  387.         CheckNode    = CheckNode->ln_Succ;
  388.     }
  389.  
  390.     return(Number);
  391. }
  392.  
  393.  
  394.  
  395.  
  396.  
  397. /**********************************************************************/
  398. /*                       Calc average of a list                       */
  399. /**********************************************************************/
  400. double calc_average(struct List *Lst, BOOL LinearValueY)
  401. {
  402.     struct    DoubleNode    *CheckNode    = (struct DoubleNode *)Lst->lh_Head;
  403.     int            Number        = numbers_in_list(Lst);
  404.     double            Sum        = 0;
  405.  
  406.     if(!Number)
  407.         return(0.0);
  408.  
  409.     while(CheckNode->Link.ln_Succ)
  410.     {
  411.         if(!LinearValueY)
  412.             Sum        += CheckNode->ValueX;
  413.         else
  414.             Sum        += CheckNode->ValueY;
  415.  
  416.         CheckNode        = (struct DoubleNode *)CheckNode->Link.ln_Succ;
  417.     }
  418.  
  419.     return((Sum / (double)Number));
  420. }
  421.  
  422.  
  423.  
  424.  
  425. /**********************************************************************/
  426. /*                  Calc std-deviation of population                  */
  427. /**********************************************************************/
  428. double calc_population(struct List *Lst, BOOL LinearValueY)
  429. {
  430.     struct    DoubleNode    *CheckNode    = (struct DoubleNode *)Lst->lh_Head;
  431.     int            Number        = numbers_in_list(Lst);
  432.     double            Poss,
  433.                 Pop        = 0,
  434.                 Ave        = calc_average(Lst, LinearValueY),
  435.                 Value;
  436.  
  437.     Poss        = 1.0 / (double)Number;
  438.     while(CheckNode->Link.ln_Succ)
  439.     {
  440.         if(!LinearValueY)
  441.             Value    = CheckNode->ValueX;
  442.         else
  443.             Value    = CheckNode->ValueY;
  444.  
  445.         Pop        += (pow((Value - Ave), 2.0) * Poss);
  446.  
  447.         CheckNode        = (struct DoubleNode *)CheckNode->Link.ln_Succ;
  448.     }
  449.  
  450.     return(sqrt(Pop));
  451. }
  452.  
  453.  
  454.  
  455. /**********************************************************************/
  456. /*                    Calc std-deviation of sample                    */
  457. /**********************************************************************/
  458. double calc_sample(struct List *Lst, BOOL LinearValueY)
  459. {
  460.     struct    DoubleNode    *CheckNode    = (struct DoubleNode *)Lst->lh_Head;
  461.     int            Number        = numbers_in_list(Lst);
  462.     double            Poss,
  463.                 Pop        = 0,
  464.                 Ave        = calc_average(Lst, LinearValueY),
  465.                 Value;
  466.  
  467.     Poss        = 1.0 / ((double)Number - 1);
  468.     while(CheckNode->Link.ln_Succ)
  469.     {
  470.         if(!LinearValueY)
  471.             Value    = CheckNode->ValueX;
  472.         else
  473.             Value    = CheckNode->ValueY;
  474.  
  475.         Pop        += (pow((Value - Ave), 2.0) * Poss);
  476.  
  477.         CheckNode        = (struct DoubleNode *)CheckNode->Link.ln_Succ;
  478.     }
  479.  
  480.     return(sqrt(Pop));
  481. }
  482.  
  483.  
  484.  
  485. /**********************************************************************/
  486. /*          Calc sum of powers of two of entries within list          */
  487. /**********************************************************************/
  488. double calc_powsum(struct List *Lst, BOOL LinearValueY)
  489. {
  490.     struct    DoubleNode    *CheckNode    = (struct DoubleNode *)Lst->lh_Head;
  491.     double            Number,
  492.                 Value        = 0;
  493.  
  494.     while(CheckNode->Link.ln_Succ)
  495.     {
  496.         if(!LinearValueY)
  497.             Number    = CheckNode->ValueX;
  498.         else
  499.             Number    = CheckNode->ValueY;
  500.  
  501.         Value        += (pow(Number, 2.0));
  502.  
  503.         CheckNode        = (struct DoubleNode *)CheckNode->Link.ln_Succ;
  504.     }
  505.  
  506.     return(Value);
  507. }
  508.  
  509.  
  510.  
  511.  
  512. /**********************************************************************/
  513. /*                  Calc sum of entries within list                   */
  514. /**********************************************************************/
  515. double calc_sum(struct List *Lst, BOOL LinearValueY)
  516. {
  517.     struct    DoubleNode    *CheckNode    = (struct DoubleNode *)Lst->lh_Head;
  518.     double            Number,
  519.                 Value        = 0;
  520.  
  521.     while(CheckNode->Link.ln_Succ)
  522.     {
  523.         if(!LinearValueY)
  524.             Number    = CheckNode->ValueX;
  525.         else
  526.             Number    = CheckNode->ValueY;
  527.  
  528.         Value        += Number;
  529.  
  530.         CheckNode        = (struct DoubleNode *)CheckNode->Link.ln_Succ;
  531.     }
  532.  
  533.     return(Value);
  534. }
  535.  
  536.  
  537.  
  538.  
  539. /**********************************************************************/
  540. /*                    Calc sum of x and y members                     */
  541. /**********************************************************************/
  542. double calc_xysum(void)
  543. {
  544.     struct    DoubleNode    *CheckNode    = (struct DoubleNode *)LinearList.lh_Head;
  545.     double            Value        = 0;
  546.  
  547.     while(CheckNode->Link.ln_Succ)
  548.     {
  549.         Value        += (CheckNode->ValueX * CheckNode->ValueY);
  550.  
  551.         CheckNode    = (struct DoubleNode *)CheckNode->Link.ln_Succ;
  552.     }
  553.  
  554.     return(Value);
  555. }
  556.  
  557.  
  558.  
  559.  
  560. /**********************************************************************/
  561. /*             Calc alpha part of formular of regression              */
  562. /**********************************************************************/
  563. double calc_lralpha(void)
  564. {
  565.     double        XSum    = calc_sum(&LinearList, FALSE),
  566.             XYSum    = calc_xysum(),
  567.             YSum    = calc_sum(&LinearList, TRUE),
  568.             XQSum    = calc_powsum(&LinearList, FALSE),
  569.             Number    = (double)numbers_in_list(&LinearList);
  570.  
  571.     if((pow(XSum, 2.0) - (Number * XQSum)) == 0.0)
  572.     {
  573.         PError    = ERR_DIVBY0;
  574.         return(0.0);
  575.     }
  576.  
  577.     return( (((XSum * XYSum) - (YSum * XQSum)) / (pow(XSum, 2.0) - (Number * XQSum))) );
  578. }
  579.  
  580.  
  581.  
  582.  
  583. /**********************************************************************/
  584. /*             Calc alpha part of formular of regression              */
  585. /**********************************************************************/
  586. double calc_lrbeta(void)
  587. {
  588.     double        XSum    = calc_sum(&LinearList, FALSE),
  589.             YSum    = calc_sum(&LinearList, TRUE),
  590.             XYSum    = calc_xysum(),
  591.             XQSum    = calc_powsum(&LinearList, FALSE),
  592.             Number    = (double)numbers_in_list(&LinearList);
  593.  
  594.     if((pow(XSum, 2.0) - (Number * XQSum)) == 0.0)
  595.     {
  596.         PError    = ERR_DIVBY0;
  597.         return(0.0);
  598.     }
  599.  
  600.     return( (((XSum * YSum) - (Number * XYSum)) / (pow(XSum, 2.0) - (Number * XQSum))) );
  601. }
  602.  
  603.  
  604.  
  605.  
  606.  
  607. /**********************************************************************/
  608. /*           Assess x value of linear regression given a y            */
  609. /**********************************************************************/
  610. double calc_lrassessx(double y)
  611. {
  612.     double        Alpha    = calc_lralpha(),
  613.             Beta    = calc_lrbeta();
  614.  
  615.     if(Beta == 0.0)
  616.     {
  617.         PError    = ERR_DIVBY0;
  618.         return(0.0);
  619.     }
  620.  
  621.     return( ((y - Alpha) / Beta) );
  622. }
  623.  
  624.  
  625.  
  626.  
  627.  
  628. /**********************************************************************/
  629. /*           Assess y value of linear regression given an x           */
  630. /**********************************************************************/
  631. double calc_lrassessy(double x)
  632. {
  633.     double        Alpha    = calc_lralpha(),
  634.             Beta    = calc_lrbeta();
  635.  
  636.     return( (Alpha + (Beta * x)) );
  637. }
  638.  
  639.  
  640.  
  641. /**********************************************************************/
  642. /*                    Calc correlation coefficient                    */
  643. /**********************************************************************/
  644. double calc_lrcorr(void)
  645. {
  646.     double        XSum    = calc_sum(&LinearList, FALSE),
  647.             YSum    = calc_sum(&LinearList, TRUE),
  648.             XYSum    = calc_xysum(),
  649.             Number    = numbers_in_list(&LinearList),
  650.             XDev    = calc_population(&LinearList, FALSE),
  651.             YDev    = calc_population(&LinearList, TRUE);
  652.  
  653.     if(Number == 0.0 || XDev == 0.0 || YDev == 0.0)
  654.     {
  655.         PError    = ERR_DIVBY0;
  656.         return(0.0);
  657.     }
  658.  
  659.     return( ( ((XYSum / Number) - ((XSum / Number) * (YSum / Number))) / (XDev * YDev) ) );
  660. }
  661.  
  662.  
  663.  
  664. /**********************************************************************/
  665. /*                Calc covariant of linear regression                 */
  666. /**********************************************************************/
  667. double calc_lrcovar(void)
  668. {
  669.     double        XYSum    = calc_xysum(),
  670.             Number    = (double)numbers_in_list(&LinearList),
  671.             XAve    = calc_average(&LinearList, FALSE),
  672.             YAve    = calc_average(&LinearList, TRUE);
  673.  
  674.     if(Number == 1.0)
  675.     {
  676.         PError    = ERR_DIVBY0;
  677.         return(0.0);
  678.     }
  679.  
  680.     return( ( (XYSum - (Number * XAve * YAve)) / (Number - 1) ) );
  681. }
  682.