@kkitahara/real-algebra
v1.2.4
Published
ECMAScript modules for exactly manipulating real numbers of the form (p / q)sqrt(b).
Downloads
7
Maintainers
Readme
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