Database Reference
In-Depth Information
Using inix formulas in Incanter
There's a lot to like about lisp: macros, the simple syntax, and the rapid development cycle.
Most of the time, it is ine if you treat math operators as functions and use preix notations,
which is a consistent, function-irst syntax. This allows you to treat math operators in the same
way as everything else so that you can pass them to reduce , or anything else you want to do.
However, we're not taught to read math expressions using preix notations (with the operator
irst). And especially when formulas get even a little complicated, tracing out exactly what's
happening can get hairy.
Getting ready
For this recipe we'll just need Incanter in our project.clj ile, so we'll use the
dependencies statement—as well as the use statement—from the Loading Clojure data
structures into datasets recipe.
For data, we'll use the matrix that we created in the Converting datasets to matrices recipe.
How to do it…
Incanter has a macro that converts a standard math notation to a lisp notation. We'll explore
that in this recipe:
1.
The $= macro changes its contents to use an inix notation, which is what we're used
to from math class:
user=> ($= 7 * 4)
28
user=> ($= 7 * 4 + 3)
31
2.
We can also work on whole matrixes or just parts of matrixes. In this example, we
perform a scalar multiplication of the matrix:
user=> ($= va-matrix * 4)
A 591x3 matrix
---------------
3.28e+04 1.71e+04 8.22e+03
2.08e+03 9.16e+02 4.68e+02
1.19e+03 6.52e+02 3.08e+02
...
 
Search WWH ::




Custom Search