Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The fmin npm package is a JavaScript library for function minimization. It provides tools to find the minimum value of a function, which is useful in various optimization problems.
Unconstrained Minimization
This feature allows you to find the minimum of an unconstrained function using the Nelder-Mead algorithm. In this example, the function (x - 3)^2 is minimized, and the result should be close to x = 3.
const fmin = require('fmin');
function quadratic(x) {
return (x - 3) * (x - 3);
}
const result = fmin.nelderMead(quadratic, [0]);
console.log(result);
Multidimensional Minimization
This feature allows you to minimize functions of multiple variables. The example uses the Rosenbrock function, a common test problem for optimization algorithms, and finds the minimum in a 2D space.
const fmin = require('fmin');
function rosenbrock(x) {
return 100 * (x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0]) + (1 - x[0]) * (1 - x[0]);
}
const result = fmin.nelderMead(rosenbrock, [-1, 1]);
console.log(result);
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions, including optimization routines. Compared to fmin, math.js offers a broader set of mathematical tools but may be more complex to use for simple minimization tasks.
Numeric.js is a library for numerical computations in JavaScript. It includes functions for linear algebra, solving systems of equations, and optimization. While it offers similar minimization capabilities as fmin, it also provides additional numerical methods that can be useful for more complex problems.
Optimize-js is a library focused on optimization algorithms, including gradient descent and other methods. It provides more advanced optimization techniques compared to fmin, making it suitable for more complex or large-scale optimization problems.
Unconstrained function minimization in javascript.
This package implements some basic numerical optimization algorithms: Nelder-Mead, Gradient Descent, Wolf Line Search and Non-Linear Conjugate Gradient methods are all provided.
Interactive visualizations with D3 explaining how these algorithms work are also included in this package. Descriptions of the algorithms as well as most of the visualizations are available on my blog post An Interactive Tutorial on Numerical Optimization.
If you use NPM, npm install fmin
. Otherwise, download the latest release.
# nelderMead(f, initial)
Uses the Nelder-Mead method to minimize a function f starting at location initial.
Example usage minimizing the function f(x, y) = x2 + y2 + x sin y + y sin x is:
function loss(X) {
var x = X[0], y = X[1];
return Math.sin(y) * x + Math.sin(x) * y + x * x + y *y;
}
var solution = fmin.nelderMead(loss, [-3.5, 3.5]);
console.log("solution is at " + solution.x);
# conjugateGradient(f, initial)
Minimizes a function using the Polak–Ribière non-linear conjugate gradient method . The function f should compute both the loss and the gradient.
An example minimizing Rosenbrock's Banana function is:
function banana(X, fxprime) {
fxprime = fxprime || [0, 0];
var x = X[0], y = X[1];
fxprime[0] = 400 * x * x * x - 400 * y * x + 2 * x - 2;
fxprime[1] = 200 * y - 200 * x * x;
return (1 - x) * (1 - x) + 100 * (y - x * x) * (y - x * x);
}
var solution = fmin.conjugateGradient(banana, [-1, 1]);
console.log("solution is at " + solution.x);
FAQs
Unconstrained funcion minimization in Javascript
The npm package fmin receives a total of 116,858 weekly downloads. As such, fmin popularity was classified as popular.
We found that fmin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.