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

eigen

v0.2.2

Published

Eigen-js is a port of the [Eigen](https://eigen.tuxfamily.org/) C++ linear algebra library

Downloads

2,644

Readme

npm version Website shields.io Made with emscripten License: MIT

Eigen.js

Eigen.js is a port of the Eigen C++ linear algebra library

It uses a WebAssembly compiled subset of the Eigen library, and implements a garbage collection mechanism to manage memory

Live demo & documentation

An interactive documentation is available at eigen-js. Stress benchmarks can be found here

Usage

Eigen.js can be installed via npm or yarn

npm install eigen
yarn add eigen

In a node (v14) application or in the browser (using webpack)

// test.mjs
import eig from 'eigen';

(async () => {
  await eig.ready;
  const M = new eig.Matrix([[1, 2], [3, 4]]);
  M.print("M");
  M = M.inverse();
  M.print("Minv");
  eig.GC.flush();
})();

This minimal example can be found under ./example To run it, execute

cd example
node test.mjs

Which results in

M
[[1.00, 2.00]
 [3.00, 4.00]]
Minv
[[-2.00, 1.00]
 [1.50, -0.50]]

Allocation

The WebAssembly binary requires a manual memory management for objects allocated on the heap. Every time a matrix is created, it will be allocated on the heap and its memory won't be freed until its delete() method is invoked

// test.mjs
import eig from 'eigen';

(async () => {
  await eig.ready;
  const M = new eig.Matrix([[1, 2], [3, 4]]); // Memory is allocated for M
  M.print("M");
  M.delete(); // Memory is freed here
  M.print("M"); // This will trigger an error
})();

It can be cumbersome to call delete() on every object, especially for chained computations. Take for example const I2 = eig.Matrix.identity(2, 2).matAdd(eig.Matrix.identity(2, 2)). The identity matrix passed as an argument to matAdd(...) will be allocated but never freed, which will leak memory. To make things easier to manage, eig.GC keeps tracks of all the allocated objects on the heap and frees them all upon calling eig.GC.flush().

There could be instances where one would want to keep some matrices in memory while freeing a bunch of temporary ones used for computations. The method eig.GC.pushException(...matrices) whitelists its arguments to prevent eig.GC.flush() from flushing them. eig.GC.popException(...matrices) cancels any previous whitelisting.

// test.mjs
import eig from 'eigen';

(async () => {
  await eig.ready;
  const x = new eig.Matrix([[1, 2], [3, 4]]);
  eig.GC.pushException(x); // Whitelist x
  // Perform some computations
  const R = new eig.Matrix([[.1, 0], [.5, .1]]);
  x.matAddSelf(R.matMul(eig.Matrix.ones(2, 2)));
  // Free memory
  eig.GC.flush();
  x.print("x"); // x is still around!
})();

Documentation

The documentation is available at eigen.js

Build

Make sure Emscripten is intalled & activated in your terminal session

source path/to/emsdk/emsdk_env.sh
emcc -v

Install dependencies Eigen 3.3.9 and OSQP (optional, see below)

git submodule update --init --recursive

Now compile osqp for a Webassembly target

cd lib/osqp
mkdir build; cd build
emcmake cmake ..
emmake make

Once done, eigen.js can be compile to a wasm binary

# From the root directory
mkdir build
emcc -I lib/eigen -I lib/osqp/include -Isrc lib/osqp/build/out/libosqp.a -s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=0 -O3 -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 --bind -o build/eigen_gen.js src/cpp/embind.cc 

If you are not interested in the OSQP functionality, you can build without installing it with

emcc -D NO_OSQP -I lib/eigen  -Isrc -s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=0 -O3 -s ALLOW_MEMORY_GROWTH=1 -s MODULARIZE=1 --bind -o build/eigen_gen.js src/cpp/embind.cc

Generate the documentation

The documentation is generated from classes descriptions using documentation.js

documentation build src/classes/ -f json -o docs/doc.json