(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.000.000000000.000000000.000000000.000000000.000000000.000000000.000000000.000000000.00.0
0.010.000000000.004466290.00000000-0.001650000.220142190.220142190.220142190.002770520.00.0
0.020.000000000.008929900.00000000-0.003266670.432465570.432465570.432465570.004997780.00.0
0.030.000000000.013388160.00000000-0.004850000.637100200.637100200.637100200.006739770.00.0
0.040.000000000.017838370.00000000-0.006400000.834174860.834174860.834174860.008051640.00.0
0.050.000000000.022277860.00000000-0.007916671.023817161.023817161.023817160.008985720.00.0
0.060.000000000.026703950.00000000-0.009400001.206153471.206153471.206153470.009591640.00.0
0.070.000000000.031113960.00000000-0.010850001.381308961.381308961.381308960.009916300.00.0
0.080.000000000.035505210.00000000-0.012266671.549407601.549407601.549407600.010004020.00.0
0.090.000000000.039875030.00000000-0.013650001.710572181.710572181.710572180.009896520.00.0
.................................
7.903.966666673.762303693.943854243.847866402.165546022.165546022.165546023.913623644.03.0
7.913.970000003.786454123.949681303.862527462.349821882.349821882.349821883.922205214.03.0
7.923.973333333.810526133.955462863.877310142.534052682.534052682.534052683.930797884.03.0
7.933.976666673.834517393.961198293.892214842.718187482.718187482.718187483.939402124.03.0
7.943.980000003.858425583.966886943.907242002.902174582.902174582.902174583.948018454.03.0
7.953.983333333.882248373.972528153.922392033.085961483.085961483.085961483.956647364.03.0
7.963.986666673.905983443.978121283.937665353.269494883.269494883.269494883.965289384.03.0
7.973.990000003.929628473.983665683.953062393.452720723.452720723.452720723.973945084.03.0
7.983.993333333.953181123.989160703.968583573.635584113.635584113.635584113.982615024.03.0
7.993.996666673.976639073.994605693.984229293.818029373.818029373.818029373.991299794.03.0
8.004.000000004.000000004.000000004.000000004.000000004.000000004.000000004.000000004.03.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:monotone0.000000002
0.01:monotone0.000000002
0.02:monotone0.000000002
0.03:monotone0.000000002
0.04:monotone0.000000002
0.05:monotone0.000000002
0.06:monotone0.000000002
0.07:monotone0.000000002
0.08:monotone0.000000002
0.09:monotone0.000000002
............
7.90:akima3.847866403
7.91:akima3.862527463
7.92:akima3.877310143
7.93:akima3.892214843
7.94:akima3.907242003
7.95:akima3.922392033
7.96:akima3.937665353
7.97:akima3.953062393
7.98:akima3.968583573
7.99:akima3.984229293
8.00:akima4.000000003
(-> 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