New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

blasjs

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

blasjs

Javascript Pure Implementation of BLAS Level 1, level 2, level 3 functions

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-95.24%
Maintainers
1
Weekly downloads
 
Created
Source

BLASjs (Basic Linear Algebra Subprograms)

This is a 100% Pure Javascript ( TypeScript ) re-write of the reference implementation Basic Linear Algebra SubPrograms (BLAS) numerical library found here. This is a full manual re-write, "emscripten" was not used.

summary

BLASjs contains all the functions (Complex, Real) of the reference implementation capable for 32 bit and 64 bit floating point arithmatic:

  • :ok_hand: 100% code coverage
  • 1005 tests
  • Output off all tests equal to the BLAS FORTRAN reference implementation.
  • Level 1: all vector-vector operations implemented.
  • Level 2: all vector-matrix operations implemented.
  • Level 3: all matrix-matrix operations implemented.
  • Helper functions to easy porting of FORTRAN BLAS usage to Javascript.
Node and Web

The library js file is an agnostic UMD library, it can be used in a web client as-well as in a server side node environment.

Installation

node
npm i blasjs
//node
   const blas = require('blasjs');
//or typescript
   import * as blas from 'blasjs';
web

The module directory contains a minimized bundle for use in html <script> tag. The library is attached to the window.BLAS object after loading.

<!-- script src="your_server_url/blasjs.min.js"></script -->
<!-- this example uses unpkg as CDN -->
<script src="https://unpkg.com/blasjs@latest/dist/lib/blasjs.min.js">
<script>
  const blas = window.BLAS; //UMD exposes it as BLAS

  //fetch some level3 complex 64 bit precision matrix-matrix operations
  const {
      level3: { zsyrk, ztrmm, ztrsm }
   } = blas;
</script>

Table of Contents

Language differences with FORTRAN/BLAS.

FORTRAN language can instrinsicly work with non-zero based multidimensional arrays and complex numbers. Below are some examples from FORTRAN that have no Javascript counterpart. The reference implementation of BLAS functions expect inputs of these types.

FORTRAN complex scalar, complex array, complex Matrix

!    double precision Complex number
     COMPLEX*16 alpha
!
!    double precision Complex array with offset 2
     COMPLEX*16 vector(2,10)
!
!    double precision complex MultiDimensional Array (matrix)
!    rows 1 to 5 , columns 1 to 10
     COMPLEX*16 A(1:5,1:10)

To work with the concept of non-zero based arrays and complex numbers in JS, these FORTRAN constructs have equivalents in the blasjs library.

blasjs complex scalar, complex array, complex Matrix

  const blas = require('blasjs');

  const {
      helper:{
        /* create complex Object from 2 real numbers */
        complex,

        /* create single precision Real/complex arrays, */
        fortranArrComplex32,

        /* create double precision Real/Complex arrays */
        fortranArrComplex64,

        /* create single precision 2 dimensional Real/Complex arrays */
        fortranMatrixComplex32,

        /* Double precision 2 dimensional Real/Complex arrays */
        fortranMatrixComplex64,
      }
  } = blas;

These functions are extensively documented in the helper functions. It is recommended you read this introductory part of the documentation first. before anything else.

Helper functions

blasjs uses "FORTRAN like" complex number 32/64 bit precision multidimensional complex/real data. These helper functions have been designed to significantly ease the use of working with these data types in JavaScript.

arrayrify

Creates a new function capable to accept vectorized input, by wrapping an existing function given as argument.

Example:

const blas = require('blasjs');

const { helper: { arrayrify } } = blas;
const PI = Math.PI;
//
const sin = arrayrify(Math.sin)

sin([PI/3, PI/4, PI/6]); // returns array aswell
// [ 0.866025, 0.7071067811, 0.5 ]

sin(PI/3); // returns scalar
sin( [ PI/3 ] ); // returns scalar
// 0.866025

sin([]) // edge case
// undefined

sin() //
//NaN  same as Math.sin()

complex

Mimics the GNU Fortran extension complex. Creates a JS object that represents a complex scalar number. Used by blasjs for scalar input arguments.

Example:

const blas = require('blasjs');

const { helper: { complex } } = blas;

const c1 = complex(0.1,0.3);
//c1 = { re: 0.1, im: 0.3 }

const c2 = complex();
//c2 = { re: 0, im: 0 }

const c3 = complex(0.5);
//c3 = { re: 0.5, im:0 }

each

Curried functional analog to Array.prototype.forEach, but takes arbitrary input.

Example:

const blas = require('blasjs');

const { helper: { each } } = blas;

//Iterates over an object like a map
const curry1 = each( {  hello: 'world', ts: new Date() })
curry1( (val, key) => console.log(`${val} ':'  ${key}`)))
//world : hello
//2018-05-10T13:57:08.923Z : ts

//Handles array also
each( ['a','b','c','d'])( (v,idx) =>console.log(v,idx, typeof idx))
//a 0 number
//b 1 number
//c 2 number
//d 3 number

//Edge cases
each()(console.log)
//nothing happens

each(null)(console.log)
//nothing happens

each([])(console.log)
//nothing happens

map

Curried functional analog to Array.prototype.map, but takes arbitrary input.

:warning: Forces the output to be a an array regardless of the input.

Example:

const blas = require('blasjs');

const { helper: { map } } = blas;

//trivial
map([1,2,3])(v=>v*2);
//[ 2, 4, 6 ]

//key properties
map({ a:'A', b:'B' })( (val, key) => key+'='+val);
//[ 'a=A', 'b=B' ]

map(null)( v => '/'+v);
//[]

map()( v => '/'+v);
//[]

map()()
//[]

muxCmplx

Creates an array of complex numbers from arrayed input. The result is always an array type.

Example:

const blas = require('blasjs');

const { helper: { muxCmplx } } = blas;

const reals = [ 0.1, -0.2, 0.3, 0.45 ];
const imaginary = [ 0.1, -0.2, 0.3, 0.45 ];

// normal usage
muxCmplx(reals, imaginary)
/*[ { re: 0.1, im: 0.1 },
    { re: -0.2, im: -0.2 },
    { re: 0.3, im: 0.3 },
    { re: 0.45, im: 0.45 } ]*/

//R recycling rule is used
muxCmplx([1,2], imaginary)
/*^[ { re: 1, im: 0.1 },
     { re: 2, im: -0.2 },
     { re: 1, im: 0.3 },
     { re: 2, im: 0.45 } ]*/

//dont care about imaginary
muxCmplx(reals)
/*[ { re: 0.1, im: undefined },
    { re: -0.2, im: undefined },
    { re: 0.3, im: undefined },
    { re: 0.45, im: undefined } ]*/

muxCmplx() //
// [ { re: undefined, im: undefined } ]

muxCmplx(1) //
// [ { re: 1, im: undefined } ]

//3 specify real and imaginary
muxCmplx(1,-2)//
//[ { re: 1, im: -2 } ]
numberPrecision

Enforces significant figure of a number, or on the properties of a JS object (deep search) with numeric values.

Example:

const blas = require('blasjs');

const { helper: { numberPrecision } } = blas;

const _4 = numberPrecision(4);

_4(0.123456789);
//0.1235

_4(123456789)
//123500000

//enforce significance over properties
_4( { car: 'Mazda' , aux: { priceUSD: 24.3253E+3, maxWarpSpeed:3.42111E-4 } } );
//{ car: 'Mazda', aux: { priceUSD: 24330, maxWarpSpeed: 0.0003421 } }

_4([0.123456, 0.78901234]);
//[ 0.1235, 0.789 ]
FortranArr

Abstraction of a 1 dimensional single precision (32bit) complex/real FORTRAN array. Used by level 1 and level 2 blasjs functions. FortranArrobjects should be created by the fortranArrComplex32 and fortranArrComplex64 helper functions.

decl:

export declare type FortranArr = {
    base: number;
    r: fpArray; 
    i?: fpArray;
    s: FortranSetterGetter;
    toArr: () => Complex[] | number[];
};
  • Iterates over object properties and values.

  • Iterates over array elements fortranArrComplex32, fortranArrComplex64, fortranMatrixComplex32, fortranMatrixComplex64,

Some notes on Matrix symbols

https://fsymbols.com/generators/overline/

A̅ᵗ Conjugate transpose B̅ Conjugate

A̅ᵗ ∙ B̅

Aᵗ

Keywords

FAQs

Package last updated on 10 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc