1 | require=(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){
|
2 | var algebraRing = require('algebra-ring')
|
3 | var staticProps = require('static-props')
|
4 |
|
5 | var pkg = require('./package.json')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function msg (str) {
|
12 | return pkg.name + ': ' + str
|
13 | }
|
14 |
|
15 | var error = {}
|
16 |
|
17 | staticProps(error)({
|
18 | groupCardinalityIsNotPrime: msg('elements length must be prime'),
|
19 | elementsAreNotUnique: msg('elements must be unique')
|
20 | })
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function 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 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function 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 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function 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 |
|
135 | staticProps(algebraCyclic)({ error: error })
|
136 |
|
137 | module.exports = algebraCyclic
|
138 |
|
139 | },{"./package.json":2,"algebra-ring":5,"static-props":15}],2:[function(require,module,exports){
|
140 | module.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){
|
214 | const no = require('not-defined')
|
215 | const staticProps = require('static-props')
|
216 |
|
217 | const pkg = require('./package.json')
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | function msg (str) {
|
224 | return pkg.name + ': ' + str
|
225 | }
|
226 |
|
227 | const error = {}
|
228 |
|
229 | staticProps(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 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | function algebraGroup (given, naming) {
|
259 | if (no(given)) given = {}
|
260 | if (no(naming)) naming = {}
|
261 |
|
262 |
|
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 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | function prop (name) {
|
284 | if (typeof naming[name] === 'string') return naming[name]
|
285 | else return defaultNaming[name]
|
286 | }
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
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 |
|
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 |
|
342 | const e = given.identity
|
343 |
|
344 |
|
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 |
|
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 |
|
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 |
|
374 | staticProps(group)(definition)
|
375 |
|
376 | return group
|
377 | }
|
378 |
|
379 | staticProps(algebraGroup)({ error })
|
380 |
|
381 | module.exports = algebraGroup
|
382 |
|
383 | },{"./package.json":4,"not-defined":14,"static-props":15}],4:[function(require,module,exports){
|
384 | module.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){
|
457 | const group = require('algebra-group')
|
458 | const staticProps = require('static-props')
|
459 |
|
460 | const pkg = require('./package.json')
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | function msg (str) {
|
467 | return pkg.name + ': ' + str
|
468 | }
|
469 |
|
470 | const 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 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | function algebraRing (identity, given) {
|
494 |
|
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 |
|
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 |
|
529 |
|
530 | const one = identity[1]
|
531 |
|
532 | if (ring.notContains(one)) {
|
533 | throw new TypeError(error.doesNotContainIdentity)
|
534 | }
|
535 |
|
536 |
|
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 |
|
550 | staticProps(algebraRing)({error: error})
|
551 |
|
552 | module.exports = algebraRing
|
553 |
|
554 | },{"./package.json":6,"algebra-group":3,"static-props":15}],6:[function(require,module,exports){
|
555 | module.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){
|
630 | var ring = require('algebra-ring')
|
631 | var twoPow = Math.pow.bind(null, 2)
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 | function 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 |
|
655 |
|
656 |
|
657 | function 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 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 | function 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 |
|
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 |
|
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 |
|
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 |
|
781 |
|
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 |
|
799 |
|
800 |
|
801 |
|
802 |
|
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 |
|
855 |
|
856 |
|
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 |
|
872 | module.exports = iterateCayleyDickson
|
873 |
|
874 | },{"algebra-ring":5}],8:[function(require,module,exports){
|
875 | function 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 |
|
898 | module.exports = exports.default = indicesPermutations
|
899 |
|
900 | },{}],9:[function(require,module,exports){
|
901 | var no = require('not-defined')
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 | function matrixToArrayIndex (i, j, numCols) {
|
915 | return j + i * numCols
|
916 | }
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 | function 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 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 | function determinant (data, scalar, order) {
|
964 |
|
965 |
|
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 |
|
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 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 | var startingRow = 0
|
991 |
|
992 | for (var col = 0; col < order; col++) {
|
993 | var subData = subMatrix(data, order, order, startingRow, col)
|
994 |
|
995 |
|
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)
|
1007 | } else {
|
1008 | det = scalar.addition(det, scalar.multiplication(data[index], cofactor))
|
1009 | }
|
1010 | }
|
1011 |
|
1012 | return det
|
1013 | }
|
1014 |
|
1015 | module.exports = determinant
|
1016 |
|
1017 | },{"not-defined":14}],10:[function(require,module,exports){
|
1018 | var no = require('not-defined')
|
1019 | var staticProps = require('static-props')
|
1020 |
|
1021 | var pkg = require('./package.json')
|
1022 |
|
1023 |
|
1024 |
|
1025 |
|
1026 |
|
1027 | function msg (str) {
|
1028 | return pkg.name + ': ' + str
|
1029 | }
|
1030 |
|
1031 | var error = {}
|
1032 |
|
1033 | staticProps(error)({
|
1034 | leftMatrixNotCompatible: msg('Cannot multiply matrix at left side'),
|
1035 | rightMatrixNotCompatible: msg('Cannot multiply matrix at right side')
|
1036 | })
|
1037 |
|
1038 | var matrixToArrayIndex = (i, j, numCols) => (j + i * numCols)
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 | function matrixMultiplication (customOperator) {
|
1051 |
|
1052 |
|
1053 | if (no(customOperator)) customOperator = {}
|
1054 |
|
1055 | var add = customOperator.addition
|
1056 | var mul = customOperator.multiplication
|
1057 |
|
1058 |
|
1059 | if (no(add)) add = (a, b) => (a + b)
|
1060 | if (no(mul)) mul = (a, b) => (a * b)
|
1061 |
|
1062 | |
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 |
|
1068 | return function (middle) {
|
1069 | |
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 | return function (leftMatrix, rightMatrix) {
|
1077 |
|
1078 |
|
1079 | var cols = rightMatrix.length / middle
|
1080 | var rows = leftMatrix.length / middle
|
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 |
|
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 |
|
1121 | staticProps(matrixMultiplication)({ error })
|
1122 |
|
1123 | module.exports = matrixMultiplication
|
1124 |
|
1125 | },{"./package.json":11,"not-defined":14,"static-props":15}],11:[function(require,module,exports){
|
1126 | module.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){
|
1198 | var staticProps = require('static-props')
|
1199 |
|
1200 | var pkg = require('./package.json')
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 |
|
1206 | function msg (str) {
|
1207 | return pkg.name + ': ' + str
|
1208 | }
|
1209 |
|
1210 | var error = {}
|
1211 |
|
1212 | staticProps(error)({
|
1213 | outOfBoundIndex: msg('Index exceeds its bound')
|
1214 | })
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 |
|
1233 | function multiDimArrayIndex (dimensions, indices) {
|
1234 |
|
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 |
|
1244 | if (order === 1) return indices[0]
|
1245 |
|
1246 |
|
1247 | var n = order - 1
|
1248 | var factor = dimensions[n]
|
1249 | var index = indices[n] + factor * indices[n - 1]
|
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 |
|
1260 | staticProps(multiDimArrayIndex)({ error: error })
|
1261 |
|
1262 | module.exports = multiDimArrayIndex
|
1263 |
|
1264 | },{"./package.json":13,"static-props":15}],13:[function(require,module,exports){
|
1265 | module.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){
|
1339 | module.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 |
|
1344 |
|
1345 |
|
1346 | function staticProps (obj) {
|
1347 | |
1348 |
|
1349 |
|
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 | }
|
1376 | module.exports = exports.default = staticProps
|
1377 |
|
1378 | },{}],16:[function(require,module,exports){
|
1379 |
|
1380 | module.exports = function (cb) { cb() }
|
1381 |
|
1382 | },{}],17:[function(require,module,exports){
|
1383 | arguments[4][12][0].apply(exports,arguments)
|
1384 | },{"./package.json":18,"dup":12,"static-props":15}],18:[function(require,module,exports){
|
1385 | module.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){
|
1459 | var indicesPermutations = require('indices-permutations')
|
1460 | var multiDimArrayIndex = require('multidim-array-index')
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 | function tensorContraction (addition, indicesPair, tensorDim, tensorData) {
|
1473 |
|
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 |
|
1523 |
|
1524 |
|
1525 |
|
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 |
|
1544 | module.exports = tensorContraction
|
1545 |
|
1546 | },{"indices-permutations":8,"multidim-array-index":17}],20:[function(require,module,exports){
|
1547 | const 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 |
|
1558 | module.exports = Boole
|
1559 |
|
1560 | },{}],21:[function(require,module,exports){
|
1561 | const CayleyDickson = require('cayley-dickson')
|
1562 | const no = require('not-defined')
|
1563 |
|
1564 | const createScalar = require('./createScalar')
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 |
|
1576 |
|
1577 |
|
1578 | function 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 |
|
1590 | module.exports = CompositionAlgebra
|
1591 |
|
1592 | },{"./createScalar":27,"cayley-dickson":7,"not-defined":14}],22:[function(require,module,exports){
|
1593 | const algebraCyclic = require('algebra-cyclic')
|
1594 | const createScalar = require('./createScalar')
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 | function Cyclic (elements) {
|
1603 | const ring = algebraCyclic(elements)
|
1604 |
|
1605 | return createScalar(ring)
|
1606 | }
|
1607 |
|
1608 | module.exports = Cyclic
|
1609 |
|
1610 | },{"./createScalar":27,"algebra-cyclic":1}],23:[function(require,module,exports){
|
1611 | const determinant = require('laplace-determinant')
|
1612 | const multiplication = require('matrix-multiplication')
|
1613 | const multiDimArrayIndex = require('multidim-array-index')
|
1614 | const staticProps = require('static-props')
|
1615 | const tensorContraction = require('tensor-contraction')
|
1616 |
|
1617 | const itemsPool = require('./itemsPool')
|
1618 | const toData = require('./toData')
|
1619 |
|
1620 |
|
1621 |
|
1622 |
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 |
|
1629 |
|
1630 |
|
1631 |
|
1632 |
|
1633 |
|
1634 | function 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 |
|
1647 |
|
1648 |
|
1649 |
|
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 |
|
1659 |
|
1660 |
|
1661 | function computeDeterminant (matrix) {
|
1662 | const data = toData(matrix)
|
1663 |
|
1664 | return determinant(data, Scalar, numRows)
|
1665 | }
|
1666 |
|
1667 | |
1668 |
|
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 |
|
1686 |
|
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 |
|
1717 |
|
1718 |
|
1719 |
|
1720 |
|
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 |
|
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 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 | function computeTrace (matrix) {
|
1760 | const matrixData = toData(matrix)
|
1761 |
|
1762 | return contraction([0, 1], indices, matrixData)
|
1763 | }
|
1764 |
|
1765 | |
1766 |
|
1767 |
|
1768 |
|
1769 |
|
1770 |
|
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 |
|
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 |
|
1831 |
|
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 |
|
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 |
|
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 |
|
1903 | itemsPool.set('MatrixSpace', MatrixSpace)
|
1904 |
|
1905 | module.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){
|
1908 | const algebraRing = require('algebra-ring')
|
1909 | const createScalar = require('./createScalar')
|
1910 |
|
1911 |
|
1912 |
|
1913 |
|
1914 |
|
1915 | function Scalar (neutralElements, operators) {
|
1916 | const ring = algebraRing(neutralElements, operators)
|
1917 |
|
1918 | return createScalar(ring)
|
1919 | }
|
1920 |
|
1921 | module.exports = Scalar
|
1922 |
|
1923 | },{"./createScalar":27,"algebra-ring":5}],25:[function(require,module,exports){
|
1924 | const itemsPool = require('./itemsPool')
|
1925 | const matrixMultiplication = require('matrix-multiplication')
|
1926 | const staticProps = require('static-props')
|
1927 | const toData = require('./toData')
|
1928 |
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 |
|
1936 |
|
1937 |
|
1938 |
|
1939 |
|
1940 |
|
1941 |
|
1942 |
|
1943 | function VectorSpace (Scalar) {
|
1944 | const {
|
1945 | addition,
|
1946 | equality,
|
1947 | multiplication,
|
1948 | subtraction
|
1949 | } = Scalar
|
1950 |
|
1951 | const enumerable = true
|
1952 |
|
1953 | |
1954 |
|
1955 |
|
1956 |
|
1957 |
|
1958 |
|
1959 | return function (dimension) {
|
1960 | |
1961 |
|
1962 |
|
1963 |
|
1964 |
|
1965 |
|
1966 |
|
1967 |
|
1968 |
|
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 |
|
1994 |
|
1995 |
|
1996 |
|
1997 |
|
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 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
2017 |
|
2018 |
|
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 |
|
2035 |
|
2036 |
|
2037 |
|
2038 |
|
2039 |
|
2040 |
|
2041 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
2154 |
|
2155 |
|
2156 |
|
2157 |
|
2158 |
|
2159 |
|
2160 |
|
2161 |
|
2162 |
|
2163 |
|
2164 |
|
2165 |
|
2166 |
|
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 |
|
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 |
|
2239 | itemsPool.set('VectorSpace', VectorSpace)
|
2240 |
|
2241 | module.exports = VectorSpace
|
2242 |
|
2243 | },{"./itemsPool":28,"./toData":31,"matrix-multiplication":10,"static-props":15}],26:[function(require,module,exports){
|
2244 | const toData = require('./toData')
|
2245 |
|
2246 |
|
2247 |
|
2248 |
|
2249 |
|
2250 |
|
2251 |
|
2252 |
|
2253 |
|
2254 |
|
2255 |
|
2256 | function coerced (operator) {
|
2257 | return function () {
|
2258 | return operator.apply(null, [].slice.call(arguments).map(toData))
|
2259 | }
|
2260 | }
|
2261 |
|
2262 | module.exports = coerced
|
2263 |
|
2264 | },{"./toData":31}],27:[function(require,module,exports){
|
2265 | const coerced = require('./coerced')
|
2266 | const operators = require('./operators.json')
|
2267 | const staticProps = require('static-props')
|
2268 | const toData = require('./toData')
|
2269 |
|
2270 |
|
2271 |
|
2272 |
|
2273 |
|
2274 |
|
2275 |
|
2276 | function createScalar (ring) {
|
2277 | const attributes = {
|
2278 | zero: ring.zero,
|
2279 | one: ring.one
|
2280 | }
|
2281 |
|
2282 | |
2283 |
|
2284 |
|
2285 |
|
2286 | class Scalar {
|
2287 | constructor (data) {
|
2288 |
|
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 |
|
2371 | module.exports = createScalar
|
2372 |
|
2373 | },{"./coerced":26,"./operators.json":29,"./toData":31,"static-props":15}],28:[function(require,module,exports){
|
2374 | const itemsPool = new Map()
|
2375 |
|
2376 | module.exports = itemsPool
|
2377 |
|
2378 | },{}],29:[function(require,module,exports){
|
2379 | module.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){
|
2624 | const realField = {
|
2625 | zero: 0,
|
2626 | one: 1,
|
2627 |
|
2628 | contains: (a) => (typeof a === 'number' && isFinite(a)),
|
2629 | equality: (a, b) => {
|
2630 |
|
2631 |
|
2632 |
|
2633 |
|
2634 |
|
2635 |
|
2636 |
|
2637 |
|
2638 |
|
2639 |
|
2640 |
|
2641 |
|
2642 |
|
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 |
|
2652 | module.exports = realField
|
2653 |
|
2654 | },{}],31:[function(require,module,exports){
|
2655 | const no = require('not-defined')
|
2656 |
|
2657 |
|
2658 |
|
2659 |
|
2660 |
|
2661 |
|
2662 |
|
2663 |
|
2664 |
|
2665 | function 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 |
|
2676 | module.exports = toData
|
2677 |
|
2678 | },{"not-defined":14}],"algebra":[function(require,module,exports){
|
2679 | require('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}]},{},[]);
|