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/real-algebra

v1.2.4

Published

ECMAScript modules for exactly manipulating real numbers of the form (p / q)sqrt(b).

Downloads

20

Readme

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

RealAlgebra

ECMAScript modules for exactly manipulating real 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/real-algebra

Examples

import { ExactRealAlgebra as RealAlgebra } from '@kkitahara/real-algebra'
let ralg = new RealAlgebra()
let a, b, c

Generate a new number

a = ralg.num(1, 2, 5)
a.toString() // '(1 / 2)sqrt(5)'

a = ralg.num(1, 2)
a.toString() // '1 / 2'

a = ralg.num(3)
a.toString() // '3'

Generate a new number (short form, since v1.2.0)

a = ralg.$(1, 2, 5)
a.toString() // '(1 / 2)sqrt(5)'

a = ralg.$(1, 2)
a.toString() // '1 / 2'

a = ralg.$(3)
a.toString() // '3'

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

Copy (create a new object)

a = ralg.num(1, 2, 5)
b = ralg.copy(a)
b.toString() // '(1 / 2)sqrt(5)'

Equality

a = ralg.num(1, 2, 5)
b = ralg.num(3, 2, 5)
ralg.eq(a, b) // false

b = ralg.num(1, 2, 5)
ralg.eq(a, b) // true

Inequality

a = ralg.num(1, 2, 5)
b = ralg.num(3, 2, 5)
ralg.ne(a, b) // true

b = ralg.num(1, 2, 5)
ralg.ne(a, b) // false

isZero

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

isPositive

ralg.isPositive(ralg.num(0)) // false
ralg.isPositive(ralg.num(1, 2, 5)) // true
ralg.isPositive(ralg.num(-1, 2, 5)) // false

isNegative

ralg.isNegative(ralg.num(0)) // false
ralg.isNegative(ralg.num(1, 2, 5)) // false
ralg.isNegative(ralg.num(-1, 2, 5)) // true

isInteger (since v1.1.0)

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

Addition

a = ralg.num(1, 2, 5)
b = ralg.num(1, 2, 1)
// new object is generated
c = ralg.add(a, b)
c.toString() // '1 / 2 + (1 / 2)sqrt(5)'

In-place addition

a = ralg.num(1, 2, 5)
b = ralg.num(1, 2, 1)
// new object is not generated
a = ralg.iadd(a, b)
a.toString() // '1 / 2 + (1 / 2)sqrt(5)'

Subtraction

a = ralg.num(1, 2, 5)
b = ralg.num(1, 2, 1)
// new object is generated
c = ralg.sub(a, b)
c.toString() // '-1 / 2 + (1 / 2)sqrt(5)'

In-place subtraction

a = ralg.num(1, 2, 5)
b = ralg.num(1, 2, 1)
// new object is not generated
a = ralg.isub(a, b)
a.toString() // '-1 / 2 + (1 / 2)sqrt(5)'

Multiplication

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
b = ralg.iadd(ralg.num(-1, 2), ralg.num(1, 2, 5))
// new object is generated
c = ralg.mul(a, b)
c.toString() // '1'

In-place multiplication

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
b = ralg.iadd(ralg.num(-1, 2), ralg.num(1, 2, 5))
// new object is not generated
a = ralg.imul(a, b)
a.toString() // '1'

Division

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
b = ralg.iadd(ralg.num(-1, 2), ralg.num(1, 2, 5))
// new object is generated
c = ralg.div(a, b)
c.toString() // '3 / 2 + (1 / 2)sqrt(5)'

In-place division

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
b = ralg.iadd(ralg.num(-1, 2), ralg.num(1, 2, 5))
// new object is not generated
a = ralg.idiv(a, b)
a.toString() // '3 / 2 + (1 / 2)sqrt(5)'

Multiplication by -1

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
// new object is generated
b = ralg.neg(a)
b.toString() // '-1 / 2 - (1 / 2)sqrt(5)'

In-place multiplication by -1

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
// new object is not generated
a = ralg.ineg(a)
a.toString() // '-1 / 2 - (1 / 2)sqrt(5)'

Absolute value

a = ralg.iadd(ralg.num(1, 2), ralg.num(-1, 2, 5))
// new object is generated
b = ralg.abs(a)
b.toString() // '-1 / 2 + (1 / 2)sqrt(5)'

In-place evaluation of the absolute value

a = ralg.iadd(ralg.num(1, 2), ralg.num(-1, 2, 5))
// new object is not generated
a = ralg.iabs(a)
a.toString() // '-1 / 2 + (1 / 2)sqrt(5)'

JSON (stringify and parse)

a = ralg.iadd(ralg.num(1, 2), ralg.num(1, 2, 5))
let str = JSON.stringify(a)
b = JSON.parse(str, ralg.reviver)
ralg.eq(a, b) // true

Numerical algebra

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

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

instead of ExactRealAlgebra.

In the numerical algebra, equality can be controlled by the constructor argument eps:

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

let a = ralg.num(1, 2)
let b = ralg.num(1, 3)
ralg.eq(a, b) // false
ralg.isZero(ralg.num(1, 999999)) // false
import { RealAlgebra } from '@kkitahara/real-algebra'
let eps = 0.5
let ralg = new RealAlgebra(eps)

let a = ralg.num(1, 2)
let b = ralg.num(1, 3)
ralg.eq(a, b) // true
ralg.isZero(ralg.num(1, 2)) // true
ralg.isZero(ralg.num(2, 3)) // false

Two numbers are considered to be equal if the difference is smaller than or equal to eps.

ESDoc documents

For more examples, see ESDoc documents:

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

and open doc/index.html in your browser.

LICENSE

© 2019 Koichi Kitahara
Apache 2.0