import $ivy.`com.github.haifengl::smile-scala:3.1.0` import $ivy.`org.slf4j:slf4j-simple:2.0.13` import java.awt.Color.{BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW} import smile.interpolation._ import smile.plot.swing._ import smile.plot.show import smile.plot.Render._ val x = Array(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0) val y = Array(0.0, 0.8415, 0.9093, 0.1411, -0.7568, -0.9589, -0.2794) val controls = Array.ofDim[Double](x.length, 2) for (i <- 0 until x.length) { controls(i)(0) = x(i); controls(i)(1) = y(i); } val linear = new LinearInterpolation(x, y) val data = (0 to 60).map { i => val x = i * 0.1 val y = linear.interpolate(x) Array(x, y) }.toArray val canvas = plot(controls, '*') canvas.add(LinePlot.of(data, Line.Style.SOLID, BLUE, "Linear")) show(canvas) val cubic = new CubicSplineInterpolation1D(x, y) val data = (0 to 60).map { i => val x = i * 0.1 val y = cubic.interpolate(x) Array(x, y) }.toArray canvas.add(LinePlot.of(data, Line.Style.DASH, RED, "Cubic")) show(canvas) val kriging = new KrigingInterpolation1D(x, y) val data = Array.ofDim[Double](61, 2) for (i <- 0 to 60) { data(i)(0) = i * 0.1 data(i)(1) = kriging.interpolate(data(i)(0)) } canvas.add(LinePlot.of(data, Line.Style.DOT, CYAN, "Kriging")) show(canvas) val rbf = new RBFInterpolation1D(x, y, new smile.math.rbf.GaussianRadialBasis()) val data = Array.ofDim[Double](61, 2) for (i <- 0 to 60) { data(i)(0) = i * 0.1 data(i)(1) = rbf.interpolate(data(i)(0)) } canvas.add(LinePlot.of(data, Line.Style.DOT_DASH, GREEN, "RBF")) show(canvas) val shepard = new ShepardInterpolation1D(x, y, 3) val data = Array.ofDim[Double](61, 2) for (i <- 0 to 60) { data(i)(0) = i * 0.1 data(i)(1) = shepard.interpolate(data(i)(0)) } canvas.add(LinePlot.of(data, Line.Style.LONG_DASH, PINK, "Shepard")) show(canvas) val x1 = Array(1950.0, 1960, 1970, 1980, 1990) val x2 = Array(10.0, 20, 30) val y = Array( Array(150.697, 199.592, 187.625), Array(179.323, 195.072, 250.287), Array(203.212, 179.092, 322.767), Array(226.505, 153.706, 426.730), Array(249.633, 120.281, 598.243) ) val canvas = heatmap(y, Palette.jet(256)) canvas.setTitle("Original") show(canvas) val cubic = new CubicSplineInterpolation2D(x1, x2, y) val data = Array.ofDim[Double](101, 101) for (i <- 0 to 100; j <- 0 to 100) data(i)(j) = cubic.interpolate(1950 + i*0.4, 10 + j*0.2) val cubicPlot = heatmap(data, Palette.jet(256)) cubicPlot.setTitle("Cubic") show(cubicPlot) val bicubic = new BicubicInterpolation(x1, x2, y) for (i <- 0 to 100; j <- 0 to 100) data(i)(j) = bicubic.interpolate(1950 + i*0.4, 10 + j*0.2) val bicubicPlot = heatmap(data, Palette.jet(256)) bicubicPlot.setTitle("Bicubic") show(bicubicPlot) val zz = Array.ofDim[Double](101, 101) val ww = Array.ofDim[Double](101, 101) for (i <- 0 to 100; j <- 0 to 100) { zz(i)(j) = if (java.lang.Math.random() < 0.2) Double.NaN else data(i)(j) ww(i)(j) = zz(i)(j) } val canvas = heatmap(ww, Palette.jet(256)) canvas.setTitle("Missing Values") show(canvas) LaplaceInterpolation.interpolate(zz) val canvas = heatmap(zz, Palette.jet(256)) canvas.setTitle("Laplace") show(canvas)