Created
May 14, 2010 19:13
-
-
Save liebke/401524 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use 'incanter.core) | |
;; basic arithmetic | |
($= 7 + 8 - 2 * 6 / 2) ; => 9 | |
;; add 5 to each element of the vector [1 2 3] | |
($= [1 2 3] + 5) ; => (6 7 8) | |
;; element-by-element arithmetic on vectors | |
($= [1 2 3] + [4 5 6]) | |
($= [1 2 3] * [1 2 3]) | |
($= [1 2 3] * [1 2 3]) | |
($= [1 2 3] / (sq [1 2 3]) + [5 6 7]) | |
($= [1 2 3] + (sin [4 5 6])) | |
;; element-by-element operations on matrices | |
($= (matrix [[1 2] [4 5]]) + 6) | |
($= (trans [[1 2] [4 5]]) + 6) | |
;; exponents | |
($= 8 ** 3) | |
($= 8 ** 1/2) | |
($= 2 ** -2) | |
($= [1 2 3] ** 2) | |
;; grouping with parens | |
($= 10 + 20 * (4 - 5) / 6) | |
($= (10 + 20) * 4 - 5 / 6) | |
($= 10 + 20 * (4 - 5 / 6)) | |
($= 20 * (4 - 5) / 6) | |
($= ((5 + 4 * (3 - 4)) / (5 + 8) * 6)) | |
;; arbitrarily nested groupings | |
($= ((((5 + 4) * 5)))) | |
;; with variables | |
(let [x 10 | |
y -5] | |
($= x + y / -10)) | |
;; mathematical functions | |
($= sqrt 5 * 5 + 3 * 3) | |
($= sq [1 2 3] + [1 2 3]) | |
($= sin 2 * Math/PI * 2) | |
($= (cos 0) * 10) | |
($= (tan 2) * Math/PI * 10) | |
($= (asin 1/2) * 10) | |
($= (acos 1/2) * 10) | |
($= (atan 1/2) * 10) | |
;; boolean tests | |
($= 3 > (5 * 2/7)) | |
($= 3 <= (5 * 2/7)) | |
($= 3 != (5 * 2/7)) | |
($= 3 == (5 * 2/7)) | |
($= 3 != 8 && 6 < 12) | |
($= 3 != 8 || 6 > 12) | |
;; matrix operations | |
;; matrix multiplication | |
($= [1 2 3] <*> (trans [1 2 3])) | |
($= (trans [[1 2] [4 5]]) <*> (matrix [[1 2] [4 5]])) | |
($= (trans [1 2 3 4]) <*> [1 2 3 4]) | |
($= [1 2 3 4] <*> (trans [1 2 3 4])) | |
;; kronecker product | |
($= [1 2 3] <x> [1 2 3]) | |
($= (matrix [[1 2] [3 4] [5 6]]) <x> 4) | |
($= (matrix [[1 2] [3 4] [5 6]]) <x> (matrix [[1 2] [3 4]])) | |
($= [1 2 3 4] <x> 4) | |
($= [1 2 3 4] <x> (trans [1 2 3 4])) | |
;; plot cubic function and add LaTeX annotation | |
(use '(incanter core charts latex)) | |
(doto (function-plot (fn [x] ($= x ** 3 - 5 * x ** 2 + 3 * x + 5)) -10 10) | |
(add-latex 0 250 "x^3 - 5x^2 + 3x +5") | |
view) | |
;; plot car speed to breaking distance sample data, partitioning data where the ratio of dist/speed is less than 2 and | |
;; adding a line showing the threshold. | |
;; use with-data macro, passing it the 'cars' sample data. Use the $where function to filter the data, using a $fn, | |
;; which is a bit of syntax sugar for defining functions that operate on data sets. | |
;; ($fn [x y] (...)) => (fn [{:keys x y}] (...)) | |
;; The parameter names correspond to the column names of the dataset. | |
(use '(incanter core datasets charts)) | |
(with-data (get-dataset :cars) | |
(doto (scatter-plot :speed :dist | |
:data ($where ($fn [speed dist] | |
($= dist / speed < 2)))) | |
(add-points :speed :dist | |
:data ($where ($fn [speed dist] | |
($= dist / speed >= 2)))) | |
(add-lines ($ :speed) ($= 2 * ($ :speed))) | |
view)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment