Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('X may be recomputed', function () {
let U = pca.predict(iris);
let V = pca.getLoadings();
let S = pca.getEigenvalues();
// we scale the scores
let SU = U.divRowVector(S);
// we recompute X
let RX = SU.mmul(Matrix.diag(S)).mmul(V);
expect(RX.get(0, 0)).toBeCloseTo(-0.89767388, 6);
});
_computeWithNIPALS(dataset, nCompNIPALS) {
this.U = new Matrix(nCompNIPALS, dataset.columns);
this.S = [];
let x = dataset;
for (let i = 0; i < nCompNIPALS; i++) {
let dc = new NIPALS(x);
this.U.setRow(i, dc.w.transpose());
this.S.push(Math.pow(dc.s.get(0, 0), 2));
x = dc.xResidual;
}
this.U = this.U.transpose(); // to be compatible with API
}
}
[x2, y2, 1, 0, 0, 0, -x2 * X2, -y2 * X2],
[x3, y3, 1, 0, 0, 0, -x3 * X3, -y1 * X3],
[x4, y4, 1, 0, 0, 0, -x4 * X4, -y4 * X4],
[0, 0, 0, x1, y1, 1, -x1 * Y1, -y1 * Y1],
[0, 0, 0, x2, y2, 1, -x2 * Y2, -y2 * Y2],
[0, 0, 0, x3, y3, 1, -x3 * Y3, -y3 * Y3],
[0, 0, 0, x4, y4, 1, -x4 * Y4, -y4 * Y4]
]);
let D = Matrix.columnVector([X1, X2, X3, X4, Y1, Y2, Y3, Y4]);
let svd = new SingularValueDecomposition(S);
let T = svd.solve(D); // solve S*T = D
let [a, b, c, d, e, f, g, h] = T.to1DArray();
let Xt = new Matrix(heightRect, widthRect);
for (let channel = 0; channel < this.channels; channel++) {
for (let i = 0; i < heightRect; i++) {
for (let j = 0; j < widthRect; j++) {
Xt.set(i, j, projectionPoint(i, j, a, b, c, d, e, f, g, h, this, channel));
}
}
newImage.setMatrix(Xt, { channel: channel });
}
return newImage;
}
} else {
widthRect = Math.ceil(Math.max(distance2Points(tl, tr), distance2Points(bl, br)));
heightRect = Math.ceil(Math.max(distance2Points(tl, bl), distance2Points(tr, br)));
}
let newImage = Image.createFrom(this, { width: widthRect, height: heightRect });
let [X1, Y1] = tl;
let [X2, Y2] = tr;
let [X3, Y3] = br;
let [X4, Y4] = bl;
let [x1, y1] = [0, 0];
let [x2, y2] = [0, widthRect - 1];
let [x3, y3] = [heightRect - 1, widthRect - 1];
let [x4, y4] = [heightRect - 1, 0];
let S = new Matrix([
[x1, y1, 1, 0, 0, 0, -x1 * X1, -y1 * X1],
[x2, y2, 1, 0, 0, 0, -x2 * X2, -y2 * X2],
[x3, y3, 1, 0, 0, 0, -x3 * X3, -y1 * X3],
[x4, y4, 1, 0, 0, 0, -x4 * X4, -y4 * X4],
[0, 0, 0, x1, y1, 1, -x1 * Y1, -y1 * Y1],
[0, 0, 0, x2, y2, 1, -x2 * Y2, -y2 * Y2],
[0, 0, 0, x3, y3, 1, -x3 * Y3, -y3 * Y3],
[0, 0, 0, x4, y4, 1, -x4 * Y4, -y4 * Y4]
]);
let D = Matrix.columnVector([X1, X2, X3, X4, Y1, Y2, Y3, Y4]);
let svd = new SingularValueDecomposition(S);
let T = svd.solve(D); // solve S*T = D
let [a, b, c, d, e, f, g, h] = T.to1DArray();
function ellipse(width, height, options) {
const matrix = Matrix.zeros(height, width, options);
let yEven = 1 - height % 2;
let xEven = 1 - width % 2;
let a = Math.floor((width - 1) / 2); // horizontal ellipse axe
let b = Math.floor((height - 1) / 2); // vertical ellipse axe
let a2 = a * a;
let b2 = b * b;
if (options.filled) {
for (let y = 0; y <= b; y++) {
let shift = Math.floor(Math.sqrt(a2 - a2 * y * y / b2));
for (let x = a - shift; x <= a; x++) {
matrix.set(b - y, x, 1);
matrix.set(b + y + yEven, x, 1);
matrix.set(b - y, width - x - 1, 1);
matrix.set(b + y + yEven, width - x - 1, 1);
}
}
forward(input) {
this.input = input.slice();
this.input.push(1); // bias
var offs = 0; // offset used to get the current weights in the current perceptron
this.output = Matrix.zeros(1, this.output.length).getRow(0);
for (var i = 0; i < this.output.length; ++i) {
for (var j = 0; j < this.input.length; ++j) {
this.output[i] += this.weights[offs + j] * this.input[j];
}
if (this.isSigmoid)
this.output[i] = sigmoid(this.output[i]);
offs += this.input.length;
}
return this.output.slice();
}
for (let x = 0; x < cutWidth; x++) {
const wOffset = (x + kernelOffsetX) * channels;
for (let y = 0; y < height; y++) {
columnData[y] = convolvedData[y * cutWidth + x];
}
const result = columnConvolution.convolve(columnData);
for (let i = 0; i < result.length; i++) {
const idx = (i + kernelOffsetY) * hFactor + wOffset + c;
newImage.data[idx] = round(clamp(result[i]));
}
}
}
// Calculate kernel from separated kernels.
const matrixX = Matrix.rowVector(kernelX);
const matrixY = Matrix.columnVector(kernelY);
const kernel = matrixY.mmul(matrixX).to2DArray();
// Apply convolution on the left and right borders
for (let c = 0; c < channels; c++) {
for (let bY = 0; bY < height; bY++) {
for (let bX = 0; bX < kernelOffsetX; bX++) {
const idx = (bY * width + bX) * channels + c;
const bXopp = width - bX - 1;
const bYopp = height - bY - 1;
const idxOpp = (bYopp * width + bXopp) * channels + c;
newImage.data[idx] = computeConvolutionPixel(
bX,
bY,
constructor(inputs, outputs, options) {
super();
if (inputs === true) {
// reloading model
this.alpha = outputs.alpha;
this.inputs = outputs.inputs;
this.kernelType = outputs.kernelType;
this.kernelOptions = outputs.kernelOptions;
this.kernel = new Kernel(outputs.kernelType, outputs.kernelOptions);
} else {
inputs = Matrix.checkMatrix(inputs);
options = Object.assign({}, defaultOptions, options);
const kernelFunction = new Kernel(
options.kernelType,
options.kernelOptions
);
const K = kernelFunction.compute(inputs);
const n = inputs.rows;
K.add(Matrix.eye(n, n).mul(options.lambda));
this.alpha = solve(K, outputs);
this.inputs = inputs;
this.kernelType = options.kernelType;
this.kernelOptions = options.kernelOptions;
this.kernel = kernelFunction;
}
var selectedFunction = ACTIVATION_FUNCTIONS[options.activation];
var params = selectedFunction.activation.length;
var actFunction = params > 1 ? (val) => selectedFunction.activation(val, options.activationParam) : selectedFunction.activation;
var derFunction = params > 1 ? (val) => selectedFunction.derivate(val, options.activationParam) : selectedFunction.derivate;
this.activationFunction = function (i, j) {
this.set(i, j, actFunction(this.get(i, j)));
};
this.derivate = function (i, j) {
this.set(i, j, derFunction(this.get(i, j)));
};
if (options.model) {
// load model
this.W = Matrix.checkMatrix(options.W);
this.b = Matrix.checkMatrix(options.b);
} else {
// default constructor
this.W = Matrix.rand(this.inputSize, this.outputSize);
this.b = Matrix.zeros(1, this.outputSize);
this.W.apply(function (i, j) {
this.set(i, j, this.get(i, j) / Math.sqrt(options.inputSize));
});
}
}
var params = selectedFunction.activation.length;
var actFunction = params > 1 ? (val) => selectedFunction.activation(val, options.activationParam) : selectedFunction.activation;
var derFunction = params > 1 ? (val) => selectedFunction.derivate(val, options.activationParam) : selectedFunction.derivate;
this.activationFunction = function (i, j) {
this.set(i, j, actFunction(this.get(i, j)));
};
this.derivate = function (i, j) {
this.set(i, j, derFunction(this.get(i, j)));
};
if (options.model) {
// load model
this.W = Matrix.checkMatrix(options.W);
this.b = Matrix.checkMatrix(options.b);
} else {
// default constructor
this.W = Matrix.rand(this.inputSize, this.outputSize);
this.b = Matrix.zeros(1, this.outputSize);
this.W.apply(function (i, j) {
this.set(i, j, this.get(i, j) / Math.sqrt(options.inputSize));
});
}
}