Skip to content

Commit 0117de4

Browse files
authored
Give PolynomialFit more docs, and a debug toString (flutter#122333)
Give PolynomialFit more docs, and a debug toString
1 parent e0ea39f commit 0117de4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/flutter/lib/src/gestures/lsq_solver.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,26 @@ class PolynomialFit {
7070
PolynomialFit(int degree) : coefficients = Float64List(degree + 1);
7171

7272
/// The polynomial coefficients of the fit.
73+
///
74+
/// For each `i`, the element `coefficients[i]` is the coefficient of
75+
/// the `i`-th power of the variable.
7376
final List<double> coefficients;
7477

7578
/// An indicator of the quality of the fit.
7679
///
77-
/// Larger values indicate greater quality.
80+
/// Larger values indicate greater quality. The value ranges from 0.0 to 1.0.
81+
///
82+
/// The confidence is defined as the fraction of the dataset's variance
83+
/// that is captured by variance in the fit polynomial. In statistics
84+
/// textbooks this is often called "r-squared".
7885
late double confidence;
86+
87+
@override
88+
String toString() {
89+
final String coefficientString =
90+
coefficients.map((double c) => c.toStringAsPrecision(3)).toList().toString();
91+
return '${objectRuntimeType(this, 'PolynomialFit')}($coefficientString, confidence: ${confidence.toStringAsFixed(3)})';
92+
}
7993
}
8094

8195
/// Uses the least-squares algorithm to fit a polynomial to a set of data.

packages/flutter/test/gestures/lsq_solver_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ void main() {
6969
expect(approx(fit.confidence, 1.0), isTrue);
7070
});
7171

72+
test('Least-squares fit: toString', () {
73+
final PolynomialFit fit = PolynomialFit(2);
74+
fit.coefficients[0] = 123.45;
75+
fit.coefficients[1] = 54.321;
76+
fit.coefficients[2] = 1.3579;
77+
fit.confidence = 0.9876;
78+
expect(fit.toString(), 'PolynomialFit([123, 54.3, 1.36], confidence: 0.988)');
79+
});
7280
}

0 commit comments

Comments
 (0)