UNPKG

730 BJavaScriptView Raw
1const CayleyDickson = require('cayley-dickson')
2const createScalar = require('./createScalar')
3const no = require('not-defined')
4
5/**
6 * A composition algebra is one of ℝ, ℂ, ℍ, O:
7 * Real, Complex, Quaternion, Octonion.
8 *
9 * https://en.wikipedia.org/wiki/Composition_algebra
10 *
11 * @param {Object} field
12 * @param {Number} [num] of CayleyDickson construction iterations. Can be 1, 2, 4 or 8.
13 *
14 * @returns {Object} Scalar
15 */
16
17function CompositionAlgebra (field, num) {
18 if (no(num)) num = 1
19
20 const logBase2 = [1, 2, 4, 8].indexOf(num)
21
22 if (logBase2 === -1) {
23 throw new TypeError('Argument n must be 1, 2, 4 or 8')
24 }
25
26 return createScalar(CayleyDickson(field, logBase2))
27}
28
29module.exports = CompositionAlgebra