⍝   M a t h e m a t i c a l   d e f i n i t i o n s

    ← ∆()                       ⍝ derivative wrt ⍵

    sin(1∘○) cos(2∘○) tan(3∘○)  ⍝ sin, cos, tan
    ln(⍟)    exp(*)             ⍝ natural log, exp

      -(-)                      ⍝ neg (unary minus)
    → ∧(*)                      ⍝ pow
    ← *(×) /(÷)                 ⍝ mul div
    ← +(+) -(-)                 ⍝ add sub

    ← =()  ≡()                  ⍝ reduction equivalence
    ← ,()                       ⍝ argument separator

⍝    R  e  d  u  c  t  i  o  n     R  u  l  e  s

⍝ Pattern matching variables    See →notes.Expression_simplification←

    i  j  k :: ← 0 →            ⍝ literal numbers.
   ¯i ¯j ¯k :: ←                ⍝ negative literal numbers.
    x  y  z :: ∊ ⍺              ⍝ non-simple expressions or variables.
    a  b  c ::   ⍺              ⍝ var names for differentiation.
    p  q  r ::                  ⍝ any values.

⍝ Add / Sub

        i+x  =  x+i             ⍝ move constant to right.
        x+0  =  x
        x-0  =  x
        0-x  = -x
        x-x  =  0
       -x+y  =  y-x
       x+-y  =  x-y
     -(x-y)  =  y-x
       -x-y  = -(x+y)
        --y  =  y
       x--y  =  x+y
       x+¯j  =  x--¯j           ⍝ reclaim negative literal.
       ¯i+y  =  y--¯i
       x-¯j  =  x+-¯j

    (x+i)+j  =  x+(i+j)         ⍝ + associative: collect literals.
    (x-i)-j  =  x-(i+j)
    (x+i)-j  =  x+(i-j)
    (x-i)+j  =  x-(i-j)

    (i-x)+j  =  i+j-x
    (i-x)-j  =  i-j-x

    i-(x+j)  =  i-j-x
    i-(x-j)  =  i+j-x

    i-(j-x)  =  i-j+x

    (x+i)+y  =  x+y+i
    (x+i)-y  =  x-y+i
    (x-i)+y  =  x+y-i
    (x-i)-y  =  x-y-i

    (i-x)+y  =  y-x+i
    (i-x)-y  =  -x-y+i

    x-(y+i)  =  x-y-i
    x-(y-i)  =  x-y+i
    x-(i-y)  =  x+y-i

    x+(y+i)  =  x+y+i
    x+(y-i)  =  x+y-i
    x+(i-y)  =  x-y+i

⍝ Mul / Div

        x*i  =  i*x             ⍝ move constant to left.
        0*x  =  0
        1*x  =  x
       ¯1*x  = -x

        0/x  =  0
        x/x  =  1
        x/1  =  x

       -x*y  = -(x*y)
       x*-y  = -(x*y)

      -(i*x) = -i*x
       i*-x  = -i*x

    i*(j*x)  =  (i*j)*x
    i*(j/x)  =  (i*j)/x
    i/(j*x)  =  (i/j)/x
    i/(j/x)  =  (i/j)*x

    i*(x/j)  =  (i/j)*x
    i/(x/j)  =  (i*j)/x

    (i/x)/j  =  (i/j)/x
    (i*x)/j  =  (i/j)*x

    (x/i)/j  =  x/(i*j)

    x*(i*y)  =  i*(x*y)
    x*(i/y)  =  i*(x/y)
    x/(i*y)  =  (x/y)/i
    x/(i/y)  =  (x*y)/i

    x*(y/i)  =  (x*y)/i
    x/(y/i)  =  i*(x/y)

    (i*x)*y  =  i*(x*y)
    (i*x)/y  =  i*(x/y)
    (i/x)*y  =  i*(y/x)
    (i/x)/y  =  i/(x*y)

    (x/i)/y  =  (x/y)/i
    (x/i)*y  =  (x*y)/i

⍝ Power

            x∧0  =  1
            x∧1  =  x
            x*x  =  x∧2

          x∧p*x  =  x∧(p+1)
          x*x∧p  =  x∧(p+1)
          x/x∧p  =  x∧(1-p)
          x∧p/x  =  x∧(p-1)
        (x∧p)∧q  =  x∧(p*q)
        x∧p/x∧q  =  x∧(p-q)

    (p∧q)*(p∧r)  =  p∧(q+r)
    (p∧q)/(p∧r)  =  p∧(q-r)
        x*y/y∧i  =  x/y∧(i-1)

         p/q∧-r  = p*q∧r
         p*q∧-r  = p/q∧r
         p/q∧¯i  = p*q∧-¯i      ⍝ reclaim literal -ive.
         p*q∧¯i  = p/q∧-¯i

⍝ Factoring / distribution

            p+p  =   2*p

        (p*q)+q  =  (p+1)*q
        p+(q*p)  =  (q+1)*p
        (p*q)-q  =  (p-1)*q
        p-(q*p)  =  (1-q)*p

    (p*q)+(p*r)  =  p*(q+r)
    (p*r)+(q*r)  =  (p+q)*r
    (p*q)-(p*r)  =  p*(q-r)
    (q*r)-(q*r)  =  (p-q)*r
    (p/r)+(q/r)  =  (p+q)/r
    (p/r)-(q/r)  =  (p-q)/r

⍝ Collect common terms

      p+q+p  =  q+2*p           ⍝ add/sub
      q+p+p  =  q+2*p
    p+(p+q)  =  2*p+q
    p+(q+p)  =  2*p+q

      p+q-p  =  q
      q+p-p  =  q
    p+(p-q)  =  2*p-q
    p+(q-p)  =  q

      p-q+p  =  2*p-q
      q-p+p  =  q
    p-(p+q)  = -q
    p-(q+p)  = -q

      p-q-p  = -q
      q-p-p  =  q-2*p
    p-(p-q)  =  q
    p-(q-p)  =  2*p-q

      p*q*p  =  q*p∧2           ⍝ mul/div
      q*p*p  =  q*p∧2
    p*(p*q)  =  q*p∧2
    p*(q*p)  =  q*p∧2

      p*q/p  =  q
      q*p/p  =  q
    p*(p/q)  =  q*p∧2
    p*(q/p)  =  q

      p/q*p  =  p∧2/q
      q/p*p  =  q
    p/(p*q)  =  1/q
    p/(q*p)  =  1/q

      p/q/p  =  1/q
      q/p/p  =  q/p∧2
    p/(p/q)  =  q
    p/(q/p)  =  p∧2/q

⍝ Cosmetics: remove redundant ()s

    p+(q+r)  =  p+q+r       ⍝ + associative
    x*(q*r)  =  x*q*r       ⍝ * associative

⍝ Trig

    cos(q)*sin(p) = sin(p)*cos(q)   ⍝ allows sin*cos - cos*sin => 0
    sin(x)/cos(x) = tan(x)


⍝   D  i  f  f  e  r  e  n  t  i  a  t  i  o  n    See →notes.Differentiation←

⍝ Base Cases

        (i)∆a  =  0
        (a)∆a  =  1

⍝ Arithmetic

      (p+q)∆a  =  p∆a + q∆a
      (p-q)∆a  =  p∆a - q∆a
       (-p)∆a  = -p∆a

      (p*q)∆a  =  p∆a * q  +  p * q∆a
      (p/q)∆a  =  p∆a / q  -  p * q∆a / q∧2

      (p∧q)∆a  =  p∆a * q * p∧(q-1)  +  q∆a * (ln p) * p∧q

⍝ Log / Exp

    (ln  p)∆a  =  p∆a / p
    (exp p)∆a  =  p∆a * (exp p)

⍝ Trig

    (sin p)∆a  =  p∆a * (cos p)
    (cos p)∆a  = -p∆a * (sin p)
    (tan p)∆a =   p∆a * 1/(cos p)∧2

⍝ Test cases:
⍝
⍝   1+x+2                               -> x+3              ⍝ collect constants
⍝   -----x                              -> -x
⍝   1+x+y+z+x+y+z+2                     -> x+y+z+x+y+z+3    ⍝ (not optimal,
⍝   2*x*y*z*x*y*z*3                     -> 6*(x*y*z*x*y*z)  ⍝  see notes.Bugs)
⍝   x∧x+y*x∧x                           -> (y+1)*x∧x
⍝   x+x+x+x*x*x                         -> 3*x+x∧3
⍝   2*x + x*3                           -> 5*x
⍝   x∧2 * x∧3                           -> x∧5
⍝   (x∧y)∧z                             -> x∧(y*z)
⍝   (x+3-x)+(x*4/x)                     -> 7
⍝   x+y+z, x=y+y, y=z+z, z=3            -> 21
⍝   (2∧x+x∧2)∆x                         -> 0.69315*2∧x+2*x
⍝   (ln(x∧x))∆x, x=2                    -> 1+⍟2
⍝   (4*x∧3 + 3*x∧2 + 2*x∧1 + 1*x∧0)∆x   -> 12*x∧2+6*x+2
⍝    sin(x)∧2 + cos(x)∧2,   x=42        ->  1
⍝   (sin(x)∧2 + cos(x)∧2)∆x             ->  0           ⍝ 1st differential
⍝   (ln x)∆x∆x                          -> -1/x∧2       ⍝ 2nd       ..
⍝   (ln x)∆x∆x∆x                        -> 2/x∧3        ⍝ 3rd       ..
⍝   (ln x)∆x∆x∆x∆x∆x, x=1               -> 24           ⍝ 5th       ..
⍝   (ln x)∆x∆x∆x∆x∆x∆x                  -> -120/x∧6     ⍝ 6th       ..
⍝   ln exp ln exp 99                    -> 99
⍝   (x/exp(x))∆x                        -> (1-x)/exp(x)
⍝   x+-3                                -> x-3
⍝   x/y∧-2                              -> x*y∧2
⍝   2∆3                                 -> 2∆3
⍝   a+(b+(c+d))                         -> a+b+c+d      ⍝ + associative
⍝   a*(b*(c*d))                         -> a*b*c*d      ⍝ * associative
⍝   
⍝   Back to: Contents