UNPKG

64.3 kBJavaScriptView Raw
1require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2var algebraRing = require('algebra-ring')
3var staticProps = require('static-props')
4
5var pkg = require('./package.json')
6
7/**
8 * Prepend package name to error message
9 */
10
11function msg (str) {
12 return pkg.name + ': ' + str
13}
14
15var error = {}
16
17staticProps(error)({
18 groupCardinalityIsNotPrime: msg('elements length must be prime'),
19 elementsAreNotUnique: msg('elements must be unique')
20})
21
22/**
23 * Check if a number is prime
24 *
25 * @param {Number} n
26 *
27 * @returns {Boolean}
28 */
29
30function isPrime (n) {
31 if (n === 1) return false
32 if (n === 2) return true
33
34 var m = Math.sqrt(n)
35
36 for (var i = 2; i <= m; i++) if (n % i === 0) return false
37
38 return true
39}
40
41/**
42 * Check if given elements are unique
43 *
44 * @param {Array} elements
45 *
46 * @returns {Boolean}
47 */
48
49function unique (elements) {
50 for (var i = 0; i < elements.length - 1; i++) {
51 for (var j = i + 1; j < elements.length; j++) {
52 if (elements[i] === elements[j]) return false
53 }
54 }
55
56 return true
57}
58
59/**
60 * Construct a space isomorphic to Zp: the cyclic group of order p, where p is prime.
61 *
62 * @param {Array|String} elements
63 *
64 * @returns {Object} cyclic ring
65 */
66
67function algebraCyclic (elements) {
68 if (!isPrime(elements.length)) {
69 throw new TypeError(error.groupCardinalityIsNotPrime)
70 }
71
72 if (!unique(elements)) {
73 throw new TypeError(error.elementsAreNotUnique)
74 }
75
76 var zero = elements[0]
77 var one = elements[1]
78
79 function numOf (element) {
80 return elements.indexOf(element)
81 }
82
83 function addition (element1, element2) {
84 var n = numOf(element1) + numOf(element2)
85
86 n = n % elements.length
87
88 return elements[n]
89 }
90
91 function contains (element) {
92 return elements.indexOf(element) > -1
93 }
94
95 function multiplication (element1, element2) {
96 var n = numOf(element1) * numOf(element2)
97
98 n = n % elements.length
99
100 return elements[n]
101 }
102
103 function inversion (element) {
104 for (var i = 0; i < elements.length; i++) {
105 if (elements[1] === multiplication(element, elements[i])) {
106 return elements[i]
107 }
108 }
109 }
110
111 function negation (element) {
112 var n = numOf(element)
113
114 if (n === 0) return element
115
116 n = elements.length - n
117
118 return elements[n]
119 }
120
121 function equality (element1, element2) {
122 return element1 === element2
123 }
124
125 return algebraRing([zero, one], {
126 equality: equality,
127 contains: contains,
128 addition: addition,
129 negation: negation,
130 multiplication: multiplication,
131 inversion: inversion
132 })
133}
134
135staticProps(algebraCyclic)({ error: error })
136
137module.exports = algebraCyclic
138
139},{"./package.json":2,"algebra-ring":5,"static-props":15}],2:[function(require,module,exports){
140module.exports={
141 "_args": [
142 [
143 "algebra-cyclic@0.2.4",
144 "/Users/io/github.com/fibo/algebra"
145 ]
146 ],
147 "_from": "algebra-cyclic@0.2.4",
148 "_id": "algebra-cyclic@0.2.4",
149 "_inBundle": false,
150 "_integrity": "sha512-XHcsVP3qp/RtfRgEaeL7G2OrUMFL69j9sIimJhccajQiA+O5z/pQ1ZGb6R70XSr1Ev8Ky36gemdZ6PckZNgCnQ==",
151 "_location": "/algebra-cyclic",
152 "_phantomChildren": {},
153 "_requested": {
154 "type": "version",
155 "registry": true,
156 "raw": "algebra-cyclic@0.2.4",
157 "name": "algebra-cyclic",
158 "escapedName": "algebra-cyclic",
159 "rawSpec": "0.2.4",
160 "saveSpec": null,
161 "fetchSpec": "0.2.4"
162 },
163 "_requiredBy": [
164 "/"
165 ],
166 "_resolved": "https://registry.npmjs.org/algebra-cyclic/-/algebra-cyclic-0.2.4.tgz",
167 "_spec": "0.2.4",
168 "_where": "/Users/io/github.com/fibo/algebra",
169 "author": {
170 "name": "Gianluca Casati",
171 "url": "http://g14n.info"
172 },
173 "bugs": {
174 "url": "https://github.com/fibo/algebra-cyclic/issues"
175 },
176 "dependencies": {
177 "algebra-ring": "^0.6.3",
178 "static-props": "^1.1.1"
179 },
180 "description": "creates a space isomorphic to Zp: the cyclic ring of order p, where p is prime",
181 "devDependencies": {
182 "pre-commit": "^1.1.2",
183 "standa": "^2.0.1",
184 "tape": "^4.9.0"
185 },
186 "homepage": "http://g14n.info/algebra-cyclic",
187 "keywords": [
188 "math",
189 "algebra",
190 "prime",
191 "cyclic"
192 ],
193 "license": "MIT",
194 "main": "index.js",
195 "name": "algebra-cyclic",
196 "pre-commit": [
197 "lint",
198 "test"
199 ],
200 "repository": {
201 "type": "git",
202 "url": "git+https://github.com/fibo/algebra-cyclic.git"
203 },
204 "scripts": {
205 "check-deps": "npm outdated",
206 "lint": "standa --fix",
207 "postversion": "git push origin v${npm_package_version}; npm publish; git push origin master",
208 "test": "tape test.js"
209 },
210 "version": "0.2.4"
211}
212
213},{}],3:[function(require,module,exports){
214const no = require('not-defined')
215const staticProps = require('static-props')
216
217const pkg = require('./package.json')
218
219/**
220 * Prepend package name to error message
221 */
222
223function msg (str) {
224 return pkg.name + ': ' + str
225}
226
227const error = {}
228
229staticProps(error)({
230 argumentIsNotInGroup: msg('argument is not contained in group set'),
231 equalityIsNotReflexive: msg('"equality" is not reflexive'),
232 identityIsNotInGroup: msg('"identity" must be contained in group set'),
233 identityIsNotNeutral: msg('"identity" is not neutral')
234})
235
236/**
237 * Defines an algebra group structure
238 *
239 * @param {Object} given
240 * @param {*} given.identity a.k.a neutral element
241 * @param {Function} given.contains
242 * @param {Function} given.equality
243 * @param {Function} given.compositionLaw
244 * @param {Function} given.inversion
245 * @param {Object} [naming]
246 * @param {String} [naming.identity=zero]
247 * @param {String} [naming.contains=contains]
248 * @param {String} [naming.equality=equality]
249 * @param {String} [naming.disequality=disequality]
250 * @param {String} [naming.compositionLaw=addition]
251 * @param {String} [naming.inversion=negation]
252 * @param {String} [naming.inverseCompositionLaw=subtraction]
253 * @param {String} [naming.notContains=notContains]
254 *
255 * @returns {Object} group
256 */
257
258function algebraGroup (given, naming) {
259 if (no(given)) given = {}
260 if (no(naming)) naming = {}
261
262 // default attribute naming
263
264 const defaultNaming = {
265 compositionLaw: 'addition',
266 contains: 'contains',
267 disequality: 'disequality',
268 equality: 'equality',
269 identity: 'zero',
270 inverseCompositionLaw: 'subtraction',
271 inversion: 'negation',
272 notContains: 'notContains'
273 }
274
275 /**
276 * Returns a prop custom name or its default
277 *
278 * @param {String} name
279 *
280 * @returns {String} actualName
281 */
282
283 function prop (name) {
284 if (typeof naming[name] === 'string') return naming[name]
285 else return defaultNaming[name]
286 }
287
288 /**
289 * Wraps operator by checking if arguments are contained in group.
290 *
291 * @param {Object} given operators
292 * @param {String} operator name
293 * @param {Number} arity
294 *
295 * @returns {Function} internalOperator
296 */
297
298 function internalOperator (given, operator, arity) {
299 return function () {
300 const args = [].slice.call(arguments, 0, arity)
301
302 if (contains.apply(null, args)) {
303 return given[operator].apply(null, args)
304 } else {
305 throw new TypeError(error.argumentIsNotInGroup)
306 }
307 }
308 }
309
310 // operators
311
312 const secureCompositionLaw = internalOperator(given, 'compositionLaw', 2)
313 const secureInversion = internalOperator(given, 'inversion', 1)
314
315 function compositionLaw () {
316 return [].slice.call(arguments).reduce(secureCompositionLaw)
317 }
318
319 function contains () {
320 const arg = [].slice.call(arguments)
321
322 for (var i in arg) {
323 if (!given.contains(arg[i])) {
324 return false
325 }
326 }
327
328 return true
329 }
330
331 function notContains (a) { return !contains(a) }
332
333 function disequality (a, b) { return !given.equality(a, b) }
334
335 function inverseCompositionLaw (a) {
336 const rest = [].slice.call(arguments, 1)
337
338 return secureCompositionLaw(a, rest.map(secureInversion).reduce(secureCompositionLaw))
339 }
340
341 // identity element
342 const e = given.identity
343
344 // Check that e=e.
345 if (given.equality(e, e) !== true) {
346 throw new TypeError(error.equalityIsNotReflexive)
347 }
348
349 if (!given.contains(e)) {
350 throw new TypeError(error.identityIsNotInGroup)
351 }
352
353 // Check that e+e=e.
354 if (!given.equality(given.compositionLaw(e, e), e)) {
355 throw new TypeError(error.identityIsNotNeutral)
356 }
357
358 const definition = {}
359
360 definition[prop('identity')] = e
361
362 // Wrap functions otherwise staticProps will treat them as getters.
363 definition[prop('contains')] = () => contains
364 definition[prop('notContains')] = () => notContains
365 definition[prop('compositionLaw')] = () => compositionLaw
366 definition[prop('inversion')] = () => secureInversion
367 definition[prop('inverseCompositionLaw')] = () => inverseCompositionLaw
368 definition[prop('equality')] = () => given.equality
369 definition[prop('disequality')] = () => disequality
370
371 const group = {}
372
373 // Add immutable props to group.
374 staticProps(group)(definition)
375
376 return group
377}
378
379staticProps(algebraGroup)({ error })
380
381module.exports = algebraGroup
382
383},{"./package.json":4,"not-defined":14,"static-props":15}],4:[function(require,module,exports){
384module.exports={
385 "_args": [
386 [
387 "algebra-group@0.6.2",
388 "/Users/io/github.com/fibo/algebra"
389 ]
390 ],
391 "_from": "algebra-group@0.6.2",
392 "_id": "algebra-group@0.6.2",
393 "_inBundle": false,
394 "_integrity": "sha512-//pQGrgIU/Yn9B3UlquJao+GNgKILnD4j5SPpKxQCVe234Mqce6VclldvuDNa8r8HpY67up8UyY8IwIeQeVVAA==",
395 "_location": "/algebra-group",
396 "_phantomChildren": {},
397 "_requested": {
398 "type": "version",
399 "registry": true,
400 "raw": "algebra-group@0.6.2",
401 "name": "algebra-group",
402 "escapedName": "algebra-group",
403 "rawSpec": "0.6.2",
404 "saveSpec": null,
405 "fetchSpec": "0.6.2"
406 },
407 "_requiredBy": [
408 "/algebra-ring"
409 ],
410 "_resolved": "https://registry.npmjs.org/algebra-group/-/algebra-group-0.6.2.tgz",
411 "_spec": "0.6.2",
412 "_where": "/Users/io/github.com/fibo/algebra",
413 "author": {
414 "name": "Gianluca Casati",
415 "url": "http://g14n.info"
416 },
417 "bugs": {
418 "url": "https://github.com/fibo/algebra-group/issues"
419 },
420 "dependencies": {
421 "not-defined": "^2.0.1",
422 "static-props": "^1.1.1"
423 },
424 "description": "defines and algebra group structure",
425 "devDependencies": {
426 "dot-editorconfig": "^1.1.0",
427 "pre-commit": "^1.2.2",
428 "standa": "^2.0.1",
429 "tape": "^4.9.0"
430 },
431 "homepage": "http://g14n.info/algebra-group",
432 "keywords": [
433 "algebra"
434 ],
435 "license": "MIT",
436 "main": "algebra-group.js",
437 "name": "algebra-group",
438 "pre-commit": [
439 "lint",
440 "test",
441 "check-deps"
442 ],
443 "repository": {
444 "type": "git",
445 "url": "git://github.com/fibo/algebra-group.git"
446 },
447 "scripts": {
448 "check-deps": "npm outdated",
449 "lint": "standa",
450 "postversion": "git push origin v${npm_package_version}; npm publish; git push origin master",
451 "test": "NODE_PATH=. tape test.js"
452 },
453 "version": "0.6.2"
454}
455
456},{}],5:[function(require,module,exports){
457const group = require('algebra-group')
458const staticProps = require('static-props')
459
460const pkg = require('./package.json')
461
462/**
463 * Prepend package name to error message
464 */
465
466function msg (str) {
467 return pkg.name + ': ' + str
468}
469
470const error = {
471 cannotDivideByZero: msg('Cannot divide by zero'),
472 doesNotContainIdentity: msg('"identity" must be contained in ring set'),
473 identityIsNotNeutral: msg('"identity" is not neutral')
474}
475
476/**
477 * Define an algebra ring structure
478 *
479 * @param {Array} identity
480 * @param {*} identity[0] a.k.a zero
481 * @param {*} identity[1] a.k.a uno
482 * @param {Object} given operator functions
483 * @param {Function} given.contains
484 * @param {Function} given.equality
485 * @param {Function} given.addition
486 * @param {Function} given.negation
487 * @param {Function} given.multiplication
488 * @param {Function} given.inversion
489 *
490 * @returns {Object} ring
491 */
492
493function algebraRing (identity, given) {
494 // A ring is a group, with multiplication.
495
496 const ring = group({
497 identity: identity[0],
498 contains: given.contains,
499 equality: given.equality,
500 compositionLaw: given.addition,
501 inversion: given.negation
502 })
503
504 // operators
505
506 function multiplication () {
507 return [].slice.call(arguments).reduce(given.multiplication)
508 }
509
510 function inversion (a) {
511 if (ring.equality(a, ring.zero)) {
512 throw new TypeError(error.cannotDivideByZero)
513 }
514
515 return given.inversion(a)
516 }
517
518 function division (a) {
519 const rest = [].slice.call(arguments, 1)
520
521 return given.multiplication(a, rest.map(inversion).reduce(given.multiplication))
522 }
523
524 ring.multiplication = multiplication
525 ring.inversion = inversion
526 ring.division = division
527
528 // Multiplicative identity.
529
530 const one = identity[1]
531
532 if (ring.notContains(one)) {
533 throw new TypeError(error.doesNotContainIdentity)
534 }
535
536 // Check that one*one=one.
537 if (ring.disequality(given.multiplication(one, one), one)) {
538 throw new TypeError(error.identityIsNotNeutral)
539 }
540
541 if (ring.notContains(identity[1])) {
542 throw new TypeError(error.doesNotContainIdentity)
543 }
544
545 ring.one = identity[1]
546
547 return ring
548}
549
550staticProps(algebraRing)({error: error})
551
552module.exports = algebraRing
553
554},{"./package.json":6,"algebra-group":3,"static-props":15}],6:[function(require,module,exports){
555module.exports={
556 "_args": [
557 [
558 "algebra-ring@0.6.3",
559 "/Users/io/github.com/fibo/algebra"
560 ]
561 ],
562 "_from": "algebra-ring@0.6.3",
563 "_id": "algebra-ring@0.6.3",
564 "_inBundle": false,
565 "_integrity": "sha512-aNALbw7Pal6APNE9EqbsFGmQZ+9ZSBXGIwsLVydypeEZC+YCaymnAkqV0hJYuPGjUqdwTa5XP7JsmNnmqAw4HA==",
566 "_location": "/algebra-ring",
567 "_phantomChildren": {},
568 "_requested": {
569 "type": "version",
570 "registry": true,
571 "raw": "algebra-ring@0.6.3",
572 "name": "algebra-ring",
573 "escapedName": "algebra-ring",
574 "rawSpec": "0.6.3",
575 "saveSpec": null,
576 "fetchSpec": "0.6.3"
577 },
578 "_requiredBy": [
579 "/algebra-cyclic",
580 "/cayley-dickson"
581 ],
582 "_resolved": "https://registry.npmjs.org/algebra-ring/-/algebra-ring-0.6.3.tgz",
583 "_spec": "0.6.3",
584 "_where": "/Users/io/github.com/fibo/algebra",
585 "author": {
586 "name": "Gianluca Casati",
587 "url": "http://g14n.info"
588 },
589 "bugs": {
590 "url": "https://github.com/fibo/algebra-ring/issues"
591 },
592 "dependencies": {
593 "algebra-group": "^0.6.1",
594 "static-props": "^1.1.1"
595 },
596 "description": "defines an algebra ring structure",
597 "devDependencies": {
598 "pre-commit": "^1.1.2",
599 "standa": "^2.0.1",
600 "tape": "^4.9.0"
601 },
602 "homepage": "http://g14n.info/algebra-ring",
603 "keywords": [
604 "algebra",
605 "ring",
606 "structure"
607 ],
608 "license": "MIT",
609 "main": "algebra-ring.js",
610 "name": "algebra-ring",
611 "pre-commit": [
612 "lint",
613 "test",
614 "check-deps"
615 ],
616 "repository": {
617 "type": "git",
618 "url": "git+https://github.com/fibo/algebra-ring.git"
619 },
620 "scripts": {
621 "check-deps": "npm outdated",
622 "lint": "standa --fix",
623 "postversion": "git push origin v${npm_package_version}; npm publish; git push origin master",
624 "test": "NODE_PATH=. tape test.js"
625 },
626 "version": "0.6.3"
627}
628
629},{}],7:[function(require,module,exports){
630var ring = require('algebra-ring')
631var twoPow = Math.pow.bind(null, 2)
632
633/**
634 * Turn unary operator on single value to operator on n values.
635 */
636
637function arrayfy1 (operator, dim) {
638 if (dim === 1) {
639 return operator
640 } else {
641 return function (a) {
642 var b = []
643
644 for (var i = 0; i < dim; i++) {
645 b.push(operator(a[i]))
646 }
647
648 return b
649 }
650 }
651}
652
653/**
654 * Turn binary operator on single value to operator on n values.
655 */
656
657function arrayfy2 (operator, dim) {
658 if (dim === 1) {
659 return operator
660 } else {
661 return function (a, b) {
662 var c = []
663
664 for (var i = 0; i < dim; i++) {
665 c.push(operator(a[i], b[i]))
666 }
667
668 return c
669 }
670 }
671}
672
673/**
674 * Iterate Cayley-Disckson construction
675 *
676 * @params {Object} given field
677 * @params {*} given.zero
678 * @params {*} given.one
679 * @params {Function} given.equality
680 * @params {Function} given.contains
681 * @params {Function} given.addition
682 * @params {Function} given.negation
683 * @params {Function} given.multiplication
684 * @params {Function} given.inversion
685 * @params {Number} iterations
686 *
687 * @returns {Object} algebra
688 */
689
690function iterateCayleyDickson (given, iterations) {
691 var field = ring([given.zero, given.one], given)
692
693 if (iterations === 0) {
694 return field
695 }
696
697 var fieldZero = field.zero
698 var fieldOne = field.one
699 var fieldAddition = field.addition
700 var fieldMultiplication = field.multiplication
701 var fieldNegation = field.negation
702 var fieldDisequality = field.disequality
703 var fieldNotContains = field.notContains
704
705 // identities
706
707 var one = []
708 var zero = []
709 var dim = twoPow(iterations)
710
711 one.push(fieldOne)
712 zero.push(fieldZero)
713
714 for (var i = 1; i < dim; i++) {
715 one.push(fieldZero)
716 zero.push(fieldZero)
717 }
718
719 // operators
720
721 function equality (a, b) {
722 for (var i = 0; i < dim; i++) {
723 if (fieldDisequality(a[i], b[i])) {
724 return false
725 }
726 }
727
728 return true
729 }
730
731 function contains (a) {
732 for (var i = 0; i < dim; i++) {
733 if (fieldNotContains(a[i])) {
734 return false
735 }
736 }
737
738 return true
739 }
740
741 function buildConjugation (fieldNegation, iterations) {
742 if (iterations === 0) {
743 return function (a) { return a }
744 }
745
746 var dim = twoPow(iterations)
747
748 // b -> p looks like complex conjugation simmetry (:
749 function conjugation (b) {
750 var p = [b[0]]
751
752 for (var i = 1; i < dim; i++) {
753 p.push(fieldNegation(b[i]))
754 }
755
756 return p
757 }
758
759 return conjugation
760 }
761
762 var conjugation = buildConjugation(fieldNegation, iterations)
763
764 function buildMultiplication (fieldAddition, fieldNegation, fieldMultiplication, iterations) {
765 if (iterations === 0) {
766 return function (a, b) {
767 return fieldMultiplication(a[0], b[0])
768 }
769 }
770
771 var dim = twoPow(iterations)
772 var halfDim = twoPow(iterations - 1)
773
774 var add = arrayfy2(fieldAddition, halfDim)
775 var conj = buildConjugation(fieldNegation, iterations - 1)
776 var mul = buildMultiplication(fieldAddition, fieldNegation, fieldMultiplication, iterations - 1)
777 var neg = arrayfy1(fieldNegation, halfDim)
778
779 function multiplication (a, b) {
780 // a = (p, q)
781 // b = (r, s)
782
783 var p = []
784 var q = []
785 var r = []
786 var s = []
787
788 for (var i1 = 0; i1 < halfDim; i1++) {
789 p.push(a[i1])
790 r.push(b[i1])
791 }
792
793 for (var i2 = halfDim; i2 < dim; i2++) {
794 q.push(a[i2])
795 s.push(b[i2])
796 }
797
798 // var denote conj(x) as x`
799 //
800 // Multiplication law is given by
801 //
802 // (p, q)(r, s) = (pr - s`q, sp + qr`)
803
804 var t = add(mul(p, r), neg(mul(conj(s), q)))
805 var u = add(mul(s, p), mul(q, conj(r)))
806
807 if (halfDim === 1) {
808 return [t, u]
809 } else {
810 var c = []
811
812 for (var i3 = 0; i3 < halfDim; i3++) {
813 c.push(t[i3])
814 }
815
816 for (var i4 = 0; i4 < halfDim; i4++) {
817 c.push(u[i4])
818 }
819
820 return c
821 }
822 }
823
824 return multiplication
825 }
826
827 var multiplication = buildMultiplication(fieldAddition, fieldNegation, fieldMultiplication, iterations)
828
829 function norm (a) {
830 var n = fieldZero
831 var squares = multiplication(a, conjugation(a))
832
833 for (var i = 0; i < dim; i++) {
834 n = fieldAddition(n, squares[i])
835 }
836
837 return n
838 }
839
840 function inversion (a) {
841 var n = norm(a)
842 var b = conjugation(a)
843
844 for (var i = 0; i < dim; i++) {
845 b[i] = field.division(b[i], n)
846 }
847
848 return b
849 }
850
851 var addition = arrayfy2(fieldAddition, dim)
852 var negation = arrayfy1(fieldNegation, dim)
853
854 // Cayley-Dickson construction take a field as input but the result can be often a ring,
855 // this means that it can be *not-commutative*.
856 // To elevate it to an algebra, we need a bilinear form which is given by the norm.
857 var algebra = ring([zero, one], {
858 contains,
859 equality,
860 addition,
861 negation,
862 multiplication,
863 inversion
864 })
865
866 algebra.conjugation = conjugation
867 algebra.norm = norm
868
869 return algebra
870}
871
872module.exports = iterateCayleyDickson
873
874},{"algebra-ring":5}],8:[function(require,module,exports){
875function indicesPermutations (accumulator, currentValue, index, array) {
876 const arrayLength = array.length
877 const result = []
878
879 if (arrayLength === 1) {
880 for (let i = 0; i < currentValue; i++) {
881 result.push([i])
882 }
883 } else {
884 const arrayWithoutLastElement = array.slice(0, arrayLength - 1)
885
886 const previousIteration = arrayWithoutLastElement.reduce(indicesPermutations, [])
887
888 for (let l = 0; l < previousIteration.length; l++) {
889 for (let k = 0; k < currentValue; k++) {
890 result.push(previousIteration[l].concat(k))
891 }
892 }
893 }
894
895 return result
896}
897
898module.exports = exports.default = indicesPermutations
899
900},{}],9:[function(require,module,exports){
901var no = require('not-defined')
902
903/**
904 * Convert a pair of indices to a 1-dimensional index
905 *
906 * @function
907 * @param {Number} i index row
908 * @param {Number} j index column
909 * @param {Number} numCols
910 *
911 * @returns {Number} index
912 */
913
914function matrixToArrayIndex (i, j, numCols) {
915 return j + i * numCols
916}
917
918/**
919 * Compute the sub-matrix formed by deleting the i-th row and j-th column
920 *
921 * @function
922 *
923 * @param {Array} data set
924 * @param {Number} numRows
925 * @param {Number} numCols
926 * @param {Number} row index deleted
927 * @param {Number} col index deleted
928 *
929 * @returns {Array} sub data-set
930 */
931
932function subMatrix (data, numRows, numCols, row, col) {
933 var sub = []
934
935 for (var i = 0; i < numRows; i++) {
936 for (var j = 0; j < numCols; j++) {
937 if ((i !== row) && (j !== col)) {
938 sub.push(data[matrixToArrayIndex(i, j, numCols)])
939 }
940 }
941 }
942
943 return sub
944}
945
946/**
947 * Computes the determinant of a matrix using Laplace's formula
948 *
949 * See https://en.wikipedia.org/wiki/Laplace_expansion
950 *
951 * @function
952 *
953 * @param {Array} data, lenght must be a square.
954 * @param {Object} [scalar]
955 * @param {Function} [scalar.addition = (a, b) -> a + b ]
956 * @param {Function} [scalar.multiplication = (a, b) -> a * b ]
957 * @param {Function} [scalar.negation = (a) -> -a ]
958 * @param {Number} [order], defaults to Math.sqrt(data.length)
959 *
960 * @returns {*} det
961 */
962
963function determinant (data, scalar, order) {
964 // Recursion will stop here:
965 // the determinant of a 1x1 matrix is its only element.
966 if (data.length === 1) return data[0]
967
968 if (no(order)) order = Math.sqrt(data.length)
969
970 if (order % 1 !== 0) {
971 throw new TypeError('data.lenght must be a square')
972 }
973
974 // Default to common real number field.
975 if (no(scalar)) {
976 scalar = {
977 addition: function (a, b) { return a + b },
978 multiplication: function (a, b) { return a * b },
979 negation: function (a) { return -a }
980 }
981 }
982
983 var det
984
985 // TODO choose best row or column to start from, i.e. the one with more zeros
986 // by now we start from first row, and walk by column
987 // needs scalar.isZero
988 //
989 // is scalar.isZero is a function will be used, but should remain optional
990 var startingRow = 0
991
992 for (var col = 0; col < order; col++) {
993 var subData = subMatrix(data, order, order, startingRow, col)
994
995 // +-- Recursion here.
996 // ↓
997 var cofactor = determinant(subData, scalar, order - 1)
998
999 if ((startingRow + col) % 2 === 1) {
1000 cofactor = scalar.negation(cofactor)
1001 }
1002
1003 var index = matrixToArrayIndex(startingRow, col, order)
1004
1005 if (no(det)) {
1006 det = scalar.multiplication(data[index], cofactor) // first iteration
1007 } else {
1008 det = scalar.addition(det, scalar.multiplication(data[index], cofactor))
1009 }
1010 }
1011
1012 return det
1013}
1014
1015module.exports = determinant
1016
1017},{"not-defined":14}],10:[function(require,module,exports){
1018var no = require('not-defined')
1019var staticProps = require('static-props')
1020
1021var pkg = require('./package.json')
1022
1023/**
1024 * Prepend package name to error message
1025 */
1026
1027function msg (str) {
1028 return pkg.name + ': ' + str
1029}
1030
1031var error = {}
1032
1033staticProps(error)({
1034 leftMatrixNotCompatible: msg('Cannot multiply matrix at left side'),
1035 rightMatrixNotCompatible: msg('Cannot multiply matrix at right side')
1036})
1037
1038var matrixToArrayIndex = (i, j, numCols) => (j + i * numCols)
1039
1040/**
1041 * Multiply two matrices, row by column.
1042 *
1043 * @param {Number} customOperator
1044 * @param {Function} [customOperator.addition]
1045 * @param {Function} [customOperator.multiplication]
1046 *
1047 * @returns {Function} operator
1048 */
1049
1050function matrixMultiplication (customOperator) {
1051 // operators
1052
1053 if (no(customOperator)) customOperator = {}
1054
1055 var add = customOperator.addition
1056 var mul = customOperator.multiplication
1057
1058 // Default to operators over Reals.
1059 if (no(add)) add = (a, b) => (a + b)
1060 if (no(mul)) mul = (a, b) => (a * b)
1061
1062 /**
1063 * @param {Number} middle
1064 *
1065 * @returns {Function} mul
1066 */
1067
1068 return function (middle) {
1069 /**
1070 * @param {Array} leftMatrix
1071 * @param {Array} rightMatrix
1072 *
1073 * @returns {Array} matrix
1074 */
1075
1076 return function (leftMatrix, rightMatrix) {
1077 // Compatibilty check.
1078
1079 var cols = rightMatrix.length / middle // right num cols
1080 var rows = leftMatrix.length / middle // left num rows
1081
1082 var colsIsNotInteger = Math.floor(cols) !== cols
1083 var rowsIsNotInteger = Math.floor(rows) !== rows
1084
1085 if (colsIsNotInteger) throw new TypeError(error.rightMatrixNotCompatible)
1086 if (rowsIsNotInteger) throw new TypeError(error.leftMatrixNotCompatible)
1087
1088 // Compute result data.
1089
1090 var data = []
1091
1092 for (var i = 0; i < rows; i++) {
1093 for (var j = 0; j < cols; j++) {
1094 var leftIndex = matrixToArrayIndex(i, 0, middle)
1095 var rightIndex = matrixToArrayIndex(0, j, cols)
1096
1097 var rightElement = rightMatrix[rightIndex]
1098 var leftElement = leftMatrix[leftIndex]
1099
1100 var element = mul(leftElement, rightElement)
1101
1102 for (var k = 1; k < middle; k++) {
1103 leftIndex = matrixToArrayIndex(i, k, middle)
1104 rightIndex = matrixToArrayIndex(k, j, cols)
1105
1106 rightElement = rightMatrix[rightIndex]
1107 leftElement = leftMatrix[leftIndex]
1108
1109 element = add(element, mul(rightElement, leftElement))
1110 }
1111
1112 data.push(element)
1113 }
1114 }
1115
1116 return data
1117 }
1118 }
1119}
1120
1121staticProps(matrixMultiplication)({ error })
1122
1123module.exports = matrixMultiplication
1124
1125},{"./package.json":11,"not-defined":14,"static-props":15}],11:[function(require,module,exports){
1126module.exports={
1127 "_args": [
1128 [
1129 "matrix-multiplication@0.5.2",
1130 "/Users/io/github.com/fibo/algebra"
1131 ]
1132 ],
1133 "_from": "matrix-multiplication@0.5.2",
1134 "_id": "matrix-multiplication@0.5.2",
1135 "_inBundle": false,
1136 "_integrity": "sha512-rr3Adfxn9cktAn8zYAkYiDbFZFkFflwjm9oSm5drBIQJPjFoqUlT9nq7aMwXpr+Nr4uurQKgxy+9pfk5X2YmYA==",
1137 "_location": "/matrix-multiplication",
1138 "_phantomChildren": {},
1139 "_requested": {
1140 "type": "version",
1141 "registry": true,
1142 "raw": "matrix-multiplication@0.5.2",
1143 "name": "matrix-multiplication",
1144 "escapedName": "matrix-multiplication",
1145 "rawSpec": "0.5.2",
1146 "saveSpec": null,
1147 "fetchSpec": "0.5.2"
1148 },
1149 "_requiredBy": [
1150 "/"
1151 ],
1152 "_resolved": "https://registry.npmjs.org/matrix-multiplication/-/matrix-multiplication-0.5.2.tgz",
1153 "_spec": "0.5.2",
1154 "_where": "/Users/io/github.com/fibo/algebra",
1155 "author": {
1156 "name": "Gianluca Casati",
1157 "url": "http://g14n.info"
1158 },
1159 "bugs": {
1160 "url": "https://github.com/fibo/matrix-multiplication/issues"
1161 },
1162 "dependencies": {
1163 "not-defined": "^2.0.1",
1164 "static-props": "^1.1.1"
1165 },
1166 "description": "implements row by column multiplication",
1167 "devDependencies": {
1168 "pre-commit": "^1.2.2",
1169 "standa": "^1.0.2",
1170 "tape": "^4.8.0"
1171 },
1172 "homepage": "http://g14n.info/matrix-multiplication",
1173 "keywords": [
1174 "algebra"
1175 ],
1176 "license": "MIT",
1177 "main": "matrix-multiplication.js",
1178 "name": "matrix-multiplication",
1179 "pre-commit": [
1180 "lint",
1181 "test",
1182 "check-deps"
1183 ],
1184 "repository": {
1185 "type": "git",
1186 "url": "git://github.com/fibo/matrix-multiplication.git"
1187 },
1188 "scripts": {
1189 "check-deps": "npm outdated",
1190 "lint": "standa",
1191 "postversion": "git push origin v${npm_package_version}; npm publish; git push origin master",
1192 "test": "NODE_PATH=. tape test.js"
1193 },
1194 "version": "0.5.2"
1195}
1196
1197},{}],12:[function(require,module,exports){
1198var staticProps = require('static-props')
1199
1200var pkg = require('./package.json')
1201
1202/**
1203 * Prepend package name to error message
1204 */
1205
1206function msg (str) {
1207 return pkg.name + ': ' + str
1208}
1209
1210var error = {}
1211
1212staticProps(error)({
1213 outOfBoundIndex: msg('Index exceeds its bound')
1214})
1215
1216/**
1217 * Maps multidimensional array indices to monodimensional array index
1218 *
1219 * Given
1220 *
1221 * dimensions d_1, d_2, d_3 .. d_n
1222 * and
1223 * indices i_1, i_2, i_3 .. i_n
1224 *
1225 * index is computed by formula
1226 * index = i_n + i_(n-1) * d_n + i_(n-2) * d_n * d_(n-1) + ... + i_2 * d_n * d_(n-1) * ... * d_3 + i_1 * d_n * ... * d_2
1227 *
1228 * @param {Array} dimensions
1229 * @param {Array} indices
1230 * @returns {Number} index
1231 */
1232
1233function multiDimArrayIndex (dimensions, indices) {
1234 // Check that indices fit inside dimensions shape.
1235 for (var i = 0; i < dimensions.length; i++) {
1236 if (indices[i] > dimensions[i]) {
1237 throw new TypeError(error.outOfBoundIndex)
1238 }
1239 }
1240
1241 var order = dimensions.length
1242
1243 // Handle order 1
1244 if (order === 1) return indices[0]
1245
1246 //* index = i_n + i_(n-1) * d_n + i_(n-2) * d_n * d_(n-1) + ... + i_2 * d_n * d_(n-1) * ... * d_3 + i_1 * d_n * ... * d_2
1247 var n = order - 1
1248 var factor = dimensions[n] // d_n
1249 var index = indices[n] + factor * indices[n - 1] // i_n + i_(n-1) * d_n
1250
1251 for (var j = 2; j < order; j++) {
1252 factor *= dimensions[n - j]
1253
1254 index += factor * indices[n - j]
1255 }
1256
1257 return index
1258}
1259
1260staticProps(multiDimArrayIndex)({ error: error })
1261
1262module.exports = multiDimArrayIndex
1263
1264},{"./package.json":13,"static-props":15}],13:[function(require,module,exports){
1265module.exports={
1266 "_args": [
1267 [
1268 "multidim-array-index@0.6.0",
1269 "/Users/io/github.com/fibo/algebra"
1270 ]
1271 ],
1272 "_from": "multidim-array-index@0.6.0",
1273 "_id": "multidim-array-index@0.6.0",
1274 "_inBundle": false,
1275 "_integrity": "sha512-ojHXo7TNXU8i/MxkbC6BqLPR0z1Elr77PuX0xCLoQUSdo/53UjlRBcrDiaOyoLscQp1j84+qQTG1WwHPl6Vz/g==",
1276 "_location": "/multidim-array-index",
1277 "_phantomChildren": {},
1278 "_requested": {
1279 "type": "version",
1280 "registry": true,
1281 "raw": "multidim-array-index@0.6.0",
1282 "name": "multidim-array-index",
1283 "escapedName": "multidim-array-index",
1284 "rawSpec": "0.6.0",
1285 "saveSpec": null,
1286 "fetchSpec": "0.6.0"
1287 },
1288 "_requiredBy": [
1289 "/"
1290 ],
1291 "_resolved": "https://registry.npmjs.org/multidim-array-index/-/multidim-array-index-0.6.0.tgz",
1292 "_spec": "0.6.0",
1293 "_where": "/Users/io/github.com/fibo/algebra",
1294 "author": {
1295 "name": "Gianluca Casati",
1296 "url": "http://g14n.info"
1297 },
1298 "bugs": {
1299 "url": "https://github.com/fibo/multidim-array-index/issues"
1300 },
1301 "dependencies": {
1302 "static-props": "^1.0.0"
1303 },
1304 "description": "maps multidimensional array indices to monodimensional array index",
1305 "devDependencies": {
1306 "dot-editorconfig": "^1.1.0",
1307 "pre-commit": "^1.2.2",
1308 "standa": "^2.0.1",
1309 "tape": "^4.9.0"
1310 },
1311 "homepage": "http://g14n.info/multidim-array-index",
1312 "keywords": [
1313 "array",
1314 "multidim",
1315 "index"
1316 ],
1317 "license": "MIT",
1318 "main": "multidim-array-index.js",
1319 "name": "multidim-array-index",
1320 "pre-commit": [
1321 "check-deps",
1322 "lint",
1323 "test"
1324 ],
1325 "repository": {
1326 "type": "git",
1327 "url": "git://github.com/fibo/multidim-array-index.git"
1328 },
1329 "scripts": {
1330 "check-deps": "npm outdated",
1331 "lint": "standa",
1332 "postversion": "git push origin v${npm_package_version}; npm publish; git push origin master",
1333 "test": "NODE_PATH=. tape test.js"
1334 },
1335 "version": "0.6.0"
1336}
1337
1338},{}],14:[function(require,module,exports){
1339module.exports=function(x){return x==null||(typeof x == 'number'&&isNaN(x))||(x.length<1&&typeof x!='function')||(typeof x=='object'&&Object.keys(x).length<1)}
1340
1341},{}],15:[function(require,module,exports){
1342/**
1343 * @param {Object} obj
1344 * @returns {Function}
1345 */
1346function staticProps (obj) {
1347 /**
1348 * @param {Object} props
1349 * @param {Boolean} [enumerable]
1350 */
1351 return function (props, enumerable) {
1352 var staticProps = {}
1353
1354 for (var propName in props) {
1355 var staticProp = {
1356 configurable: false,
1357 enumerable: enumerable
1358 }
1359
1360 var prop = props[propName]
1361
1362 if (typeof prop === 'function') {
1363 staticProp.get = prop
1364 } else {
1365 staticProp.value = prop
1366
1367 staticProp.writable = false
1368 }
1369
1370 staticProps[propName] = staticProp
1371 }
1372
1373 Object.defineProperties(obj, staticProps)
1374 }
1375}
1376module.exports = exports.default = staticProps
1377
1378},{}],16:[function(require,module,exports){
1379// In browserify context, fall back to a no op.
1380module.exports = function (cb) { cb() }
1381
1382},{}],17:[function(require,module,exports){
1383arguments[4][12][0].apply(exports,arguments)
1384},{"./package.json":18,"dup":12,"static-props":15}],18:[function(require,module,exports){
1385module.exports={
1386 "_args": [
1387 [
1388 "multidim-array-index@0.6.0",
1389 "/Users/io/github.com/fibo/algebra"
1390 ]
1391 ],
1392 "_from": "multidim-array-index@0.6.0",
1393 "_id": "multidim-array-index@0.6.0",
1394 "_inBundle": false,
1395 "_integrity": "sha512-ojHXo7TNXU8i/MxkbC6BqLPR0z1Elr77PuX0xCLoQUSdo/53UjlRBcrDiaOyoLscQp1j84+qQTG1WwHPl6Vz/g==",
1396 "_location": "/tensor-contraction/multidim-array-index",
1397 "_phantomChildren": {},
1398 "_requested": {
1399 "type": "version",
1400 "registry": true,
1401 "raw": "multidim-array-index@0.6.0",
1402 "name": "multidim-array-index",
1403 "escapedName": "multidim-array-index",
1404 "rawSpec": "0.6.0",
1405 "saveSpec": null,
1406 "fetchSpec": "0.6.0"
1407 },
1408 "_requiredBy": [
1409 "/tensor-contraction"
1410 ],
1411 "_resolved": "https://registry.npmjs.org/multidim-array-index/-/multidim-array-index-0.6.0.tgz",
1412 "_spec": "0.6.0",
1413 "_where": "/Users/io/github.com/fibo/algebra",
1414 "author": {
1415 "name": "Gianluca Casati",
1416 "url": "http://g14n.info"
1417 },
1418 "bugs": {
1419 "url": "https://github.com/fibo/multidim-array-index/issues"
1420 },
1421 "dependencies": {
1422 "static-props": "^1.0.0"
1423 },
1424 "description": "maps multidimensional array indices to monodimensional array index",
1425 "devDependencies": {
1426 "dot-editorconfig": "^1.1.0",
1427 "pre-commit": "^1.2.2",
1428 "standa": "^2.0.1",
1429 "tape": "^4.9.0"
1430 },
1431 "homepage": "http://g14n.info/multidim-array-index",
1432 "keywords": [
1433 "array",
1434 "multidim",
1435 "index"
1436 ],
1437 "license": "MIT",
1438 "main": "multidim-array-index.js",
1439 "name": "multidim-array-index",
1440 "pre-commit": [
1441 "check-deps",
1442 "lint",
1443 "test"
1444 ],
1445 "repository": {
1446 "type": "git",
1447 "url": "git://github.com/fibo/multidim-array-index.git"
1448 },
1449 "scripts": {
1450 "check-deps": "npm outdated",
1451 "lint": "standa",
1452 "postversion": "git push origin v${npm_package_version}; npm publish; git push origin master",
1453 "test": "NODE_PATH=. tape test.js"
1454 },
1455 "version": "0.6.0"
1456}
1457
1458},{}],19:[function(require,module,exports){
1459var indicesPermutations = require('indices-permutations')
1460var multiDimArrayIndex = require('multidim-array-index')
1461
1462/**
1463 * Computes tensor contraction
1464 *
1465 * @params {Function} addition
1466 * @params {Array} indicesPair
1467 * @params {Array} tensorDim
1468 * @params {Array} tensorData
1469 * @returns {Array} contractedTensorData
1470 */
1471
1472function tensorContraction (addition, indicesPair, tensorDim, tensorData) {
1473 // Sort indices pair, otherwise algorithm gets unnecessary complicated.
1474 indicesPair.sort()
1475
1476 var p0 = indicesPair[0]
1477 var p1 = indicesPair[1]
1478 var dim0 = tensorDim[p0]
1479 var dim1 = tensorDim[p1]
1480
1481 if (dim0 !== dim1) {
1482 throw new TypeError('Contraction indices does not have the same dimension: ' +
1483 p0 + '-th index = ' + dim0 + ' but ' + p1 + '-th index = ' + dim1 + '.')
1484 }
1485
1486 function varyingTensorDim (result, element, index) {
1487 if ((index !== p0) && (index !== p1)) {
1488 result.push(element)
1489 }
1490
1491 return result
1492 }
1493
1494 function copyArray (result, element) {
1495 result.push(element)
1496
1497 return result
1498 }
1499
1500 function sumOverVarying (tensorData) {
1501 return function (result, varyingCombination) {
1502 var firstCombination = varyingCombination.reduce(copyArray, [])
1503 firstCombination.splice(p0, 0, 0)
1504 firstCombination.splice(p1, 0, 0)
1505 var firstIndex = multiDimArrayIndex(tensorDim, firstCombination)
1506 var element = tensorData[firstIndex]
1507
1508 for (var i = 1; i < dim0; i++) {
1509 var combination = varyingCombination.reduce(copyArray, [])
1510 combination.splice(p0, 0, i)
1511 combination.splice(p1, 0, i)
1512 var index = multiDimArrayIndex(tensorDim, combination)
1513 element = addition(element, tensorData[index])
1514 }
1515
1516 result.push(element)
1517
1518 return result
1519 }
1520 }
1521
1522 // If given tensor has order 2, the contracted tensor will be a scalar
1523 // so it makes sense to return an element, not an array.
1524 // Furthermore, varyingTensorDim will be an empty array so generic algorithm
1525 // will not even be triggered. Then it will be simply computed the trace.
1526 if (tensorDim.length === 2) {
1527 var trace = tensorData[0]
1528
1529 for (var i = 1; i < dim0; i++) {
1530 var combination = [i, i]
1531 var index = multiDimArrayIndex(tensorDim, combination)
1532 trace = addition(trace, tensorData[index])
1533 }
1534
1535 return trace
1536 } else {
1537 return tensorDim
1538 .reduce(varyingTensorDim, [])
1539 .reduce(indicesPermutations, [])
1540 .reduce(sumOverVarying(tensorData), [])
1541 }
1542}
1543
1544module.exports = tensorContraction
1545
1546},{"indices-permutations":8,"multidim-array-index":17}],20:[function(require,module,exports){
1547const Boole = {
1548 zero: false,
1549 one: true,
1550 contains: (a) => (typeof a === 'boolean'),
1551 addition: (a, b) => (a || b),
1552 equality: (a, b) => (a === b),
1553 negation: (a) => (a),
1554 multiplication: (a, b) => (a && b),
1555 inversion: (a) => (a)
1556}
1557
1558module.exports = Boole
1559
1560},{}],21:[function(require,module,exports){
1561const CayleyDickson = require('cayley-dickson')
1562const no = require('not-defined')
1563
1564const createScalar = require('./createScalar')
1565
1566/**
1567 * A composition algebra is one of ℝ, ℂ, ℍ, O:
1568 * Real, Complex, Quaternion, Octonion.
1569 *
1570 * https://en.wikipedia.org/wiki/Composition_algebra
1571 *
1572 * @param {Object} field
1573 * @param {Number} [num] of CayleyDickson construction iterations. Can be 1, 2, 4 or 8.
1574 *
1575 * @returns {Object} Scalar
1576 */
1577
1578function CompositionAlgebra (field, num) {
1579 if (no(num)) num = 1
1580
1581 const logBase2 = [1, 2, 4, 8].indexOf(num)
1582
1583 if (logBase2 === -1) {
1584 throw new TypeError('Argument n must be 1, 2, 4 or 8')
1585 }
1586
1587 return createScalar(CayleyDickson(field, logBase2))
1588}
1589
1590module.exports = CompositionAlgebra
1591
1592},{"./createScalar":27,"cayley-dickson":7,"not-defined":14}],22:[function(require,module,exports){
1593const algebraCyclic = require('algebra-cyclic')
1594const createScalar = require('./createScalar')
1595
1596/**
1597 * Create a Cyclic algebra.
1598 *
1599 * @param {String|Array} elements
1600 */
1601
1602function Cyclic (elements) {
1603 const ring = algebraCyclic(elements)
1604
1605 return createScalar(ring)
1606}
1607
1608module.exports = Cyclic
1609
1610},{"./createScalar":27,"algebra-cyclic":1}],23:[function(require,module,exports){
1611const determinant = require('laplace-determinant')
1612const multiplication = require('matrix-multiplication')
1613const multiDimArrayIndex = require('multidim-array-index')
1614const staticProps = require('static-props')
1615const tensorContraction = require('tensor-contraction')
1616
1617const itemsPool = require('./itemsPool')
1618const toData = require('./toData')
1619
1620/**
1621 * Space of m x n matrices
1622 *
1623 * ```
1624 * const R = algebra.R
1625 *
1626 * const R2x2 = algebra.MatrixSpace(R)(2)
1627 * ```
1628 *
1629 * @param {Object} Scalar
1630 *
1631 * @returns {Function} anonymous with signature (numRows[, numCols])
1632 */
1633
1634function MatrixSpace (Scalar) {
1635 const {
1636 addition,
1637 equality,
1638 subtraction
1639 } = Scalar
1640
1641 const contraction = tensorContraction.bind(null, addition)
1642
1643 const enumerable = true
1644
1645 /**
1646 * @param {Number} numRows
1647 * @param {Number} [numCols] defaults to a square matrix.
1648 *
1649 * @returns {class} Matrix
1650 */
1651
1652 return function (numRows, numCols = numRows) {
1653 const dimension = numRows * numCols
1654 const indices = [numRows, numCols]
1655 const isSquare = (numRows === numCols)
1656
1657 /**
1658 * Determinant computation is defined only if it is a square matrix.
1659 */
1660
1661 function computeDeterminant (matrix) {
1662 const data = toData(matrix)
1663
1664 return determinant(data, Scalar, numRows)
1665 }
1666
1667 /**
1668 * Matrix addition is the scalar addition for every item.
1669 */
1670
1671 function matrixAddition (matrix1, matrix2) {
1672 const matrixData1 = toData(matrix1)
1673 const matrixData2 = toData(matrix2)
1674
1675 let result = []
1676
1677 for (let i = 0; i < dimension; i++) {
1678 result.push(addition(matrixData1[i], matrixData2[i]))
1679 }
1680
1681 return result
1682 }
1683
1684 /**
1685 * Matrix equality checks that all elements are equal.
1686 * It also tries to check if numCols and numRows correspond.
1687 */
1688
1689 function matrixEquality (matrix1, matrix2) {
1690 if (matrix1 instanceof Matrix && matrix2 instanceof Matrix) {
1691 if (matrix1.numCols !== matrix2.numCols) {
1692 return false
1693 }
1694
1695 if (matrix1.numRows !== matrix2.numRows) {
1696 return false
1697 }
1698 }
1699
1700 const matrixData1 = toData(matrix1)
1701 const matrixData2 = toData(matrix2)
1702
1703 if (matrixData1.length !== matrixData2.length) {
1704 return false
1705 }
1706
1707 for (let i = 0; i < dimension; i++) {
1708 if (!equality(matrixData1[i], matrixData2[i])) {
1709 return false
1710 }
1711 }
1712
1713 return true
1714 }
1715 /**
1716 * Multiplies row by column to the right.
1717 *
1718 * @param {Object|Array} rightMatrix
1719 *
1720 * @returns {Object} matrix
1721 */
1722
1723 function matrixMultiplication (leftMatrix, rightMatrix) {
1724 const leftMatrixData = toData(leftMatrix)
1725 const rightMatrixData = toData(rightMatrix)
1726
1727 const rowByColumnMultiplication = multiplication(Scalar)(numCols)
1728
1729 return rowByColumnMultiplication(leftMatrixData, rightMatrixData)
1730 }
1731
1732 /**
1733 * Matrix subtraction is the scalar subtraction for every item.
1734 */
1735
1736 function matrixSubtraction (matrix1, matrix2) {
1737 const matrixData1 = toData(matrix1)
1738 const matrixData2 = toData(matrix2)
1739
1740 let result = []
1741
1742 for (let i = 0; i < dimension; i++) {
1743 result.push(subtraction(matrixData1[i], matrixData2[i]))
1744 }
1745
1746 return result
1747 }
1748
1749 /**
1750 * Calculates the matrix trace.
1751 *
1752 * @see {@link https://en.wikipedia.org/wiki/Trace_(linear_algebra)}
1753 *
1754 * @param {Object|Array} matrix
1755 *
1756 * @returns {Object} scalar
1757 */
1758
1759 function computeTrace (matrix) {
1760 const matrixData = toData(matrix)
1761
1762 return contraction([0, 1], indices, matrixData)
1763 }
1764
1765 /**
1766 * Calculates the transpose of a matrix.
1767 *
1768 * @param {Object|Array} matrix
1769 *
1770 * @returns {Array} matrix
1771 */
1772
1773 function transpose (matrix) {
1774 const matrixData = toData(matrix)
1775 const transposedData = []
1776
1777 for (let i = 0; i < numRows; i++) {
1778 for (let j = 0; j < numCols; j++) {
1779 const index = multiDimArrayIndex([numRows, numCols], [i, j])
1780 const transposedIndex = multiDimArrayIndex([numCols, numRows], [j, i])
1781
1782 transposedData[transposedIndex] = matrixData[index]
1783 }
1784 }
1785
1786 return transposedData
1787 }
1788
1789 /**
1790 * Matrix element.
1791 */
1792
1793 class Matrix {
1794 constructor (data) {
1795 staticProps(this)({
1796 data,
1797 numCols,
1798 numRows
1799 }, enumerable)
1800
1801 staticProps(this)({
1802 Scalar,
1803 tr: () => this.transposed
1804 })
1805
1806 if (isSquare) {
1807 staticProps(this)({
1808 determinant: () => {
1809 const result = computeDeterminant(this)
1810
1811 return new Scalar(result)
1812 },
1813
1814 trace: () => {
1815 const result = computeTrace(this)
1816
1817 return new Scalar(result)
1818 }
1819 })
1820 }
1821 }
1822
1823 equality (matrix) {
1824 return matrixEquality(this, matrix)
1825 }
1826
1827 get transposed () {
1828 const transposedElements = transpose(this)
1829
1830 // Get a class matrix in the transposed matrix space.
1831 // Note that numCols and numRows order as arguments is inverted.
1832 const TransposedMatrix = MatrixSpace(Scalar)(numCols, numRows)
1833
1834 return new TransposedMatrix(transposedElements)
1835 }
1836
1837 addition (matrix) {
1838 const result = matrixAddition(this, matrix)
1839
1840 return new Matrix(result)
1841 }
1842
1843 multiplication (matrix) {
1844 const result = matrixMultiplication(this, matrix)
1845
1846 return new Matrix(result)
1847 }
1848
1849 subtraction (matrix) {
1850 const result = matrixSubtraction(this, matrix)
1851
1852 return new Matrix(result)
1853 }
1854 }
1855
1856 // Method aliases.
1857 Matrix.prototype.add = Matrix.prototype.addition
1858 Matrix.prototype.eq = Matrix.prototype.equality
1859 Matrix.prototype.equal = Matrix.prototype.equality
1860 Matrix.prototype.mul = Matrix.prototype.multiplication
1861 Matrix.prototype.sub = Matrix.prototype.subtraction
1862
1863 staticProps(Matrix)({
1864 numCols,
1865 numRows
1866 })
1867
1868 // Matrix static operators.
1869
1870 staticProps(Matrix)({
1871 addition: () => matrixAddition,
1872 equality: () => matrixEquality,
1873 multiplication: () => matrixMultiplication,
1874 subtraction: () => matrixSubtraction,
1875 transpose: () => transpose
1876 })
1877
1878 staticProps(Matrix)({
1879 add: Matrix.addition,
1880 eq: Matrix.equality,
1881 mul: Matrix.multiplication,
1882 sub: Matrix.subtraction,
1883 tr: Matrix.transpose
1884 })
1885
1886 if (isSquare) {
1887 Matrix.prototype.det = Matrix.prototype.determinant
1888
1889 staticProps(Matrix)({
1890 determinant: () => computeDeterminant,
1891 trace: () => computeTrace
1892 })
1893
1894 staticProps(Matrix)({
1895 det: Matrix.determinant
1896 })
1897 }
1898
1899 return Matrix
1900 }
1901}
1902
1903itemsPool.set('MatrixSpace', MatrixSpace)
1904
1905module.exports = MatrixSpace
1906
1907},{"./itemsPool":28,"./toData":31,"laplace-determinant":9,"matrix-multiplication":10,"multidim-array-index":12,"static-props":15,"tensor-contraction":19}],24:[function(require,module,exports){
1908const algebraRing = require('algebra-ring')
1909const createScalar = require('./createScalar')
1910
1911/**
1912 * Create a Scalar.
1913 */
1914
1915function Scalar (neutralElements, operators) {
1916 const ring = algebraRing(neutralElements, operators)
1917
1918 return createScalar(ring)
1919}
1920
1921module.exports = Scalar
1922
1923},{"./createScalar":27,"algebra-ring":5}],25:[function(require,module,exports){
1924const itemsPool = require('./itemsPool')
1925const matrixMultiplication = require('matrix-multiplication')
1926const staticProps = require('static-props')
1927const toData = require('./toData')
1928
1929/**
1930 * Space of vectors
1931 *
1932 * ```
1933 * const V = VectorSpace(R)(2)
1934 *
1935 * const v = new V([1, 2])
1936 * ```
1937 *
1938 * @param {Object} Scalar
1939 *
1940 * @returns {Function} anonymous with signature (dimension)
1941 */
1942
1943function VectorSpace (Scalar) {
1944 const {
1945 addition,
1946 equality,
1947 multiplication,
1948 subtraction
1949 } = Scalar
1950
1951 const enumerable = true
1952
1953 /**
1954 * @param {Number} dimension
1955 *
1956 * @returns {Function} Vector
1957 */
1958
1959 return function (dimension) {
1960 /**
1961 * Computes the cross product of two vectors.
1962 *
1963 * It is defined only in dimension 3.
1964 *
1965 * @param {Object|Array} vector1
1966 * @param {Object|Array} vector2
1967 *
1968 * @returns {Array} vector
1969 */
1970
1971 function crossProduct (vector1, vector2) {
1972 const vectorData1 = toData(vector1)
1973 const vectorData2 = toData(vector2)
1974
1975 const ux = vectorData1[0]
1976 const uy = vectorData1[1]
1977 const uz = vectorData1[2]
1978
1979 const vx = vectorData2[0]
1980 const vy = vectorData2[1]
1981 const vz = vectorData2[2]
1982
1983 let vector = []
1984
1985 vector.push(subtraction(multiplication(uy, vz), multiplication(uz, vy)))
1986 vector.push(subtraction(multiplication(uz, vx), multiplication(ux, vz)))
1987 vector.push(subtraction(multiplication(ux, vy), multiplication(uy, vx)))
1988
1989 return vector
1990 }
1991
1992 /**
1993 * Multiply a column vector by matrix on right side
1994 *
1995 * @returns {Object} scalar
1996 * @param leftVector
1997 * @param rightMatrix
1998 */
1999
2000 function multiplicationByMatrix (leftVector, rightMatrix) {
2001 const leftVectorData = toData(leftVector)
2002 const rightMatrixData = toData(rightMatrix)
2003
2004 const rowByColumnMultiplication = matrixMultiplication(Scalar)(dimension)
2005
2006 return rowByColumnMultiplication(leftVectorData, rightMatrixData)
2007 }
2008
2009 /**
2010 * Norm of a vector
2011 *
2012 * Given v = (x1, x2, ... xN)
2013 *
2014 * norm is defined as n = x1 * x1 + x2 * x2 + ... + xN * xN
2015 *
2016 * @param {Object|Array} vector
2017 *
2018 * @returns {Object} scalar
2019 */
2020
2021 function norm (vector) {
2022 const data = toData(vector)
2023
2024 let value = multiplication(data[0], data[0])
2025
2026 for (let i = 1; i < dimension; i++) {
2027 value = addition(value, multiplication(data[i], data[i]))
2028 }
2029
2030 return new Scalar(value)
2031 }
2032
2033 /**
2034 * Scalar product
2035 *
2036 * @see {@link https://en.wikipedia.org/wiki/Dot_product}
2037 *
2038 * @param {Object|Array} vector1
2039 * @param {Object|Array} vector2
2040 *
2041 * @returns {*} scalar
2042 */
2043
2044 function scalarProduct (vector1, vector2) {
2045 const vectorData1 = toData(vector1)
2046 const vectorData2 = toData(vector2)
2047
2048 if (vectorData1.length !== vectorData2.length) {
2049 throw new TypeError('Vectors have not the same dimension')
2050 }
2051
2052 let result = multiplication(vectorData1[0], vectorData2[0])
2053
2054 for (let i = 1; i < dimension; i++) {
2055 result = addition(result, multiplication(vectorData1[i], vectorData2[i]))
2056 }
2057
2058 return result
2059 }
2060
2061 /**
2062 * Vector addition is the scalar addition for every coordinate.
2063 */
2064
2065 function vectorAddition (vector1, vector2) {
2066 const vectorData1 = toData(vector1)
2067 const vectorData2 = toData(vector2)
2068
2069 let result = []
2070
2071 for (let i = 0; i < dimension; i++) {
2072 result.push(addition(vectorData1[i], vectorData2[i]))
2073 }
2074
2075 return result
2076 }
2077
2078 /**
2079 * Vector equality checks that all coordinates are equal.
2080 */
2081
2082 function vectorEquality (vector1, vector2) {
2083 const vectorData1 = toData(vector1)
2084 const vectorData2 = toData(vector2)
2085
2086 if (vectorData1.length !== vectorData2.length) {
2087 return false
2088 }
2089
2090 for (let i = 0; i < dimension; i++) {
2091 if (!equality(vectorData1[i], vectorData2[i])) {
2092 return false
2093 }
2094 }
2095
2096 return true
2097 }
2098
2099 /**
2100 * Vector subtraction is the scalar subtraction for every coordinate.
2101 */
2102
2103 function vectorSubtraction (vector1, vector2) {
2104 const vectorData1 = toData(vector1)
2105 const vectorData2 = toData(vector2)
2106
2107 let result = []
2108
2109 for (let i = 0; i < dimension; i++) {
2110 result.push(subtraction(vectorData1[i], vectorData2[i]))
2111 }
2112
2113 return result
2114 }
2115
2116 /**
2117 * Vector element.
2118 */
2119
2120 class Vector {
2121 constructor (data) {
2122 staticProps(this)({ data }, enumerable)
2123
2124 staticProps(this)({
2125 norm: norm(data),
2126 dimension,
2127 Scalar
2128 })
2129
2130 // Method aliases.
2131
2132 staticProps(this)({
2133 add: () => this.addition,
2134 eq: () => this.equality,
2135 equal: () => this.equality,
2136 mul: () => this.multiplication,
2137 scalar: () => this.scalarProduct,
2138 sub: () => this.subtraction
2139 })
2140 }
2141
2142 addition (vector) {
2143 const result = vectorAddition(this, vector)
2144
2145 return new Vector(result)
2146 }
2147
2148 equality (vector) {
2149 return vectorEquality(this, vector)
2150 }
2151
2152 /**
2153 * Multiplication of a vector by a right matrix.
2154 *
2155 * Actually the vector it is supposed to be transposed, so it
2156 * becomes a row-vector, while by convention all vectors are column-vectors,
2157 * and after it is transposed, it can be multiplied by a right matrix.
2158 *
2159 * The transposition happens here implicitly.
2160 *
2161 * If you do not know what it means, do not worry. It is part of
2162 * Geometry first course at the first year of University, and you can
2163 * ignore it, since it has no consequences but it is hard to spot.
2164 *
2165 * I would like to thank and remember here in this comment, my awesome
2166 * prof. of Geometry. Thank you, Monti Bragadin.
2167 */
2168
2169 multiplication (rightMatrix) {
2170 const MatrixSpace = itemsPool.get('MatrixSpace')
2171
2172 const leftVectorData = this.data
2173 const result = multiplicationByMatrix(leftVectorData, rightMatrix)
2174
2175 const rightNumRows = dimension
2176 const rightNumCols = result.length / rightNumRows
2177
2178 const Matrix = MatrixSpace(Scalar)(rightNumRows, rightNumCols)
2179
2180 return new Matrix(result)
2181 }
2182
2183 scalarProduct (vector) {
2184 const result = scalarProduct(this, vector)
2185
2186 return new Scalar(result)
2187 }
2188
2189 subtraction (vector) {
2190 const result = vectorSubtraction(this, vector)
2191
2192 return new Vector(result)
2193 }
2194 }
2195
2196 staticProps(Vector)({
2197 dimension
2198 }, enumerable)
2199
2200 // Vector static operators.
2201
2202 staticProps(Vector)({
2203 addition: () => vectorAddition,
2204 equality: () => vectorEquality,
2205 norm: () => norm,
2206 scalarProduct: () => scalarProduct,
2207 subtraction: () => vectorSubtraction
2208 })
2209
2210 staticProps(Vector)({
2211 add: () => Vector.addition,
2212 eq: () => Vector.equality,
2213 scalar: () => Vector.scalarProduct,
2214 sub: () => Vector.subtraction
2215 })
2216
2217 function crossProductMethod (vector) {
2218 const data = this.data
2219
2220 const result = crossProduct(data, vector)
2221
2222 return new Vector(result)
2223 }
2224
2225 if (dimension === 3) {
2226 Vector.prototype.cross = crossProductMethod
2227 Vector.prototype.crossProduct = crossProductMethod
2228
2229 staticProps(Vector)({
2230 crossProduct: () => crossProduct,
2231 cross: () => crossProduct
2232 })
2233 }
2234
2235 return Vector
2236 }
2237}
2238
2239itemsPool.set('VectorSpace', VectorSpace)
2240
2241module.exports = VectorSpace
2242
2243},{"./itemsPool":28,"./toData":31,"matrix-multiplication":10,"static-props":15}],26:[function(require,module,exports){
2244const toData = require('./toData')
2245
2246/**
2247 * Get an operator that coerces arguments to data.
2248 *
2249 * @api private
2250 *
2251 * @param {Function} operator
2252 *
2253 * @returns {Function} anonymous coerced operator
2254 */
2255
2256function coerced (operator) {
2257 return function () {
2258 return operator.apply(null, [].slice.call(arguments).map(toData))
2259 }
2260}
2261
2262module.exports = coerced
2263
2264},{"./toData":31}],27:[function(require,module,exports){
2265const coerced = require('./coerced')
2266const operators = require('./operators.json')
2267const staticProps = require('static-props')
2268const toData = require('./toData')
2269
2270/**
2271 * @param {Object} ring
2272 *
2273 * @returns {Function} Scalar
2274 */
2275
2276function createScalar (ring) {
2277 const attributes = {
2278 zero: ring.zero,
2279 one: ring.one
2280 }
2281
2282 /**
2283 * Scalar element.
2284 */
2285
2286 class Scalar {
2287 constructor (data) {
2288 // validate data
2289 if (ring.notContains(data)) {
2290 throw new TypeError('Invalid data = ' + data)
2291 }
2292
2293 const enumerable = true
2294 staticProps(this)({ data }, enumerable)
2295
2296 staticProps(this)(attributes)
2297 }
2298 }
2299
2300 staticProps(Scalar)(attributes)
2301
2302 const scalarOperators = ({ categories }) => categories.includes('scalar')
2303
2304 operators.filter(scalarOperators).forEach(operator => {
2305 const isBinary = operator.categories.includes('binary')
2306 const isClosed = operator.isClosed
2307 const isInstanceMethod = operator.isInstanceMethod
2308 const isStaticMethod = operator.isStaticMethod
2309 const isUnary = operator.categories.includes('unary')
2310 const operatorName = operator.name
2311
2312 if (isBinary) {
2313 if (isInstanceMethod) {
2314 Scalar.prototype[operatorName] = function () {
2315 const args = [].slice.call(arguments)
2316 const operands = [this.data].concat(args)
2317
2318 const data = coerced(ring[operatorName]).apply(null, operands)
2319
2320 if (isClosed) {
2321 return new Scalar(data)
2322 } else {
2323 return data
2324 }
2325 }
2326 }
2327
2328 if (isStaticMethod) {
2329 Scalar[operatorName] = function () {
2330 const operands = [].slice.call(arguments).map(toData)
2331
2332 return coerced(ring[operatorName]).apply(null, operands)
2333 }
2334 }
2335 }
2336
2337 if (isUnary) {
2338 if (isInstanceMethod) {
2339 Scalar.prototype[operatorName] = function () {
2340 const data = Scalar[operatorName](this.data)
2341
2342 if (isClosed) {
2343 return new Scalar(data)
2344 } else {
2345 return data
2346 }
2347 }
2348 }
2349
2350 if (isStaticMethod) {
2351 Scalar[operatorName] = function (operand) {
2352 return ring[operatorName](toData(operand))
2353 }
2354 }
2355 }
2356
2357 operator.aliases.forEach(alias => {
2358 if (isInstanceMethod) {
2359 Scalar.prototype[alias] = Scalar.prototype[operatorName]
2360 }
2361
2362 if (isStaticMethod) {
2363 Scalar[alias] = Scalar[operatorName]
2364 }
2365 })
2366 })
2367
2368 return Scalar
2369}
2370
2371module.exports = createScalar
2372
2373},{"./coerced":26,"./operators.json":29,"./toData":31,"static-props":15}],28:[function(require,module,exports){
2374const itemsPool = new Map()
2375
2376module.exports = itemsPool
2377
2378},{}],29:[function(require,module,exports){
2379module.exports=[
2380 {
2381 "aliases": [
2382 "eq",
2383 "equal"
2384 ],
2385 "categories": [
2386 "binary",
2387 "scalar"
2388 ],
2389 "isClosed": false,
2390 "isInstanceMethod": true,
2391 "isStaticMethod": true,
2392 "name": "equality"
2393 },
2394 {
2395 "aliases": [
2396 "ne",
2397 "notEqual"
2398 ],
2399 "categories": [
2400 "binary",
2401 "scalar"
2402 ],
2403 "isClosed": false,
2404 "isInstanceMethod": true,
2405 "isStaticMethod": true,
2406 "name": "disequality"
2407 },
2408 {
2409 "aliases": [
2410 ],
2411 "categories": [
2412 "scalar",
2413 "unary"
2414 ],
2415 "isClosed": false,
2416 "isInstanceMethod": false,
2417 "isStaticMethod": true,
2418 "name": "contains"
2419 },
2420 {
2421 "aliases": [
2422 ],
2423 "categories": [
2424 "scalar",
2425 "unary"
2426 ],
2427 "isClosed": false,
2428 "isInstanceMethod": false,
2429 "isStaticMethod": true,
2430 "name": "notContains"
2431 },
2432 {
2433 "aliases": [
2434 ],
2435 "categories": [
2436 "scalar",
2437 "unary"
2438 ],
2439 "isClosed": false,
2440 "isInstanceMethod": true,
2441 "isStaticMethod": false,
2442 "name": "belongsTo"
2443 },
2444 {
2445 "aliases": [
2446 "add"
2447 ],
2448 "categories": [
2449 "binary",
2450 "group",
2451 "matrix",
2452 "scalar"
2453 ],
2454 "isClosed": true,
2455 "isInstanceMethod": true,
2456 "isStaticMethod": true,
2457 "name": "addition"
2458 },
2459 {
2460 "aliases": [
2461 "neg"
2462 ],
2463 "categories": [
2464 "scalar",
2465 "unary"
2466 ],
2467 "isClosed": true,
2468 "isInstanceMethod": true,
2469 "isStaticMethod": true,
2470 "name": "negation"
2471 },
2472 {
2473 "aliases": [
2474 "sub"
2475 ],
2476 "categories": [
2477 "binary",
2478 "group",
2479 "matrix",
2480 "scalar"
2481 ],
2482 "isClosed": true,
2483 "isInstanceMethod": true,
2484 "isStaticMethod": true,
2485 "name": "subtraction"
2486 },
2487 {
2488 "aliases": [
2489 "mul"
2490 ],
2491 "categories": [
2492 "binary",
2493 "scalar"
2494 ],
2495 "isClosed": true,
2496 "isInstanceMethod": true,
2497 "isStaticMethod": true,
2498 "name": "multiplication"
2499 },
2500 {
2501 "aliases": [
2502 "div"
2503 ],
2504 "categories": [
2505 "binary",
2506 "matrix",
2507 "scalar"
2508 ],
2509 "isClosed": true,
2510 "isInstanceMethod": true,
2511 "isStaticMethod": true,
2512 "name": "division"
2513 },
2514 {
2515 "aliases": [
2516 "inv"
2517 ],
2518 "categories": [
2519 "matrix",
2520 "scalar",
2521 "unary"
2522 ],
2523 "isClosed": true,
2524 "isInstanceMethod": true,
2525 "isStaticMethod": true,
2526 "name": "inversion"
2527 },
2528 {
2529 "aliases": [
2530 "conj"
2531 ],
2532 "categories": [
2533 "matrix",
2534 "scalar",
2535 "vector",
2536 "unary"
2537 ],
2538 "isClosed": true,
2539 "isInstanceMethod": true,
2540 "isStaticMethod": true,
2541 "name": "conjugation"
2542 },
2543 {
2544 "aliases": [
2545 "tr"
2546 ],
2547 "categories": [
2548 "matrix",
2549 "unary"
2550 ],
2551 "isClosed": true,
2552 "isInstanceMethod": true,
2553 "isStaticMethod": true,
2554 "name": "transposition"
2555 },
2556 {
2557 "aliases": [
2558 "dot",
2559 "dotProduct"
2560 ],
2561 "categories": [
2562 "binary",
2563 "vector"
2564 ],
2565 "isClosed": false,
2566 "isInstanceMethod": true,
2567 "isStaticMethod": true,
2568 "name": "scalarProduct"
2569 },
2570 {
2571 "aliases": [
2572 "det"
2573 ],
2574 "categories": [
2575 "unary",
2576 "matrix"
2577 ],
2578 "isClosed": false,
2579 "isInstanceMethod": true,
2580 "isStaticMethod": true,
2581 "name": "determinant"
2582 },
2583 {
2584 "aliases": [
2585 ],
2586 "categories": [
2587 "unary",
2588 "vector"
2589 ],
2590 "isClosed": false,
2591 "isInstanceMethod": true,
2592 "isStaticMethod": true,
2593 "name": "norm"
2594 },
2595 {
2596 "aliases": [
2597 "adj"
2598 ],
2599 "categories": [
2600 "unary",
2601 "matrix"
2602 ],
2603 "isClosed": true,
2604 "isInstanceMethod": true,
2605 "isStaticMethod": true,
2606 "name": "adjoint"
2607 },
2608 {
2609 "aliases": [
2610 "cross"
2611 ],
2612 "categories": [
2613 "binary",
2614 "vector"
2615 ],
2616 "isClosed": true,
2617 "isInstanceMethod": true,
2618 "isStaticMethod": true,
2619 "name": "crossProduct"
2620 }
2621]
2622
2623},{}],30:[function(require,module,exports){
2624const realField = {
2625 zero: 0,
2626 one: 1,
2627 // NaN, Infinity and -Infinity are not allowed.
2628 contains: (a) => (typeof a === 'number' && isFinite(a)),
2629 equality: (a, b) => {
2630 // Consider
2631 //
2632 // 0.1 + 0.2 === 0.3
2633 //
2634 // It evaluates to false. Actually the expression
2635 //
2636 // 0.1 + 0.2
2637 //
2638 // will return
2639 //
2640 // 0.30000000000000004
2641 //
2642 // Hence we need to approximate equality with an epsilon.
2643
2644 return Math.abs(a - b) < Number.EPSILON
2645 },
2646 addition: (a, b) => a + b,
2647 negation: (a) => -a,
2648 multiplication: (a, b) => a * b,
2649 inversion: (a) => 1 / a
2650}
2651
2652module.exports = realField
2653
2654},{}],31:[function(require,module,exports){
2655const no = require('not-defined')
2656
2657/**
2658 * Extract data attribute, if any, and check it
2659 *
2660 * @param {*} arg
2661 *
2662 * @returns {*} data
2663 */
2664
2665function toData (arg) {
2666 let data
2667
2668 if (no(arg.data)) data = arg
2669 else data = arg.data
2670
2671 if (no(data)) throw new TypeError('No data')
2672
2673 return data
2674}
2675
2676module.exports = toData
2677
2678},{"not-defined":14}],"algebra":[function(require,module,exports){
2679require('strict-mode')(() => {
2680 const Boole = require('./src/Boole')
2681 exports.Boole = Boole
2682
2683 const CompositionAlgebra = require('./src/CompositionAlgebra')
2684 exports.CompositionAlgebra = CompositionAlgebra
2685
2686 const Cyclic = require('./src/Cyclic')
2687 exports.Cyclic = Cyclic
2688
2689 const Scalar = require('./src/Scalar')
2690 exports.Scalar = Scalar
2691
2692 const realField = require('./src/realField')
2693
2694 const Real = CompositionAlgebra(realField, 1)
2695 const Complex = CompositionAlgebra(realField, 2)
2696 const Quaternion = CompositionAlgebra(realField, 4)
2697 const Octonion = CompositionAlgebra(realField, 8)
2698
2699 exports.Real = Real
2700 exports.Complex = Complex
2701 exports.Quaternion = Quaternion
2702 exports.Octonion = Octonion
2703
2704 const VectorSpace = require('./src/VectorSpace')
2705 const MatrixSpace = require('./src/MatrixSpace')
2706
2707 exports.C = Complex
2708 exports.C2x2 = MatrixSpace(Complex)(2)
2709 exports.H = Quaternion
2710 exports.R = Real
2711 exports.R2 = VectorSpace(Real)(2)
2712 exports.R3 = VectorSpace(Real)(3)
2713 exports.R2x2 = MatrixSpace(Real)(2)
2714
2715 exports.VectorSpace = VectorSpace
2716 exports.MatrixSpace = MatrixSpace
2717})
2718
2719},{"./src/Boole":20,"./src/CompositionAlgebra":21,"./src/Cyclic":22,"./src/MatrixSpace":23,"./src/Scalar":24,"./src/VectorSpace":25,"./src/realField":30,"strict-mode":16}]},{},[]);