diff --git a/packages/flutter/lib/src/gestures/lsq_solver.dart b/packages/flutter/lib/src/gestures/lsq_solver.dart index 6bc3b40674..46a05d7036 100644 --- a/packages/flutter/lib/src/gestures/lsq_solver.dart +++ b/packages/flutter/lib/src/gestures/lsq_solver.dart @@ -70,12 +70,26 @@ class PolynomialFit { PolynomialFit(int degree) : coefficients = Float64List(degree + 1); /// 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 coefficients; /// 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; + + @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. diff --git a/packages/flutter/test/gestures/lsq_solver_test.dart b/packages/flutter/test/gestures/lsq_solver_test.dart index 0254f4ff80..90d3284973 100644 --- a/packages/flutter/test/gestures/lsq_solver_test.dart +++ b/packages/flutter/test/gestures/lsq_solver_test.dart @@ -69,4 +69,12 @@ void main() { 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)'); + }); }