% File TRGNEG.ALG	(c)	08/05/81	The Soft Warehouse %

HALF: 1/2 $

FUNCTION NEGLT (EX1),
  WHEN SUM (EX1),
    WHEN POSMULT (NUMNUM, 2),
      NEGCOEF (SECOND(EX1)) EXIT EXIT,
  NEGCOEF (EX1),
ENDFUN $

FUNCTION MULTANGLE (EX1, EX2, EX3, EX4),
  WHEN ZERO(EX1), EX4 EXIT,
  WHEN POSITIVE (EX1), MULTANGLE1 (EX1, EX3, EX4) EXIT,
ENDFUN $

FUNCTION MULTANGLE1 (EX1, EX3, EX4),
  % Fluid var from MULTANGLE: EX2 %
  WHEN EX1 EQ 1, EX3 EXIT,
  MULTANGLE1 (EX1 - 1, EX2*EX3 - EX4, EX3),
ENDFUN $

FOURONPI: 4 / #PI $

FUNCTION SIN (EX1,
  % Local: %  EX2),
  WHEN NEGLT (EX1), -SIN(-EX1) EXIT,
  WHEN NUMBER(EX2:EX1*FOURONPI),
    EX1: DIVIDE (NUM(EX2), EX2:DEN(EX2)),
    SINCOS (MOD(POP(EX1),8), EX1/EX2)  EXIT,
  WHEN POSMULT (TRGEXPD, 7),
    #I * (1/(EX2:#E^(#I*EX1)) - EX2) * HALF  EXIT,
  SIMPU ('SIN, EX1),
ENDFUN $

FUNCTION COS (EX1,
  % Local: %  EX2),
  WHEN NEGLT (EX1), COS(-EX1) EXIT,
  WHEN NUMBER(EX2:EX1*FOURONPI),
    EX1: DIVIDE (NUM(EX2), EX2:DEN(EX2)),
    SINCOS (MOD(2+POP(EX1),8), EX1/EX2)  EXIT,
  WHEN POSMULT (TRGEXPD, 7),
    (1/(EX2:#E^(#I*EX1)) + EX2) * HALF  EXIT,
  SIMPU ('COS, EX1),
ENDFUN $

FUNCTION SINCOS (EX1, EX2),
  WHEN LESSER(3,EX1), -SINCOS(EX1-4, EX2)  EXIT,
  WHEN ZERO(EX1), SIN1(EX2)  EXIT,
  WHEN EX1 EQ 1, COS1(1-EX2)  EXIT,
  WHEN EX1 EQ 2, COS1(EX2)  EXIT,
  SIN1(1-EX2),
ENDFUN $

PION4: 1/FOURONPI $
ONRT2: 2^-HALF $
TWO3RDS: 2/3 $

FUNCTION SIN1 (EX1),
  WHEN ZERO(EX1), EX1 EXIT,
  WHEN EX1 EQ 1, ONRT2 EXIT,
  WHEN EX1 = TWO3RDS,  HALF  EXIT,
  LIST ('SIN, EX1*PION4),
ENDFUN $

RT3ON2: 3^HALF * HALF $

FUNCTION COS1 (EX1),
  WHEN ZERO(EX1), 1  EXIT,
  WHEN EX1 EQ 1, ONRT2  EXIT,
  WHEN EX1 = TWO3RDS,  RT3ON2  EXIT,
  LIST ('COS, EX1*PION4),
ENDFUN $

PROPERTY SIN, *, FUNCTION (EX1, EX2),
  WHEN NEGMULT (TRGEXPD, 3),
    MULTANGLE (EX1, 2*COS(EX2), SIN(EX2), 0) EXIT,
ENDFUN $

PROPERTY COS, *, FUNCTION (EX1, EX2),
  WHEN NEGMULT (TRGEXPD, 3),
    MULTANGLE (EX1, 2*COS(EX2), COS(EX2), 1) EXIT,
ENDFUN $

PROPERTY SIN, +, FUNCTION (EX1, EX2),
  WHEN NEGMULT (TRGEXPD, 5),
    SIN(EX1)*COS(EX2) + COS(EX1)*SIN(EX2) EXIT,
ENDFUN $

PROPERTY COS, +, FUNCTION (EX1, EX2),
  WHEN NEGMULT (TRGEXPD, 5),
    COS(EX1)*COS(EX2) - SIN(EX1)*SIN(EX2) EXIT,
ENDFUN $

PROPERTY BASE, TAN, FUNCTION (EX1, EX2),
  WHEN EX1<0,
    WHEN NEGMULT(TRGEXPD,2),  COT(EX2) ^ -EX1  EXIT EXIT,
ENDFUN $

PROPERTY BASE, COT, FUNCTION (EX1, EX2),
  WHEN EX1<0,
    WHEN NEGMULT(TRGEXPD,2),  TAN(EX2) ^ -EX1  EXIT EXIT,
ENDFUN $

PROPERTY BASE, SEC, FUNCTION (EX1, EX2),
  WHEN EX1<0,
    WHEN NEGMULT(TRGEXPD,2),  COS(EX2) ^ -EX1  EXIT EXIT,
ENDFUN $

PROPERTY BASE, CSC, FUNCTION (EX1, EX2),
  WHEN EX1<0,
    WHEN NEGMULT(TRGEXPD,2),  SIN(EX2) ^ -EX1  EXIT EXIT,
ENDFUN $

PROPERTY *, TAN, FUNCTION (EX1, EX2),
  WHEN SECOND(EX1)=EX2,
    WHEN FIRST(EX1) EQ 'COS, SIN(EX2) EXIT,
    WHEN FIRST(EX1) EQ 'COT, 1 EXIT EXIT,
ENDFUN $

PROPERTY *, COT, FUNCTION (EX1, EX2),
  WHEN FIRST(EX1) EQ 'SIN,
    WHEN SECOND(EX1)=EX2,  COS(EX2)  EXIT EXIT,
ENDFUN $

PROPERTY *, SEC, FUNCTION (EX1, EX2),
  WHEN NOT ATOM(EX1),
    WHEN SECOND(EX1)=EX2,
      WHEN (EX1:FIRST(EX1)) EQ 'COS,  1  EXIT,
      WHEN EX1 EQ 'SIN, TAN(EX2)  EXIT ,
      WHEN EX1 EQ 'COT, CSC(EX2)  EXIT EXIT EXIT,
ENDFUN $

PROPERTY *, CSC, FUNCTION (EX1, EX2),
  WHEN NOT ATOM(EX1),
    WHEN SECOND(EX1)=EX2,
      WHEN (EX1:FIRST(EX1)) EQ 'SIN,  1  EXIT,
      WHEN EX1 EQ 'COS, COT(EX2)  EXIT,
      WHEN EX1 EQ 'TAN, SEC(EX2)  EXIT EXIT EXIT,
ENDFUN $

PROPERTY TAN, ATAN, IDENTITY $
PROPERTY SIN, ASIN, IDENTITY $
PROPERTY COS, ACOS, IDENTITY $
PROPERTY COT, ACOT, IDENTITY $
PROPERTY CSC, ACSC, IDENTITY $
PROPERTY SEC, ASEC, IDENTITY $

FUNCTION TRGEXPD (EX1, TRGEXPD),
  EVAL (EX1),
ENDFUN $

RDS() $
