Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mecks

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mecks - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

lib/constants.js

14

gulpfile.js

@@ -6,2 +6,3 @@ var gulp = require('gulp');

var jscs = require('gulp-jscs');
var istanbul = require('gulp-istanbul');
var runSequence = require('run-sequence');

@@ -43,1 +44,14 @@

});
gulp.task('coverage', function (cb) {
gulp.src(srcFiles)
.pipe(istanbul()) // Covering files
.pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', function () {
gulp.src(unitTestFiles)
.pipe(mocha())
.pipe(istanbul.writeReports()) // Creating the reports after tests runned
.on('end', cb);
});
});

8

index.js

@@ -1,3 +0,5 @@

var Matrix2 = require('./lib/matrix2');
module.exports.Matrix2 = Matrix2;
exports.Matrix2 = require('./lib/matrix2');
exports.Matrix3 = require('./lib/matrix3');
exports.Matrix4 = require('./lib/matrix4');
exports.Plane = require('./lib/plane');
exports.Constants = require('./lib/constants');

@@ -0,7 +1,43 @@

/** @module Matrix2 */
'use strict';
var Vec2 = require('vecks').Vec2;
var EPSILON = 1e-10;
var I_FACTOR = 2;
var EPSILON = require('./constants').EPSILON;
var ROW_COL_LENGTH = 2;
var NUM_ELEMS = 4;
/**
* Null Matrix Data
*
* @type {number[]}
*/
var nullMatrixData = [
0, 0,
0, 0
];
/**
* Identity Matrix Data
* @type {number[]}
*/
var identityMatrixData = [
1, 0,
0, 1
];
function mulS(data, s){
for (var n = 0; n < NUM_ELEMS; ++n) {
data[n] *= s;
}
}
function addS(data, s){
for (var n = 0; n < NUM_ELEMS; ++n) {
data[n] += s;
}
}
/**
* A 2x2 matrix

@@ -16,42 +52,176 @@ */

function construct(){
if(!data){
data = nullMatrixData;
}// initialize to zero if no values were given
my.data = data;
}
/** Access an element of the matrix.
*
* @param {number} i row index
* @param {number} j column index
*/
this.at = function(i, j){
return at(i, j);
};
/** Get all elements of the matrix.
*
* @returns {Array.<number>} linearized data of the matrix
*/
this.data = function(){
return my.data.slice(0);
};
/**
* Calculate the determinant of the matrix.
*
* @returns {float}
*/
this.det = function(){
return at(0, 0) * at(1, 1) - at(1, 0) * at(0, 1);
var a11 = data[0];
var a12 = data[1];
var a21 = data[2];
var a22 = data[3];
return a11 * a22 - a21 * a12;
};
this.inverse = function(){
/**
* Invert the Matrix.
*
* @returns {Matrix2}
*/
this.inv = function(){
var det = this.det();
if (Math.abs(det) < EPSILON) {
throw new Error('Can not invert matrix, since determinant is near 0');
return null;
}
return new Matrix2([
at(1, 1) / det,
-at(0, 1) / det,
-at(1, 0) / det,
at(0, 0) / det
data[3] / det,
-data[1] / det,
-data[2] / det,
data[0] / det
]);
};
this.multiplyVector = function(v){
/**
* Multiply a given vector with this matrix.
*
* @param v
* @returns {Vec2}
*/
this.mulV = function(v){
return new Vec2(
at(0, 0) * v.x + at(0, 1) * v.y,
at(1, 0) * v.x + at(1, 1) * v.y
my.data[0] * v.x + my.data[1] * v.y,
my.data[2] * v.x + my.data[3] * v.y
);
};
this.at = function(i, j){
return at(i, j);
/**
* Add a given righthandside Matrix2 to this Matrix.
*
* @param rhs
* @returns {Matrix2}
*/
this.add = function(rhs){
var rhsData = rhs._raw();
for(var i = 0; i < NUM_ELEMS; i++){
data[i] += rhsData[i];
}
return new Matrix2(data);
};
this.data = function(){
return my.data.slice(0);
/**
* Subtract a given righthandside Matrix2 to this Matrix.
*
* @param rhs
* @returns {Matrix2}
*/
this.sub = function(rhs){
var rhsData = rhs._raw();
for(var i = 0; i < NUM_ELEMS; i++){
data[i] -= rhsData[i];
}
return new Matrix2(data);
};
// private
/**
* Multiply the Matrix by a given righthandside Matrix2.
*
* @param rhs
* @returns {Matrix2}
*/
this.mul = function(rhs){
var result = nullMatrixData.slice(0);
var lhsData = my.data;
var rhsData = rhs._raw();
for (var n = 0; n < NUM_ELEMS; ++n) {
var j = n % ROW_COL_LENGTH;
for (var k = 0; k < ROW_COL_LENGTH; ++k) {
result[n] += lhsData[n + k - j] * rhsData[ROW_COL_LENGTH * k + j];
}
}
return new Matrix2(result);
};
/**
* Add a Scalar value to the Matrix.
*
* @param s
* @returns {Matrix2}
*/
this.addS = function(s){
var data = this.data();
addS(data, s);
return new Matrix2(data);
};
/**
* Subtract a Scalar value from the Matrix.
*
* @param s
* @returns {Matrix2}
*/
this.subS = function(s){
var data = this.data();
addS(data, -s);
return new Matrix2(data);
};
/**
* Multiply the Matrix by a Scalar Value.
*
* @param s
* @returns {Matrix2}
*/
this.mulS = function(s){
var data = this.data();
mulS(data, s);
return new Matrix2(data);
};
/**
* Divide the Matrix by a Scalar Value.
*
* @param s
* @returns {Matrix2}
*/
this.divS = function(s){
var data = this.data();
mulS(data, 1 / s);
return new Matrix2(data);
};
this._raw = function(){
return my.data;
};
var at = function(i, j){
return my.data[I_FACTOR * i + j];
return my.data[2 * i + j];
};

@@ -61,3 +231,20 @@

}
/**
* Returns a new Identity Matrix.
*
* @returns {Matrix2}
*/
Matrix2.identity = function(){
return new Matrix2(identityMatrixData);
};
/**
* Returns a new Null Matrix.
*
* @returns {Matrix2}
*/
Matrix2.nullMatrix = function(){
return new Matrix2(nullMatrixData);
};
module.exports = Matrix2;
{
"name": "mecks",
"version": "0.1.2",
"version": "0.1.3",
"description": "Minimum Linear Algebra Library",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/ubimake/mecks.git"
"directories": {
"test": "test"
},
"license": "MIT",
"keywords": [
"linear",
"algebra",
"math"
],
"scripts": {

@@ -20,10 +13,11 @@ "test": "gulp"

"devDependencies": {
"chai": "^1.9.2",
"gulp": "^3.8.10",
"gulp-jscs": "^1.3.0",
"gulp-jshint": "^1.9.0",
"chai": "^1.10.0",
"gulp": "^3.9.1",
"gulp-istanbul": "^0.9.0",
"gulp-jscs": "^1.6.0",
"gulp-jshint": "^1.12.0",
"gulp-mocha": "^1.1.1",
"jshint-stylish": "^1.0.0",
"mocha": "^2.0.1",
"run-sequence": "^1.1.0"
"jshint-stylish": "^1.0.2",
"mocha": "^2.4.5",
"run-sequence": "^1.1.5"
},

@@ -33,29 +27,7 @@ "dependencies": {

},
"gitHead": "5cefbe104fe29802b285f1f71531ee7a049dacd8",
"bugs": {
"url": "https://github.com/ubimake/mecks/issues"
},
"homepage": "https://github.com/ubimake/mecks#readme",
"_id": "mecks@0.0.1",
"_shasum": "8b2dc56c6ae22f61f55889395af05922fefb0d88",
"_from": "mecks@",
"_npmVersion": "2.9.1",
"_nodeVersion": "0.10.37",
"_npmUser": {
"name": "xoryouyou",
"email": "xoryouyou@googlemail.com"
},
"dist": {
"shasum": "8b2dc56c6ae22f61f55889395af05922fefb0d88",
"tarball": "https://registry.npmjs.org/mecks/-/mecks-0.0.1.tgz"
},
"maintainers": [
{
"author": {
"name": "xoryouyou",
"email": "xoryouyou@googlemail.com"
}
],
"directories": {},
"_resolved": "https://registry.npmjs.org/mecks/-/mecks-0.0.1.tgz",
"readme": "ERROR: No README data found!"
},
"license": "MIT"
}
# mecks
[![Build Status](https://travis-ci.org/ubimake/mecks.svg#branch=dev)](https://travis-ci.org/ubimake/mecks)
Minimum Linear Algebra Library

@@ -8,4 +11,551 @@

mecks was built immutable on purpose and relies on the `vecks` vector library.
## Contributing
While developing make sure you use `gulp watch` to ease the process of testing your contribution.
## Documentation
* * *
# Matrix2
**Members:**
+ nullMatrixData
+ identityMatrixData
* * *
### Matrix2.Matrix2()
A 2x2 matrix
### Matrix2.construct()
Construct the matrix using a linearized array
### Matrix2.at(i, j)
Access an element of the matrix.
**Parameters**
**i**: `number`, row index
**j**: `number`, column index
### Matrix2.data()
Get all elements of the matrix.
**Returns**: `Array.&lt;number&gt;`, linearized data of the matrix
### Matrix2.det()
Calculate the determinant of the matrix.
**Returns**: `float`
### Matrix2.inv()
Invert the Matrix.
**Returns**: `Matrix2`
### Matrix2.mulV(v)
Multiply a given vector with this matrix.
**Parameters**
**v**: , Multiply a given vector with this matrix.
**Returns**: `Vec2`
### Matrix2.add(rhs)
Add a given righthandside Matrix2 to this Matrix.
**Parameters**
**rhs**: , Add a given righthandside Matrix2 to this Matrix.
**Returns**: `Matrix2`
### Matrix2.sub(rhs)
Subtract a given righthandside Matrix2 to this Matrix.
**Parameters**
**rhs**: , Subtract a given righthandside Matrix2 to this Matrix.
**Returns**: `Matrix2`
### Matrix2.mul(rhs)
Multiply the Matrix by a given righthandside Matrix2.
**Parameters**
**rhs**: , Multiply the Matrix by a given righthandside Matrix2.
**Returns**: `Matrix2`
### Matrix2.addS(s)
Add a Scalar value to the Matrix.
**Parameters**
**s**: , Add a Scalar value to the Matrix.
**Returns**: `Matrix2`
### Matrix2.subS(s)
Subtract a Scalar value from the Matrix.
**Parameters**
**s**: , Subtract a Scalar value from the Matrix.
**Returns**: `Matrix2`
### Matrix2.mulS(s)
Multiply the Matrix by a Scalar Value.
**Parameters**
**s**: , Multiply the Matrix by a Scalar Value.
**Returns**: `Matrix2`
### Matrix2.divS(s)
Divide the Matrix by a Scalar Value.
**Parameters**
**s**: , Divide the Matrix by a Scalar Value.
**Returns**: `Matrix2`
### Matrix2.identity()
Returns a new Identity Matrix.
**Returns**: `Matrix2`
### Matrix2.nullMatrix()
Returns a new Null Matrix.
**Returns**: `Matrix2`
* * *
# Matrix3
**Members:**
+ nullMatrixData
+ identityMatrixData
* * *
### Matrix3.Matrix3()
A 3x3 matrix
### Matrix3.construct()
Construct the matrix using a linearized array
### Matrix3.at(i, j)
Access an element of the matrix.
**Parameters**
**i**: `number`, row index
**j**: `number`, column index
### Matrix3.data()
Get all elements of the matrix.
**Returns**: `Array.&lt;number&gt;`, linearized data of the matrix
### Matrix3.det()
Calculate the determinant of the matrix.
**Returns**: `float`
### Matrix3.inv()
Invert the Matrix.
**Returns**: `Matrix3`
### Matrix3.add(rhs)
Add a given righthandside Matrix4 to this Matrix.
**Parameters**
**rhs**: , Add a given righthandside Matrix4 to this Matrix.
**Returns**: `Matrix3`
### Matrix3.sub(rhs)
Subtract a given righthandside Matrix4 to this Matrix.
**Parameters**
**rhs**: , Subtract a given righthandside Matrix4 to this Matrix.
**Returns**: `Matrix3`
### Matrix3.mul(rhs)
Multiply the Matrix by a given righthandside Matrix.
**Parameters**
**rhs**: , Multiply the Matrix by a given righthandside Matrix.
**Returns**: `Matrix3`
### Matrix3.mulV(rhs)
Multiply a given vector
**Parameters**
**rhs**: , Multiply a given vector
**Returns**: `Vec3`
### Matrix3.addS(s)
Add a Scalar value to the Matrix.
**Parameters**
**s**: , Add a Scalar value to the Matrix.
**Returns**: `Matrix4`
### Matrix3.subS(s)
Subtract a Scalar value from the Matrix.
**Parameters**
**s**: , Subtract a Scalar value from the Matrix.
**Returns**: `Matrix4`
### Matrix3.mulS(s)
Multiply the Matrix by a Scalar Value.
**Parameters**
**s**: , Multiply the Matrix by a Scalar Value.
**Returns**: `Matrix4`
### Matrix3.divS(s)
Divide the Matrix by a Scalar Value.
**Parameters**
**s**: , Divide the Matrix by a Scalar Value.
**Returns**: `Matrix4`
### Matrix3.identity()
Returns a new Identity Matrix.
**Returns**: `Matrix3`
### Matrix3.nullMatrix()
Returns a new Null Matrix.
**Returns**: `Matrix3`
* * *
# Matrix4
**Members:**
+ nullMatrixData
+ identityMatrixData
* * *
### Matrix4.Matrix4()
A 4x4 matrix
### Matrix4.at(i, j)
Access an element of the matrix.
**Parameters**
**i**: `number`, row index
**j**: `number`, column index
### Matrix4.data()
Get all elements of the matrix.
**Returns**: `Array.&lt;number&gt;`, linearized data of the matrix
### Matrix4.det()
Calculate the determinant of the matrix.
**Returns**: `float`
### Matrix4.inv()
Invert the Matrix.
**Returns**: `Matrix2`
### Matrix4.mulV(rhs)
Multiply a given vector with this matrix.
**Parameters**
**rhs**: , Multiply a given vector with this matrix.
**Returns**: `Vec3`
### Matrix4.add(rhs)
Add a given righthandside Matrix4 to this Matrix.
**Parameters**
**rhs**: , Add a given righthandside Matrix4 to this Matrix.
**Returns**: `Matrix4`
### Matrix4.sub(rhs)
Subtract a given righthandside Matrix4 to this Matrix.
**Parameters**
**rhs**: , Subtract a given righthandside Matrix4 to this Matrix.
**Returns**: `Matrix4`
### Matrix4.mul(rhs)
Multiply the Matrix by a given righthandside Matrix4.
**Parameters**
**rhs**: , Multiply the Matrix by a given righthandside Matrix4.
**Returns**: `Matrix4`
### Matrix4.addS(s)
Add a Scalar value to the Matrix.
**Parameters**
**s**: , Add a Scalar value to the Matrix.
**Returns**: `Matrix4`
### Matrix4.subS(s)
Subtract a Scalar value from the Matrix.
**Parameters**
**s**: , Subtract a Scalar value from the Matrix.
**Returns**: `Matrix4`
### Matrix4.mulS(s)
Multiply the Matrix by a Scalar Value.
**Parameters**
**s**: , Multiply the Matrix by a Scalar Value.
**Returns**: `Matrix4`
### Matrix4.divS(s)
Divide the Matrix by a Scalar Value.
**Parameters**
**s**: , Divide the Matrix by a Scalar Value.
**Returns**: `Matrix4`
### Matrix4.identity()
Returns a new Identity Matrix.
**Returns**: `Matrix4`
### Matrix4.nullMatrix()
Returns a new Null Matrix.
**Returns**: `Matrix4`
* * *
# Plane
* * *
### Plane.Plane()
A Plane representation
### Plane.project()
Project a point onto the plane.
* * *
var chai = require('chai');
var assert = chai.assert;
var Matrix2 = require('./..').Matrix2;
var EPSILON = require('./..').Constants.EPSILON;
describe('Matrix2', function(){
it('has data', function(){
it('can be constructed from arrays', function(){
var m = new Matrix2([45, 345, -89, 4]);
assert.equal(m.at(0, 0), 45);

@@ -13,2 +17,101 @@ assert.equal(m.at(0, 1), 345);

});
it('is immutable', function(){
var data = [45, 345, -89, 4];
var m = new Matrix2(data);
assert(m.data() !== data);
});
it('can be multiplied with vectors', function(){
var m = new Matrix2([3, 7, -2, 2]);
var v = {
x: 3,
y: 2
};
var result = m.mulV(v);
assert.equal(result.x, 23);
assert.equal(result.y, -2);
});
it('can be multiplied by a scalar', function(){
var m = new Matrix2([1, 2, 3, 4]);
var s = 5;
var result = m.mulS(s).data();
assert.deepEqual(result, [5, 10, 15, 20]);
});
it('can be divided by a scalar', function(){
var m = new Matrix2([5, 10, 15, 20]);
var s = 5;
var result = m.divS(s).data();
assert.deepEqual(result, [1, 2, 3, 4]);
});
it('can be added by a scalar', function(){
var m = new Matrix2([1, 2, 3, 4]);
var s = 5;
var result = m.addS(s).data();
assert.deepEqual(result, [6, 7, 8, 9]);
});
it('can be subtracted by a scalar', function(){
var m = new Matrix2([1, 2, 3, 4]);
var s = 5;
var result = m.subS(s).data();
assert.deepEqual(result, [-4, -3, -2, -1]);
});
it('multiplied with another 2x2 matrix', function(){
var m = new Matrix2([1, 2, 3, 4]);
var n = new Matrix2([5, 6, 7, 8]);
var result = m.mul(n);
assert.deepEqual(result.data(), [19, 22, 43, 50]);
});
it('can be added to another 2x2 matrix', function(){
var m = new Matrix2([1, 2, 3, 4]);
var n = new Matrix2([4, 3, 2, 1]);
var result = m.add(n);
assert.deepEqual(result.data(), [5, 5, 5, 5]);
});
it('can be subtracted by another 2x2 matrix', function(){
var m = new Matrix2([1, 2, 3, 4]);
var n = new Matrix2([4, 3, 2, 1]);
var result = m.sub(n);
assert.deepEqual(result.data(), [-3, -1, 1, 3]);
});
it('can calculate its determinant', function(){
var m = new Matrix2([1, 2, 3, 4]);
var result = m.det();
assert.equal(result, -2 );
});
it('can be inverted', function(){
var m = new Matrix2([4, 3, 2, 1]);
var result = m.inv();
assert.deepEqual(result.data(), [-0.5, 1.5, 1, -2]);
});
it('preserves primary invariant of inversion', function(){
var m = new Matrix2([5, 8, 4, 3]);
var inverted = m.inv();
var identity = m.mul(inverted).data();
assert.closeTo(identity[0], 1, EPSILON);
assert.closeTo(identity[1], 0, EPSILON);
assert.closeTo(identity[2], 0, EPSILON);
assert.closeTo(identity[3], 1, EPSILON);
});
it('returns null if matrix is non invertible', function(){
var m = new Matrix2();
var result = m.inv();
assert.isNull(result);
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc