tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | README | LICENSE

math.tsp (1414B)


      1 ;;; Constants
      2 
      3 (def pi (* 4 (arctan 1.)))
      4 (def tau (* 2 pi))
      5 (def e (exp 1.))
      6 
      7 ;;; Functions
      8 
      9 ; TODO rem
     10 (def /= (compose not =))
     11 (def (inc x) (+ x 1))
     12 (def (dec x) (- x 1))
     13 (def (truncate x) (* (floor (abs x)) (sgn x)))
     14 (def (sqr x) (* x x))
     15 (def (cube x) (* x (* x x)))
     16 (def (root b p) (^ b (/ p)))
     17 (def (sqrt x) (root x 2))
     18 (def (cbrt x) (root x 3))
     19 (def (logb b x) (/ (log x) (log b)))
     20 (def (log10 x) (logb 10. x))
     21 
     22 (defmacro (++ x . n)
     23   `(set! ,x (+ ,x ,(default n 1))))
     24 (defmacro (-- x . n)
     25   `(set! ,x (- ,x ,(default n 1))))
     26 
     27 ;;; Trig
     28 
     29 (def (csc x)     (/ (sin x)))
     30 (def (arccsc x)  (/ (arcsin x)))
     31 (def (csch x)    (/ (sinh x)))
     32 (def (arccsch x) (/ (arcsinh x)))
     33 (def (sec x)     (/ (cos x)))
     34 (def (arcsec x)  (/ (arccos x)))
     35 (def (sech x)    (/ (cosh x)))
     36 (def (arcsech x) (/ (arccosh x)))
     37 (def (cot x)     (/ (tan x)))
     38 (def (arccot x)  (/ (arctan x)))
     39 (def (coth x)    (/ (tanh x)))
     40 (def (arccoth x) (/ (arctanh x)))
     41 
     42 (def (abs x) (if (>= x 0) x (- x)))
     43 (def (sgn x) (if (= x 0) x (/ (abs x) x)))
     44 ; TODO many args
     45 (def (max a b) (if (> a b) a b))
     46 (def (min a b) (if (< a b) a b))
     47 
     48 (def (positive? x) (> x 0))
     49 (def (negative? x) (< x 0))
     50 (def (zero? x) (= x 0))
     51 (def (even? x) (= (mod x 2) 0))
     52 (def (odd? x)  (= (mod x 2) 1))
     53 
     54 (def (dot v w)
     55   (if v
     56     (+ (* (car v) (car w))
     57        (dot (cdr v) (cdr w)))
     58     0))
     59 (def (norm v) (sqrt (dot v v)))
     60 
     61 (def (! n)
     62   (if (= n 1)
     63     1
     64     (* n (! (- n 1)))))