Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// getting optimal thetas using normal equation
var thetas = normalEqn(X, y);
// creating a model that accepts
const model = function (X) {
// create predictions by multiplying theta^T * X, creating a model that looks like (theta_1 * x_1) + (theta_2 * x_2) + (theta_3 * x_3) etc.
return math.multiply(math.transpose(thetas), X);
}
return model;
}
/* TESTS */
// test values for X and y
var Xmatrix = math.matrix([[2, 1, 3], [7, 1, 9], [1, 8, 1], [3, 7, 4]]);
var ylabels = math.matrix([[2], [5], [5], [6]]);
// should show thetas (in the _data part of object) to be 0.008385744234748138, 0.5681341719077577, 0.4863731656184376
console.log(normalEqn(Xmatrix, ylabels));
// test values #2
Xmatrix = math.matrix([[1], [2], [3], [4]]);
ylabels = math.matrix([[1], [2], [3], [4]]);
// should show theta of 1 (which forms a perfectly diagonal line if plotted)
console.log(normalEqn(Xmatrix, ylabels));
// test values #3
Xmatrix = math.matrix([[1, 5], [1, 2], [1, 4], [1, 5]]);
ylabels = math.matrix([[1], [6], [4], [2]]);
// matrices
// load math.js (using node.js)
const math = require('mathjs')
// create matrices and arrays. a matrix is just a wrapper around an Array,
// providing some handy utilities.
console.log('create a matrix')
const a = math.matrix([1, 4, 9, 16, 25])
print(a) // [1, 4, 9, 16, 25]
const b = math.matrix(math.ones([2, 3]))
print(b) // [[1, 1, 1], [1, 1, 1]]
print(b.size()) // [2, 3]
// the Array data of a Matrix can be retrieved using valueOf()
const array = a.valueOf()
print(array) // [1, 4, 9, 16, 25]
// Matrices can be cloned
const clone = a.clone()
print(clone) // [1, 4, 9, 16, 25]
console.log()
// perform operations with matrices
console.log('perform operations')
print(math.sqrt(a)) // [1, 2, 3, 4, 5]
const c = [1, 2, 3, 4, 5]
console.log('\n');
// Matrix Scalar Division
const matrixH = math.matrix([[2, 4], [6, 2], [4, -4]]);
const matrixH2 = math.divide(matrixH, 2);
console.log('Matrix Scalar Division:');
console.log(matrixH2.valueOf());
console.log('\n');
// Matrix-Vector Multiplication
const matrixI = math.matrix([[0, 1], [2, 3], [4, 5]]);
const matrixJ = math.matrix([[2], [1]]);
const matrixIJ = math.multiply(matrixI, matrixJ);
console.log('Matrix-Vector Multiplication:');
console.log(matrixIJ.valueOf());
console.log('\n');
// Matrix Multiplication
const matrixK = math.matrix([[0, 1], [2, 3], [4, 5]]);
const matrixL = math.matrix([[2, 4], [6, 2]]);
const matrixKL = math.multiply(matrixK, matrixL);
console.log('Matrix Multiplication:');
console.log(matrixKL.valueOf());
const matrixV_T = math.transpose(matrixV);
console.log('Transpose Matrix:');
console.log(matrixV_T.valueOf());
console.log('\n');
// Example: Predicting Housing Prices with 3 competing Hypotheses
console.log('Predicting Housing Prices with 3 competing Hypotheses:');
console.log('const HOUSE_SIZES = [2104, 1416, 1534, 852];');
console.log('const h1 = x => -40 + 0.25 * x;');
console.log('const h2 = x => 200 + 0.1 * x;');
console.log('const h3 = x => -150 + 0.4 * x;');
console.log('\n');
const houseSizeMatrix = math.matrix([
[1, 2104],
[1, 1416],
[1, 1534],
[1, 852],
]);
const hypothesesMatrix = math.matrix([
[-40, 200, -150],
[0.25, 0.1, 0.4],
]);
const competingResults = math.multiply(houseSizeMatrix, hypothesesMatrix);
console.log('Column: Result for each Hypothesis');
console.log('Row: Result for each House Size');
console.log(competingResults.valueOf());
console.log('\n');
// Matrix Multiplication
const matrixK = math.matrix([[0, 1], [2, 3], [4, 5]]);
const matrixL = math.matrix([[2, 4], [6, 2]]);
const matrixKL = math.multiply(matrixK, matrixL);
console.log('Matrix Multiplication:');
console.log(matrixKL.valueOf());
console.log('\n');
// Matrix Division
const matrixY = math.matrix([[0, 2], [2, 4], [4, 6]]);
const matrixZ = math.matrix([[2, 1], [2, 2]]);
const matrixYZ = math.divide(matrixY, matrixZ);
console.log('Matrix Division:');
console.log(matrixYZ.valueOf());
console.log('\n');
// Matrix Property: Not Commutative
const matrixN = math.matrix([[0, 1], [2, 3], [4, 5]]);
const matrixO = math.matrix([[2, 4], [6, 2]]);
const matrixNO = math.multiply(matrixN, matrixO);
const matrixON = math.multiply(matrixO, matrixL);
export function create_beam_polygon_on_3dplane(radius, p0, p1, n_sides=6) {
// -> calc rot matrix
let distance = math.norm(math.subtract(math.matrix(p1), math.matrix(p0)));
let normal0 = math.multiply(1.0/distance, math.subtract(math.matrix(p0), math.matrix(p1))).valueOf();
let normal1 = math.multiply(1.0/distance, math.subtract(math.matrix(p1), math.matrix(p0))).valueOf();
//let argmax = normal.reduce((i_max, x, i, arr) => Math.abs(x) > Math.abs(arr[i_max]) ? i : i_max, 0);
//let a = normal.slice();
//[a[argmax], a[(argmax + 1)%3]] = [a[(argmax + 1)%3], a[argmax]];
//a = math.matrix(a)
//let b = math.cross(normal, a);
//let d0 = -math.dot(normal0, p0); // <-- plane equation
//let d1 = -math.dot(normal1, p1); // <-- plane equation
let project_to_3dplane = (x, y, n, d=0) => {
let k = -(n[0]*x + n[1]*y + d)/math.dot(n, n);
return [x + n[0]*k, y + k*n[1], k*n[2]];
};
export function create_beam_polygon_on_3dplane(radius, p0, p1, n_sides=6) {
// -> calc rot matrix
let distance = math.norm(math.subtract(math.matrix(p1), math.matrix(p0)));
let normal0 = math.multiply(1.0/distance, math.subtract(math.matrix(p0), math.matrix(p1))).valueOf();
let normal1 = math.multiply(1.0/distance, math.subtract(math.matrix(p1), math.matrix(p0))).valueOf();
//let argmax = normal.reduce((i_max, x, i, arr) => Math.abs(x) > Math.abs(arr[i_max]) ? i : i_max, 0);
//let a = normal.slice();
//[a[argmax], a[(argmax + 1)%3]] = [a[(argmax + 1)%3], a[argmax]];
//a = math.matrix(a)
//let b = math.cross(normal, a);
//let d0 = -math.dot(normal0, p0); // <-- plane equation
//let d1 = -math.dot(normal1, p1); // <-- plane equation
let project_to_3dplane = (x, y, n, d=0) => {
let k = -(n[0]*x + n[1]*y + d)/math.dot(n, n);
return [x + n[0]*k, y + k*n[1], k*n[2]];
};
let vertices = [];
describe('function weightedLeastSquare', function () {
const caseOne = {
x: [
[1, 1, 1, 1],
[1, 3, 5, 7]
],
y: [14, 17, 19, 20],
w: [1, 1, 1, 1],
expect: {
beta: math.matrix([13.5, 1]),
yhat: math.matrix([14.5, 16.5, 18.5, 20.5]),
residual: math.matrix([-0.5, 0.5, 0.5, -0.5])
}
}
const caseTwo = {
x: [
[1, 1, 1, 1],
[1, 3, 5, 7]
],
y: [14, 17, 19, 20],
w: [1, 3, 3, 1],
expect: {
beta: math.matrix([13.75, 1]),
yhat: math.matrix([14.75, 16.75, 18.75, 20.75]),
residual: math.matrix([-0.75, 0.25, 0.25, -0.75])
console.log('Predicting Housing Prices with 3 competing Hypotheses:');
console.log('const HOUSE_SIZES = [2104, 1416, 1534, 852];');
console.log('const h1 = x => -40 + 0.25 * x;');
console.log('const h2 = x => 200 + 0.1 * x;');
console.log('const h3 = x => -150 + 0.4 * x;');
console.log('\n');
const houseSizeMatrix = math.matrix([
[1, 2104],
[1, 1416],
[1, 1534],
[1, 852],
]);
const hypothesesMatrix = math.matrix([
[-40, 200, -150],
[0.25, 0.1, 0.4],
]);
const competingResults = math.multiply(houseSizeMatrix, hypothesesMatrix);
console.log('Column: Result for each Hypothesis');
console.log('Row: Result for each House Size');
console.log(competingResults.valueOf());
console.log('\n');
const makeG = (mux, muy, sigma) => {
const k = -1 / 2 * Math.pow(sigma, 2);
const data = myFunc(_linSpace, k, mux, muy);
return math.matrix(Array.prototype.map.call(data, (row) => Array.prototype.slice.call(row)));
}