npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kuotie/vector

v1.0.0

Published

Linear Algebra Vectors for Kuotie

Downloads

4

Readme

@kuotie/vector

Vector class with linear algebra methods. Methods consider the vector as inmutable by default, so when performing operations the vector will not be modified. If you want a method to mutate the vector, then provide a last parameter with option { inplace: true }

Installation

  npm i @kuotie/vector

Create a vector

You can create a new Vector instance providing the components as parameters:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

You can create a vector from an array:

const { Vector } = require('@kuotie/vector');

const vector = Vector.fromArray([1, 2, 3]);
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

Clone a vector

You can create a clone of a vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
const other = vector.clone();
console.log(other);
// Vector { data: [ 1, 2, 3 ] }

Size of a vector

You can get the size (number of dimensions) of the vector

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.size());
// 3

Length of a vector

You can calculate the lenght (magnitude) of a vector

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.length());
// 3.741657386773941

Scale a vector

You can scale a vector by an scalar.

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.scale(3));
// Vector { data: [ 3, 6, 9 ] }
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

You can provide the { inplace: true} to mutate the original vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.scale(3, { inplace: true }));
// Vector { data: [ 3, 6, 9 ] }
console.log(vector);
// Vector { data: [ 3, 6, 9 ] }

Add vectors

You can add one or more vectors by providing the vectors to add or equivalent arrays. It will not mutate the input vector.

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.add(new Vector(4, 5, 6), [7, 8, 9]);
// Vector { data: [ 12, 15, 18 ] }
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

You can provide the { inplace: true} to mutate the original vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.add(new Vector(4, 5, 6), [7, 8, 9], { inplace: true });
// Vector { data: [ 12, 15, 18 ] }
console.log(vector);
// Vector { data: [ 12, 15, 18 ] }

Change Length

You can change the length of a vector, this will generates a new vector and will not mutate the input vector.

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.changeLength(2));
// Vector { data: [ 0.5345224838248488, 1.0690449676496976, 1.6035674514745464 ] }
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

You can provide the { inplace: true} to mutate the original vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.changeLength(2, { inplace: true }));
// Vector { data: [ 0.5345224838248488, 1.0690449676496976, 1.6035674514745464 ] }
console.log(vector);
// Vector { data: [ 0.5345224838248488, 1.0690449676496976, 1.6035674514745464 ] }

Normalize

This will return a Unit Vector from this one:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.normalize());
// Vector { data: [ 0.2672612419124244, 0.5345224838248488, 0.8017837257372732 ] }
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

You can provide the { inplace: true} to mutate the original vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.normalize({ inplace: true }));
// Vector { data: [ 0.2672612419124244, 0.5345224838248488, 0.8017837257372732 ] }
console.log(vector);
// Vector { data: [ 0.2672612419124244, 0.5345224838248488, 0.8017837257372732 ] }

Negate

This will return a nage version of the vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.negate());
// Vector { data: [ -1, -2, -3 ] }
console.log(vector);
// Vector { data: [ 1, 2, 3 ] }

You can provide the { inplace: true} to mutate the original vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.normalize({ inplace: true }));
// Vector { data: [ -1, -2, -3 ] }
console.log(vector);
// Vector { data: [ -1, -2, -3 ] }

Dot Product

It should calculate the dot product of two vectors

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.dotProduct(new Vector(4, 5, 6)));
// 32

An array can be provided instead of a vector

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.dotProduct([4, 5, 6]));
// 32

Equals

Indicates if this vector equals another one:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.equals(new Vector(1, 2, 3)));
// true
console.log(vector.equals(new Vector(1, 2, 5)));
// false

An array can be provided instead of a vector

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.equals([1, 2, 3]));
// true
console.log(vector.equals([1, 2, 5])));
// false

Angle between two vectors

It will calculate the angle between two vectors, in radians

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.angleBetween(new Vector(4, 5, 6)));
// 0.2257261285527342

An array can be provided instead of a vector

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.angleBetween([4, 5, 6]));
// 0.2257261285527342

A version for degrees also exists:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(1, 2, 3);
console.log(vector.angleBetweenDegrees(new Vector(4, 5, 6)));
// 12.933154491899135

Cross Product

It can calculate the cross product of two vectors. It will not mutate the vector.

const { Vector } = require('@kuotie/vector');

const vector = new Vector(3, -3, 1);
console.log(vector.crosProduct(new Vector(4, 9, 2)));
// Vector { data: [ -15, -2, 39 ] }
console.log(vector);
// Vector { data: [ 3, -3, 1 ] }

You can provide the { inplace: true} to mutate the original vector:

const { Vector } = require('@kuotie/vector');

const vector = new Vector(3, -3, 1);
console.log(vector.crosProduct(new Vector(4, 9, 2)));
// Vector { data: [ -15, -2, 39 ] }
console.log(vector);
// Vector { data: [ -15, -2, 39 ] }