Coder Social home page Coder Social logo

andrewb330 / sparsejs Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 73 KB

A small JavaScript library that allows you to work efficiently with sparse matrices.

License: MIT License

CSS 12.06% HTML 5.01% JavaScript 82.93%
sparse-matrix library sparse-matrices matrix math pagerank page-rank sparse

sparsejs's Introduction

SparseJS

A small JavaScript library that allows you to work efficiently with sparse matrices.

Click to see demo

Usage

// create
let m1 = new SparseMatrix(5, 6);
let m2 = new SparseMatrix(5, 5).identity();
let m3 = new SparseMatrix().fromArray([[0, 1], [-1, 0.5]]);

// change
m1.set(0, 0, -3);
m1.transpose();
m1.reshape(3, 3);

// copy
m2 = m1.copy();

// get
console.log(m1.get(0, 0)); // -3

// examples
console.log(new SparseMatrix(3, 3).identity().toArray());
// [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

let a = 0.33; // angle
let r = new SparseMatrix().fromArray([ // rotation matrix
    [Math.cos(a), -Math.sin(a)], 
    [Math.sin(a), Math.cos(a)], 
]);
r.reshape(3, 3).set(2, 2, 1); // expand matrix and set new value
let res = r.mul(r.copy().transpose()); // multiply
console.log(res.toArray());
// [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

Page Rank example

let n = 13; // number of pages
let damping_factor = 0.8;
// links: (from, to, number of links)
let links = [[0, 11, 2],
    [12, 11, 3],
    // ... more links ...
    [3, 10, 1]];

let mat = new SparseMatrix(n, n);

links.forEach(function (link) {
    mat.set(link[1], link[0], link[2]);
}); // set links

// normalize matrix
for (let i = 0; i < n; i++) {
    let column_sum = mat.computeByColumn(i, (a, b) => a + b, 0);
    if (column_sum > 0) mat.mapByColumn(i, x => x / column_sum);
}

let page_rank = new Array(n).fill(1 / n); // initialize page_rank with 1/n values

for (let iteration = 0; iteration < 16; iteration++) {
    let next_page_rank = mat.mul(page_rank); // multiply vector by matrix (M*v)
    for (let i = 0; i < n; i++) {
        page_rank[i] = next_page_rank[i] * damping_factor + (1 - damping_factor) / n; // recompute page rank
    }
}

console.log(page_rank);
// [0.0153, 0.0289, 0.0853, 0.0811, 0.0660, 0.1721, 0.0235, 0.0153, 0.0211, 0.0563, 0.1525, 0.2213, 0.0606]

sparsejs's People

Contributors

andrewb330 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.