(ns demo
(:require [fastmath.interpolation :as interpolation]
[scicloj.tableplot.v1.plotly :as plotly]
[tablecloth.api :as tc]))
(def px [0 1 3 4 5 8 9])
(def py [0 0 1 7 3 4 6])
(def in (tc/dataset {:x (range 0 8.0 0.01)}))
subset of available interpolations.
(def kinds [:linear
:cubic
:monotone
:akima
:neville
:divided-difference
:polynomial
:sprague
:step-before
:step-after])
kinds
[:linear
:cubic
:monotone
:akima
:neville
:divided-difference
:polynomial
:sprague
:step-before
:step-after]
(def results
(->> (for [k kinds]
(let [f (interpolation/interpolation k px py)]
[k (mapv f x)]))
(reduce (fn [acc [k v]]
(tc/add-column acc k v)) in)))
results
_unnamed [801 11]:
:x | :linear | :cubic | :monotone | :akima | :neville | :divided-difference | :polynomial | :sprague | :step-before | :step-after |
---|---|---|---|---|---|---|---|---|---|---|
0.00 | 0.00000000 | 0.00000000 | 0.00000000 | 0.00000000 | 0.00000000 | 0.00000000 | 0.00000000 | 0.00000000 | 0.0 | 0.0 |
0.01 | 0.00000000 | 0.00446629 | 0.00000000 | -0.00165000 | 0.22014219 | 0.22014219 | 0.22014219 | 0.00277052 | 0.0 | 0.0 |
0.02 | 0.00000000 | 0.00892990 | 0.00000000 | -0.00326667 | 0.43246557 | 0.43246557 | 0.43246557 | 0.00499778 | 0.0 | 0.0 |
0.03 | 0.00000000 | 0.01338816 | 0.00000000 | -0.00485000 | 0.63710020 | 0.63710020 | 0.63710020 | 0.00673977 | 0.0 | 0.0 |
0.04 | 0.00000000 | 0.01783837 | 0.00000000 | -0.00640000 | 0.83417486 | 0.83417486 | 0.83417486 | 0.00805164 | 0.0 | 0.0 |
0.05 | 0.00000000 | 0.02227786 | 0.00000000 | -0.00791667 | 1.02381716 | 1.02381716 | 1.02381716 | 0.00898572 | 0.0 | 0.0 |
0.06 | 0.00000000 | 0.02670395 | 0.00000000 | -0.00940000 | 1.20615347 | 1.20615347 | 1.20615347 | 0.00959164 | 0.0 | 0.0 |
0.07 | 0.00000000 | 0.03111396 | 0.00000000 | -0.01085000 | 1.38130896 | 1.38130896 | 1.38130896 | 0.00991630 | 0.0 | 0.0 |
0.08 | 0.00000000 | 0.03550521 | 0.00000000 | -0.01226667 | 1.54940760 | 1.54940760 | 1.54940760 | 0.01000402 | 0.0 | 0.0 |
0.09 | 0.00000000 | 0.03987503 | 0.00000000 | -0.01365000 | 1.71057218 | 1.71057218 | 1.71057218 | 0.00989652 | 0.0 | 0.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
7.90 | 3.96666667 | 3.76230369 | 3.94385424 | 3.84786640 | 2.16554602 | 2.16554602 | 2.16554602 | 3.91362364 | 4.0 | 3.0 |
7.91 | 3.97000000 | 3.78645412 | 3.94968130 | 3.86252746 | 2.34982188 | 2.34982188 | 2.34982188 | 3.92220521 | 4.0 | 3.0 |
7.92 | 3.97333333 | 3.81052613 | 3.95546286 | 3.87731014 | 2.53405268 | 2.53405268 | 2.53405268 | 3.93079788 | 4.0 | 3.0 |
7.93 | 3.97666667 | 3.83451739 | 3.96119829 | 3.89221484 | 2.71818748 | 2.71818748 | 2.71818748 | 3.93940212 | 4.0 | 3.0 |
7.94 | 3.98000000 | 3.85842558 | 3.96688694 | 3.90724200 | 2.90217458 | 2.90217458 | 2.90217458 | 3.94801845 | 4.0 | 3.0 |
7.95 | 3.98333333 | 3.88224837 | 3.97252815 | 3.92239203 | 3.08596148 | 3.08596148 | 3.08596148 | 3.95664736 | 4.0 | 3.0 |
7.96 | 3.98666667 | 3.90598344 | 3.97812128 | 3.93766535 | 3.26949488 | 3.26949488 | 3.26949488 | 3.96528938 | 4.0 | 3.0 |
7.97 | 3.99000000 | 3.92962847 | 3.98366568 | 3.95306239 | 3.45272072 | 3.45272072 | 3.45272072 | 3.97394508 | 4.0 | 3.0 |
7.98 | 3.99333333 | 3.95318112 | 3.98916070 | 3.96858357 | 3.63558411 | 3.63558411 | 3.63558411 | 3.98261502 | 4.0 | 3.0 |
7.99 | 3.99666667 | 3.97663907 | 3.99460569 | 3.98422929 | 3.81802937 | 3.81802937 | 3.81802937 | 3.99129979 | 4.0 | 3.0 |
8.00 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.00000000 | 4.0 | 3.0 |
(defn plot-it [data kind]
(-> data
(plotly/layer-line
{:=x :x
:=y kind})))
(for [k kinds]
`(~'plot-it ~'results ~k))
((plot-it results :linear)
(plot-it results :cubic)
(plot-it results :monotone)
(plot-it results :akima)
(plot-it results :neville)
(plot-it results :divided-difference)
(plot-it results :polynomial)
(plot-it results :sprague)
(plot-it results :step-before)
(plot-it results :step-after))
(plot-it results :linear)
(plot-it results :cubic)
(plot-it results :monotone)
(plot-it results :akima)
(plot-it results :neville)
(plot-it results :divided-difference)
(plot-it results :polynomial)
(plot-it results :sprague)
(plot-it results :step-before)
(plot-it results :step-after)
(def indices
(->> kinds (map-indexed (fn [idx x] [x idx])) (into {})))
(def wide-results
(-> (tc/pivot->longer results
(complement #{:x})
{:target-columns :interpolation
:value-column-name :y})
(tc/map-rows (fn [{:keys [interpolation] :as r}]
{:z (indices interpolation)}))))
wide-results
_unnamed [8010 4]:
:x | :interpolation | :y | :z |
---|---|---|---|
0.00 | :monotone | 0.00000000 | 2 |
0.01 | :monotone | 0.00000000 | 2 |
0.02 | :monotone | 0.00000000 | 2 |
0.03 | :monotone | 0.00000000 | 2 |
0.04 | :monotone | 0.00000000 | 2 |
0.05 | :monotone | 0.00000000 | 2 |
0.06 | :monotone | 0.00000000 | 2 |
0.07 | :monotone | 0.00000000 | 2 |
0.08 | :monotone | 0.00000000 | 2 |
0.09 | :monotone | 0.00000000 | 2 |
... | ... | ... | ... |
7.90 | :akima | 3.84786640 | 3 |
7.91 | :akima | 3.86252746 | 3 |
7.92 | :akima | 3.87731014 | 3 |
7.93 | :akima | 3.89221484 | 3 |
7.94 | :akima | 3.90724200 | 3 |
7.95 | :akima | 3.92239203 | 3 |
7.96 | :akima | 3.93766535 | 3 |
7.97 | :akima | 3.95306239 | 3 |
7.98 | :akima | 3.96858357 | 3 |
7.99 | :akima | 3.98422929 | 3 |
8.00 | :akima | 4.00000000 | 3 |
(-> wide-results
(plotly/layer-line
{:=x :x
:=y :y
:=color :interpolation }))
(-> wide-results
(plotly/layer-line
{:=x :x
:=y :y
:=z :z
:=color :interpolation
:=coordinates :3d}))
source: demo.clj