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 🙏

© 2025 – Pkg Stats / Ryan Hefner

jsalgebra

v1.0.6

Published

simple linear algebra library for JavaScript

Readme

Linear algebra library for JavaScript

This library contains some useful classes and functions for dealing with linear algebra in JavaScript.


Overview

  • class Vector : This class represents a vector of arbitray size and operations on it.

    • constructor Vector(N) : creates a zero vector of size N
    • constructor Vector(N, components) : creates a vector of size N with the given components.
    • createUnitBasis(pos) : converts this vector in a unit basis vector and returns it.
    • component(pos) : returns the specified component (indexing at 0)
    • changeComponent(pos, value) : change the specified component.
    • toString() : returns a string representation of this vector.
    • size() : returns the size of the vector. (not the eulidean length!)
    • eulideanLength() : computes the eulidean length of this vector.
    • add(other) : vector addition, returns the rersult.
    • sub(other) : vector subtraction, returns the rersult.
    • dot(other) : computes the dot-product and returns it.
    • scalar(s) : scalar (s) multiplication. returns the result.
    • norm() : normalizes this vector and returns it.
    • equal(other) : returns true if the vectors are equal, otherwise false.
  • function unitBasisVector(N,pos) : returns a unit basis vector of size N with a One on position 'pos'

  • function randomVectorInt(N,a,b) : returns a random vector with integer components (between 'a' and 'b') of size N.

  • function randomVectorFloat(N,a,b) : returns a random vector with floating point components (between 'a' and 'b') of size N.

  • class Matrix : This class represents a matrix of arbitrary size and operations on it.

    • constructor(rows, cols) : creates a zero matrix of dimension rows x cols.
    • constructor(rows, cols, components) : creates a matrix with fix numbers of dimension rows x cols.
    • component(x,y) : returns the specified component.
    • changeComponent(x,y,value) : changes the specified component with the new value 'value'.
    • toString() : returns a string representation of this matrix.
    • dimension() : returns the dimension of this matrix as number arras [rows,cols].
    • add(other) : returns the result of the matrix addition.
    • equal(other) : returns true if the matrices are equal, otherwise false.
    • scalar(c) : returns the result of the matrix-scalar multiplication.

Documentation

The module is well documented in its source code. Look in the TypeScript file la_lib.ts.


Usage

    const LinearAlgebra = require('jsalgebra')

    var x = LinearAlgebra.Vector(size, [.......]);

    var k = LinearAlgebra.Matrix(row, col);

The namespace LinearAlgebra contains useful classes and functions for dealing with linear algebra under JavaScript.

Some examples:

// ---------------------------- Examples ------------------------------------------

// creates vectors 
var x = new LinearAlgebra.Vector(5, [1, 2, 3, 4, 5]);
var y = new LinearAlgebra.Vector(5, [1, 2, 3, 4, 5]);

// prints size of the vector
console.log(x.size()); // ==> 5

// changes the 2-th component with 7
x.changeComponent(2,7);

// print the 2-th component.
console.log(x.component(2)); // ==> 3

// prints the full vector as string.
console.log(x.toString()); // ==> (1,2,3,4,5)

// vector addition
console.log(x.add(y).toString()); // ==> (2,3,6,8,10)

// set 1 at provided index rest fill with zero
console.log(x.createUnitBasis(1).toString()); // ==> (0,1,0,0,0)

// computes the dot-product
console.log(x.dot(y)); // ==> 55

// computes and prints the scalar-product
console.log(x.scalar(5).toString()); // ==> (5,10,15,20,25)

// creates a unit basis vector
console.log(LinearAlgebra.unitBasisVector(3, 0).toString()); // ==> (1,0,0)

// creates random Integer vectors
console.log(LinearAlgebra.randomVectorInt(3, 0, 5).toString());

// creates random Float vectors
console.log(LinearAlgebra.randomVectorFloat(3, 0, 5).toString());

Tests

Go in the directory of the project and type in:
npm install
npm test
The test-suite use the JavaScript test-framework mocha.


Contributing

You can contribute to this project. Feel free and pull request some new features or documention.
TODO: Global functions for special matrices.
TODO: Documention of the classes and functions.