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
const blas = require('blasjs');
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="https://unpkg.com/blasjs@latest/dist/lib/blasjs.min.js">
<script>
const blas = window.BLAS;
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:{
complex,
fortranArrComplex32,
fortranArrComplex64,
fortranMatrixComplex32,
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]);
sin(PI/3);
sin( [ PI/3 ] );
sin([])
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);
const c2 = complex();
const c3 = complex(0.5);
each
Curried functional analog to Array.prototype.forEach
, but takes arbitrary input.
Example:
const blas = require('blasjs');
const { helper: { each } } = blas;
const curry1 = each( { hello: 'world', ts: new Date() })
curry1( (val, key) => console.log(`${val} ':' ${key}`)))
each( ['a','b','c','d'])( (v,idx) =>console.log(v,idx, typeof idx))
each()(console.log)
each(null)(console.log)
each([])(console.log)
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;
map([1,2,3])(v=>v*2);
map({ a:'A', b:'B' })( (val, key) => key+'='+val);
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 ];
muxCmplx(reals, imaginary)
muxCmplx([1,2], imaginary)
muxCmplx(reals)
muxCmplx()
muxCmplx(1)
muxCmplx(1,-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);
_4(123456789)
_4( { car: 'Mazda' , aux: { priceUSD: 24.3253E+3, maxWarpSpeed:3.42111E-4 } } );
_4([0.123456, 0.78901234]);
FortranArr
Abstraction of a 1 dimensional single precision (32bit) complex/real FORTRAN array.
Used by level 1 and level 2 blasjs
functions.
FortranArr
objects 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ᵗ