30 lines
617 B
Go
30 lines
617 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestInterpLagrange(t *testing.T) {
|
|
x := []float64{0, 1, 2, 3, 4}
|
|
y := []float64{0, 1, 4, 9, 16}
|
|
|
|
interp := &LagrangeInterpolator{}
|
|
if err := interp.Fit(x, y); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
fmt.Println("x = 2.5, y =", interp.Predict(2.5))
|
|
fmt.Println("x = 5.5, y =", interp.Predict(5.5))
|
|
fmt.Println("x = 2, y =", interp.Predict(2.0))
|
|
|
|
p := &PolynomialInterpolator{}
|
|
if err := p.Fit(x, y); err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
fmt.Println("x = 2.5, y =", p.Predict(2.5))
|
|
fmt.Println("x = 5.5, y =", p.Predict(5.5))
|
|
fmt.Println("x = 2, y =", p.Predict(2.0))
|
|
}
|