packages feed

egison-5.1.0: lib/math/common/functions.egi

--
-- Mathematical Functions
--

declare mathfunc abs
declare apply abs x := if isRational x then i.abs x else 'abs x

-- Mathematical functions are declared via `declare mathfunc` (which gives
-- them a default symbolic-factor wrapper) and then their algorithmic
-- simplification is given via `declare apply`. Pattern rewrite rules such
-- as `i^2 = -1` and `(sqrt $x)^2 = x` live separately as `declare rule auto`
-- declarations in lib/math/normalize.egi.

declare mathfunc exp
declare apply exp x :=
  if isTerm x
    then match x as termExpr with
      | #0 -> 1
      | #1 -> e
      | mult $a #(i * π) -> (-1) ^ a
      | _ -> 'exp x
    else 'exp x

declare mathfunc log
declare apply log x :=
  match x as mathValue with
    | #1 -> 0
    | #e -> 1
    | _ -> 'log x

declare mathfunc cos
declare apply cos x :=
  match x as mathValue with
    | #0 -> 1
    | mult $n #π -> (-1) ^ abs n
    | (mult _ #π) / #2 -> 0
    | _ -> 'cos x

declare mathfunc sin
declare apply sin x :=
  match x as mathValue with
    | #0 -> 0
    | mult _ #π -> 0
    | (mult $n #π) / #2 -> (-1) ^ ((abs n - 1) / 2)
    | _ -> 'sin x

declare mathfunc tan
declare apply tan x :=
  match x as mathValue with
    | #0 -> 0
    | _ -> 'tan x

--def acos : MathValue -> MathValue := f.acos
--def asin : MathValue -> MathValue := f.asin
--def atan : MathValue -> MathValue := f.atan

declare mathfunc cosh
declare apply cosh x :=
  match x as mathValue with
    | #0 -> 1
    | _ -> 'cosh x

declare mathfunc sinh
declare apply sinh x :=
  match x as mathValue with
    | #0 -> 0
    | _ -> 'sinh x

declare mathfunc tanh
declare apply tanh x :=
  match x as mathValue with
    | #0 -> 0
    | _ -> 'tanh x

--def acosh : MathValue -> MathValue := f.acosh
--def asinh : MathValue -> MathValue := f.asinh
--def atanh : MathValue -> MathValue := f.atanh

def sinc (x: MathValue) : MathValue :=
  match x as mathValue with
    | #0 -> 1
    | _ -> sin x / x

def sigmoid (z: MathValue) : MathValue := 1 / (1 + exp (- z))

def kroneckerDelta (js: [Integer]) : Integer := 
  if all (= head js) (tail js) then 1 else 0

def eulerTotientFunction (n: Integer) : MathValue := 
  n * product (map (\p -> 1 - 1 / p) (unique (pF n)))

def ε : Integer -> Tensor Integer :=
  memoizedLambda n ->
    let (es, os) := evenAndOddPermutations' n
     in generateTensor
          (\is -> if member is es then 1 else if member is os then -1 else 0)
          (take n (repeat1 n))

def ε' : Integer -> Integer -> Tensor Integer :=
  memoizedLambda n k ->
    let (es, os) := evenAndOddPermutations' n
     in generateTensor
          (\is ->
            match drop k is as list integer with
              | _ ++ $x :: _ ++ ?(< x) :: _ -> 0
              | _ -> if member is es then 1 else if member is os then -1 else 0)
          (take n (repeat1 n))