Unverified Commit 0117de40 authored by Greg Price's avatar Greg Price Committed by GitHub

Give PolynomialFit more docs, and a debug toString (#122333)

Give PolynomialFit more docs, and a debug toString
parent e0ea39fe
...@@ -70,12 +70,26 @@ class PolynomialFit { ...@@ -70,12 +70,26 @@ class PolynomialFit {
PolynomialFit(int degree) : coefficients = Float64List(degree + 1); PolynomialFit(int degree) : coefficients = Float64List(degree + 1);
/// The polynomial coefficients of the fit. /// The polynomial coefficients of the fit.
///
/// For each `i`, the element `coefficients[i]` is the coefficient of
/// the `i`-th power of the variable.
final List<double> coefficients; final List<double> coefficients;
/// An indicator of the quality of the fit. /// An indicator of the quality of the fit.
/// ///
/// Larger values indicate greater quality. /// Larger values indicate greater quality. The value ranges from 0.0 to 1.0.
///
/// The confidence is defined as the fraction of the dataset's variance
/// that is captured by variance in the fit polynomial. In statistics
/// textbooks this is often called "r-squared".
late double confidence; late double confidence;
@override
String toString() {
final String coefficientString =
coefficients.map((double c) => c.toStringAsPrecision(3)).toList().toString();
return '${objectRuntimeType(this, 'PolynomialFit')}($coefficientString, confidence: ${confidence.toStringAsFixed(3)})';
}
} }
/// Uses the least-squares algorithm to fit a polynomial to a set of data. /// Uses the least-squares algorithm to fit a polynomial to a set of data.
......
...@@ -69,4 +69,12 @@ void main() { ...@@ -69,4 +69,12 @@ void main() {
expect(approx(fit.confidence, 1.0), isTrue); expect(approx(fit.confidence, 1.0), isTrue);
}); });
test('Least-squares fit: toString', () {
final PolynomialFit fit = PolynomialFit(2);
fit.coefficients[0] = 123.45;
fit.coefficients[1] = 54.321;
fit.coefficients[2] = 1.3579;
fit.confidence = 0.9876;
expect(fit.toString(), 'PolynomialFit([123, 54.3, 1.36], confidence: 0.988)');
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment