Files
tensor/linalg/fitpoly_test.go
petrbalvin af4ee19703
Release / gates (push) Successful in 4m38s
Test / test (push) Successful in 5m16s
Release / release (push) Successful in 35s
feat: initial release
Assisted-by: GLM 5.3 Flash
2026-09-03 10:00:00 +02:00

34 lines
953 B
Go

// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
package linalg
import (
"math"
"testing"
"sourcedock.dev/petrbalvin/tensor/internal/core"
)
// TestFitEvaluatePolynomial moved with FitPolynomial from the root
// package: a quadratic fit of y = 2 + x must recover it perfectly.
func TestFitEvaluatePolynomial(t *testing.T) {
x := mustFloats(t, []float64{0, 1, 2}, 3)
y := mustFloats(t, []float64{2, 3, 4}, 3)
coeffs, err := FitPolynomial(x, y, 1)
if err != nil {
t.Fatal(err)
}
if math.Abs(coeffs.FloatAt(0)-2) > 1e-9 || math.Abs(coeffs.FloatAt(1)-1) > 1e-9 {
t.Errorf("poly coeffs: %v", coeffs.RawFloats())
}
q, _ := core.FromFloats([]float64{5, 7}, 2)
out, err := core.EvaluatePolynomial(coeffs, q)
if err != nil {
t.Fatal(err)
}
if math.Abs(out.FloatAt(0)-7) > 1e-9 || math.Abs(out.FloatAt(1)-9) > 1e-9 {
t.Errorf("poly eval: %v", out.RawFloats())
}
}