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

@rgsoft/math

v1.0.3

Published

Yet another JS math library

Downloads

29

Readme

math-js

Yet another JS math library

Installation

npm install @rgsoft/math

Tests

npm run test

Complex Numbers

The complex number library has some methods for performing calculations on complex numbers. To create a complex number of the form a + bi:

const cpx = new Complex(a, b);

📝 Every complex number is inmutable; both, the real and the imaginary parts are readonly.


Magnitude

const c = new Complex(5, 12);
console.log(c.mag); // 13

Conjugate

const c = new Complex(2, 7);
console.log(`${c.conjugate()}`); // 2 - 7i

Addition

Scalar Addition

// r + (a + bi) = (a + r) + bi
const c1 = new Complex(5, 12);
const c2 = c1.add(4);
console.log(`${c2}`); // 9 + 12i

Complex Addition

// (a + bi) + (c + di) = (a + c) + (b + d)i
const c1 = new Complex(4, -8);
const c2 = new Complex(-3, 6);
const c3 = c1.add(c2);
console.log(`${c3}`); // 1 + 2i

Substraction

Scalar Substraction

// (a + bi) - r = (a - r) + bi
const c1 = new Complex(9, 1);
const c2 = c1.sub(10);
console.log(`${c2}`); // -1 + 1i

Complex Substraction

// (a + bi) - (c + di) = (a - c) + (b - d)i
const c1 = new Complex(4, -8);
const c2 = new Complex(-3, 6);
const c3 = c1.add(c2);
console.log(`${c3}`); // 1 + 2i

Multiplication

Scalar Multiplication

// r (a + bi) = ra + bi
const c1 = new Complex(4, -1);
const c2 = c1.mult(-5);
console.log(`${c2}`); // -20 + 5i

Complex Multiplication

// (a + bi) (c + di) = (ac - bd) + (ad + bc)i
const c1 = new Complex(3, -1);
const c2 = new Complex(-2, 1);
const c3 = c2.mult(c2);
console.log(`${c3}`); // -5 + 5i

Division

Scalar Division

// (a + bi) / r = a / r + (b / r)i
const c1 = new Complex(4, -1);
const c2 = c1.div(-2);
console.log(`${c2}`); // -2 + 0.5i

Complex Division

let c1 = new Complex(2, 1);
let c2 = new Complex(-1, -1);
let c3 = c1.div(c2);
console.log(`${c3}`); // -1.5 + 0.5i

Square Root

let c = new Complex(4, 0);
c = c.sqrt();
console.log(`${c}`); // 2 + 0i

Vectors

Instantiation:

const vector = new Vector(x, y);

Angle

const v = new Vector(1, 1);
console.log(v.angle); // (Math.PI * 0.5);

Magnitude

const v = new Vector(1, 1);
console.log(v.mag); // (Math.SQRT2);

Normalization

const v = new Vector(4, 4);
v.nomalize();
console.log(v.mag); // 1;

Scalar Multiplication

const v = new Vector(0, 3);
v.mult(4);
console.log(v.mag); // 12;
console.log(v.y); // 12;

Addition

const v = new Vector(-2, 3);
v.add(new Vector(3, -5));
console.log(v.x); // 1
console.log(v.y); // -2

Substraction

const v = new Vector(-2, 3);
v.sub(new Vector(3, -5));
console.log(v.x); // -5
console.log(v.y); // 8

Distance to Another Vector

const v = new Vector(7, 2);
console.log(v.dist(new Vector(3, -1))); // 5

Angle to Another Vector

const v1 = new Vector(0, 1);
const v2 = new Vector(1, 0);
console.log(v1.angleTo(v2)); // Math.PI * 0.5

Create Vector from Angle

let v = Vector.fromAngle(Math.PI * 0.5);
console.log(v.x); //  0
console.log(v.y); //  1

Equality with Other Vector

const v1 = new Vector(7, 2);
const v2 = new Vector(7, 2);
console.log(v1.equals(v2)); // true

Copy Vector

const v = new Vector(7, 2);
const cv = v.copy();