Giter Site home page Giter Site logo

matrix.onchain.js's Introduction

matrix.onchain.js

A micro JS library (254 bytes) for matrix operations.

This library is intended for use in environments where the available storage space is very limited; like blockchains for example. Everything is stripped down to the bare essentials.

GitHub GitHub tag (latest SemVer) GitHub Workflow Status

Usage

Operating on matrices

Perform operations on two matrices with equal dimensions. For example, If you'd want to do a add operation:

let a = [
  [1, 2, 3],
  [0, 1, 0]
]
let b = [
  [3, 2, 1],
  [1, 0, 1]
]

Mtx.op(a, b, (x, y) => x + y)
// => [
//      [4, 4, 4],
//      [1, 1, 1]
//    ]

Passing a numeric value as the second argument will create a uniform matrix of equal dimensions as the one passed as the first argument, before performing the operation:

Mtx.op(a, 2, (x, y) => x + y)
// => [
//   [3, 4, 5],
//   [2, 3, 2]
// ]

Alternatively, a single matrix row can be passed as the second argument, which will be used to build a veritcally uniform matrix before performing the operation:

Mtx.op(a, [1, 11, 1], (x, y) => x + y)
// => [
//   [2, 13, 4],
//   [1, 12, 1]
// ]

Common operations

Often you'll need to repeat the same matrix operation multiple times. That's where common operations come in.

First, register your favourite operation:

Mtx.co['+'] = (x, y) => x + y

Then use and re-use that operation wherever needed:

Mtx.op(a, b, Mtx.co['+'])

Creating new matrices

With the fill method, a new matrix can be created with a given number of rows and columns:

Mtx.fill(3, 4)
// => [
//   [0, 0, 0, 0],
//   [0, 0, 0, 0],
//   [0, 0, 0, 0]
// ]

Optionally, an initial value can be passed as the third argument:

Mtx.fill(3, 4, 2)
// => [
//   [2, 2, 2, 2],
//   [2, 2, 2, 2],
//   [2, 2, 2, 2]
// ]

Alternatively, a full row with columns can be passed instead of the number of columns, to create a matrix with uniform columns:

Mtx.fill(3, [0, 1, 0])
// => [
//   [0, 1, 0],
//   [0, 1, 0],
//   [0, 1, 0]
// ]

License

matrix.onchain.js is licensed under the terms of the MIT License.

matrix.onchain.js's People

Contributors

wout avatar

Stargazers

 avatar

Watchers

 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.