Pure TypeScript implementation of BLAS (Basic Linear Algebra Subprograms)
npm install blas-ts

Pure TypeScript implementation of BLAS (Basic Linear Algebra Subprograms).
This package provides high-performance linear algebra operations implemented in pure TypeScript, following the reference FORTRAN BLAS implementations for accuracy and performance.
- 🚀 Pure TypeScript - No native dependencies, works everywhere
- 📊 BLAS Compatible - Follows reference FORTRAN implementations
- 🎯 Type Safe - Full TypeScript support with detailed types
- ⚡ Optimized - Loop unrolling and performance optimizations
- 🌐 Universal - Works in Node.js, browsers, and edge environments
- ✅ Comprehensive - Complete Level 1, Level 2, and Level 3 BLAS operations
``bash`
npm install blas-ts
`typescript
import { daxpy, ddot, dnrm2, dscal, dcopy } from "blas-ts";
// DAXPY: y = alpha * x + y
const x = [1, 2, 3, 4];
const y = [5, 6, 7, 8];
daxpy(4, 2.0, x, 1, y, 1);
// y is now [7, 10, 13, 16]
// DDOT: compute dot product
const dot = ddot(4, x, 1, y, 1);
// DNRM2: compute Euclidean norm
const norm = dnrm2(4, x, 1);
// DSCAL: scale a vector
dscal(4, 2.0, x, 1); // x = 2.0 * x
// DCOPY: copy a vector
dcopy(4, x, 1, y, 1); // y = x
`
`typescript
import { dgemv, dger, BLASTranspose } from "blas-ts";
// DGEMV: matrix-vector multiply y = alphaAx + beta*y
const A = [1, 2, 3, 4, 5, 6]; // 2x3 matrix in column-major order
const x = [1, 2, 3];
const y = [0, 0];
dgemv(BLASTranspose.NoTranspose, 2, 3, 1.0, A, 2, x, 1, 0.0, y, 1);
// DGER: rank-1 update A = alphaxy^T + A
const x2 = [1, 2];
const y2 = [3, 4, 5];
dger(2, 3, 1.0, x2, 1, y2, 1, A, 2);
`
`typescript
import { dgemm, dsymm, BLASTranspose, BLASUplo, BLASSide } from "blas-ts";
// DGEMM: general matrix multiply C = alphaAB + beta*C
const A = [1, 2, 3, 4]; // 2x2 matrix
const B = [5, 6, 7, 8]; // 2x2 matrix
const C = [0, 0, 0, 0]; // 2x2 result matrix
dgemm(
BLASTranspose.NoTranspose,
BLASTranspose.NoTranspose,
2,
2,
2,
1.0,
A,
2,
B,
2,
0.0,
C,
2
);
// DSYMM: symmetric matrix multiply
dsymm(BLASSide.Left, BLASUplo.Upper, 2, 2, 1.0, A, 2, B, 2, 0.0, C, 2);
`
`typescript
// Works with regular arrays
const x1: number[] = [1, 2, 3];
const y1: number[] = [4, 5, 6];
// Works with Float64Array
const x2 = new Float64Array([1, 2, 3]);
const y2 = new Float64Array([4, 5, 6]);
// Works with Float32Array
const x3 = new Float32Array([1, 2, 3]);
const y3 = new Float32Array([4, 5, 6]);
daxpy(3, 2.0, x1, 1, y1, 1);
daxpy(3, 2.0, x2, 1, y2, 1);
daxpy(3, 2.0, x3, 1, y3, 1);
`
All Level 1 functions support strided access via incx and incy parameters.
- daxpy(n, alpha, x, incx, y, incy) - Compute y = alpha*x + ydscal(n, alpha, x, incx)
- - Scale vector: x = alpha*xdcopy(n, x, incx, y, incy)
- - Copy vector: y = xdswap(n, x, incx, y, incy)
- - Swap vectors: x <-> yddot(n, x, incx, y, incy)
- - Dot product: returns x^T * ydnrm2(n, x, incx)
- - Euclidean norm: returns ||x||_2dasum(n, x, incx)
- - Sum of absolute values: returns Σ|x_i|idamax(n, x, incx)
- - Index of maximum absolute valuedrotg(a, b)
- - Generate Givens rotationdrot(n, x, incx, y, incy, c, s)
- - Apply Givens rotation
Matrix storage uses column-major order (Fortran-style). The leading dimension ldA specifies the stride between columns.
- dgemv(trans, m, n, alpha, A, ldA, x, incx, beta, y, incy) - General matrix-vector multiply: y = alphaop(A)x + beta*ydsymv(uplo, n, alpha, A, ldA, x, incx, beta, y, incy)
- - Symmetric matrix-vector multiplydtrmv(uplo, trans, diag, n, A, ldA, x, incx)
- - Triangular matrix-vector multiply: x = op(A)*xdtrsv(uplo, trans, diag, n, A, ldA, x, incx)
- - Solve triangular system: op(A)*x = bdger(m, n, alpha, x, incx, y, incy, A, ldA)
- - Rank-1 update: A = alphaxy^T + Adsyr(uplo, n, alpha, x, incx, A, ldA)
- - Symmetric rank-1 update: A = alphaxx^T + Adsyr2(uplo, n, alpha, x, incx, y, incy, A, ldA)
- - Symmetric rank-2 update: A = alphaxy^T + alphayx^T + A
All Level 3 operations use column-major matrix storage.
- dgemm(transA, transB, m, n, k, alpha, A, ldA, B, ldB, beta, C, ldC) - General matrix multiply: C = alphaop(A)op(B) + beta*Cdsymm(side, uplo, m, n, alpha, A, ldA, B, ldB, beta, C, ldC)
- - Symmetric matrix multiplydtrmm(side, uplo, transA, diag, m, n, alpha, A, ldA, B, ldB)
- - Triangular matrix multiply: B = alphaop(A)B or B = alphaBop(A)dtrsm(side, uplo, transA, diag, m, n, alpha, A, ldA, B, ldB)
- - Solve triangular system: op(A)X = alphaB or Xop(A) = alphaBdsyrk(uplo, trans, n, k, alpha, A, ldA, beta, C, ldC)
- - Symmetric rank-k update: C = alphaAA^T + betaC or C = alphaA^TA + betaCdsyr2k(uplo, trans, n, k, alpha, A, ldA, B, ldB, beta, C, ldC)
- - Symmetric rank-2k update
`typescript
enum BLASTranspose {
NoTranspose, // Use A
Transpose, // Use A^T
ConjugateTranspose, // Use A^H (for complex matrices)
}
enum BLASUplo {
Upper, // Upper triangular
Lower, // Lower triangular
}
enum BLASDiag {
NonUnit, // Diagonal is stored in matrix
Unit, // Diagonal is assumed to be 1
}
enum BLASSide {
Left, // op(A)*B
Right, // B*op(A)
}
`
This project uses a devcontainer for consistent development environment.
1. Open in VS Code with the Dev Containers extension
2. Rebuild and reopen in container when prompted
3. Start developing!
`bash`
npm run build
`bash`
npm run dev
1. Fork the repository
2. Create your feature branch (git checkout -b feature/amazing-feature)git commit -m 'Add some AmazingFeature'
3. Commit your changes ()git push origin feature/amazing-feature`)
4. Push to the branch (
5. Open a Pull Request
MIT
- [x] Level 1 BLAS - Complete implementation with 10 functions (DAXPY, DSCAL, DCOPY, DSWAP, DDOT, DNRM2, DASUM, IDAMAX, DROTG, DROT)
- [x] Level 2 BLAS - Complete implementation with 7 functions (DGEMV, DSYMV, DTRMV, DTRSV, DGER, DSYR, DSYR2)
- [x] Level 3 BLAS - Complete implementation with 6 functions (DGEMM, DSYMM, DTRMM, DTRSM, DSYRK, DSYR2K)
- [x] Comprehensive test suite - 114+ tests covering all operations
- [x] Loop unrolling optimizations - Performance optimizations in Level 1 operations
- [x] TypeScript types - Full type safety with enums for BLAS parameters
- [ ] Complex number support (ZGEMM, ZAXPY, etc.)
- [ ] Performance benchmarks
- [ ] Additional Level 1 operations (DSDOT, DROTM, DROTMG)
- [ ] Banded matrix operations (DGBMV, DSBMV, etc.)
- [ ] Packed storage format support (DSPMV, DSPR, etc.)