Tiny, immutable and fast n-dimensional vector with pure functional calculations
- NPM:
npm install vktr
- Yarn:
yarn add vktr
<script src="vktr.js"></script>
<script>
const { multiply, Vector } = vktr;
const a = new Vector(1, 2, 3);
const b = 2;
multiply(a, b); // [2, 4, 6]
</script>
const { add, Vector } = require('vktr');
const a = new Vector(1, 2, 3);
const b = new Vector(3, 2, 1);
add(a, b); // [4, 4, 4]
import { substract, Vector } from 'vktr';
const a = new Vector(1, 2, 3);
const b = new Vector(3, 2, 1);
substract(a, b); // [-2, 0, 2]
import map from 'vktr/src/map';
import Vector from 'vktr/src/vector';
const a = new Vector(4, 5, 6);
const fn = (component) => component * 2;
map(a, fn); // [8, 10, 12]
Creates a n-dimensional Vector
object which can be used for calculations. A
Vector has an arbitrary number of components, which has to be greater than
zero.
Once a vector is created, it's components cannot be modified without producing a new Vector. This effect is call immutability, which causes predictable and efficient code.
However, there are some computed properties that are not calculated until you try to access them. Appart from this properties, the only way of making calculations is producing new Vectors.
angle: number
: Vector's polar form angle. Only valid for 2-dimensional vectors.components: Array<number>
: Array of components of the vector.dimensions: number
: number of dimensions of the vector.magnitude: number
: Euclidean length of the vector. Alias for length property.length: number
: Euclidean length of the the vector.
x: number
: Returns the first component of the vector, if any.y: number
: Returns the second component of the vector, if any.z: number
: Returns the third component of the vector, if any.t: number
: Returns the fourth component of the vector, if any.
When using notation like
n-Vector
inside this documentation, it does not refer to differentVector
classes, just the number of dimensions allowed for each vector.
As Vector
s are not mutable, every math function applied to them produces
a new Vector
instead of modifying the existing. This results in an predictable
easy-to-test environment. Each function has complete documentation about the
expected data and result: