npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

numericalts

v0.7.1

Published

numerical.ts is a comprehensive TypeScript library for linear algebra and various mathematical operations, including vector and matrix operations, eigenvalue calculations, optimization algorithms, parsing mathematical statements, complex numbers, and more

Downloads

22

Readme

numerical.ts is a TypeScript library that focuses primarily on linear algebra functions, with plans to expand to include other mathematical functions in the future. It aims to provide a comprehensive set of tools for performing various linear algebra operations efficiently in TypeScript-

The library leverages the power of generic types to enable flexible usage with different data types while ensuring type safety. This allows you to perform linear algebra operations on various numeric types, such as number, bigint, or custom numerical types. The use of generics provides versatility and adaptability to different use cases and data structures.

Please note that numerical.ts is currently a work in progress

Features (Planned)

  • Matrix operations: Addition, subtraction, multiplication, transpose, determinant calculation, and more.
  • Eigenvalue and eigenvector calculations.
  • Singular value decomposition (SVD).
  • LU decomposition.
  • QR decomposition.
  • Least squares regression.
  • Optimization algorithms.
  • Parsing mathematical statements
  • Complex numbers
  • And many more

Usage

Install numerical.ts using npm:

npm install numericalts

For a full list of currently implemented methods and types please see here

Current native Numerical implementations

  • Number
  • Bigint
  • Complex number

These three Numerical interfaces have been implemented meaning you don't have to create them from scratch. Furthermore you can create matrixes without giving the constuctor the Numerical class

import { Matrix } from 'numericalts';

const A: Matrix<number> = new Matrix([[1,2],[3,4]])
const B: Matrix<bigint> = new Matrix([[1n,2n],[3n,4n]])
const C: Matrix<ComplexNumber> = new Matrix([
    [{real: 1, imaginary: 1},{real: 2, imaginary: 2}],
    [{real: 3, imaginary: 3},{real: 4, imaginary: 4}]
    ])

Matrix Operations


import { Matrix } from 'numericalts';

const A: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const B: Matrix<number> = new Matrix([[5, 6, 7, 8], 2, 2]);
const stringA: Matrix<string>

//A = new Matrix<number>([["1","2"],["3","4"]]) throws a typeerror while new Matrix<string>([["1","2"],["3","4"]]) does not

// Matrix multiplication
const C: Matrix<number> = A.multiply(B);
console.log(C.toArray()); // [[19, 22], [43, 50]]

// Matrix exponentiation
const D: Matrix<number> = A.pow(2);
console.log(D.toArray()); // [[7, 10], [15, 22]]

// Matrix multiplication using Strassen's algorithm
const E: Matrix<number> = A.strassenMultiply(B);
console.log(E.toArray()); // [[19, 22], [43, 50]]

// QR decomposition
const { Q, R } = A.QRDecomposition();
console.log(Q.toArray()); // [[-0.31622776601683794, -0.9486832980505138], [-0.9486832980505138, 0.31622776601683794]]
console.log(R.toArray()); // [[-3.1622776601683795, -4.427188724235731], [0, 0.6324555320336759]]

Example of a Custom Numerical Class

Notice a complex number Numerical class have since been added

This section demonstrates how to create a custom class that implements the Numerical interface and how to use it with the Matrix class.

// Import The Matrix class and the Numerical interface
import { Matrix, Numerical} from 'numericalts';

// Skeleton for a custom Complex class
class Complex implements Numerical<Complex>{
zeroValue: new Complex(0,0);
oneValue: new Complex(1,0);

constructor(realValue: number, imagValue: number){/*Logic here*/}
public zeroValue(): Complex {/*Logic here*/}
public oneValue(): Complex {/*Logic here*/}
public add(x: Complex, y: Complex): Complex {/*Logic here*/}
public subtract(x: Complex, y: Complex): Complex {/*Logic here*/}
public multiply(x: Complex, y: Complex): Complex {/*Logic here*/}
public divide(x: Complex, y: Complex): Complex {/*Logic here*/}
public sqrt(x: Complex): Complex {/*Logic here*/}
public fromIntegral(n: number): Complex {/*Logic here*/}
public toIntegral(x: Complex): number {/*Logic here*/}
public toString(x:Complex):string {/*Logic here*/}
public signOperator(x: Complex): number {/*Logic here*/}
}


// Now that the custom class has been implemented just use the Matrix class as normal

const c1: Complex = new Complex(1, 1);
const c2: Complex = new Complex(2, 2);
const c3: Complex = new Complex(3, 3);
const c4: Complex = new Complex(4, 4);

const complexMatrix: Matrix<Complex> = new Matrix<Complex>([[c1, c2], [c3, c4]], {numerical: new Complex(0, 0)});

// Matrix multiplication
const A: Matrix<Complex> = complexMatrix.multiply(complexMatrix);
console.log(C.toString());
/*
 *  "14i 20i"
 *  "30i 44i"
 */

// Matrix addition
const B: Matrix<Complex> = complexMatrix.add(complexMatrix);
console.log(B.toString());
/*
 *  "2+2i 4+4i"
 *  "6+6i 8+8i"
 */

Number Manipulation

import { math } from 'numericalts';

const num: number = 3.14159;

// Get the fractional part of a number
const frac: number = math.fracPart(num);
console.log(frac); // 0.14159

// Calculate the greatest common divisor (GCD) of two numbers
const gcd: number = math.GCD(24, 36);
console.log(gcd); // 12

// Calculate the least common divisor (LCD) of two numbers
const lcd: number = math.LCD(24, 36);
console.log(lcd); // 72

// Round a number to a specified number of decimal places
const rounded: number = math.toFixedNumber(num, 2);
console.log(rounded); // 3.14

// Count the number of decimal places in a number
const decimalPlaces: number = math.countDecimals(num);
console.log(decimalPlaces); // 5

// Calculate the dot product of two vectors
const vector1: number[] = [1, 2, 3];
const vector2: number[] = [4, 5, 6];
const dotProduct: number = math.dot(vector1, vector2);
console.log(dotProduct); // 32

// Normalize a vector
const normalizedVector: number[] = math.normalize(vector1);
console.log(normalizedVector); // [0.2672612419124244, 0.5345224838248488, 0.8017837257372732]

Documentation (Coming Soon)

Once numerical.ts is fully released, detailed documentation and examples will be provided to guide you through the library's usage and functionality. The documentation will cover various linear algebra operations, math operations and so forth, and provide code examples to help you understand how to use numerical.ts effectively. For now a simple documentation can be see here

Contributing

Contributions to numerical.ts are welcome! If you are interested in contributing to the development of the library, please stay tuned for updates on how you can get involved. I appreciate your support and contributions to make numerical.ts even better.

License

numerical.ts is released under the Apache License 2.0.

Contact

For any inquiries or questions, please contact me at [email protected].

Andreas Hummelmose Student at AAU