/lib/math.arc

http://github.com/alimoeeny/arc · Unknown · 179 lines · 147 code · 32 blank · 0 comment · 0 complexity · 29bdc3763ad8aa24473af55d196b5337 MD5 · raw file

  1. (def zeros (x)
  2. (n-of x 0))
  3. (def ones (x)
  4. (n-of x 1))
  5. ;calculus fns
  6. (def deriv (f)
  7. "provides differential of a function of a single vairable"
  8. (fn (x)
  9. (let dx (max 1d-9 (abs:* x 1d-9))
  10. (/ (- (f (+ x dx))
  11. (f x))
  12. dx))))
  13. (mac partial-diff (f n arity)
  14. "returns deriv function of f (w/ num args ARITY) wrt Nth variable(from 0) "
  15. (withs (arglis (n-of arity (uniq))
  16. a-lis (firstn n arglis)
  17. x arglis.n
  18. b-lis (nthcdr (+ n 1) arglis))
  19. `(fn ,arglis
  20. (let dx (max 1d-9 (abs:* ,x 1d-9)
  21. (/ (- (,f ,@a-lis (+ ,x dx) ,@b-lis)
  22. (,f ,@a-lis ,x ,@b-lis))
  23. dx)))))
  24. (mac partial-diff-vec (f n arity)
  25. "returns vector with each element differentiated with respect to Nth argument"
  26. (withs (arglis (n-of arity (uniq))
  27. a-lis (firstn n arglis)
  28. x arglis.n
  29. b-lis (nthcdr (+ n 1) arglis))
  30. `(fn ,arglis
  31. (let dx (max 1d-9 (abs:* ,x 1d-9)
  32. (vec-scale (vec- (,f ,@a-lis (+ ,x dx) ,@b-lis)
  33. (,f ,@a-lis ,x ,@b-lis))
  34. (/ 1 dx))))))
  35. (def grad (f)
  36. "gradient of 3D scalar field given by F"
  37. (fn (x y z)
  38. (list ((partial-diff f 0 3) x y z)
  39. ((partial-diff f 1 3) x y z)
  40. ((partial-diff f 2 3) x y z))))
  41. (def div (f)
  42. "divergence of 3D vector field given by F"
  43. (fn (x y z)
  44. (+ (((partial-diff-vec f 0 3) x y z) 0)
  45. (((partial-diff-vec f 1 3) x y z) 1)
  46. (((partial-diff-vec f 2 3) x y z) 2))))
  47. (def curl (f)
  48. "curl of 3D vector field given by F"
  49. (with (d/dx (partial-diff-vec f 0 3)
  50. d/dy (partial-diff-vec f 1 3)
  51. d/dz (partial-diff-vec f 2 3))
  52. (fn (x y z)
  53. (list (- ((d/dy x y z) 2)
  54. ((d/dz x y z) 1))
  55. (- ((d/dz x y z) 0)
  56. ((d/dx x y z) 2))
  57. (- ((d/dx x y z) 1)
  58. ((d/dy x y z) 0))))))
  59. (def integral (f)
  60. "returns the integral of a single argument function"
  61. (fn (lower upper (o its 10000))
  62. (withs (dx (/ (- upper lower) its)
  63. x lower
  64. current (f x)
  65. next (f (+ x dx))
  66. accum 0)
  67. (while (<= x (- upper dx))
  68. (++ accum (* (/ (+ current next) 2) dx))
  69. (++ x dx)
  70. (= current next)
  71. (= next (f:+ x dx)))
  72. accum)))
  73. ; vector fns
  74. (def vec-dot (v1 v2)
  75. (apply + (map (fn (x y) (* x y)) v1 v2)))
  76. (def vec-cross (v1 v2 . args)
  77. (if (car args)
  78. (vec-cross (vec-cross v1 v2) (car args) (cdr args))
  79. (list (- (* v1.1 v2.2) (* v2.1 v1.2))
  80. (- (* v1.2 v2.0) (* v2.2 v1.0))
  81. (- (* v1.0 v2.1) (* v2.0 v1.1)))))
  82. (with (v+ (fn (v1 v2)
  83. (map (fn (x y) (+ x y)) v1 v2))
  84. v- (fn (v1 v2)
  85. (map (fn (x y) (- x y)) v1 v2)))
  86. (def vec+ (v1 . args)
  87. (if no.args v1
  88. (reduce v+ (cons v1 args))))
  89. (def vec- (v1 . args)
  90. (if no.args (map [- _] v1)
  91. (v- v1 (apply vec+ args))))
  92. )
  93. (def vec-scale (vec . scalars)
  94. (let c (apply * scalars)
  95. (map [* _ c] vec)))
  96. (def quad-add args
  97. ((afn (tot xs)
  98. (if no.xs (sqrt tot)
  99. (self (+ tot (expt (car xs) 2)) (cdr xs))))
  100. 0 args))
  101. (def vec-norm (vec)
  102. (vec-scale vec (/ (apply quad-add vec))))
  103. ;others
  104. (def fact (num)
  105. "factorial of num (num must be a fixnum)"
  106. (if (or (< num 0)(no:isa num 'int)) (err "num must be a positive integer")
  107. ((afn (n x)
  108. (if (<= n 1)
  109. x
  110. (self (- n 1) (* n x))))
  111. num 1)))
  112. (def n-bessel (n (o terms 100))
  113. "gives a fn for the nth bessel function of the first kind evaluated at x"
  114. (fn (x)
  115. (with (i 0
  116. tot 0)
  117. (while (< i terms)
  118. (++ tot (/ (* (expt -1 i) (expt (/ x 2) (+ n i i)))
  119. (* (fact i) (fact (+ n i)))))
  120. (++ i 1))
  121. tot)))
  122. (def mean lis
  123. ((afn (tot i x . xs)
  124. (if no.xs (/ (+ tot x) (+ i 1))
  125. (self (+ tot x) (+ i 1) car.xs cdr.xs)))
  126. 0 0 car.lis cdr.lis))
  127. (def std-dev lis
  128. (let m mean.lis
  129. (sqrt:/ (apply + (map [expt (- _ m) 2] lis))
  130. len.lis)))
  131. (def choose (n x)
  132. "number of ways of picking n elements from x (ie no. of ways of mixing 2 different sets of identical objects of size n and (- x n))"
  133. (if (> n x)
  134. (do (prn "")(pr n)(pr " is greater than ")(prn x))
  135. (/ (fact x) (* (fact n) (fact (- x n))))))
  136. (def gauss-random (sigma (o middle 0))
  137. "aproximation to a gausian distributed random with width sigma around middle, max possible deviation +/- 1000sigma"
  138. (let tot 0
  139. (for i 0 999
  140. (++ tot (- (* (rand) 2.0) 1)))
  141. (+ (* tot (/ sigma 25)) middle)))
  142. (def quad-roots (a b c)
  143. "returns roots of the equation ax²+bx+c=0"
  144. (let sqroot (sqrt (- (* b b) (* 4 a c)))
  145. (rem-dups
  146. (list (/ (- sqroot b) 2 a)
  147. (/ (- 0 sqroot b) 2 a)))))