%File INT.DIF	(c)		07/31/81	The Soft Warehouse %


FUNCTION QUERY (EX1, LEX1, LEX2, LEX3, READCHAR),
    % EX1: relative subexpression %
    % LEX1: query question %
    % LEX2: possible responses %
    % LEX3: AList of previous expressions & answers %
    % READCHAR: set to FALSE for raw input mode %
    % RSLT: dotted pair of response and revised AList %
  WHEN ASSOC(EX1,LEX3),
    ADJOIN (REST(ASSOC(EX1,LEX3)), LEX3) EXIT,
  NEWLINE (),
  PRINT ("???"), SPACES (2),
  PRTMATH (EX1, 0, 0, TRUE),
  SPACES (2), PRINTLINE ("???"),
  PRINT ('ENTER),
  LOOP
    SPACES (1),
    WHEN EMPTY (LEX1) EXIT,
    PRINT (POP(LEX1))
  ENDLOOP,
  PRINT (LEX2),
  PRINT ("? "),
  BLOCK
    WHEN BELL, PRINT ("") EXIT,
  ENDBLOCK,
  LOOP
    WHEN MEMBER (READCHAR(), LEX2) EXIT,
  ENDLOOP,
  PRINTLINE (SCAN),
  ADJOIN (SCAN, ADJOIN(ADJOIN(EX1,SCAN),LEX3))
ENDFUN $

FUNCTION SIGN(EX1),
  WHEN ZERO(EX1), EX1  EXIT,
  WHEN EX1 > 0,  1  EXIT,
  WHEN EX1 < 0, -1  EXIT,
  WHEN EX1 EQ PINF, 1  EXIT,
  WHEN EX1 EQ MINF, -1  EXIT,
  WHEN EX1 = PINF-MINF, 1  EXIT,
  EX1: QUERY (EX1, '(SIGN), LIST(0, '+, '-), SIGN),
  SIGN: REST(EX1),
  EX1: FIRST(EX1),
  WHEN ZERO(EX1), EX1 EXIT,
  WHEN EX1 EQ '+, 1 EXIT,
  -1
ENDFUN $

HALF: 1/2 $

FUNCTION INT3 (),
 % Global var HALF=1/2 & fluid vars INDET from INT & EX1 from INT1 %
  WHEN EX1 = INDET,  HALF*EX1^2  EXIT,
  APPLY (GET('INT,FIRST(EX1)), ARGEX(EX1)),
ENDFUN $

FUNCTION DRVDIV (LEX1),
  % Fluid vars, from INT & INT1:  INDET, EX1, EX2, EX3, EX4, EX5  %
  WHEN EMPTY (LEX1),  INT3()  EXIT,
  WHEN (EX4:POP(LEX1)) = INDET,  DRVDIV (LEX1)  EXIT,
  EX5: EX1 / EX4,
  WHEN ZERO (EX3:DIF(EX4,INDET)),
    EX2: EX2*EX4,  EX1: EX5,  DRVDIV (LEX1)  EXIT,
  WHEN FREE (EX3:EXPD(EX5/EX3), INDET),  HALF*EX3*EX4^2  EXIT,
  WHEN SUM(EX4),  DRVDIV (LEX1)  EXIT,
  WHEN ZERO (EX3: DIF(SECOND(EX4), INDET)),
    WHEN ZERO(EX3:DIF(THIRD(EX4),INDET)),  DRVDIV (LEX1)  EXIT,
    WHEN FREE(EX3:EXPD(EX5/EX3), INDET),
      WHEN EX5: APPLY (GET('INT, FIRST(EX4)),
        LIST (SECOND(EX4), INDET)),
        EX3 * EVSUB(EX5,INDET,THIRD(EX4))  EXIT,
      DRVDIV (LEX1)  EXIT,
    DRVDIV (LEX1)  EXIT,
  WHEN FREE (RREST(EX4), INDET),
    WHEN FREE (EX3:EXPD(EX5/EX3), INDET),
      WHEN EX5: APPLY(GET('INT,FIRST(EX4)), ADJOIN(INDET,RREST(EX4))),
        EX3 * EVSUB(EX5,INDET,SECOND(EX4))  EXIT,
      DRVDIV (LEX1)  EXIT,
    DRVDIV (LEX1)  EXIT,
  DRVDIV (LEX1),
ENDFUN $

FUNCTION INT2 (),
  % Fluid vars, from INT & INT1:  INDET, EX1 %
  WHEN PRODUCT(EX1), DRVDIV (REST(EX1))  EXIT,
  WHEN FREE(EX1,INDET), EX1*INDET  EXIT,
  INT3(),
ENDFUN $

FUNCTION INT1 (EX1,
  % Local: %  EX2, EX3, EX4, EX5),
  EX2: 1,
  WHEN EX3:INT2(), EX2*EX3  EXIT,
  TRGEXPD: LOGEXPD: NUMNUM: DENDEN: DENNUM: 30,
  PWREXPD: 6,
  EX1: EVAL(EX1),
  WHEN EX3:INT2(), EX2*EX3  EXIT,
  NUMNUM: DENDEN: DENNUM: -30,
  TRGEXPD: 7,
  EX1: EVAL(EX1),
  WHEN EX3:INT2(), TRGEXPD:-7, EX2*EVAL(EX3)  EXIT,
  EX2 * LIST ('INT, EX1, INDET),
ENDFUN $

FUNCTION INT (EX1, INDET,
  % Local: %  PWREXPD, NUMNUM, DENDEN, DENNUM, NUMDEN,
    BASEXP, EXPBAS, LOGEXPD, TRGEXPD, SIGN),
  PWREXPD: NUMDEN: LOGEXPD: TRGEXPD: 0,
  NUMNUM: DENNUM: 6,
  DENDEN: 2,
  BASEXP: -30,
  EXPBAS: 30,
  INT1 (EX1),
ENDFUN $

PROPERTY INT, COS, FUNCTION (EX1,
  % Local: %  EX2),
  WHEN FREE (EX2:DIF(EX1,INDET), INDET),  SIN(EX1) / EX2  EXIT,
ENDFUN $

PROPERTY INT, SIN, FUNCTION (EX1,
  % Local: %  EX2),
  WHEN FREE (EX2:DIF(EX1,INDET), INDET),  -COS(EX1) / EX2  EXIT,
ENDFUN $

PROPERTY INT, LOG, FUNCTION (EX2, EX3),
  WHEN EX3 EQ #E,
    WHEN FREE (EX3:DIF(EX2,INDET), INDET),
      EX2 * (LN(EX2)-1)/EX3  EXIT EXIT,
ENDFUN $

MINUSHALF: -HALF $

PROPERTY INT, ^, FUNCTION (EX2, EX3,
  % Local: %  EX4, EX5),
  % Globals HALF=1/2, MINUSHALF=-1/2 %
  % Fluid var INDET from INT %
  WHEN FREE (EX3, INDET),
    WHEN EX2 = INDET,
      WHEN EX3 EQ -1,  LN(INDET)  EXIT,
      INDET^(EX3+1) / (EX3+1)  EXIT,
    APPLY (GET(INTPWR, FIRST(EX2)), ARGEX(EX2))  EXIT,
  WHEN FREE (EX2, INDET),
    WHEN FREE (EX4:DIF(EX3,INDET), INDET),
      LIST('^, EX2, EX3) / EX4 / LN(EX2) EXIT,
    WHEN FREE (EX5:DIF(EX4,INDET), INDET),
      (MINUSHALF*#PI/EX5/LN(EX2))^HALF * EX2^(EX3-HALF*EX4^2/EX5)
        * ERF (-EX4*(MINUSHALF*LN(EX2)/EX5)^HALF)  EXIT EXIT,
ENDFUN $

PROPERTY INTPWR, +, FUNCTION (
  % Local: % EX6, EX7),
  % Fluid vars from INT, INT1 & property INT ^:  
      INDET, EX1, EX2, EX3, EX4  %
  WHEN FREE (EX6:DIF(EX2,INDET), INDET),
    WHEN EX3 EQ -1,  LN(EX2)/EX6  EXIT,
    EX2^(EX3+1) / (EX3+1) / EX6  EXIT,
  WHEN POSITIVE(EX3), FALSE EXIT,
  WHEN FREE (EX7:DIF(EX6,INDET), INDET),
    WHEN ZERO (EX4: SIGN(EX5: EXPD(2*EX2*EX7-EX6^2))),
      (2*EX7)^-EX3 * INT1(EX6^(2*EX3))  EXIT,
    WHEN EX3 EQ -1,
      WHEN EX4 EQ 1,
        2 * EX5^MINUSHALF * ATAN(EX6*EX5^MINUSHALF)  EXIT,
      (-EX5)^MINUSHALF
        * LN (((-EX5)^HALF-EX6) / ((-EX5)^HALF+EX6))  EXIT,
    WHEN NEGATIVE(EX3),
      ((2*EX3+3)*EX7*INT1(EX2^(EX3+1)) - EX6*EX2^(EX3+1))
        / (EX3+1) / EX5  EXIT,
    WHEN EX3 = MINUSHALF,
      WHEN ZERO (EX4:SIGN(EX7)),
        INT1 (EXPD ((EX2-HALF*EX7*INDET)^MINUSHALF))  EXIT,
      WHEN EX4 EQ 1,
        (HALF*EX7)^MINUSHALF * LN((2*EX2*EX7)^HALF+EX6)  EXIT,
      -(MINUSHALF*EX7)^MINUSHALF * ASIN(EX6*(-EX5)^MINUSHALF) EXIT,
    WHEN NEGATIVE(2*EX3),
      ((2*EX3+3)*EX7*INT1(EX2^(EX3+1)) - EX6*EX2^(EX3+1))
        / (EX3+1) / EX5  EXIT,
    WHEN POSITIVE(2*EX3),
      HALF * (EX1*EX6 + EX3*EX5*INT1(EX2^(EX3-1)))
        / (HALF+EX3) / EX7  EXIT EXIT,
ENDFUN $

PROPERTY INT, +, FUNCTION (EX1, EX2),
  EX1: INT1(EX1),
  WHEN SUM (EX2),
    POP (EX2),
    LOOP
      EX1: EX1 + INT1(POP(EX2)),
      WHEN EMPTY (EX2), EX1 EXIT,
    ENDLOOP EXIT,
  EX1 + INT1(EX2),
ENDFUN $

RDS() $
