@kkitahara/quaternion-algebra
v1.0.4
Published
ECMAScript modules for exactly manipulating quaternions of which coefficients are numbers of the form (p / q)sqrt(b).
Downloads
9
Maintainers
Readme
QuaternionAlgebra
ECMAScript modules for exactly manipulating quaternions of which coefficients 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/quaternion-algebra @kkitahara/real-algebra
Examples
import { ExactRealAlgebra as RealAlgebra } from '@kkitahara/real-algebra'
import { QuaternionAlgebra } from '@kkitahara/quaternion-algebra'
let r = new RealAlgebra()
let h = new QuaternionAlgebra(r)
let q1, q2, q3
Generate a new quaternion
q1 = h.$(1, 2, 3, 4)
q1.toString() // '1 + i(2) + j(3) + k(4)'
q1 = h.$(0, 0, 0, r.$(1, 2, 5))
q1.toString() // 'k((1 / 2)sqrt(5))'
Real and imaginary parts
q1 = h.$(1, 2, 3, 4)
q1.re.toString() // '1'
q1.im instanceof Array // true
q1.im[0].toString() // '2'
q1.im[1].toString() // '3'
q1.im[2].toString() // '4'
Copy (create a new object)
q1 = h.$(1, 2, 3, 4)
q2 = h.copy(q1)
q2.toString() // '1 + i(2) + j(3) + k(4)'
Equality
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, -2, 3, 4)
h.eq(q1, q2) // false
q2 = h.$(1, 2, 3, 4)
h.eq(q1, q2) // true
Inequality
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, -2, 3, 4)
h.ne(q1, q2) // true
q2 = h.$(1, 2, 3, 4)
h.ne(q1, q2) // false
isZero
h.isZero(h.$(0, 0, 0, 0)) // true
h.isZero(h.$(1, 0, 0, 0)) // false
h.isZero(h.$(0, 1, 0, 0)) // false
h.isZero(h.$(0, 0, 1, 0)) // false
h.isZero(h.$(0, 0, 0, 1)) // false
isInteger
h.isInteger(h.$(1, 2, 3, 4)) // true
h.isInteger(h.$(1, r.$(2, 3), 3, 4)) // false
h.isInteger(h.$(1, r.$(4, 2), 3, 4)) // true
Addition
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, 1, 2, 1)
// new object is generated
q3 = h.add(q1, q2)
q3.toString() // '2 + i(3) + j(5) + k(5)'
In-place addition
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, 1, 2, 1)
// new object is not generated
q1 = h.iadd(q1, q2)
q1.toString() // '2 + i(3) + j(5) + k(5)'
Subtraction
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, 1, 2, 1)
// new object is generated
q3 = h.sub(q1, q2)
q3.toString() // 'i(1) + j(1) + k(3)'
In-place subtraction
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, 1, 2, 1)
// new object is not generated
q1 = h.isub(q1, q2)
q1.toString() // 'i(1) + j(1) + k(3)'
Maltiplication
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, -2, -3, -4)
// new object is generated
q3 = h.mul(q1, q2)
q3.toString() // '30'
// some fundametal products
h.mul(h.$(1, 0, 0, 0), h.$(0, 1, 0, 0)).toString() // 'i(1)'
h.mul(h.$(0, 1, 0, 0), h.$(1, 0, 0, 0)).toString() // 'i(1)'
h.mul(h.$(1, 0, 0, 0), h.$(0, 0, 1, 0)).toString() // 'j(1)'
h.mul(h.$(0, 0, 1, 0), h.$(1, 0, 0, 0)).toString() // 'j(1)'
h.mul(h.$(1, 0, 0, 0), h.$(0, 0, 0, 1)).toString() // 'k(1)'
h.mul(h.$(0, 0, 0, 1), h.$(1, 0, 0, 0)).toString() // 'k(1)'
h.mul(h.$(1, 0, 0, 0), h.$(1, 0, 0, 0)).toString() // '1'
h.mul(h.$(0, 1, 0, 0), h.$(0, 1, 0, 0)).toString() // '-1'
h.mul(h.$(0, 0, 1, 0), h.$(0, 0, 1, 0)).toString() // '-1'
h.mul(h.$(0, 0, 0, 1), h.$(0, 0, 0, 1)).toString() // '-1'
h.mul(h.$(0, 1, 0, 0), h.$(0, 0, 1, 0)).toString() // 'k(1)'
h.mul(h.$(0, 0, 1, 0), h.$(0, 1, 0, 0)).toString() // 'k(-1)'
h.mul(h.$(0, 0, 1, 0), h.$(0, 0, 0, 1)).toString() // 'i(1)'
h.mul(h.$(0, 0, 0, 1), h.$(0, 0, 1, 0)).toString() // 'i(-1)'
h.mul(h.$(0, 0, 0, 1), h.$(0, 1, 0, 0)).toString() // 'j(1)'
h.mul(h.$(0, 1, 0, 0), h.$(0, 0, 0, 1)).toString() // 'j(-1)'
In-place multiplication
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, -2, -3, -4)
// new object is not generated
q1 = h.imul(q1, q2)
q1.toString() // '30'
Division
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, 2, 3, 4)
// new object is generated
q3 = h.div(q1, q2)
q3.toString() // '1'
In-place division
q1 = h.$(1, 2, 3, 4)
q2 = h.$(1, 2, 3, 4)
// new object is not generated
q1 = h.idiv(q1, q2)
q1.toString() // '1'
Multiplication by -1
q1 = h.$(1, 2, 3, 4)
// new object is generated
q2 = h.neg(q1)
q2.toString() // '-1 + i(-2) + j(-3) + k(-4)'
In-place multiplication by -1
q1 = h.$(1, 2, 3, 4)
// new object is not generated
q1 = h.ineg(q1)
q1.toString() // '-1 + i(-2) + j(-3) + k(-4)'
Conjugate
q1 = h.$(1, 2, 3, 4)
// new object is generated
q2 = h.cjg(q1)
q2.toString() // '1 + i(-2) + j(-3) + k(-4)'
In-place evaluation of the conjugate
q1 = h.$(1, 2, 3, 4)
// new object is not generated
q1 = h.icjg(q1)
q1.toString() // '1 + i(-2) + j(-3) + k(-4)'
Square of the absolute value
q1 = h.$(1, 2, 3, 4)
let a = h.abs2(q1)
a.toString() // '30'
// return value is not a quaternion (but a real number)
a.re // undefined
a.im // undefined
JSON (stringify and parse)
q1 = h.$(1, 2, 3, 4)
let str = JSON.stringify(q1)
q2 = JSON.parse(str, h.reviver)
h.eq(q1, q2) // true
ESDoc documents
For more examples, see ESDoc documents:
cd node_modules/@kkitahara/quaternion-algebra
npm install --only=dev
npm run doc
and open doc/index.html
in your browser.
LICENSE
© 2019 Koichi Kitahara
Apache 2.0