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

@kkitahara/complex-algebra

v1.2.4

Published

ECMAScript modules for exactly manipulating complex numbers of which real and imaginary parts are numbers of the form (p / q)sqrt(b).

Downloads

39

Readme

JavaScript Style Guide license pipeline status coverage report version bundle size downloads per month downloads total

ComplexAlgebra

ECMAScript modules for exactly manipulating complex numbers of which real and imaginary parts are numbers of the form (p / q)sqrt(b), where p is an integer, q is a positive (non-zero) integer, and b is a positive, square-free integer.

Installation

npm install @kkitahara/complex-algebra @kkitahara/real-algebra

Examples

import { ExactRealAlgebra as RealAlgebra } from '@kkitahara/real-algebra'
import { ComplexAlgebra } from '@kkitahara/complex-algebra'
let ralg = new RealAlgebra()
let calg = new ComplexAlgebra(ralg)
let z, w, v

Generate a new real number

z = calg.num(1, 2, 5)
z.toString() // '(1 / 2)sqrt(5)'

z = calg.num(1, 2)
z.toString() // '1 / 2'

z = calg.num(3)
z.toString() // '3'

Generate a new imaginary number

z = calg.inum(1, 2, 5)
z.toString() // 'i((1 / 2)sqrt(5))'

z = calg.inum(1, 2)
z.toString() // 'i(1 / 2)'

z = calg.inum(3)
z.toString() // 'i(3)'

:warning: num and inum methods do not check if (the absolute value of) the 3rd parameter is a square-free integer or not (must be square-free!).

Real and imaginary parts

z = calg.num(1, 2, 5)
z.re.toString() // '(1 / 2)sqrt(5)'
z.im.toString() // '0'

z = calg.inum(1, 2, 5)
z.re.toString() // '0'
z.im.toString() // '(1 / 2)sqrt(5)'

Generate from two real numbers (since v1.2.0)

let a = ralg.$(1, 2, 3)
let b = ralg.$(1, 2, 5)
z = calg.$(a, b)
z.toString() // '(1 / 2)sqrt(3) + i((1 / 2)sqrt(5))'

Copy (create a new object)

z = calg.inum(1, 2, 5)
w = calg.copy(z)
w.toString() // 'i((1 / 2)sqrt(5))'

Equality

z = calg.num(1, 2, 5)
w = calg.inum(1, 2, 5)
calg.eq(z, w) // false

w = calg.num(1, 2, 5)
calg.eq(z, w) // true

Inequality

z = calg.num(1, 2, 5)
w = calg.inum(1, 2, 5)
calg.ne(z, w) // true

w = calg.num(1, 2, 5)
calg.ne(z, w) // false

isZero

calg.isZero(calg.num(0)) // true
calg.isZero(calg.inum(0)) // true
calg.isZero(calg.num(1, 2, 5)) // false
calg.isZero(calg.inum(1, 2, 5)) // false
calg.isZero(calg.num(-1, 2, 5)) // false
calg.isZero(calg.inum(-1, 2, 5)) // false

isInteger (since v1.1.0)

calg.isInteger(calg.num(0)) // true
calg.isInteger(calg.inum(0)) // true
calg.isInteger(calg.num(1, 2)) // false
calg.isInteger(calg.inum(1, 2)) // false
calg.isInteger(calg.num(6, 3)) // true
calg.isInteger(calg.inum(6, 3)) // true
calg.isInteger(calg.num(6, 3, 2)) // false
calg.isInteger(calg.inum(6, 3, 2)) // false

Addition

z = calg.num(1, 2, 5)
w = calg.inum(1, 2)
// new object is generated
v = calg.add(z, w)
v.toString() // '(1 / 2)sqrt(5) + i(1 / 2)'

In-place addition

z = calg.num(1, 2, 5)
w = calg.inum(1, 2)
// new object is not generated
z = calg.iadd(z, w)
z.toString() // '(1 / 2)sqrt(5) + i(1 / 2)'

Subtraction

z = calg.num(1, 2, 5)
w = calg.inum(1, 2)
// new object is generated
v = calg.sub(z, w)
v.toString() // '(1 / 2)sqrt(5) + i(-1 / 2)'

In-place subtraction

z = calg.num(1, 2, 5)
w = calg.inum(1, 2)
// new object is not generated
z = calg.isub(z, w)
z.toString() // '(1 / 2)sqrt(5) + i(-1 / 2)'

Multiplication

z = calg.inum(1, 2, 5)
w = calg.inum(1, 2)
// new object is generated
v = calg.mul(z, w)
v.toString() // '-(1 / 4)sqrt(5)'

In-place multiplication

z = calg.inum(1, 2, 5)
w = calg.inum(1, 2)
// new object is not generated
z = calg.imul(z, w)
z.toString() // '-(1 / 4)sqrt(5)'

Division

z = calg.inum(1, 2, 5)
w = calg.inum(1, 2)
// new object is generated
v = calg.div(z, w)
v.toString() // 'sqrt(5)'

In-place division

z = calg.inum(1, 2, 5)
w = calg.inum(1, 2)
// new object is not generated
z = calg.idiv(z, w)
z.toString() // 'sqrt(5)'

Multiplication by -1

z = calg.inum(1, 2, 5)
// new object is generated
w = calg.neg(z)
w.toString() // 'i(-(1 / 2)sqrt(5))'

In-place multiplication by -1

z = calg.inum(1, 2, 5)
// new object is not generated
z = calg.ineg(z)
z.toString() // 'i(-(1 / 2)sqrt(5))'

Complex conjugate

z = calg.num(1, 2, 5)
w = calg.inum(1, 2, 5)
// new object is generated
v = calg.cjg(z)
v.toString() // '(1 / 2)sqrt(5)'
v = calg.cjg(w)
v.toString() // 'i(-(1 / 2)sqrt(5))'

In-place evaluation of the complex conjugate

z = calg.num(1, 2, 5)
w = calg.inum(1, 2, 5)
// new object is not generated
z = calg.icjg(z)
z.toString() // '(1 / 2)sqrt(5)'
w = calg.icjg(w)
w.toString() // 'i(-(1 / 2)sqrt(5))'

Square of the absolute value

z = calg.iadd(calg.num(3), calg.inum(4))
let a = calg.abs2(z)
a.toString() // '25'
// return value is not a complex number (but a real number)
a.re // undefined
a.im // undefined

JSON (stringify and parse)

z = calg.iadd(calg.num(1, 2, 5), calg.inum(-1, 2, 7))
let str = JSON.stringify(z)
w = JSON.parse(str, calg.reviver)
calg.eq(z, w) // true

Numerical algebra

The above codes work with built-in numbers if you use

import { RealAlgebra } from '@kkitahara/real-algebra'
import { ComplexAlgebra } from '@kkitahara/complex-algebra'
let ralg = new RealAlgebra()
let calg = new ComplexAlgebra(ralg)

instead of ExactRealAlgebra. See the documents of @kkitahara/real-algebra for more details.

ESDoc documents

For more examples, see ESDoc documents:

cd node_modules/@kkitahara/complex-algebra
npm install --only=dev
npm run doc

and open doc/index.html in your browser.

LICENSE

© 2019 Koichi Kitahara
Apache 2.0