|
| 1 | +import * as os from 'node:os'; |
| 2 | + |
1 | 3 | import { describe, expect, it, test } from 'vitest'; |
2 | 4 |
|
3 | 5 | import { levenbergMarquardt } from '../index.js'; |
@@ -177,37 +179,45 @@ describe('curve', () => { |
177 | 179 | }); |
178 | 180 | }); |
179 | 181 |
|
180 | | - it('should return solution with lowest error', () => { |
181 | | - const data = { |
182 | | - x: [ |
183 | | - 0, 0.6283185307179586, 1.2566370614359172, 1.8849555921538759, |
184 | | - 2.5132741228718345, 3.141592653589793, 3.7699111843077517, |
185 | | - 4.39822971502571, 5.026548245743669, 5.654866776461628, |
186 | | - ], |
187 | | - y: [ |
188 | | - 0, 1.902113032590307, 1.1755705045849465, -1.175570504584946, |
189 | | - -1.9021130325903073, -4.898587196589413e-16, 1.902113032590307, |
190 | | - 1.1755705045849467, -1.1755705045849456, -1.9021130325903075, |
191 | | - ], |
192 | | - }; |
193 | | - const options = { |
194 | | - damping: 1.5, |
195 | | - initialValues: [0.594398586701882, 0.3506424963635226], |
196 | | - gradientDifference: 1e-2, |
197 | | - maxIterations: 100, |
198 | | - errorTolerance: 1e-2, |
199 | | - }; |
| 182 | + it( |
| 183 | + 'should return solution with lowest error', |
| 184 | + { |
| 185 | + // On macOS, `Math.sin` gives different results for some values. |
| 186 | + // Refs: https://issues.chromium.org/issues/333194604 |
| 187 | + fails: os.platform() === 'darwin', |
| 188 | + }, |
| 189 | + () => { |
| 190 | + const data = { |
| 191 | + x: [ |
| 192 | + 0, 0.6283185307179586, 1.2566370614359172, 1.8849555921538759, |
| 193 | + 2.5132741228718345, 3.141592653589793, 3.7699111843077517, |
| 194 | + 4.39822971502571, 5.026548245743669, 5.654866776461628, |
| 195 | + ], |
| 196 | + y: [ |
| 197 | + 0, 1.902113032590307, 1.1755705045849465, -1.175570504584946, |
| 198 | + -1.9021130325903073, -4.898587196589413e-16, 1.902113032590307, |
| 199 | + 1.1755705045849467, -1.1755705045849456, -1.9021130325903075, |
| 200 | + ], |
| 201 | + }; |
| 202 | + const options = { |
| 203 | + damping: 1.5, |
| 204 | + initialValues: [0.594398586701882, 0.3506424963635226], |
| 205 | + gradientDifference: 1e-2, |
| 206 | + maxIterations: 100, |
| 207 | + errorTolerance: 1e-2, |
| 208 | + }; |
200 | 209 |
|
201 | | - const actual = levenbergMarquardt(data, sinFunction, options); |
202 | | - const manualCalculatedError = data.x |
203 | | - .map(sinFunction(actual.parameterValues)) |
204 | | - .reduce((acc, yHat, i) => acc + (data.y[i] - yHat) ** 2, 0); |
205 | | - expect(actual.parameterError).toBeCloseTo( |
206 | | - manualCalculatedError, |
207 | | - options.errorTolerance, |
208 | | - ); |
209 | | - expect(actual.parameterError).toBeCloseTo(15.5, options.errorTolerance); |
210 | | - }); |
| 210 | + const actual = levenbergMarquardt(data, sinFunction, options); |
| 211 | + const manualCalculatedError = data.x |
| 212 | + .map(sinFunction(actual.parameterValues)) |
| 213 | + .reduce((acc, yHat, i) => acc + (data.y[i] - yHat) ** 2, 0); |
| 214 | + expect(actual.parameterError).toBeCloseTo( |
| 215 | + manualCalculatedError, |
| 216 | + options.errorTolerance, |
| 217 | + ); |
| 218 | + expect(actual.parameterError).toBeCloseTo(15.5, options.errorTolerance); |
| 219 | + }, |
| 220 | + ); |
211 | 221 | }); |
212 | 222 |
|
213 | 223 | describe('"Real-world" problems (noisy data)', () => { |
|
0 commit comments