You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As described in the publication by Van Benthem and Keenan (10.1002/cem.889), which is in turn based on the active-set method algorithm previously published by Lawson and Hanson. The basic active-set method is implemented in the nnls repository.
Given the matrices $\mathbf{X}$ and $\mathbf{Y}$, the code finds the matrix $\mathbf{K}$ that minimises the squared Frobenius norm $$\mathrm{argmin}_K ||\mathbf{XK} -\mathbf{Y}||^2_F$$ subject to $\mathbf{K}\geq 0$.
import{fcnnlsVector}from'ml-fcnnls';constX=[[1,1,2],[10,11,-9],[-1,0,0],[-5,6,-7],];consty=[-1,11,0,1];constk=fcnnlsVector(X,y).K.to1DArray();/* k = [0.4610, 0.5611, 0] */
Multiple RHS, using Matrix instances as inputs.
import{fcnnls}from'ml-fcnnls';import{Matrix}from'ml-matrix';//npm i ml-matrix// Example with multiple RHSconstX=newMatrix([[1,1,2],[10,11,-9],[-1,0,0],[-5,6,-7],]);// Y can either be a Matrix or an array of arraysconstY=newMatrix([[-1,0,0,9],[11,-20,103,5],[0,0,0,0],[1,2,3,4],]);constK=fcnnls(X,Y).K;// `K.to2DArray()` converts the matrix to array./*K = Matrix([ [0.4610, 0, 4.9714, 0], [0.5611, 0, 4.7362, 2.2404], [0, 1.2388, 0, 1.9136],])*/
Using the options
constK=fcnnls(X,Y,{info: true,// returns the error/iteration.maxIterations: 5,gradientTolerance: 0,});/* same result than 2*/