Small math? Simple math? Or supplemental math? Canonically, "SMath" is pronounced "smath" and stands for "small math (library.)" Similar to JavaScript's builtin Math object, SMath exports one global object with several math-related helper functions. There is no need to instantiate the class, just call functions directly. See the examples below to get started using SMath!
SMath is also packaged with an executabe that can be run directly through npx in the terminal - even outside of a NodeJS project! In fact, open your terminal now, and type the following to show a list of valid npx smath commands!
npx smath help
Commands are all structured like this.
npx smath [cmd] [args]
This example command returns the value 0.4.
npx smath normalize 4 0 10
Most
SMathfunctions are available throughnpx, except for calculus functions which require a functional argument.
SMath also exports the DataFit object which contains only 1 function, fit() which is used for curve fitting. All other exports are purely for defining types used within fit(). fit() uses a genetic-style algorithm to fit a curve.
It generates many sets of parameters to test, and keeps ones with smaller error than the previous iteration. Parameter sets with larger errors are discarded. Each subsequent iteration uses the sets of parameters with the least error and "mutates" them randomly.
Because of these random mutations, running the same code multiple times may yield slightly different results. See best practices for mitigation tactics.
The folling factors affect computation time and resources:
Each one alone has a linear effect on performance, but combined has an incremental effect.
time = iterations * ( parameters + dataset )
The dimensionality, or number of free x variables per data point, should not have an impact on computation time.
For production software that relies on a best curve fit for data, it's best to avoid critical operations using fit() for a few reasons.
fit() uses an algorithm that generates random mutations in a set of parameters, which could yield slightly different results, even if run on the same dataset.To circumvent some of these issues, the following is recommended.
datafit during the testing phase of application development, and use the best-fit parameters as constants in the final application.datafit may be helpful for determining an initial guess of curve fit constants, which can be input to fit() during production. The number of iterations could be reduced if the initial guess is reasonably close to the desired result.datafit primarily for data visualization or rough estimation.datafit, a suggestion would be to run multiple iterations of fit() itself, and using each output as the subsequent call's input. This will converge to a result more effectively but could take longer.And here are some general good practices.
fit() allows this for the rare chance that it is needed. Typically, having more, accurate data, is better for curve fitting.fit() does not do this. Any outliers existing in the dataset will be treated like normal data points and could negatively impact the best fit.There are also some great arguments and use cases for this function, namely...
Small math function library