vektor
A robotics-focused linear algebra module
About
This code was officially presented at JSConf US 2013. There are slides from the talk here: AI.js: Robots with Brains
Install
npm install vektor
Running Tests
npm test
Vectors
initializing a vector
var v = vector;var a = 1 0 0;// orvar b = x: 1 y: 2 z: 3;
Note: Vectors can be initialized with 2 or 3 arguments only.
add two vectors
var c = a; // [2, 2, 3]
dot product
var c = a; // 1
cross product
var c = a; // [0, -3, 2]
distance between two vectors
var c = a; // 3.6
length
var c = blength; // 3.74
scale
var c = b; // [2, 4, 6]
Matrices
initializing a matrix
Note: matrices can be of any size
var m = matrix;var A = 2; // a 2x2 empty matrixvar B = 2 3; // a 2x3 empty matrixvar I_3 = 3 3 true; // a 3x3 identity matrix
setting values
/* our matrices:A = [ [1, 2],[3, 4] ];B = [ [5, 6],[7, 8] ];*/A;A;A;A;B;B;B;B;
getting values
var c = A; // 4
scaling a matrix
var C = A; // A = [ [-1, -2], [-3, -4] ]
adding two matrices
var C = A; // C = [ [6, 8], [10, 12] ]
multiplying two matrices
var C = A; // C = [ [19, 22], [43, 50] ]
getting the transpose
var C = A; // C = [ [1, 3], [2, 4] ]
calculating the determinant
var c = A; // c = -2
calculating the trace
Note: only works with square matrices... for now.
var c = A; // c = 5
Homogenous Matrices (aka Transforms)
- Translations
Rotations
initializing a rotation matrix
var r = rotate; // if the second argument is true, the first argument is in degreesvar a = 90 true; // matrix for a 90 degree rotation about the x axisvar b = 180 true; // matrix for a 180 degree rotation about the y axis// if the second argument is false or not set, the first argument is in radiansvar c = 2 * MathPI; // matrix for a 2 Pi radians (360 degrees) rotation about the z axisassert;assert;
rotate a vector around axes
var v = vector;var vector = 100; // original vector // Rotate (1, 0, 0) by 90 degrees on the Z axis and then by 45 degrees on the X axis var rotationMatrix = MathPI/2; // z-axis rotation matrix for Pi/2 radians (90 degrees)vector = rotationMatrix; // multiply rotation matrix by vector to rotate the vectorrotationMatrix = 45 true; // y-axis rotation matrix for 45 degrees (Pi/4 radians)vector = rotationMatrix;var coordinates = vectorv // [ -0.707106781187, -0.500000000001, 0.500000000001 ]
Examples
- Manipulator
Coming Soon
- Tutorials :-)
Contributions
Please, please, please help make this module more robust!
- Send in pull requests (make sure the tests pass)
- Discuss additional features in the Issues section
- Add your name and Github handle here:
- Rick Waldron - rwaldron
- Forbes Lindsay - ForbesLindesay
- Alexander Beletsky - alexanderbeletsky
- Bill Mills - BillMills