all files / algebra/src/ CompositionAlgebra.js

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