From 8ac94e9fa4dc1db7cdb31d053e6fcda079d6da2c Mon Sep 17 00:00:00 2001 From: Nikos M Date: Fri, 17 May 2019 19:13:46 +0300 Subject: [PATCH] v.0.9.0 * LatinSquares, MagicSquares algorithms and classes --- README.md | 8 +- src/js/Abacus.js | 808 +++++++++++++------- src/js/Abacus.min.js | 6 +- test/combination_subsets.txt | 52 +- test/combinations.txt | 30 +- test/combinations_repeats.txt | 96 +-- test/compositions.txt | 8 +- test/conditional_permutations_algebraic.txt | 6 +- test/conditional_permutations_boolean.txt | 4 +- test/connected_permutations.txt | 10 +- test/cyclic_permutations.txt | 8 +- test/derangements.txt | 86 +-- test/involutions.txt | 12 +- test/k-derangements.txt | 4 +- test/latin_squares.js | 57 ++ test/latin_squares.txt | 41 + test/magic_squares.js | 77 ++ test/magic_squares.txt | 92 +++ test/multiset_permutations.txt | 314 ++++---- test/ordered_combinations.txt | 200 ++--- test/ordered_combinations_repeats.txt | 218 +++--- test/partially_fixed_permutations.txt | 4 +- test/partitions.txt | 8 +- test/permutations-bigint.txt | 4 +- test/permutations.txt | 36 +- test/permutations_of_combinations.txt | 4 +- test/permutations_of_permutations.txt | 4 +- test/restricted_compositions.txt | 14 +- test/restricted_partitions.txt | 14 +- test/subsets.txt | 218 +++--- test/tensors.txt | 8 +- test/test.bat | 2 + test/tuples.txt | 40 +- 33 files changed, 1501 insertions(+), 992 deletions(-) create mode 100644 test/latin_squares.js create mode 100644 test/latin_squares.txt create mode 100644 test/magic_squares.js create mode 100644 test/magic_squares.txt diff --git a/README.md b/README.md index ec77d46..dbef157 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # Abacus -A combinatorics library for Node/XPCOM/JS, PHP, Python, C/C++, Java +A combinatorics library for Node.js / Browser / XPCOM Javascript, PHP, Python, C/C++, Java (php/python/java/c implementations in progress) -**version 0.8.8** (~ 66kB minified, ~ 22kB zipped) +**version 0.9.0** (~ 66kB minified, ~ 22kB zipped) ![abacus combinatorial numbers](/abacus.jpg) @@ -72,6 +72,8 @@ A combinatorics library for Node/XPCOM/JS, PHP, Python, C/C++, Java * `Composition` (`test/compositions.js`) **rank/unrank methods missing, partial support for COLEX** * `RestrictedPartition` (`test/restricted_partitions.js`) **exactly M max. part** * `RestrictedComposition` (`test/restricted_compositions.js`) **exactly K #parts** +* `LatinSquare` (`test/latin_squares.js`) +* `MagicSquare` (`test/magic_squares.js`) * **multiple (combined) iterator orderings & traversals**: `lex`, `colex`, `random`, `reversed`, `reflected`, `minimal` (not implemented yet). For example: `"revlex"` (equivalent to `"lex,reversed"`), `"refcolex"` (equivalent to `"colex,reflected"`), and so on.. * **arbitrary range** of combinatorial objects in a number of supported orderings (ie `lex`, `colex`, `random`,..). **Note** `rank`/`unrank` have to be implemented for this feature to work * **algebraic composition** of combinatorial objects (of **fixed** dimensions at present) to construct new combinatorial objects (eg `all combinations` = `all permutations` **OF** `all unique combinations`, see `test/permutations_of_combinations.js` and `test/permutations_of_permutations.js`) @@ -569,10 +571,10 @@ o.dispose() * make sure the `.random` methods **uniformly and unbiasedly sample the combinatorial object space** (methods use unbiased sampling algorithms, however results in certain cases might depend on [quality of PRNGs](http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf)) [DONE] * support algebraic composition/cascading of combinatorial objects (of **fixed** dimensions at present) to construct new combinatorial objects (eg `all combinations` = `all permutations` **OF** `all unique combinations`) [DONE] * support generation of supported combinatorial objects with additional **user-defined patterns/templates of constraints** to satisfy e.g *"only combinatorial objects matching `'(n)(m)(1){2}(){3}(0)((n+1))((n+m)){4}'`"* pattern.. [DONE] +* add `LatinSquare`, `MagicSquare` algorithms [DONE] * add efficient `rank`/`unrank` methods for `DerangementPermutation`, `InvolutionPermutation`, `ConnectedPermutation`, `Composition` & `Partition` (TODO) * full support for `colex` ordering `Composition` & `Partition` [DONE PARTIALY] * support `minimal`/`gray` ordering (and successor) for all supported combinatorial objects (TODO) * use numeric arrays (ie `Uint32`) to store combinatorial items and/or make faster `successor` methods and other numerical routines to `asm.js` (TODO?) -* add `LatinSquare`, `MagicSquare` algorithms (TODO) * support generation (and counting) of combinatorial objects (including the basic supported ones) based on **generic user-defined symbolic constraints / symmetries / rules** to satisfy, for example `permutations` defined symbolicaly and directly by their *symmetries / constraints* instead of being hardcoded as elementary objects (TODO?) * support *graph-based* combinatorial objects like `Graph`, `Grammar`,.. (TODO?) (for regular grammars and expressions see [RegexAnalyzer](https://github.com/foo123/RegexAnalyzer) for an example) diff --git a/src/js/Abacus.js b/src/js/Abacus.js index 90deadb..1183d91 100644 --- a/src/js/Abacus.js +++ b/src/js/Abacus.js @@ -1,8 +1,8 @@ /** * * Abacus -* A combinatorics library for Node/XPCOM/JS, PHP, Python, Java, C/C++ -* @version: 0.8.8 +* A combinatorics library for Node.js / Browser / XPCOM Javascript, PHP, Python, Java, C/C++ +* @version: 0.9.0 * https://github.com/foo123/Abacus **/ !function( root, name, factory ){ @@ -17,19 +17,19 @@ else if ( ('function'===typeof define)&&define.amd&&('function'===typeof require define(name,['module'],function(module){factory.moduleUri = module.uri; return factory.call(root);}); else if ( !(name in root) ) /* Browser/WebWorker/.. */ (root[name] = factory.call(root)||1)&&('function'===typeof(define))&&define.amd&&define(function(){return root[name];} ); -}( /* current root */ this, +}( /* current root */ this, /* module name */ "Abacus", /* module factory */ function ModuleFactory__Abacus( undef ){ "use strict"; -var Abacus = {VERSION: "0.8.8"}, stdMath = Math, PROTO = 'prototype', CLASS = 'constructor' +var Abacus = {VERSION: "0.9.0"}, stdMath = Math, PROTO = 'prototype', CLASS = 'constructor' ,slice = Array.prototype.slice, HAS = Object[PROTO].hasOwnProperty, toString = Object[PROTO].toString ,log2 = stdMath.log2 || function(x) { return stdMath.log(x) / stdMath.LN2; } ,trim_re = /^\s+|\s+$/g ,trim = String.prototype.trim ? function( s ){ return s.trim(); } : function( s ){ return s.replace(trim_re, ''); } ,pos_re = /\[(\d+)\]/g, pos_test_re = /\[(\d+)\]/ ,in_set_re = /^\{(\d+(?:(?:\.\.\d+)?|(?:,\d+)*))\}$/, not_in_set_re = /^!\{(\d+(?:(?:\.\.\d+)?|(?:,\d+)*))\}$/ - + ,Extend = Object.create ,Merge = function Merge(a, b) { for (var p in b) if (HAS.call(b,p)) a[p] = b[p]; @@ -43,13 +43,14 @@ var Abacus = {VERSION: "0.8.8"}, stdMath = Math, PROTO = 'prototype', CLASS = ' return ctor; } ,V_EQU=1, V_DIFF=-1, V_INC=3, V_DEC=-3, V_NONINC=-2, V_NONDEC=2 - + ,REVERSED = 1, REFLECTED = 2 ,LEX = 4, COLEX = 8, MINIMAL = 16, RANDOM = 32 ,LEXICAL = LEX | COLEX | MINIMAL ,ORDERINGS = LEXICAL | RANDOM | REVERSED | REFLECTED - + ,CombinatorialIterator, Tensor, Permutation, Combination, Subset, Partition + ,LatinSquare, MagicSquare ; // utility methods @@ -79,7 +80,7 @@ function operate( F, F0, x, i0, i1, ik, strict ) } if ( null == ik ) ik = i0 > i1 ? -1 : 1; if ( (0 === ik) || (x_array && !x.length) || (0 >= stdMath.floor((i1-i0)/ik)+1) ) return Fv; - + if ( 0 > ik ) { // remove not reachable range (not multiple of step ik) @@ -96,7 +97,7 @@ function operate( F, F0, x, i0, i1, ik, strict ) } // unroll the rest range mod 16 + remainder i0r = i0+ik*(stdMath.floor((i1-i0)/ik+1)&15); - + if ( x_array ) { i00 = stdMath.max(0,i00); i11 = stdMath.min(x.length-1,i11); @@ -199,7 +200,7 @@ function reversion( n, n0 ) }) : ((n===+n)&&(n0===+n0) ? (n0-n) : Abacus.Arithmetic.sub(Abacus.Arithmetic.N(n0),n)); } function gray( b, a, n, a0, a1 ) -{ +{ // adapted from https://en.wikipedia.org/wiki/Gray_code#n-ary_Gray_code if ( null == a ) return b; var s = 0; @@ -253,23 +254,23 @@ function intersection( comm, a, b, dir, a0, a1, b0, b1 ) if ( null == a1 ) a1 = a.length-1; if ( null == b0 ) b0 = 0; if ( null == b1 ) b1 = b.length-1; - + var ak = a0 > a1 ? -1 : 1, bk = b0 > b1 ? -1 : 1, al = ak*(a1-a0)+1, bl = bk*(b1-b0)+1, ai = a0, bi = b0, il = 0; if ( null == comm ) comm = new Array(stdMath.min(al,bl)); if ( 0 === comm.length ) return comm; - + // O(min(al,bl)) // assume lists are already sorted ascending/descending (indepentantly) while( (0 <= ak*(a1-ai)) && (0 <= bk*(b1-bi)) ) { if ( (1===dir && a[ai]b[bi]) ) - { - ai+=ak; + { + ai+=ak; } else if ( (1===dir && a[ai]>b[bi]) || (-1===dir && a[ai] a1 ? -1 : 1, bk = b0 > b1 ? -1 : 1, al = ak*(a1-a0)+1, bl = bk*(b1-b0)+1, ai = a0, bi = b0, dl = 0; if ( !b || !b.length ) return a === +a ? array(a, a0, ak) : (a ? a.slice() : a); if ( null == diff ) diff = new Array(duplicates?2*al:al); - + // O(al) // assume lists are already sorted ascending/descending (independantly) if ( a === +a ) @@ -308,10 +309,10 @@ function difference/*complement*/( diff, a, b, dir, a0, a1, b0, b1, duplicates ) } else if ( (1===dir && ai>b[bi]) || (-1===dir && aib[bi]) ) - { + { diff[dl++] = ai; ai+=ak; } } @@ -328,10 +329,10 @@ function difference/*complement*/( diff, a, b, dir, a0, a1, b0, b1, duplicates ) } else if ( (1===dir && a[ai]>b[bi]) || (-1===dir && a[ai]b[bi]) ) - { + { diff[dl++] = a[ ai ]; ai+=ak; } } @@ -347,12 +348,12 @@ function multi_difference( diff, mult, a, b, a0, a1, b0, b1 ) if ( null == a1 ) a1 = a.length-1; if ( null == b0 ) b0 = 0; if ( null == b1 ) b1 = b ? b.length-1 : -1; - + var ak = a0 > a1 ? -1 : 1, bk = b0 > b1 ? -1 : 1, al = ak*(a1-a0)+1, bl = bk*(b1-b0)+1, ai = a0, bi = b0, dl = 0; if ( !b || !b.length ) return a ? a.slice() : a; if ( null == diff ) diff = new Array(al); - + // O(al) // assume lists are already sorted ascending/descending (independantly) while( (0 <= ak*(a1-ai)) && (0 <= bk*(b1-bi)) ) @@ -371,10 +372,10 @@ function multi_difference( diff, mult, a, b, a0, a1, b0, b1 ) } else if ( a[ai]>b[bi] ) { - bi+=bk; + bi+=bk; } else//if ( a[ai] a1 ? -1 : 1, bk = b0 > b1 ? -1 : 1, al = ak*(a1-a0)+1, bl = bk*(b1-b0)+1, ul = al+bl, ai = a0, bi = b0, ui = 0, last = null, with_duplicates = !unique; if ( null == union ) union = new Array(ul); if ( 0 === union.length ) return inplace ? a : union; - + // O(al+bl) // assume lists are already sorted ascending/descending (independantly), even with duplicate values while( (0 <= ak*(a1-ai)) && (0 <= bk*(b1-bi)) ) @@ -433,12 +434,12 @@ function merge/*union*/( union, a, b, dir, a0, a1, b0, b1, indices, unique, inpl if ( indices ) { if ( (1===dir && a[ai][0]b[bi][0]) ) - { + { union[ui++] = last=a[ai]; ai+=ak; } else if ( (1===dir && a[ai][0]>b[bi][0]) || (-1===dir && a[ai][0]b[bi]) ) - { + { union[ui++] = last=a[ai]; ai+=ak; } else if ( (1===dir && a[ai]>b[bi]) || (-1===dir && a[ai]=0; i--) // O(k) times - { + { selected = backup[ i ]; value = a[ a0+n ]; a[ a0+n ] = a[ a0+selected ]; @@ -805,7 +806,7 @@ function factorial( n, m ) O = Arithmetic.O, I = Arithmetic.I, J = Arithmetic.J, NUM = Arithmetic.N, add = Arithmetic.add, sub = Arithmetic.sub, div = Arithmetic.div, mul = Arithmetic.mul, key; - + if ( null == m ) { // http://www.luschny.de/math/factorial/index.html @@ -895,7 +896,7 @@ function stirling( n, k, s ) var Arithmetic = Abacus.Arithmetic, O = Arithmetic.O, I = Arithmetic.I, V = Arithmetic.V, add = Arithmetic.add, mul = Arithmetic.mul, key; - + if ( 0 > n || 0 > k ) return O; if ( 2 === s ) { @@ -1210,10 +1211,10 @@ Abacus.Math = { // pluggable arithmetics, eg biginteger Arithmetic Abacus.Arithmetic = { - + // whether using default arithmetic or using external implementation (eg big-int or other) isDefault: function( ){return (0 === Abacus.Arithmetic.O) && (Abacus.Arithmetic.add === addn);} - + ,O: 0 ,I: 1 ,J: -1 @@ -1259,7 +1260,7 @@ Abacus.Arithmetic = { // array/list utilities Abacus.Util = { - + array: array ,operate: operate ,intersection: intersection @@ -1277,72 +1278,72 @@ Abacus.Util = { ,shuffle: shuffle ,pick: pick ,pluck: pluck - + }; Abacus.BitArray = Class({ - + constructor: function BitArray(n) { var self = this; if ( !(self instanceof BitArray) ) return new BitArray(n); self.length = n; self.bits = new Uint32Array(stdMath.ceil(n/32)); } - + ,length: 0 ,bits: null - + ,dispose: function( ) { var self = this; self.length = null; self.bits = null; return self; } - + ,clone: function( ) { var self = this, c = new Abacus.BitArray(self.length); c.bits = new Uint32Array( self.bits ); return c; } - + ,fromArray: function( b ) { var self = this; self.bits = new Uint32Array( b ); return self; } - + ,toArray: function( ) { return slice.call( this.bits ); } - + ,toString: function( ) { var a = this.toArray( ), i, l; for(i=0,l=a.length; i>>5] & (1<<(bit&31))); } - + ,set: function( bit ) { var self = this; self.bits[bit>>>5] |= 1<<(bit&31); return self; } - + ,unset: function( bit ) { var self = this; self.bits[bit>>>5] &= ~(1<<(bit&31)); return self; } - + ,toggle: function( bit ) { var self = this; self.bits[bit>>>5] ^= 1<<(bit&31); @@ -1355,9 +1356,9 @@ function kronecker( /* var args here */ ) { var nv = arguments.length, k, a, r, l, i, j, vv, tensor, tl, kl, product; - + if ( !nv ) return []; - + if ( true === arguments[0] ) { // flat tensor product @@ -1426,9 +1427,9 @@ function conditional_combinatorial_tensor( v, value_conditions, extra_conditions var k, kl, a, r, l, i, vv, nv = v.length, v0, v1, tensor, t0, t1, nvalid, product, p, pv, pe, npv, seen = null, valid = null, invalid, queue, ql, expr, e, el; - + if ( !nv ) return []; - + if ( is_callable(extra_conditions) ) { valid = extra_conditions; @@ -1438,12 +1439,12 @@ function conditional_combinatorial_tensor( v, value_conditions, extra_conditions { extra_conditions = false; } - + if ( !(V_EQU===value_conditions || V_DIFF===value_conditions || V_INC===value_conditions || V_DEC===value_conditions || V_NONINC===value_conditions || V_NONDEC===value_conditions) ) { value_conditions = false; } - + pe = new Array(nv); pv = []; for(kl=1,k=0; k' === value_conditions) ) value_conditions = V_DIFF; @@ -1687,14 +1688,14 @@ function gen_combinatorial_data( n, data, pos, value_conditions, options ) else if ( '>' === value_conditions ) value_conditions = V_DEC; else if ( ('>=' === value_conditions) || ('=>' === value_conditions) ) value_conditions = V_NONINC; else value_conditions = false; - + // check additional conditions additional_conditions = is_callable(options.extra_conditions) ? function(v,i0,i1){ return (min<=v[i0] && v[i0]<=max) && options.extra_conditions(v,i0,i1); } : function(v,i0,i1){ return (min<=v[i0] && v[i0]<=max); }; - + // compute valid combinatorial data satisfying conditions return true === options.lazy ? data : conditional_combinatorial_tensor(data, value_conditions, additional_conditions); } @@ -1703,7 +1704,7 @@ function gen_combinatorial_data( n, data, pos, value_conditions, options ) tpl = String(tpl||''); var l = tpl.length, i, j, k, p, c, s, n, m, paren, N = 0, data = [], position = []; - + i = 0; p = 0; while( i < l ) { @@ -1727,7 +1728,7 @@ function gen_combinatorial_data( n, data, pos, value_conditions, options ) } s = trim(s); m = s.length ? (s.match(not_in_set_re) || s.match(in_set_re) || pos_test_re.test(s)) : null; - + if ( !m ) { // any term @@ -1861,7 +1862,7 @@ function packpartition( partition, dir ) reflected = -1 === dir, a = 1, b = 0, push = "push", last, part; - + if ( reflected ) { a = -a; @@ -1983,7 +1984,7 @@ function permutation2inversion( inversion, permutation, N ) // "Efficient Algorithms to Rank and Unrank Permutations in Lexicographic Order", Blai Bonet (http://ldc.usb.ve/~bonet/reports/AAAI08-ws10-ranking.pdf) var n = permutation.length, k = stdMath.ceil(log2(N||n)), twok = 1 << k, Tl = (1<<(1+k))-1, T = array(Tl, 0, 0); - + return operate(function(inv,ctr,i){ // Starting bottom-up at the leaf associated with pi for(var node=ctr+twok,j=0; j min_cycle ) @@ -2098,7 +2099,7 @@ function swaps2permutation( swaps, n ) { // swap swap = s[i]; - t = permutation[s[0]]; + t = permutation[s[0]]; permutation[s[0]] = permutation[s[1]]; permutation[s[1]] = t; } @@ -2240,12 +2241,107 @@ function is_kcycle( perm, kcycles, compare, fixed ) } return "<="===compare ? ncycles<=kcycles : (">="===compare ? ncycles>=kcycles : ncycles===kcycles); } +function is_magic( square ) +{ + if ( !square ) return false; + var n = square.length, n2 = n*n, i, j, k, + summa_row = 0, summa_col = 0, summa_d1 = 0, summa_d2 = 0, + summa = (n*n2+n)>>>1, seen = new Array(n2); + for (i=0; i n2 || i !== seen[k-1][0] || 0 !== seen[k-1][1] ) return false; + summa_row = k; + k = square[0][i]; + if ( !seen[k-1] ) seen[k-1] = [0, i]; + if ( k < 1 || k > n2 || 0 !== seen[k-1][0] || i !== seen[k-1][1] ) return false; + summa_col = k; + summa_d1 += square[i][i]; + summa_d2 += square[i][n-1-i]; + for (j=1; j n2 || i !== seen[k-1][0] || j !== seen[k-1][1] ) return false; + summa_row += k; + k = square[j][i]; + if ( !seen[k-1] ) seen[k-1] = [j, i]; + if ( k < 1 || k > n2 || j !== seen[k-1][0] || i !== seen[k-1][1] ) return false; + summa_col += k; + } + if ( (summa_row !== summa) || (summa_col !== summa) ) return false; + } + if ( (summa_d1 !== summa) || (summa_d2 !== summa) ) return false; + return true; +} +function is_latin( square ) +{ + if ( !square ) return false; + var n = square.length, i, j, k, m, seen = new Array(n); + for (i=0; i k || 0 < seen[k] ) return false; + seen[k] = 1; + } + // columns + for(k=0; k k || 0 < seen[k] ) return false; + seen[k] = 1; + } + } + return true; +} +/*function swap( M, i, j, axis ) +{ + var n = M.length, m = M[0].length, t, k, l; + axis = (axis || "rows").toLowerCase(); + if ( "rows" === axis ) + { + for(k=0; kdir ? $.seq.length-1 : 0) : -1; - + if ( RANDOM & order ) { // a uniform random traversal over all traversals of the combinatorial space @@ -2775,37 +2871,37 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ } self._index = self.__index; } - + self._item = klass.output(self.__item, self.__index, n, $); self._prev = (RANDOM & order) || (0 < dir) ? false : null != self.__item; self._next = (0 > dir) && !(RANDOM & order) ? false : null != self.__item; - + return self; } - + ,_update: function( ) { // compute and store any extra item information // needed between successive runs to run faster, eg cat or loopless, instead of linear this.item__ = null; return this; } - + ,order: function( order, reverse ) { if ( !arguments.length ) return this._order; - + var self = this, klass = self[CLASS], Arithmetic = Abacus.Arithmetic, O = Arithmetic.O, I = Arithmetic.I, suborder, r, n, $, dir, rewind = true === order, i, l; - + reverse = -1 === reverse; n = self.n; $ = self.$; - + if ( self._traversed ) { self._traversed.dispose( ); self._traversed = null; } - + if ( rewind ) { order = $.order; @@ -2829,7 +2925,7 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ //dir = REVERSED & order ? -1 : 1; // T dir = reverse ? -1 : 1; // T $.order = order; - + if ( $.sub ) { if ( rewind ) $.sub.rewind(dir); @@ -2846,14 +2942,14 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ self._subindex = null; self._subitem = null; } - + if ( "sequence" === $.type && $.seq && $.seq.length ) { for(i=0,l=$.seq.length; i dir) && (RANDOM & order) ) return null; - + dI = 0 > dir ? J : I; if ( RANDOM & order ) { @@ -3137,7 +3233,7 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ if ( null != self.__item ) self.__index = Arithmetic.add(self.__index, dI); } has_next = null != self.__item; - + if ( !has_next ) { if ( $.sub && $.sub.hasNext(dir) ) @@ -3180,9 +3276,9 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ self._next = has_next; } } - + self._item = klass.output(self.__item, self.__index, n, $); - + if ( $.sub ) { has_next = has_next && (null != self.__subitem); @@ -3191,10 +3287,10 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ if ( 0 > dir ) self._prev = has_next; else self._next = has_next; } - + return current; } - + ,range: function( start, end ) { var self = this, Arithmetic = Abacus.Arithmetic, N = Arithmetic.N, O = Arithmetic.O, I = Arithmetic.I, @@ -3244,13 +3340,13 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ ,self._prev ,self._next ]; - - if ( not_randomised ) self.index( start ); + + if ( not_randomised ) self.index( start ); count = Arithmetic.val(Arithmetic.sub(end, start)); range = operate(function(range,ri,i){ range[i] = self.next( ); return range; }, new Array(count+1), null, 0>dir?count:0, 0>dir?0:count, 0>dir?-1:1); - + // restore previous iterator state self.$.order = iter_state[0]; self.__index = iter_state[1]; @@ -3271,7 +3367,7 @@ CombinatorialIterator = Abacus.CombinatorialIterator = Class({ } return range; } - + // javascript @@iterator/@@iterable interface, if supported // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols ,__iter__: function( ) { @@ -3289,7 +3385,7 @@ if ( ('undefined' !== typeof Symbol) && ('undefined' !== typeof Symbol.iterator) // https://en.wikipedia.org/wiki/Tensor_product // also a combinatorial iterator for partial (explicit and/or as conditional expressions) combinatorial data Tensor = Abacus.Tensor = Class(CombinatorialIterator, { - + // extends and implements CombinatorialIterator constructor: function Tensor( /*dims here ..*/ ) { var self = this, sub = null, n = slice.call(arguments), $; @@ -3297,16 +3393,16 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { if ( n.length && is_array(n[0]) ) n = n[0]; if ( !n || !n.length ) n = []; if ( !(self instanceof Tensor) ) return new Tensor(n, $); - + $.type = String($.type || "tensor").toLowerCase(); $.order = $.order || LEX; $.rand = $.rand || {}; - + if ( "partial" === $.type ) { n = is_array(n)&&n.length ? n[0] : n; var nsub = -1, data = $.data||[], pos = $.position||null; - + if ( n instanceof CombinatorialIterator ) { sub = n; @@ -3319,7 +3415,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { sub = $.sub; } n = (+(n||0))||0; - + if ( data.length && (is_string(data[0]) || (data[0].length && (true === data[0][0] || false === data[0][0]))) ) { // conditions: ALGEBRAIC(STRING EXPR) AND/OR BOOLEAN(POSITIVE / NEGATIVE) => [values] per position @@ -3327,7 +3423,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { data = Tensor.generate( n, data, pos, $.ordering||null ); } if ( nsub === n ) { n += (data.length?data[0].length:0)||0; nsub = -1; } - + $.data = data; $.position = pos || array((data.length?data[0].length:0)||0, 0, 1); $.dimension = $.position.length; $.base = n; $.rand["partial"] = 1; @@ -3378,7 +3474,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { } CombinatorialIterator.call(self, "Tensor", n, $, sub?{method:"partial"===$.type?($.submethod||"complete"):$.submethod,iter:sub,pos:"partial"===$.type?($.subpos||$.position):$.subpos,cascade:$.subcascade}:null); } - + ,__static__: { C: CombinatorialIterator.C ,P: CombinatorialIterator.P @@ -3393,12 +3489,12 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { // last (0>dir) is C-symmetric of first (0 dir ? array(n.length, function(i){return n[i]-1;}): array(n.length, 0, 0)) ); - + item = klass.DUAL(item, n, $); } - + return item; } ,succ: function( item, index, n, $, dir, TI ) { @@ -3427,7 +3523,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { { if ( !$.data || !$.data.length ) return null; if ( REVERSED & order ) dir = -dir; - var i = null == index ? $.data.indexOf(item) : Abacus.Arithmetic.val(index); + var i = null == index ? $.data.indexOf(item) : Abacus.Arithmetic.val(index); return 0>dir ? (0<=i-1 ? $.data[i-1] : null) : (0<=i && i+1<$.data.length ? $.data[i+1] : null); } return !n[0] || (0 >= n[0]) ? null : next_tensor(item, n, dir, type, order, TI); @@ -3436,7 +3532,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { var rndInt = Abacus.Math.rndInt, klass = this, item, type = $ && $.type ? $.type : "tensor"; - + if ( "partial" === type ) { item = $.data&&$.data.length ? $.data[rndInt(0,$.data.length-1)] : null; @@ -3450,21 +3546,21 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { // p ~ 1 / n1*n2*..nk, O(n) !n.length ? [] : array(n.length, function(i){return rndInt(0, n[i]-1);}) ); - + item = klass.DUAL(item, n, $); } - + return item; } // random unranking, another method for unbiased random sampling ,randu: CombinatorialIterator.rand - ,rank: function( item, n, $ ) { + ,rank: function( item, n, $ ) { var klass = this, Arithmetic = Abacus.Arithmetic, order = $ && null!=$.order?$.order:LEX, type = $ && $.type ? $.type : "tensor", add = Arithmetic.add, sub = Arithmetic.sub, mul = Arithmetic.mul, index = Arithmetic.O, J = Arithmetic.J, index, nd, i; - + if ( "partial" === type ) { index = Arithmetic.N($.data&&$.data.length ? $.data.indexOf(item) : -1); @@ -3473,7 +3569,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { { // O(n) item = klass.DUAL(item, n, $); - + if ( "tuple" === type ) { nd = n[0]; @@ -3487,26 +3583,26 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { for(i=0; i dir ) { @@ -3660,7 +3756,7 @@ function next_tensor( item, N, dir, type, order, TI ) //T-symmetric of LEX dir = -dir; } - + if ( 0 > dir ) { invalid = false; @@ -3696,7 +3792,7 @@ function next_tensor( item, N, dir, type, order, TI ) // https://en.wikipedia.org/wiki/Permutations Permutation = Abacus.Permutation = Class(CombinatorialIterator, { - + // extends and implements CombinatorialIterator constructor: function Permutation( n, $ ) { var self = this, sub = null; @@ -3726,7 +3822,7 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { } CombinatorialIterator.call(self, "Permutation", n, $, sub?{method:$.submethod,iter:sub,pos:$.subpos,cascade:$.subcascade}:null); } - + ,__static__: { C: CombinatorialIterator.C ,P: CombinatorialIterator.P @@ -3762,9 +3858,9 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { kcycles = $ && null!=$['cycles='] ? $['cycles=']|0 : null, kfixed = $ && null!=$['fixed='] ? $['fixed=']|0 : null ; - + if ( 0===n ) return []; - + dir = -1 === dir ? -1 : 1; if ( (!(COLEX&order) && (REVERSED&order)) || ((COLEX&order) && !(REVERSED&order)) ) dir = -dir; @@ -3803,9 +3899,9 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { { item = kcycles ? null : (0 > dir ? array(n, n-1, -1) : array(n, 0, 1)); } - + item = klass.DUAL(item, n, $); - + return item; } ,succ: function( item, index, n, $, dir, PI ) { @@ -3825,7 +3921,7 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { kfixed = $ && null!=$['fixed='] ? $['fixed=']|0 : null ; if ( 0===n ) return []; - + if ( "cyclic" === type ) { // p ~ 1 / n, O(n) @@ -3916,14 +4012,14 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { // fisher-yates-knuth unbiased shuffling item = shuffle(array(n, 0, 1)); } - + item = klass.DUAL(item, n, $); - + return item; } // random unranking, another method for unbiased random sampling ,randu: CombinatorialIterator.rand - ,rank: function( item, n, $ ) { + ,rank: function( item, n, $ ) { var klass = this, Arithmetic = Abacus.Arithmetic, type = $ && $.type ? $.type : "permutation", kcycles = $ && null!=$['cycles='] ? $['cycles=']|0 : null, @@ -3933,12 +4029,12 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { mul = Arithmetic.mul, div = Arithmetic.div, index = Arithmetic.O, i, ii, m, I = Arithmetic.I, J = Arithmetic.J, N, M; - + n = n || item.length; if ( !n ) return index; - + item = klass.DUAL(item, n, $); - + if ( "cyclic"=== type ) { // O(1) @@ -3975,13 +4071,13 @@ Permutation = Abacus.Permutation = Class(CombinatorialIterator, { item = permutation2inversion(null, item); for(m=n-1,i=0; i dir ? array(n[1], n[0]-1, 0) : array(n[1], 0, 0) @@ -4487,9 +4583,9 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { ) : ( 0 > dir ? array(n[1], n[0]-n[1], 1) : array(n[1], 0, 1) )); - + item = klass.DUAL(item, n, $); - + return item; } ,succ: function( item, index, n, $, dir, CI ) { @@ -4502,7 +4598,7 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { item, i, k = n[1], n_k, c, selected, rndInt = Abacus.Math.rndInt; if ( 0===k ) return []; - + n = n[0]; n_k = n-k; c = n-1; // O(klogk) worst/average-case, unbiased if ( ("repeated" === type) || ("ordered+repeated" === type) ) @@ -4558,9 +4654,9 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { }),1,true) ))); } - + item = klass.DUAL(item, n, $); - + return item; } // random unranking, another method for unbiased random sampling @@ -4572,10 +4668,10 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { index = O, i, c, j, k = n[1], N, binom, order = $ && null!=$.order ? $.order : LEX, type = $ && $.type ? $.type : "unordered", factorial = Abacus.Math.factorial; - + if ( 0===k ) return O; item = klass.DUAL(item, n, $); - + if ( "ordered+repeated" === type ) { // O(k) @@ -4589,7 +4685,7 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { for(i=1; i<=k; i++) { // "Algorithms for Unranking Combinations and Other Related Choice Functions", Zbigniew Kokosi´nski 1995 (http://riad.pk.edu.pl/~zk/pubs/95-1-006.pdf) - // adjust the order to match MSB to LSB + // adjust the order to match MSB to LSB // reverse of wikipedia article http://en.wikipedia.org/wiki/Combinatorial_number_system c = N-1-item[i-1]-i+1; j = k+1-i; if ( j <= c ) index = add(index, factorial(c, j)); @@ -4612,17 +4708,17 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { for(i=1; i<=k; i++) { // "Algorithms for Unranking Combinations and Other Related Choice Functions", Zbigniew Kokosi´nski 1995 (http://riad.pk.edu.pl/~zk/pubs/95-1-006.pdf) - // adjust the order to match MSB to LSB + // adjust the order to match MSB to LSB // reverse of wikipedia article http://en.wikipedia.org/wiki/Combinatorial_number_system c = N-1-item[i-1]; j = k+1-i; if ( j <= c ) index = add(index, factorial(c, j)); } index = sub(sub(binom,I),index); } - + if ( (!(COLEX&order) && (REVERSED&order)) || ((COLEX&order) && !(REVERSED&order)) ) index = sub($ && null!=$.last?$.last:sub(klass.count(n, $),I), index); - + return index; } ,unrank: function( index, n, $ ) { @@ -4634,15 +4730,15 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { type = $ && $.type ? $.type : "unordered", repeated, order = $ && null!=$.order ? $.order : LEX; n = n[0]; - + if ( null==index || !Arithmetic.inside(index, Arithmetic.J, $ && null!=$.count ? $.count : klass.count(n, $)) ) return null; - + if ( 0===k ) return []; - + if ( (!(COLEX&order) && (REVERSED&order)) || ((COLEX&order) && !(REVERSED&order)) ) index = sub($ && null!=$.last?$.last:sub(klass.count(n, $),Arithmetic.I), index); - + item = array(k); if ( "ordered+repeated" === type ) { @@ -4668,13 +4764,13 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { else//if ( ("repeated" === type) || ("unordered" === type) || ("binary" === type) ) { // "Algorithms for Unranking Combinations and Other Related Choice Functions", Zbigniew Kokosi´nski 1995 (http://riad.pk.edu.pl/~zk/pubs/95-1-006.pdf) - // adjust the order to match MSB to LSB + // adjust the order to match MSB to LSB // O(k) repeated = "repeated" === type; N = repeated ? n+k-1 : n; binom = $ && $.count ? $.count : Abacus.Math.factorial(N, k); index = sub(sub(binom,I),index); - binom = div(mul(binom,N-k),N); + binom = div(mul(binom,N-k),N); t = N-k+1; m = k; p = N-1; do { if ( lte(binom, index) ) @@ -4682,21 +4778,21 @@ Combination = Abacus.Combination = Class(CombinatorialIterator, { item[k-m] = repeated ? N-t-k+1 : N-t-m+1; if ( gt(binom, O) ) { - index = sub(index, binom); + index = sub(index, binom); binom = div(mul(binom,m),p); } m--; p--; } else { - binom = div(mul(binom,p-m),p); + binom = div(mul(binom,p-m),p); t--; p--; } } while( m > 0 ); } - + item = klass.DUAL(item, n, $); - + return item; } ,fusion: CombinatorialIterator.fusion @@ -4736,7 +4832,7 @@ function next_combination( item, N, dir, type, order, CI ) { //maybe "use asm" var k = N[1], n = N[0], i, j, index, curr, i0, DI, MIN, MAX, a, b, da, db, inc, repeated; - + // some C-P-T dualities, symmetries & processes at play here // LEX MIN = 0; MAX = k-1; @@ -4761,7 +4857,7 @@ function next_combination( item, N, dir, type, order, CI ) //T-symmetric of LEX dir = -dir; } - + // constant average delay (CAT) for ordered+repeated (=tuple) // constant average delay (CAT) for ordered (or linear if "CI" map is computed at run-time) // constant average delay (CAT) for unordered(repated or not) (or linear if "CI" map is computed at run-time) @@ -4966,7 +5062,7 @@ function next_combination( item, N, dir, type, order, CI ) // http://en.wikipedia.org/wiki/Power_set // PowerSet(n) = Combinations(n,0) + Combinations(n,1) + .. + Combinations(n,n-1) + Combinations(n,n) Subset = Abacus.Powerset = Abacus.Subset = Class(CombinatorialIterator, { - + // extends and implements CombinatorialIterator constructor: function Subset( n, $ ) { var self = this, sub = null; @@ -4989,7 +5085,7 @@ Subset = Abacus.Powerset = Abacus.Subset = Class(CombinatorialIterator, { if ( "binary"===$.output ) $.output = function(item,n){ return Subset.binary(item,n,1); }; CombinatorialIterator.call(self, "Subset", n, $, sub?{method:$.submethod,iter:sub,pos:$.subpos,cascade:$.subcascade}:null); } - + ,__static__: { C: CombinatorialIterator.D ,P: CombinatorialIterator.P @@ -5018,7 +5114,7 @@ Subset = Abacus.Powerset = Abacus.Subset = Class(CombinatorialIterator, { dir = -1 === dir ? -1 : 1; if ( (!(COLEX&order) && (REVERSED&order)) || ((COLEX&order) && !(REVERSED&order)) ) dir = -dir; - + // O(n) // fixed-length item, with effective length as extra last pos item = new Array(n+1); item[n] = 0; @@ -5031,9 +5127,9 @@ Subset = Abacus.Powerset = Abacus.Subset = Class(CombinatorialIterator, { { if ( 0>dir ) { item[0] = n-1; item[n] = 1; } } - + item = klass.DUAL(item, n, $, 1); - + return item; } ,succ: function( item, index, n, $, dir, SI ) { @@ -5046,53 +5142,53 @@ Subset = Abacus.Powerset = Abacus.Subset = Class(CombinatorialIterator, { for(var list = null,i=n-1; i>=0; i--) if ( rndInt(0,1) ) list = {len:list?list.len+1:1, k:i, next:list}; item = list ? array(list.len, function(i){var k = list.k; list = list.next; return k;}): []; - + // fixed-length item, with effective length as extra last pos //if ( !$ || "binary" !== $.type ) item = item.concat(item.length dir ) @@ -5230,7 +5326,7 @@ function next_subset( item, N, dir, order ) // integer compositions (resp. restricted k-compositions) have bijections ("isomorphisms") to subsets (resp. k-subsets=combinations) // via "partial-sums mapping": x_1=y_1,..,x_k=y_k-y_{k-1},..,x_m (composition) ::=> y_1=x_1,..,y_k=y_{k-1}+x_k,..,y_m (subset) Partition = Abacus.Partition = Class(CombinatorialIterator, { - + // extends and implements CombinatorialIterator constructor: function Partition( n, $ ) { var self = this, sub = null; @@ -5261,7 +5357,7 @@ Partition = Abacus.Partition = Class(CombinatorialIterator, { else if ( "packed"===$.output ) $.output = function(item,n){ return Partition.pack(item,1); }; CombinatorialIterator.call(self, "Partition", n, $, sub?{method:$.submethod,iter:sub,pos:$.subpos,cascade:$.subcascade}:null); } - + ,__static__: { C: function( item, N, LEN, $, dir ) { // C process / symmetry, ie Rotation/Complementation/Conjugation, CC = I @@ -5314,14 +5410,14 @@ Partition = Abacus.Partition = Class(CombinatorialIterator, { order = $ && null!=$.order ? $.order : LEX, LEN = K ? K : (M ? n-M+1 : n), is_composition = "composition" === type, conj = false; - + if ( (0 >= n) || (K && M && ((K+M > n+1) || (K*M < n))) || (K && K > n) || (M && M > n) ) return null; - + dir = -1 === dir ? -1 : 1; - + if ( (!(COLEX&order) && (REVERSED&order)) || ((COLEX&order) && !(REVERSED&order)) ) dir = -dir; - + // O(n) item = new Array(LEN+1); item[LEN] = 0; if ( K && M ) @@ -5406,9 +5502,9 @@ Partition = Abacus.Partition = Class(CombinatorialIterator, { }, item, 0 > dir ? [n] : array(n, 1, 0)); } } - + item = klass.DUAL(item, n, $, 1); - + return item; } ,succ: function( item, index, n, $, dir, PI ) { @@ -5429,17 +5525,17 @@ Partition = Abacus.Partition = Class(CombinatorialIterator, { list, item, m, x, y, y1 = 0, yn = 0, itemlen, LEN = K ? K : (M ? n-M+1 : n), is_composition = "composition" === type, conj = false; - + if ( (0 >= n) || (K && M && ((K+M > n+1) || (K*M < n))) || (K && K > n) || (M && M > n) ) return null; - + if ( K && M ) { // not implemented return null; } - + if ( M && !K ){ K=M; conj=true; } - + // generate random (k-)composition (resp. diff of (k-)subset) // transform to partition (resp. composition) by sorting (resp. shuffling) // partition is a sorted composition, composition is a shuffled partition @@ -5498,11 +5594,11 @@ Partition = Abacus.Partition = Class(CombinatorialIterator, { // get conjugate if ( conj ) item = conjugatepartition(0,item); } - + itemlen = item.length; if ( itemlen dir ) { // compute prev partition @@ -5799,7 +5895,7 @@ function next_composition( item, N, dir, K, M, order, PI ) MIN = 0; MAX = item[LEN]-1; i0 = MIN; i1 = MAX; } - + if ( 0 > dir ) { // compute prev composition @@ -5881,7 +5977,7 @@ function next_composition( item, N, dir, K, M, order, PI ) if ( d ) PI[0]--; } if ( 0>PI[0] ) PI[0]=0; - + if ( 0 < rem ) { if ( MIN<=i+DI && i+DI<=MAX ) @@ -5890,7 +5986,7 @@ function next_composition( item, N, dir, K, M, order, PI ) item[i]=stdMath.min(M,rem); rem-=item[i]; if ( M === item[i] ) PI[0]++; if ( 0 < j ) - { + { if ( 0 > DI ) item.splice(0, j); else item.splice(i+1, j); } @@ -5914,7 +6010,7 @@ function next_composition( item, N, dir, K, M, order, PI ) else { if ( 0 < j ) - { + { if ( 0 > DI ) item.splice(0, j); else item.splice(i+1, j); } @@ -6126,6 +6222,148 @@ function next_composition( item, N, dir, K, M, order, PI ) return item; } +LatinSquare = Abacus.LatinSquare = Class({ + constructor: function LatinSquare( n ) { + var self = this; + if ( !(self instanceof LatinSquare) ) return new LatinSquare(n); + self.n = n; + } + + ,__static__: { + isLatinSquare: is_latin + ,make: function( n ) { + var i, j, k=1, s = new Array(n); + s[0] = new Array(n); for (j=0; j= n || 2 === n ) return null; + if ( 1 === n ) return [[n]]; + + var i, j, k, + odd = n&1, even = 1-odd, + doubly_even = 0 === (n%4), + nn = n*n, n2 = (n-odd)>>>1, + n12, n21, magic; + + magic = new Array(n); + for (i=0; i= n ) { i++; j=0; } + magic[i][j] = ((n12+j-i)%n)*n + ((n21+i+j)%n)+1; + } + } + + else if ( doubly_even ) // doubly-even order + { + for (k=0,i=0,j=0; k= n ) { i++; j=0; } + if ( ((i+1)%4)>>>1 === ((j+1)%4)>>>1 ) + magic[i][j] = nn-k; + else + magic[i][j] = k+1; + } + } + + else if ( even ) // singly-even order + { + var O = magic_square(n2), o, n22 = n2*n2, a = 2*n22, b = 3*n22, lc, rc, t; + for (k=0,i=0,j=0; k= n2 ) { i++; j=0; } + o = O[i][j]; + magic[i][j] = o; + magic[i+n2][j+n2] = o + n22; + magic[i+n2][j] = o + a; + magic[i][j+n2] = o + b; + } + lc = n2>>>1; rc = lc; + for (j=0; j n - rc) || (i === lc && j === lc)) && + !(i === 0 && j === lc) ) + { + t = magic[i][j]; + magic[i][j] = magic[i][j + n2]; + magic[i][j + n2] = t; + } + } + } + } + return magic; + } + ,product: function( /* args */) { + if ( 1 >= arguments.length ) return arguments[0]; + var m = arguments, nm = m.length, m1, m2, mm = m[0], mult, n1, n2, n22, n12, k=1, i, j, i1, i2, j1, j2; + while (k < nm) + { + m1 = mm; m2 = m[k++]; + n1 = m1.length; n2 = m2.length; n22 = n2*n2; n12 = n1*n2; + mm = new Array(n12); + for (i=0; i= n2 ) + { + i2++; j2=0; + if ( i2 >= n2 ) + { + j1++; j+=n2; i2=0; j2=0; + if ( j1 >= n1 ) { i1++; i+=n2; j1=0; j=0; i2=0; j2=0; } + if ( i1 < n1 && j1 < n1 ) mult = (m1[i1][j1]-1)*n22; + } + } + } + } + return mm; + } + } + + ,n: null + + ,dispose: function( ) { + var self = this; + self.n = null; + return self; + } +}); + // export it return Abacus; }); diff --git a/src/js/Abacus.min.js b/src/js/Abacus.min.js index c60b6aa..7f4972f 100644 --- a/src/js/Abacus.min.js +++ b/src/js/Abacus.min.js @@ -1,8 +1,8 @@ /** * * Abacus -* A combinatorics library for Node/XPCOM/JS, PHP, Python, Java, C/C++ -* @version: 0.8.8 +* A combinatorics library for Node.js / Browser / XPCOM Javascript, PHP, Python, Java, C/C++ +* @version: 0.9.0 * https://github.com/foo123/Abacus **/ -!function(n,e,t){"use strict";"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils.import?(n.$deps=n.$deps||{})&&(n.EXPORTED_SYMBOLS=[e])&&(n[e]=n.$deps[e]=t.call(n)):"object"==typeof module&&module.exports?(module.$deps=module.$deps||{})&&(module.exports=module.$deps[e]=t.call(n)):"undefined"!=typeof System&&"function"==typeof System.register&&"function"==typeof System.import?System.register(e,[],function(r){r(e,t.call(n))}):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(e)?define(e,["module"],function(e){return t.moduleUri=e.uri,t.call(n)}):e in n||(n[e]=t.call(n)||1)&&"function"==typeof define&&define.amd&&define(function(){return n[e]})}(this,"Abacus",function(n){"use strict";function e(){throw new Error("Method not implemented!")}function t(n){return n instanceof Array||"[object Array]"===In.call(n)}function r(n){return n instanceof String||"[object String]"===In.call(n)}function u(n){return"function"==typeof n}function o(n){var e=n.toString(2),t=32-e.length;return t>0?new Array(t+1).join("0")+e:e}function s(n,e,r,u,l,i,o){var s,a,f,c,d,m,h,p,_=e,g=null,b=r&&t(r);if(b&&(null==u&&(u=0),null==l&&(l=r.length-1)),null==i&&(i=u>l?-1:1),0===i||b&&!r.length||0>=xn.floor((l-u)/i)+1)return _;if(0>i?((p=(u-l)%-i)&&(g=l),m=l+=p,h=u,c=-1,f=-(-i<<4)):((p=(l-u)%i)&&(g=l),m=u,h=l-=p,c=1,f=i<<4),d=u+i*(15&xn.floor((l-u)/i+1)),b){for(m=xn.max(0,m),h=xn.min(r.length-1,h),s=u;m<=s&&s<=h&&00?new Array(n):[];if(0<(n=l.length)&&null!=e){r=r||0;var i=e;s(u(e)?function(n,t,r){return n[r]=e(r),n}:e===+e?function(n,e,t){return n[t]=i,i+=r,n}:function(n,t,r){return n[r]=e,n},l,l)}return l}function f(n,e,t){return s(function(n,e,r){return n[r]=e[t],n},n,e)}function c(n,e,r,u,l){return null==e?n:s(t(r)?function(n,e,t){return n[t]=r[t]-1-e,n}:function(n,e,t){return n[t]=r-1-e,n},n,e,u,l)}function d(n,e,t,r,u){if(null==e)return n;if(null==r&&(r=0),null==u&&(u=e.length-1),n!==e||r0?(e+i)%r[t]:0,i+=r[t]-n[t],n}:function(n,e,t){return n[t]=(e+i)%r,i+=r-n[t],n},n,e,u,l)}function p(n,e,t,r,u){return null==e?n:(null==u&&(u=e.length-1),null==r&&(r=0),n!==e||0!==t?s(function(n,e,r){return n[r+t]=e,n},n,e,0>t?r:u,0>t?u:r,0>t?1:-1):n)}function _(n,e,t,r,u,l,i,o){if(null==e)return null;null==t&&(t=1),null==r&&(r=0),null==u&&(u=0),null==l&&(l=e.length-1),null==i&&(i=u),null==o&&(o=l);var a=0,f=i>o?-1:1,c=i;return s(function(n,e,u){return e=r+t*e,n[c]=e-a,a=e,c+=f,n},n,e,u,l)}function g(n,e,t,r,u,l,i,o){if(null==e)return null;null==t&&(t=1),null==r&&(r=0),null==u&&(u=0),null==l&&(l=e.length-1),null==i&&(i=u),null==o&&(o=l);var a=0,f=i>o?-1:1,c=i;return s(function(n,e,u){return a+=e,n[c]=r+t*a,c+=f,n},n,e,u,l)}function b(n,e,t,r,u,l,i,o,s){s=!0===s,r=-1===r?-1:1,null==u&&(u=0),null==l&&(l=e===+e?e-1:e.length-1),null==i&&(i=0),null==o&&(o=t?t.length-1:-1);var f=u>l?-1:1,c=i>o?-1:1,d=f*(l-u)+1,m=u,h=i,p=0;if(!t||!t.length)return e===+e?a(e,u,f):e?e.slice():e;if(null==n&&(n=new Array(s?2*d:d)),e===+e){for(;0<=f*(l-m)&&0<=c*(o-h);)m===t[h]?(s&&(n[p++]=m),m+=f,h+=c):1===r&&m>t[h]||-1===r&&mt[h]||-1===r&&e[m]l?-1:1,d=i>o?-1:1,m=c*(l-u)+1+(d*(o-i)+1),h=u,p=i,_=0,g=null,b=!a;if(null==n&&(n=new Array(m)),0===n.length)return f?e:n;for(;0<=c*(l-h)&&0<=d*(o-p);){if(a&&_){if(e[h]===g){h+=c;continue}if(t[p]===g){p+=d;continue}}s?1===r&&e[h][0]t[p][0]?(n[_++]=g=e[h],h+=c):1===r&&e[h][0]>t[p][0]||-1===r&&e[h][0]t[p][1]?(n[_++]=g=e[h],b&&(n[_++]=t[p])):(n[_++]=g=t[p],b&&(n[_++]=e[h])),h+=c,p+=d):1===r&&e[h]t[p]?(n[_++]=g=e[h],h+=c):1===r&&e[h]>t[p]||-1===r&&e[h]c?l:u,_=0;_i){if(1===f){a=o-1;break}0===f&&(f=-1)}l=i}if(0===f&&(f=1),-1===a)a=t,r[0]=s,r[1]=a,r[2]=f;else{for(c=a+1,d=-1,l=u?n[c][0]:n[c],o=c+1;o<=t;o++){if(i=u?n[o][0]:n[o],li){if(1===m){d=o-1;break}0===m&&(m=-1)}l=i}-1===d&&(d=t),0===m&&(m=1),r[0]=s,r[1]=a,r[2]=f,r[3]=c,r[4]=d,r[5]=m}}function x(n,e,t,r,u,l){null==u&&(u=0),null==l&&(l=n.length-1);var i=u>l?-1:1,o=i*(l-u)+1;if(r=!0===r,1>=o)return r?1===o?[u]:[]:n;e=-1===e?-1:1;var a,c,m,h,p,_=o,g=1,b=2,x=xn.min,A=new Array(o);if(r&&(n=s(function(n,e,t){return n[t-u]=[e,t],n},new Array(o),n,u,l,1),u=0,l=o-1),!0===t){h=u,p=-1,a=[-1,-1,0,-1,-1,0];do{y(n,u,l,a,r),-1===a[3]?(e!==a[2]&&u>=1;return r?f(n,n,1):n}function A(n,e,t,r){var u,l,i,o=n.length;if(null==t&&(t=0),null==r&&(r=o-1),null==e||0===e){for(e=0,l=n[t],u=t+1;u<=r;u++){if(i=n[u],li){if(1===e)return 0;0===e&&(e=-1)}l=i}return 0===e?1:e}if(e=-1===e?-1:1,t>=r)return e;if(-1===e)for(l=n[t],u=t+1;u<=r;u++){if(i=n[u],li)return 0;l=i}return e}function w(n,e,r,u){var l,i=yn.Math.rndInt,o=!0===e?1:0;return t(r)?1<(l=r.length)&&s(function(n){if(o=0;s--)a=u[s],f=n[l+d],n[l+d]=n[l+a],n[l+a]=f,d++;return t&&x(o),o}function M(n,e){return n+e}function I(n,e,t,r){return s(yn.Arithmetic.add,yn.Arithmetic.O,n,e,t,r)}function S(n){var e=yn.Arithmetic;return e.shl(e.I,e.N(n))}function q(n,e){var r,u=yn.Arithmetic,l=u.O,i=u.I,o=u.J,a=u.N,f=u.add,c=(u.sub,u.div),d=u.mul;return null==e?10>=n?0>n?l:0===n?i:a([1,2,6,24,120,720,5040,40320,362880,3628800][n-1]):(r=String(n),null==q.mem1[r]&&(q.mem1[r]=d(q(n-1),n)),q.mem1[r]):!1===e?10>=n?2>n?l:a([1,2,9,44,265,1854,14833,133496,1334961][n-2]):(r="!"+String(n),null==q.mem2[r]&&(q.mem2[r]=f(1&n?o:i,d(q(n-1,!1),n))),q.mem2[r]):!0===e?10>=n?0>n?l:0===n?i:a([1,2,4,10,26,76,232,764,2620,9496][n-1]):(r="I"+String(n),null==q.mem2[r]&&(q.mem2[r]=f(q(n-1,!0),d(q(n-2,!0),n-1))),q.mem2[r]):t(e)?e.length?0>n?l:(r=String(n)+"@"+x(e.slice(),1,!0).join(","),null==q.mem3[r]&&(q.mem3[r]=c(q(n),s(function(n,e){return d(n,q(e))},q(e[e.length-1]),e,e.length-2,0))),q.mem3[r]):0>n?l:q(n):e===+e?0>e?-e>=n?-e===n?q(n):l:(r=String(n)+"@"+String(e),null==q.mem3[r]&&(q.mem3[r]=s(d,i,null,n+e+1,n)),q.mem3[r]):0>e||1>n||e>n?l:(e+e>n&&(e=n-e),0===e||1===n?i:1===e?a(n):(r=String(n)+"@"+String(e),null==q.mem3[r]&&(q.mem3[r]=u.isDefault()?xn.round(s(function(e,t){return e*(1+n/t)},1+(n-=e),null,2,e)):f(q(n-1,e-1),q(n-1,e))),q.mem3[r])):l}function L(n,e,t){var r,u=yn.Arithmetic,l=u.O,i=u.I,o=u.V,s=u.add,a=u.mul;return 0>n||0>e?l:2===t?n===e||1===e&&0>n?i:0===n||0===e?l:(r=String(n)+","+String(e),null==L.mem2[r]&&(L.mem2[r]=s(L(n-1,e-1,2),a(L(n-1,e,2),e))),L.mem2[r]):-1===t?e>n||0===e&&0>n?l:n===e?i:(r="-"+String(n)+","+String(e),null==L.mem1[r]&&(L.mem1[r]=s(L(n-1,e-1,-1),a(L(n-1,e,-1),1-n))),L.mem1[r]):e>n||0===e&&0>n?l:n===e?i:1===e?q(n-1):n-e&1?o(L(n,e,-1)):L(n,e,-1)}function C(n,e,t,r){var u,l,i=yn.Arithmetic,o=i.add,a=i.I,f=i.O;return 0>n||0>=e||t>r||t+e>n+1||e*rn||e&&t&&(e+t>n+1||e*tn||e&&e>n?c:(t&&!e&&(i=0,o=1,a=t,f=t,e=t,t=null),r=String(n)+"|"+String(e)+"|"+String(t),null==E.mem[r]&&(E.mem[r]=s(function(e,t){return s(function(e,r){return l(e,C(n,t,1,r))},e,null,o,i||n-t+1,1)},c,null,a,f,1)),E.mem[r])}function $(n,e,t,r){var u,l=yn.Arithmetic,i=l.add,o=l.O,a=l.I;return 0>n||0>=e||t>r||t*e>n||e*rn||e&&t&&(e+t>n+1||e*tn||e&&e>n?a:(r=String(n)+"|"+String(e)+"|"+String(t),null==k.mem[r]&&(k.mem[r]=e&&t?e*t===n?i:$(n,e,1,t):e?$(n,e,1,n):t?n===t?i:s(function(e,r){return o(e,$(n,r,1,t))},a,null,xn.ceil(n/t),n-t+1,1):1<=n?S(n-1):i),k.mem[r])}function D(n){var e,t=yn.Arithmetic,r=t.O,u=t.N,l=t.div,i=t.mul;return 14>=n?0>n?r:u([1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900][n]):(e=String(n),null==D.mem[e]&&(D.mem[e]=l(i(D(n-1),4*n-2),n+1)),D.mem[e])}function R(n){var e,t=yn.Arithmetic,r=t.O,u=t.N,l=t.add,i=t.mul;return 12>=n?0>n?r:u([1,1,2,5,15,52,203,877,4140,21147,115975,678570][n]):(e=String(n),null==R.mem[e]&&(R.mem[e]=s(function(e,t){return l(e,i(q(n-1,t),R(t)))},r,null,0,n-1,1)),R.mem[e])}function N(n){var e,t=yn.Arithmetic,r=t.O,u=t.N,l=t.add;return 29>=n?0>n?r:u([0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811][n]):(e=String(n),null==N.mem[e]&&(N.mem[e]=l(N(n-1),N(n-2))),N.mem[e])}function U(n){if(!arguments.length||null==n)return Vn;var e=0;if(r(n)){for(var t=0,u=(n=n.toUpperCase().split(",")).length;t=e&&(e=Vn)}else e=Bn&n?Bn&n:Vn;return Hn&e&&Wn&e&&(e&=~Wn),Fn&e&&(Gn|Vn)&e&&(e&=~(Gn|Vn)),Gn&e&&Vn&e&&(e&=~Vn),Hn&e&&(e&=~Xn),e}function P(){var n,e,r,u,l,i,o,s,a,f,c,d=arguments.length;if(!d)return[];if(!0===arguments[0]){for(f=arguments[1].length,n=2;n=0;e--)if(u=arguments[e].length,l=r%u,r=~~(r/u),o=arguments[e][l],t(o))for(i=o.length-1;i>=0;i--)s[d-++a]=o[i];else s[d-++a]=o;c[n]=s}}return c}function T(n,e,t){var r,l,i,o,s,a,f,c,d,m,h,p,_,g,b,v,y,x,A,w,O,M,I,S,q=n.length,L=null,C=null;if(!q)return[];for(u(t)?(C=t,t=!0):t=!1,Rn!==e&&Nn!==e&&Un!==e&&Pn!==e&&Tn!==e&&jn!==e&&(e=!1),y=new Array(q),v=[],l=1,r=0;r=l)return[];if(!v.length)return[];for(g=new Array(l),_=0,p=q-1,x=v.length-1,w=new Array(q),O=0,r=0;r=0;i--)if(b=v[i],s=n[b].length,a=o%s,o=~~(o/s),m[b]=n[b][a],null!=y[b]){w[0]=b,O=1;do{for(M=y[b=w[--O]],f=m[b],I=0,S=M.length;I=0;h--){if(c=m[h],Rn===e&&d!==c||Nn===e&&1===L[c]||Un===e&&c>=d||Pn===e&&c<=d||Tn===e&&cd||t&&!C(m,h,p)){A=!0;break}Nn===e&&(L[c]=1),d=c}A||(g[_++]=m)}return g.length>_&&(g.length=_),g}function j(n,e,t){if(null==e||!e.length||1>=e.length)return 1===e.length?a(n-1,function(n){return n=e)return[];var t,r=a(e,0,0),u=n.length;for(e-=1,t=0;t=(u=s(M,0,e))?l=e.slice():(0<(u-=s(M,0,l=G(j(u-1,V(e,i-1,r)))))&&l.push(u),0>r&&d(l,l));else{var o,f,c,m,h,p=1,_=0,g=0,b="push";if(0>r&&(p=-p,_=i-1-_,b="unshift"),t(e[_]))for(l=[[(h=e[_])[1],h[0]]],o=0,c=1,m=p+_;cr?0:o,h[1]===l[f][0]?l[f][1]+=h[0]:(l[b]([h[1],h[0]]),o++);else for(l=a(u=e[_],1,0),0>r&&(g=u-1-g),c=1,m=p+_;c0;)l[f]++,h--,o++,f+=p}return l}function H(n,e){if(null==n)return null;var t,r,u,l,i=[],o=n.length,s=1,a=0,f="push";for(-1===e&&(s=-s,a=o-1-a,f="unshift"),l=[u=n[a],1],t=1;t=l&&(u=0,r+=l);if(!0===t)for(r=0;r=i&&(u=0,r++);else for(r=0,u=0;r=i&&(u=0,r++);return n}function z(n,e,t){var r=n?n.length:0,u=1,l=0,i=0,o=i=o&&(l+=u,o=++i>>1<<1]),i[r]+=1,r>>>=1;return i[r]+=1,n[t]=e,n},n||new Array(r),e)}function en(n,e,t){var r,u,l,i=e.length,o=xn.ceil(Sn(t||i)),a=1<=c[r<<=1]&&(e-=c[r],r++);return c[r]=0,n[t]=r-a,n},n||new Array(i),e)}function tn(n,e,t){var r,u=n.length,l=null==e;if(u>1)for(l&&(e=new Array(u-1),t=0),r=u-1;r>=1;r--)e[t++]=[n[0],n[r]];else l&&(e=[]);return l?e:t}function rn(n,e){var t,r,u,l,i=n.length,o=new Array(i),s=!0===e?1:0,a=new Array(i),f=0,c=0;for(t=0;ts&&(u.length=l,o[c++]=u),u=new Array(i),l=0;fs&&(u.length=l,o[c++]=u),cu?function(t,r){return t[r]=r=e+r&&(e+=r,r=n[++t].length),e+n[t][u-e]})}function fn(n,e,t,r,u,l){var i,o,s,a,f,c,d,m,h,p=e;if("tuple"===r?(i=p[0],p=p[1]):i=p.length,0,h=i-1,c=1,a=h,f=0,d=1,m=0,Gn&u&&(c=-c,a=h-a,f=h-f,d=-d,m=h-m),Jn&u&&(c=-c,a=h-a,f=h-f,d=-d,m=h-m),Xn&u&&(t=-t),0>t)if("tuple"===r){for(o=a;0<=o&&h>=o&&0===n[o];)o-=c;if(0<=o&&h>=o)for(p-=1,n[o]=n[o]-1,s=o+c;0<=s&&h>=s;s+=c)n[s]=p;else n=null}else{for(o=a;0<=o&&h>=o&&0===n[o];)o-=c;if(0<=o&&h>=o)for(n[o]=n[o]-1,s=o+c;0<=s&&h>=s;s+=c)n[s]=p[d*s+m]-1;else n=null}else if("tuple"===r){for(o=a;0<=o&&h>=o&&n[o]+1===p;)o-=c;if(0<=o&&h>=o)for(n[o]=n[o]+1,s=o+c;0<=s&&h>=s;s+=c)n[s]=0;else n=null}else{for(o=a;0<=o&&h>=o&&n[o]+1===p[d*o+m];)o-=c;if(0<=o&&h>=o)for(n[o]=n[o]+1,s=o+c;0<=s&&h>=s;s+=c)n[s]=0;else n=null}return n}function cn(n,e,t,r,u,l,i){var o,s,a,f,c,d,m,h,p,_,g,b,v,y=e,x=null==l?y:l;if(0,v=y-1,h=1,m=v,p=1,_=0,g=1,b=0,Gn&u&&(h=-h,m=v-m,p=-p,_=x-1-_,t=-t),Jn&u&&(h=-h,m=v-m,g=-1,b=x-1),Xn&u&&(t=-t),0>t)if("cyclic"===r)if(o=v-m,p*n[o]+_>0)for(g=y-1,h=y+h,a=0;a0&&p*n[o]<=p*n[s];)s-=h;for(c=n[o],n[o]=n[s],n[s]=c,a=o+h,f=m;0<=a&&a<=v&&0<=f&&f<=v&&h*(f-a)>0;)c=n[a],n[a]=n[f],n[f]=c,d=d||g*a+b===n[a]||g*f+b===n[f],a+=h,f-=h;if("derangement"===r)for(0<=s&&s<=v&&(d=d||g*s+b===n[s]),0<=f&&f<=v&&(d=d||g*f+b===n[f]),d=d||g*o+b===n[o],a=o-h;!d&&0<=a&&a<=v;a-=h)d=g*a+b===n[a];else d=!1}else n=null}while(n&&d);else if("cyclic"===r)if(o=v-m,p*n[o]+_=p*n[o+h];)o-=h;if(0<=o&&o<=v){for(s=m;0<=s&&s<=v&&h*(s-o)>0&&p*n[o]>=p*n[s];)s-=h;for(c=n[o],n[o]=n[s],n[s]=c,a=o+h,f=m;0<=a&&a<=v&&0<=f&&f<=v&&h*(f-a)>0;)c=n[a],n[a]=n[f],n[f]=c,d=d||g*a+b===n[a]||g*f+b===n[f],a+=h,f-=h;if("derangement"===r)for(0<=s&&s<=v&&(d=d||g*s+b===n[s]),0<=f&&f<=v&&(d=d||g*f+b===n[f]),d=d||g*o+b===n[o],a=o-h;!d&&0<=a&&a<=v;a-=h)d=g*a+b===n[a];else d=!1}else n=null}while(n&&d);return n}function dn(n,e,t,r,u){if(null==n)return null;var l,i=null;if("ordered"===u)for(i={},l=0;lt)if("ordered+repeated"===r){for(i=f;0<=i&&i<=d&&0===n[i];)i-=c;if(0<=i&&i<=d)for(y-=1,n[i]=n[i]-1,o=i+c;0<=o&&o<=d;o+=c)n[o]=y;else n=null}else if("ordered"===r){for(null==l&&(l=dn(n,y,v,u,r)),i=f,s=-1;-1===s&&0<=i&&i<=d;){if(m*n[i]+h-m>=0)for(o=m*n[i]+h-m;0<=o&&oc?0:d,!b&&n[o]+1>v||b&&n[o]>0){if(b)for(;0<=i&&i<=d&&0===n[i];)i+=c;else for(;0<=i&&i<=d&&p*i+_===n[i];)i+=c;for(n[i]-=1,i-=c;0<=i&&i<=d;)n[i]=n[i+c]-g,i-=c}else n=null;else{for(s=-1,i=f;0<=i-c&&i-c<=d;i-=c)if(n[i]>n[i-c]+g){s=i;break}if(!(0<=s&&s<=d)&&0c?d:0]&&(s=0>c?d:0),0<=s&&s<=d){for(a=y-1+g,i=f;0<=i&&i<=d&&0i?(o=e-1,s=u=e-(n[a]||1)):(o=u=0,s=n[a]-1),0>t?00?1===n[a]||l>n[s-i]+1?(n[s]-=1,n[s+i]=f,n[a]++):n[a]--:n[a]=0:n=null:0===n[a]?(n[u]=0,n[a]=1):1*n[o]+0h?(a=_=b-1,f=p=b-(n[b]||1)):(a=p=0,f=_=n[b]-1),0>t)if(r)n=null;else if(Gn&l)n=null;else if(s=u?a+h:a,p<=s&&s<=_&&n[s]>1){for(o=f,m=0;p<=o&&o<=_&&h*(o-s)>=0&&1===n[o];)m+=n[o],o-=h;if(d=n[o]-1,m++,n[o]=d,n[b]=0>h?b-o:o+1,dc||n[a+(c-1)*h]0&&n[o]===n[o-h];)m+=n[o],o-=h;for(n[o]++,m--,n[b]=0>h?b-o:o+1;0d?(s=h=_-1,a=m=_-(n[_]||1)):(s=m=0,a=h=n[_]-1),0>t)if(r)if(u)n=null;else if(Gn&l)if(f=n[s],p-r+1>f){for(n[s]=1,o=s+d;m<=o&&o<=h&&1===n[o];)o+=d;n[o]--,m<=o-d&&o-d<=h&&(n[o-d]=1+f)}else n=null;else if(f=n[a],p-r+1>f){for(n[a]=1,o=a;m<=o&&o<=h&&1===n[o];)o-=d;n[o]--,m<=o+d&&o+d<=h&&(n[o+d]=1+f)}else n=null;else if(u)n=null;else if(Gn&l)n=null;else if(p>n[_]){for(o=a,c=0;m<=o&&o<=h&&1===n[o];)o-=d,c++;if(f=n[o]-1,n[o]=f,0<++c)if(m<=o+d&&o+d<=h)n[o+=d]=c,c=0,n[_]=0>d?_-o:o+1;else for(;0n[a]){for(o=s;m<=o&&o<=h&&1===n[o];)o+=d;f=n[o],n[o]=1,n[s]=f-1,m<=o+d&&o+d<=h&&n[o+d]++}else n=null;else if(p-r+1>n[s]){for(o=a;m<=o&&o<=h&&1===n[o];)o-=d;f=n[o],n[o]=1,n[a]=f-1,m<=o-d&&o-d<=h&&n[o-d]++}else n=null;else if(u)n=null;else if(Gn&l)n=null;else if(p>n[s])for(c=n[a],n[a-d]++,n[_]--,o=a-d,c--;0=e},lte:function(n,e){return n<=e},gt:function(n,e){return n>e},lt:function(n,e){return n=e&&n<=t:n>e&&nt?t:n},wrap:function(n,e,t){return nt?e:n},wrapR:function(n,e){return n<0?n+e:n},add:M,sub:function(n,e){return n-e},mul:function(n,e){return n*e},div:function(n,e){return yn.Math.floor(n/e)},mod:function(n,e){return n%e},pow:xn.pow,shl:function(n,e){return n<>e},bor:function(n,e){return n|e},band:function(n,e){return n&e},xor:function(n,e){return n^e},abs:xn.abs,min:xn.min,max:xn.max,floor:xn.floor,ceil:xn.ceil,round:xn.round,num:function(n){return"number"==typeof n?yn.Math.floor(n):parseInt(n,10)},val:function(n){return yn.Math.floor(n.valueOf())},sum:I,product:function(n,e,t,r){return s(yn.Arithmetic.mul,yn.Arithmetic.I,n,e,t,r)},pow2:S,exp:function(n,e){var t=yn.Arithmetic,r=t.N;return t.pow(r(n),r(e))},factorial:q,stirling:L,partitions:E,compositions:k,bell:R,catalan:D,fibonacci:N},yn.Arithmetic={isDefault:function(){return 0===yn.Arithmetic.O&&yn.Arithmetic.add===M},O:0,I:1,J:-1,N:yn.Math.N,V:yn.Math.V,equ:yn.Math.equ,gte:yn.Math.gte,lte:yn.Math.lte,gt:yn.Math.gt,lt:yn.Math.lt,inside:yn.Math.inside,clamp:yn.Math.clamp,wrap:yn.Math.wrap,wrapR:yn.Math.wrapR,add:yn.Math.add,sub:yn.Math.sub,mul:yn.Math.mul,div:yn.Math.div,mod:yn.Math.mod,pow:yn.Math.pow,shl:yn.Math.shl,shr:yn.Math.shr,bor:yn.Math.bor,band:yn.Math.band,xor:yn.Math.xor,abs:yn.Math.abs,min:yn.Math.min,max:yn.Math.max,floor:yn.Math.floor,ceil:yn.Math.ceil,round:yn.Math.round,rnd:yn.Math.rndInt,num:yn.Math.num,val:yn.Math.val},yn.Util={array:a,operate:s,intersection:function(n,e,t,r,u,l,i,o){r=-1===r?-1:1,null==u&&(u=0),null==l&&(l=e.length-1),null==i&&(i=0),null==o&&(o=t.length-1);var s=u>l?-1:1,a=i>o?-1:1,f=s*(l-u)+1,c=a*(o-i)+1,d=u,m=i,h=0;if(null==n&&(n=new Array(xn.min(f,c))),0===n.length)return n;for(;0<=s*(l-d)&&0<=a*(o-m);)1===r&&e[d]t[m]?d+=s:1===r&&e[d]>t[m]||-1===r&&e[d]l?-1:1,a=i>o?-1:1,f=s*(l-u)+1,c=u,d=i,m=0;if(!r||!r.length)return t?t.slice():t;for(null==n&&(n=new Array(f));0<=s*(l-c)&&0<=a*(o-d);)t[c]===r[d]?1r[d]?d+=a:(n[m++]=t[c],e[t[c]]--,c+=s);for(;0<=s*(l-c);)0>>1),i=e[l],n===i)return l;1===t&&ni?s=l-1:o=l+1}return-1},complementation:c,reflection:d,reversion:m,gray:h,finitedifference:_,partialsum:g,sort:x,shuffle:w,pick:O,pluck:f},yn.BitArray=Dn({constructor:function n(e){var t=this;if(!(t instanceof n))return new n(e);t.length=e,t.bits=new Uint32Array(xn.ceil(e/32))},length:0,bits:null,dispose:function(){var n=this;return n.length=null,n.bits=null,n},clone:function(){var n=this,e=new yn.BitArray(n.length);return e.bits=new Uint32Array(n.bits),e},fromArray:function(n){var e=this;return e.bits=new Uint32Array(n),e},toArray:function(){return On.call(this.bits)},toString:function(){var n,e,t=this.toArray();for(n=0,e=t.length;n>>5]&1<<(31&n))},set:function(n){var e=this;return e.bits[n>>>5]|=1<<(31&n),e},unset:function(n){var e=this;return e.bits[n>>>5]&=~(1<<(31&n)),e},toggle:function(n){var e=this;return e.bits[n>>>5]^=1<<(31&n),e}}),gn=yn.CombinatorialIterator=Dn({constructor:function n(e,r,u,l){var i=this;yn.Arithmetic;if(!(i instanceof n))return new n(e,r,u,l);if(i[wn],t(e)&&(e[0]instanceof n||e[e.length-1]instanceof n)){i.$=u=r||{},u.seq=e,e=null,i.n=r=u.seq.length,u.type="sequence",u.rand=u.rand||{},u.rand.sequence=1;var o=1/0,a=-1/0,f=1/0,c=-1/0;s(function(n,e){var t=e.base(),r=e.dimension();t>a&&(a=t),tc&&(c=r),r(t=-1===t?-1:1)||Xn&(e&&null!=e.order?e.order:Vn)?e.seq[e.seq.length-1].item0(t):e.seq[0].item0(t):null},succ:function(n,e,t,r,u,l){if(null==t||null==n)return null;var i,o,s,a,f,c=this,d=yn.Arithmetic;if(u=-1===u?-1:1,r&&"sequence"===r.type){if(!(f=r.seq)||!f.length)return null;for(Xn&(r&&null!=r.order?r.order:Vn)?(i=-1,o=f.length-1):(i=1,o=0),a=i*r.seq_curr+o,s=i*u;0<=a&&au?d.J:d.I),t,r)},rand:function(n,e){var t,r,u,l,i,o=this,s=yn.Arithmetic,a=s.O;if(e&&"sequence"===e.type){if(!(u=e.seq)||!u.length)return null;for(t=null!=e.last?e.last:s.sub(o.count(n,e),s.I),r=s.rnd(a,t),l=0,i=u.length;s.gte(r,u[l].total())&&(r=s.sub(r,u[l].total()),!(++l>=i||s.lt(r,a))););return l=l||o.lt(n,s))););return us&&(s=e[n]+1),e[n]):s+t[n-e.length]}:function(n){return nn?i.seq.length-1:0:-1),Hn&f?("gen"===yn.Options.RANDOM||1===i.rand[i.type]||s.gt(t,yn.Options.MAXMEM)||s.isDefault()&&0>t?(u.__item=l.rand(o,i),u.__index=a):(u._traversed&&u._traversed.dispose(),u._traversed=new yn.BitArray(s.val(t)),e=u.random("index"),u._traversed.set(+e),u.__item=l.unrank(e,o,i),null!=u.__item&&(u.__index=e)),u._index=a):(u.__item=l.initial(o,i,n,!0),null!=u.__item&&(u.__index=0>n?r:a,u._update()),u._index=u.__index),u._item=l.output(u.__item,u.__index,o,i),u._prev=!(Hn&f||0n&&!(Hn&f))&&null!=u.__item,u},_update:function(){return this.item__=null,this},order:function(n,e){if(!arguments.length)return this._order;var t,u,l,i,o,s,a=this,f=a[wn],c=yn.Arithmetic,d=(c.O,c.I,!0===n);if(e=-1===e,a.n,l=a.$,a._traversed&&(a._traversed.dispose(),a._traversed=null),d?n=l.order:r(n)&&-1<(u=n.indexOf("|"))?(t=n.substr(u+1),n=U(n.substr(0,u))):t=n=U(n),i=e?-1:1,l.order=n,l.sub?(d?l.sub.rewind(i):l.sub.order(t,i),a.__subindex=l.sub.index(),a.__subitem=l.sub.next(i),a._subindex=null,a._subitem=null):(a.__subindex=null,a.__subitem=null,a._subindex=null,a._subitem=null),"sequence"===l.type&&l.seq&&l.seq.length)for(o=0,s=l.seq.length;o(n=-1===n?-1:1)&&Hn&g)return null;if(e=0>n?h:m,Hn&g)if(u=_.last,c.lt(a._index,u))if(t=a._traversed){for(r=a.random("index"),o=yn.Math.rnd()>.5?h:m;t.isset(+r);)r=c.wrap(c.add(r,o),d,u);t.set(+r),a.__item=f.unrank(r,p,_),null!=a.__item&&(a.__index=r)}else a.__item=f.rand(p,_),a.__index=null;else a._item=a.__item=null,a._traversed&&(a._traversed.dispose(),a._traversed=null);else a.__item=f.succ(a.__item,a.__index,p,_,n,a.item__),null!=a.__item&&(a.__index=c.add(a.__index,e));if(s=null!=a.__item)a._index=c.add(a._index,e),null===a.__index&&(a.__index=a._index),0>n?(a._prev=s,a._next=y):(a._prev=y,a._next=s);else if(_.sub&&_.sub.hasNext(n)){if(a.__subindex=_.sub.index(),a.__subitem=_.sub.next(n),"sequence"===_.type&&_.seq&&_.seq.length)for(i=0,l=_.seq.length;in?(a._prev=s,a._next=y):(a._prev=y,a._next=s);return a._item=f.output(a.__item,a.__index,p,_),_.sub&&(s=s&&null!=a.__subitem,a._subindex=s?c.add(c.mul(a.__subindex,b),a._index):null,a._subitem=s?f.fusion(_.submethod,a._item,a.__subitem,a.dimension(),a.base(),_.subpos,_.subcascade):null,0>n?a._prev=s:a._next=s),v},range:function(n,e){var t,r,u,l,i=this,o=yn.Arithmetic,a=o.N,f=o.O,c=o.I,d=i.$,m=d.sub?d.subcount:d.count,h=d.sub?o.sub(m,c):d.last,p=1,_=arguments.length,g=!(Hn&d.order);return _<1?(n=f,e=h):_<2?(n=a(n),e=h):(n=a(n),e=a(e)),n=o.wrapR(n,m),e=o.wrapR(e,m),o.gt(n,e)&&(t=n,n=e,e=t,p=-1),n=o.clamp(n,f,h),g&&(e=o.clamp(e,f,h)),o.lte(n,e)?(l=[i.$.order,i.__index,i._index,i.__item&&i.__item.slice(),i._item,i.__subindex,i._subindex,i.__subitem,i._subitem,i._prev,i._next],g&&i.index(n),u=o.val(o.sub(e,n)),r=s(function(n,e,t){return n[t]=i.next(),n},new Array(u+1),null,0>p?u:0,0>p?0:u,0>p?-1:1),i.$.order=l[0],i.__index=l[1],i._index=l[2],i.__item=l[3],i._item=l[4],i.__subindex=l[5],i._subindex=l[6],i.__subitem=l[7],i._subitem=l[8],i._prev=l[9],i._next=l[10],i._update()):r=[],r},__iter__:function(){return new gn.Iterable(this)}}),"undefined"!=typeof Symbol&&void 0!==Symbol.iterator&&(gn[An][Symbol.iterator]=gn[An].__iter__),yn.Tensor=Dn(gn,{constructor:function n(){var e,u=this,l=null,i=On.call(arguments);if(e=!i.length||i[i.length-1]instanceof gn||t(i[i.length-1])||i[i.length-1]===+i[i.length-1]?{}:i.pop()||{},i.length&&t(i[0])&&(i=i[0]),i&&i.length||(i=[]),!(u instanceof n))return new n(i,e);if(e.type=String(e.type||"tensor").toLowerCase(),e.order=e.order||Vn,e.rand=e.rand||{},"partial"===e.type){i=t(i)&&i.length?i[0]:i;var o=-1,f=e.data||[],c=e.position||null;i instanceof gn?o=i=(l=i).base():l=e.sub,i=+(i||0)||0,f.length&&(r(f[0])||f[0].length&&(!0===f[0][0]||!1===f[0][0]))&&(o===i&&(i+=f.length,o=-1),f=n.generate(i,f,c,e.ordering||null)),o===i&&(i+=(f.length?f[0].length:0)||0,o=-1),e.data=f,e.position=c||a((f.length?f[0].length:0)||0,0,1),e.dimension=e.position.length,e.base=i,e.rand.partial=1}else if("tuple"===e.type)i[0]=i[0]||1,i[1]=i[1]||1,i[0]instanceof gn?(l=i[0],i[0]=l.dimension()):i[1]instanceof gn?(l=i[1],i[1]=l.base()):l=e.sub,e.base=i[1],e.dimension=i[0],"gray"===e.output&&(e.output=function(e,t){return n.gray(e,t[1])});else{var d=s(function(n,e){return en[1]&&(n[1]=e),n},[1/0,0],i);if(e.base=i,e.minbase=d[0],e.maxbase=d[1],e.dimension=i.length,"gray"===e.output)e.output=function(e,t){return n.gray(e,t)};else if(t(e.output)){var m=e.output;e.output=function(e,t){return n.component(e,m)}}}gn.call(u,"Tensor",i,e,l?{method:"partial"===e.type?e.submethod||"complete":e.submethod,iter:l,pos:"partial"===e.type?e.subpos||e.position:e.subpos,cascade:e.subcascade}:null)},__static__:{C:gn.C,P:gn.P,T:gn.T,DUAL:gn.DUAL,count:function(n,e){var t=yn.Arithmetic.O,r=e&&e.type?e.type:"tensor";return"partial"===r?e.data&&e.data.length?yn.Arithmetic.N(e.data.length):t:"tuple"===r?!n||0>=n[0]?t:yn.Math.exp(n[1],n[0]):n&&n.length?yn.Math.product(n):t},initial:function(n,e,t){var r,u=this,l=e&&e.type?e.type:"tensor",i=e&&e.order?e.order:Vn;return t=-1===t?-1:1,(!(Gn&i)&&Xn&i||Gn&i&&!(Xn&i))&&(t=-t),"partial"===l?r=e.data&&e.data.length?0>t?e.data[e.data.length-1]:e.data[0]:null:(r="tuple"===l?n[0]?0>t?a(n[0],n[1]-1,0):a(n[0],0,0):[]:n.length?0>t?a(n.length,function(e){return n[e]-1}):a(n.length,0,0):[],r=u.DUAL(r,n,e)),r},succ:function(n,e,t,r,u,l){if(!t||null==n)return null;var i=r&&r.type?r.type:"tensor",o=r&&null!=r.order?r.order:Vn;if(u=-1===u?-1:1,"partial"===i){if(!r.data||!r.data.length)return null;Xn&o&&(u=-u);var s=null==e?r.data.indexOf(n):yn.Arithmetic.val(e);return 0>u?0<=s-1?r.data[s-1]:null:0<=s&&s+1=t[0]?null:fn(n,t,u,i,o)},rand:function(n,e){var t,r=yn.Math.rndInt,u=this,l=e&&e.type?e.type:"tensor";return"partial"===l?t=e.data&&e.data.length?e.data[r(0,e.data.length-1)]:null:(t="tuple"===l?n[0]?a(n[0],function(e){return r(0,n[1]-1)}):[]:n.length?a(n.length,function(e){return r(0,n[e]-1)}):[],t=u.DUAL(t,n,e)),t},randu:gn.rand,rank:function(n,e,t){var r,u,l=this,i=yn.Arithmetic,o=t&&null!=t.order?t.order:Vn,s=t&&t.type?t.type:"tensor",a=i.add,f=i.sub,c=i.mul,d=i.O,m=i.J;if("partial"===s)d=i.N(t.data&&t.data.length?t.data.indexOf(n):-1);else if(n=l.DUAL(n,e,t),"tuple"===s){if(!(r=e[0]))return m;for(e=e[1],u=0;u=0;l--)i=h(r,u),r=p(r,u),o[l]=_(i)}else{if(!(s=e.length))return[];for(o=new Array(s),r=n,l=s-1;l>=0;l--)i=h(r,u=e[l]),r=p(r,u),o[l]=_(i)}o=a.DUAL(o,e,t)}return o},fusion:gn.fusion,output:gn.output,gray:function(n,e){return h(new Array(n.length),n,e)},product:P,directsum:function(){var n,e,t=arguments,r=t.length,u=0;for(e=0;e=n+t[e].length&&(n+=t[e++].length),n+t[e][r-n]})},component:function(n,e){return null==n?null:null==e?n:a(n.length,function(t){return ts[1]?j(n,a(s[0]-s[1]+1,s[1],1).filter(C)).reverse():j(n,a(s[1]-s[0]+1,s[0],1).filter(C)):j(n,s[1].split(",").map(Number).filter(C))).length){I=!0;break}e.push(m)}else if(s=f.match(Cn)){if(!(m=0s[1]?a(s[0]-s[1]+1,s[0],-1):a(s[1]-s[0]+1,s[0],1)).filter(C):s[1].split(",").map(Number).filter(C)).length){I=!0;break}e.push(m)}else{if(p=-1,_=null,g=null,f=f.replace(Ln,function(n,e){return null===_&&(p=parseInt(e,10),_="v"+String(p)),_}),!C(p)){l&&l.splice(h--,1);continue}try{g=new Function(_,"return Math.floor("+f+");")}catch(n){g=null}if(!u(g)){l&&l.splice(h--,1);continue}L[p]?L[p].push(g):L[p]=[g],0>l.indexOf(p)&&q.push(p),S.push([g,null,null,p,l[h]]),e.push(S[S.length-1])}else if(t(f)){if(!(m=!1===f[0]?j(n,f.slice(1).filter(C)):f.slice(1).filter(C)).length){I=!0;break}e.push(m)}if(I&&(e=[]),q.length)for(c=0,M=q.length;ce||e>y)return!1;return!0})).length){I=!0;break}e[s[1]]=m}return I?[]:(i="="===i?Rn:"!="===i||"<>"===i?Nn:"<"===i?Un:"<="===i||"=<"===i?jn:">"===i?Pn:(">="===i||"=>"===i)&&Tn,b=u(o.extra_conditions)?function(n,e,t){return v<=n[e]&&n[e]<=y&&o.extra_conditions(n,e,t)}:function(n,e,t){return v<=n[e]&&n[e]<=y},!0===o.lazy?e:T(e,i,b))}}}),yn.Permutation=Dn(gn,{constructor:function n(e,r){var u=this,l=null;if(!(u instanceof n))return new n(e,r);(r=r||{}).type=String(r.type||"permutation").toLowerCase(),(e=e||0)instanceof gn?e=(l=e).dimension():l=r.sub,r.base=r.dimension=e,r.rand=r.rand||{},r.rand.derangement=1,r.rand.involution=1,r.rand.connected=1,"multiset"===r.type&&(r.multiplicity=t(r.multiplicity)&&r.multiplicity.length?r.multiplicity.slice():a(e,1,0),r.multiplicity=r.multiplicity.concat(a(e-s(M,0,r.multiplicity),1,0)),r.base=r.multiplicity.length,r.multiset=z(r.multiplicity,e)),gn.call(u,"Permutation",e,r,l?{method:r.submethod,iter:l,pos:r.subpos,cascade:r.subcascade}:null)},__static__:{C:gn.C,P:gn.P,T:gn.T,DUAL:gn.DUAL,count:function(n,e){var t=yn.Arithmetic,r=t.O,u=yn.Math.factorial,l=yn.Math.stirling,i=e&&e.type?e.type:"permutation",o=e&&null!=e["cycles="]?0|e["cycles="]:null,s=e&&null!=e["fixed="]?0|e["fixed="]:null;return 0>n?r:"cyclic"===i?t.N(n):"multiset"===i?u(n,e.multiplicity):"derangement"===i?s?2>n-s?r:t.mul(u(n,s),u(n-s,!1)):2>n?r:u(n,!1):"involution"===i?u(n,!0):"connected"===i?u(n-1):o?l(n,o,1):u(n)},initial:function(n,e,t){var r,u=this,l=e&&e.type?e.type:"permutation",i=e&&null!=-e.order?e.order:Vn,o=e&&null!=e["cycles="]?0|e["cycles="]:null,s=e&&null!=e["fixed="]?0|e["fixed="]:null;if(0===n)return[];if(t=-1===t?-1:1,(!(Gn&i)&&Xn&i||Gn&i&&!(Xn&i))&&(t=-t),"cyclic"===l)r=0>t?[n-1].concat(a(n-1,0,1)):a(n,0,1);else if("derangement"===l){if(s||2>n)return null;if(1&n){var f=xn.floor(n/2);r=0>t?a(n-f-1,n-1,-1).concat([f-1,f]).concat(a(f-1,f-2,-1)):a(n-3,function(n){return 1&n?n-1:n+1}).concat([n-2,n-1,n-3])}else r=0>t?a(n,n-1,-1):a(n,function(n){return 1&n?n-1:n+1})}else r="multiset"===l?0>t?e.multiset.slice().reverse():e.multiset.slice():"connected"===l?null:"involution"===l?0>t?a(n,n-1,-1):a(n,0,1):o?null:0>t?a(n,n-1,-1):a(n,0,1);return r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,l){if(!t||0>=t||null==n)return null;var i=r&&r.type?r.type:"permutation",o=r&&null!=r["cycles="]?0|r["cycles="]:null,s=r&&null!=r["fixed="]?0|r["fixed="]:null;return"derangement"===i&&s||"permutation"===i&&o?null:(u=-1===u?-1:1,cn(n,t,u,i,r&&null!=r.order?r.order:Vn,r&&null!=r.base?r.base:null))},rand:function(n,e){var t,r=yn.Math.rndInt,u=this,l=e&&e.type?e.type:"permutation",i=e&&null!=e["cycles="]?0|e["cycles="]:null,o=e&&null!=e["fixed="]?0|e["fixed="]:null;if(0===n)return[];if("cyclic"===l){var s=r(0,n-1);t=0v&&(p=O[h=r(0,x-1)],--x,g=O[h],O[h]=O[x],O[x]=g,g=t[p],t[p]=t[m],t[m]=g)}else if("multiset"===l)t=w(e.multiset.slice());else if("connected"===l)t=w(a(n,0,1),!0);else{if(i)return null;t=w(a(n,0,1))}return t=u.DUAL(t,n,e)},randu:gn.rand,rank:function(n,t,r){var u,l,i,o,s,a=this,f=yn.Arithmetic,c=r&&r.type?r.type:"permutation",d=r&&null!=r["cycles="]?0|r["cycles="]:null,m=(r&&null!=r["fixed="]&&r["fixed="],r&&null!=-r.order?r.order:Vn),h=f.sub,p=f.add,_=f.mul,g=f.div,b=f.O,v=f.I,y=f.J;if(!(t=t||n.length))return b;if(n=a.DUAL(n,t,r),"cyclic"===c)b=f.NUM(n[0]);else{if("derangement"===c||"involution"===c||"connected"===c)return e();if("multiset"===c)for(s=r.multiplicity.slice(),o=r&&null!=r.count?r.count:yn.Math.factorial(t,s),i=t-1,u=0;u=0;i--)f=b(l,s=t-i),l=v(l,s),u[i]=A(f);en(u,u)}}return u=m.DUAL(u,t,r)},fusion:gn.fusion,output:gn.output,permute:on,shuffle:function(n,e){if("cyclic"===e){var t=n.length,r=yn.Math.rndInt(0,t-1);return 0(r=n[t])||r>=e||0e)return!1;return!0!==t||r===e},is_involution:function(n){for(var e=n.length,t=0,r=n[t];tr||e<=r||n[r]!==t)return!1;return!0},is_kthroot:function(n,e){if(1>(e=e||1))return!1;var t,r,u,l=n.length;for(t=0;t=e||0t&&(t=u),t<=r)return!1;return!0},is_kcycle:function(n,e,t,r){if(!n.length||0>=e)return!1;r=!1!==r;var u,l,i,o,s,a=n.length;for(u=0,i=0,s=0,o=new Array(a);s="===t?i>=e:i===e}},_update:function(){var n=this;return n.item__=(n.__item,n.n,n.$.order,n.$.type,null),n}}),bn=yn.Combination=Dn(gn,{constructor:function n(e,r,u){var l=this,i=null;if(!(l instanceof n))return new n(e,r,u);if(t(e)?(u=r||{},r=e[1]||0,e=e[0]||0):(u=u||{},e=e||0,r=r||0),u.type=String(u.type||"unordered").toLowerCase(),-1t?a(n[1],n[0]-1,0):a(n[1],0,0):"ordered"===l?0>t?a(n[1],n[0]-1,-1):a(n[1],0,1):0>t?a(n[1],n[0]-n[1],1):a(n[1],0,1),r=u.DUAL(r,n,e))},succ:function(n,e,t,r,u,l){return!t||!t[0]||0>=t[0]||0===t[1]||null==n?null:(u=-1===u?-1:1,mn(n,t,u,r&&r.type?r.type:"unordered",r&&null!=r.order?r.order:Vn,l))},rand:function(n,e){var t,r,u,l,i=this,o=e&&e.type?e.type:"unordered",s=n[1],f=yn.Math.rndInt;return 0===s?[]:(n=n[0],r=n-s,u=n-1,"repeated"===o||"ordered+repeated"===o?(t=1===s?[f(0,u)]:a(s,function(){return f(0,u)}),1=0;s--)o=g(i,e),i=_(i,e),r[s]=x(o);else if("ordered"===w){for(i=n,s=A-1;s>=0;s--)o=g(i,l=e-s),i=_(i,l),r[s]=x(o);en(r,r,l)}else{l=(f="repeated"===w)?e+A-1:e,n=p(p(u=t&&t.count?t.count:yn.Math.factorial(l,A),h),n),u=_(b(u,l-A),l),o=l-A+1,i=A,s=l-1;do{v(u,n)?(r[A-i]=f?l-o-A+1:l-o-i+1,y(u,m)&&(n=p(n,u),u=_(b(u,i),s)),i--,s--):(u=_(b(u,s-i),s),o--,s--)}while(i>0)}return r=c.DUAL(r,e,t)},fusion:gn.fusion,output:gn.output,complement:function(n,e,t){return!0===t?w(j(e,n,!0)):j(e,n)},binary:function(n,e,t){return-1===t?J(n,e):X(n,e)},pick:function(n,e,t){return 0t&&s(function(n,e){r[e]=e},null,null,0,n,1):0>t&&(r[0]=n-1,r[n]=1),r=u.DUAL(r,n,e,1)},succ:function(n,e,t,r,u,l){return null==n?null:r&&"binary"===r.type?gn.succ.call(this,n,e,t,r,u):hn(n,t,-1===u?-1:1,r&&null!=r.order?r.order:Vn)},rand:function(n,e){for(var t,r=this,u=yn.Math.rndInt,l=null,i=n-1;i>=0;i--)u(0,1)&&(l={len:l?l.len+1:1,k:i,next:l});return t=l?a(l.len,function(n){var e=l.k;return l=l.next,e}):[],t=t.concat(t.length=n||c&&f&&(c+f>n+1||c*fn||f&&f>n?null:(t=-1===t?-1:1,(!(Gn&m)&&Xn&m||Gn&m&&!(Xn&m))&&(t=-t),r=new Array(h+1),r[h]=0,c&&f?(r[h]=c,1===c&&(r[0]=f),p?(r=s(function(n,e,t){return n[t]=e,n},r,[(n-f)%(l=xn.min(f,xn.ceil((n-f)/(c-1))))||l].concat(a(c-2,l,0)).concat([f])),0>t&&d(r,r,c,0,c-1)):0>t?(c-=u=xn.min(c,xn.floor(n/f)||1),0===(n-=u*f)&&0t&&d(r,r,c,0,c-1)):(u=n%(l=xn.ceil(n/c))||l,r=s(function(n,e,t){return n[t]=e,n},r,0>t?[n-c+1].concat(a(c-1,1,0)):a(c-1,l,0).concat([u])))):f?(u=xn.floor(n/f),l=n%f,r=p?s(function(n,e,t){return n[t]=e,n[h]++,n},r,0>t?a(u,f,0).concat(l?[l]:[]):a(n-f,1,0).concat([f])):s(function(n,e,t){return n[t]=e,n[h]++,n},r,0>t?a(u,f,0).concat(l?[l]:[]):[f].concat(a(n-f,1,0)))):r=s(function(n,e,t){return n[t]=e,n[h]++,n},r,0>t?[n]:a(n,1,0)),r=i.DUAL(r,n,e,1))},succ:function(n,e,t,r,u,l){if(null==t||null==n)return null;var i=r&&r.type?r.type:"partition",o=r&&r["max="]?0|r["max="]:null,s=r&&r["parts="]?0|r["parts="]:null;return 0>=t||s&&o&&(s+o>t+1||s*ot||o&&o>t?null:(u=-1===u?-1:1,"composition"===i?_n(n,t,u,s,o,r&&null!=r.order?r.order:Vn):pn(n,t,u,s,o,r&&null!=r.order?r.order:Vn))},rand:function(n,e){var t,r,u,l,i,o,s=this,f=yn.Math.rndInt,c=e&&e.type?e.type:"partition",d=(e&&null!=e.order&&e.order,e&&e["max="]?0|e["max="]:null),m=e&&e["parts="]?0|e["parts="]:null,h=0,p=0,_=m||(d?n-d+1:n),g="composition"===c,b=!1;if(0>=n||m&&d&&(m+d>n+1||m*dn||d&&d>n)return null;if(m&&d)return null;if(d&&!m&&(m=d,b=!0),m)1===m?(r=[n],p=n):n===m?(r=a(m,1,0),p=n):(t={},u=n-2,a(r=x(a(m-1,function(){for(i=f(0,u);1===t[i];)i=(i+1)%(u+1);return t[i]=1,i+1})),function(n){return i=r[n],l=i-h,h=i,p+=l,l}));else{for(t=null,i=1;ii?-1:1),0===u||b&&!e.length||0>=s.floor((i-r)/u)+1)return _;if(0>u?((p=(r-i)%-u)&&(g=i),d=i+=p,m=r,c=-1,f=-(-u<<4)):((p=(i-r)%u)&&(g=i),d=r,m=i-=p,c=1,f=u<<4),h=r+u*(15&s.floor((i-r)/u+1)),b){for(d=s.max(0,d),m=s.min(e.length-1,m),o=r;d<=o&&o<=m&&00?new Array(n):[];if(0<(n=r.length)&&null!=t){e=e||0;var i=t;T(P(t)?function(n,e,r){return n[r]=t(r),n}:t===+t?function(n,t,r){return n[r]=i,i+=e,n}:function(n,e,r){return n[r]=t,n},r,r)}return r}function X(n,t,e){return T(function(n,t,r){return n[r]=t[e],n},n,t)}function J(n,t,e,r,i){return null==t?n:T(N(e)?function(n,t,r){return n[r]=e[r]-1-t,n}:function(n,t,r){return n[r]=e-1-t,n},n,t,r,i)}function V(n,t,e,r,i){if(null==t)return n;if(null==r&&(r=0),null==i&&(i=t.length-1),n!==t||r0?(t+u)%e[r]:0,u+=e[r]-n[r],n}:function(n,t,r){return n[r]=(t+u)%e,u+=e-n[r],n},n,t,r,i)}function H(n,t,e,r,i){return null==t?n:(null==i&&(i=t.length-1),null==r&&(r=0),n!==t||0!==e?T(function(n,t,r){return n[r+e]=t,n},n,t,0>e?r:i,0>e?i:r,0>e?1:-1):n)}function W(n,t,e,r,i,u,l,s){if(null==t)return null;null==e&&(e=1),null==r&&(r=0),null==i&&(i=0),null==u&&(u=t.length-1),null==l&&(l=i),null==s&&(s=u);var o=0,a=l>s?-1:1,f=l;return T(function(n,t,i){return t=r+e*t,n[f]=t-o,o=t,f+=a,n},n,t,i,u)}function B(n,t,e,r,i,u,l,s){if(null==t)return null;null==e&&(e=1),null==r&&(r=0),null==i&&(i=0),null==u&&(u=t.length-1),null==l&&(l=i),null==s&&(s=u);var o=0,a=l>s?-1:1,f=l;return T(function(n,t,i){return o+=t,n[f]=r+e*o,f+=a,n},n,t,i,u)}function Y(n,t,e,r,i,u,l,s,o){o=!0===o,r=-1===r?-1:1,null==i&&(i=0),null==u&&(u=t===+t?t-1:t.length-1),null==l&&(l=0),null==s&&(s=e?e.length-1:-1);var a=i>u?-1:1,f=l>s?-1:1,c=a*(u-i)+1,h=i,d=l,m=0;if(!e||!e.length)return t===+t?j(t,i,a):t?t.slice():t;if(null==n&&(n=new Array(o?2*c:c)),t===+t){for(;0<=a*(u-h)&&0<=f*(s-d);)h===e[d]?(o&&(n[m++]=h),h+=a,d+=f):1===r&&h>e[d]||-1===r&&he[d]||-1===r&&t[h]u?-1:1,h=l>s?-1:1,d=c*(u-i)+1+(h*(s-l)+1),m=i,p=l,_=0,g=null,b=!a;if(null==n&&(n=new Array(d)),0===n.length)return f?t:n;for(;0<=c*(u-m)&&0<=h*(s-p);){if(a&&_){if(t[m]===g){m+=c;continue}if(e[p]===g){p+=h;continue}}o?1===r&&t[m][0]e[p][0]?(n[_++]=g=t[m],m+=c):1===r&&t[m][0]>e[p][0]||-1===r&&t[m][0]e[p][1]?(n[_++]=g=t[m],b&&(n[_++]=e[p])):(n[_++]=g=e[p],b&&(n[_++]=t[m])),m+=c,p+=h):1===r&&t[m]e[p]?(n[_++]=g=t[m],m+=c):1===r&&t[m]>e[p]||-1===r&&t[m]c?u:i,_=0;_l){if(1===f){a=s-1;break}0===f&&(f=-1)}u=l}if(0===f&&(f=1),-1===a)a=e,r[0]=o,r[1]=a,r[2]=f;else{for(c=a+1,h=-1,u=i?n[c][0]:n[c],s=c+1;s<=e;s++){if(u<(l=i?n[s][0]:n[s])){if(-1===d){h=s-1;break}0===d&&(d=1)}else if(u>l){if(1===d){h=s-1;break}0===d&&(d=-1)}u=l}-1===h&&(h=e),0===d&&(d=1),r[0]=o,r[1]=a,r[2]=f,r[3]=c,r[4]=h,r[5]=d}}function Q(n,t,e,r,i,u){null==i&&(i=0),null==u&&(u=n.length-1);var l=i>u?-1:1,o=l*(u-i)+1;if(r=!0===r,1>=o)return r?1===o?[i]:[]:n;t=-1===t?-1:1;var a,f,c,h,d,m=o,p=1,_=2,g=s.min,b=new Array(o);if(r&&(n=T(function(n,t,e){return n[e-i]=[t,e],n},new Array(o),n,i,u,1),i=0,u=o-1),!0===e){h=i,d=-1,a=[-1,-1,0,-1,-1,0];do{K(n,i,u,a,r),-1===a[3]?(t!==a[2]&&i>=1;return r?X(n,n,1):n}function Z(n,t,e,r){var i,u,l,s=n.length;if(null==e&&(e=0),null==r&&(r=s-1),null==t||0===t){for(t=0,u=n[e],i=e+1;i<=r;i++){if(u<(l=n[i])){if(-1===t)return 0;0===t&&(t=1)}else if(u>l){if(1===t)return 0;0===t&&(t=-1)}u=l}return 0===t?1:t}if(t=-1===t?-1:1,e>=r)return t;if(-1===t)for(u=n[e],i=e+1;i<=r;i++){if(u<(l=n[i]))return 0;u=l}else for(u=n[e],i=e+1;i<=r;i++){if(u>(l=n[i]))return 0;u=l}return t}function nn(n,t,e,r){var i,l=u.Math.rndInt,s=!0===t?1:0;return N(e)?1<(i=e.length)&&T(function(n){if(s=0;f--)c=i[f],h=n[l+m],n[l+m]=n[l+c],n[l+c]=h,m++;return e&&Q(a),a}function en(n,t){return n+t}function rn(n,t,e,r){return T(u.Arithmetic.add,u.Arithmetic.O,n,t,e,r)}function un(n){var t=u.Arithmetic;return t.shl(t.I,t.N(n))}function ln(n,t){var e,r=u.Arithmetic,i=r.O,l=r.I,o=r.J,a=r.N,f=r.add,c=(r.sub,r.div),h=r.mul;return null==t?10>=n?0>n?i:0===n?l:a([1,2,6,24,120,720,5040,40320,362880,3628800][n-1]):(e=String(n),null==ln.mem1[e]&&(ln.mem1[e]=h(ln(n-1),n)),ln.mem1[e]):!1===t?10>=n?2>n?i:a([1,2,9,44,265,1854,14833,133496,1334961][n-2]):(e="!"+String(n),null==ln.mem2[e]&&(ln.mem2[e]=f(1&n?o:l,h(ln(n-1,!1),n))),ln.mem2[e]):!0===t?10>=n?0>n?i:0===n?l:a([1,2,4,10,26,76,232,764,2620,9496][n-1]):(e="I"+String(n),null==ln.mem2[e]&&(ln.mem2[e]=f(ln(n-1,!0),h(ln(n-2,!0),n-1))),ln.mem2[e]):N(t)?t.length?0>n?i:(e=String(n)+"@"+Q(t.slice(),1,!0).join(","),null==ln.mem3[e]&&(ln.mem3[e]=c(ln(n),T(function(n,t){return h(n,ln(t))},ln(t[t.length-1]),t,t.length-2,0))),ln.mem3[e]):0>n?i:ln(n):t===+t?0>t?-t>=n?-t===n?ln(n):i:(e=String(n)+"@"+String(t),null==ln.mem3[e]&&(ln.mem3[e]=T(h,l,null,n+t+1,n)),ln.mem3[e]):0>t||1>n||t>n?i:(t+t>n&&(t=n-t),0===t||1===n?l:1===t?a(n):(e=String(n)+"@"+String(t),null==ln.mem3[e]&&(ln.mem3[e]=r.isDefault()?s.round(T(function(t,e){return t*(1+n/e)},1+(n-=t),null,2,t)):f(ln(n-1,t-1),ln(n-1,t))),ln.mem3[e])):i}function sn(n,t,e){var r,i=u.Arithmetic,l=i.O,s=i.I,o=i.V,a=i.add,f=i.mul;return 0>n||0>t?l:2===e?n===t||1===t&&0>n?s:0===n||0===t?l:(r=String(n)+","+String(t),null==sn.mem2[r]&&(sn.mem2[r]=a(sn(n-1,t-1,2),f(sn(n-1,t,2),t))),sn.mem2[r]):-1===e?t>n||0===t&&0>n?l:n===t?s:(r="-"+String(n)+","+String(t),null==sn.mem1[r]&&(sn.mem1[r]=a(sn(n-1,t-1,-1),f(sn(n-1,t,-1),1-n))),sn.mem1[r]):t>n||0===t&&0>n?l:n===t?s:1===t?ln(n-1):n-t&1?o(sn(n,t,-1)):sn(n,t,-1)}function on(n,t,e,r){var i,l,o=u.Arithmetic,a=o.add,f=o.I,c=o.O;return 0>n||0>=t||e>r||e+t>n+1||t*rn||t&&e&&(t+e>n+1||t*en||t&&t>n?c:(e&&!t&&(s=0,o=1,a=e,f=e,t=e,e=null),r=String(n)+"|"+String(t)+"|"+String(e),null==an.mem[r]&&(an.mem[r]=T(function(t,e){return T(function(t,r){return l(t,on(n,e,1,r))},t,null,o,s||n-e+1,1)},c,null,a,f,1)),an.mem[r])}function fn(n,t,e,r){var i,l=u.Arithmetic,o=l.add,a=l.O,f=l.I;return 0>n||0>=t||e>r||e*t>n||t*rn||t&&e&&(t+e>n+1||t*en||t&&t>n?f:(r=String(n)+"|"+String(t)+"|"+String(e),null==cn.mem[r]&&(cn.mem[r]=t&&e?t*e===n?o:fn(n,t,1,e):t?fn(n,t,1,n):e?n===e?o:T(function(t,r){return a(t,fn(n,r,1,e))},f,null,s.ceil(n/e),n-e+1,1):1<=n?un(n-1):o),cn.mem[r])}function hn(n){var t,e=u.Arithmetic,r=e.O,i=e.N,l=e.div,s=e.mul;return 14>=n?0>n?r:i([1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900][n]):(t=String(n),null==hn.mem[t]&&(hn.mem[t]=l(s(hn(n-1),4*n-2),n+1)),hn.mem[t])}function dn(n){var t,e=u.Arithmetic,r=e.O,i=e.N,l=e.add,s=e.mul;return 12>=n?0>n?r:i([1,1,2,5,15,52,203,877,4140,21147,115975,678570][n]):(t=String(n),null==dn.mem[t]&&(dn.mem[t]=T(function(t,e){return l(t,s(ln(n-1,e),dn(e)))},r,null,0,n-1,1)),dn.mem[t])}function mn(n){var t,e=u.Arithmetic,r=e.O,i=e.N,l=e.add;return 29>=n?0>n?r:i([0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811][n]):(t=String(n),null==mn.mem[t]&&(mn.mem[t]=l(mn(n-1),mn(n-2))),mn.mem[t])}function pn(n){if(!arguments.length||null==n)return L;var t=0;if(U(n)){for(var e=0,r=(n=n.toUpperCase().split(",")).length;e=t&&(t=L)}else t=D&n?D&n:L;return k&t&&$&t&&(t&=~$),E&t&&(C|L)&t&&(t&=~(C|L)),C&t&&L&t&&(t&=~L),k&t&&(t&=~S),t}function _n(){var n,t,e,r,i,u,l,s,o,a,f,c=arguments.length;if(!c)return[];if(!0===arguments[0]){for(a=arguments[1].length,n=2;n=0;t--)if(i=e%(r=arguments[t].length),e=~~(e/r),N(l=arguments[t][i]))for(u=l.length-1;u>=0;u--)s[c-++o]=l[u];else s[c-++o]=l;f[n]=s}}return f}function gn(n,t,e){var r,i,u,l,s,o,a,f,c,h,d,m,p,_,g,b,y,v,S,q,L,C,E,k,$=n.length,D=null,R=null;if(!$)return[];for(P(e)?(R=e,e=!0):e=!1,x!==t&&A!==t&&w!==t&&O!==t&&M!==t&&I!==t&&(t=!1),y=new Array($),b=[],i=1,r=0;r<$;r++)if(P(n[r][0]))null==y[n[r][1]]?y[n[r][1]]=[[n[r][0],r]]:y[n[r][1]].push([n[r][0],r]);else if(b.push(r),!(i*=n[r].length)||0>=i)return[];if(!b.length)return[];for(_=new Array(i),p=0,m=$-1,v=b.length-1,q=new Array($),L=0,r=0;r=0;u--)if(o=l%(s=n[g=b[u]].length),l=~~(l/s),h[g]=n[g][o],null!=y[g]){q[0]=g,L=1;do{for(C=y[g=q[--L]],a=h[g],E=0,k=C.length;E=0;d--){if(f=h[d],x===t&&c!==f||A===t&&1===D[f]||w===t&&f>=c||O===t&&f<=c||M===t&&fc||e&&!R(h,d,m)){S=!0;break}A===t&&(D[f]=1),c=f}S||(_[p++]=h)}return _.length>p&&(_.length=p),_}function bn(n,t,e){if(null==t||!t.length||1>=t.length)return 1===t.length?j(n-1,function(n){return n=t)return[];var e,r=j(t,0,0),i=n.length;for(t-=1,e=0;e=(r=T(en,0,t))?i=t.slice():(0<(r-=T(en,0,i=An(bn(r-1,xn(t,u-1,e)))))&&i.push(r),0>e&&V(i,i));else{var l,s,o,a,f,c=1,h=0,d=0,m="push";if(0>e&&(c=-c,h=u-1-h,m="unshift"),N(t[h]))for(i=[[(f=t[h])[1],f[0]]],l=0,o=1,a=c+h;oe?0:l,(f=t[a])[1]===i[s][0]?i[s][1]+=f[0]:(i[m]([f[1],f[0]]),l++);else for(i=j(r=t[h],1,0),0>e&&(d=r-1-d),o=1,a=c+h;o0;)i[s]++,f--,l++,s+=c}return i}function On(n,t,e){var r=n?n.length:0,i=1,u=0,l=0,s=l=s&&(u+=i,s=++l>>1<<1]),l[r]+=1,r>>>=1;return l[r]+=1,n[e]=t,n},n||new Array(r),t)}function Sn(n,t,e){var r,i,u,l=t.length,o=s.ceil(d(e||l)),a=1<=f[r<<=1]&&(t-=f[r],r++);return f[r]=0,n[e]=r-a,n},n||new Array(l),t)}function qn(n,t,e){var r,i=n.length,u=null==t;if(i>1)for(u&&(t=new Array(i-1),e=0),r=i-1;r>=1;r--)t[e++]=[n[0],n[r]];else u&&(t=[]);return u?t:e}function Ln(n,t){var e,r,i,u,l=n.length,s=new Array(l),o=!0===t?1:0,a=new Array(l),f=0,c=0;for(e=0;eo&&(i.length=u,s[c++]=i),i=new Array(l),u=0;fo&&(i.length=u,s[c++]=i),ci?function(e,r){return e[r]=r=t},lte:function(n,t){return n<=t},gt:function(n,t){return n>t},lt:function(n,t){return n=t&&n<=e:n>t&&ne?e:n},wrap:function(n,t,e){return ne?t:n},wrapR:function(n,t){return n<0?n+t:n},add:en,sub:function(n,t){return n-t},mul:function(n,t){return n*t},div:function(n,t){return u.Math.floor(n/t)},mod:function(n,t){return n%t},pow:s.pow,shl:function(n,t){return n<>t},bor:function(n,t){return n|t},band:function(n,t){return n&t},xor:function(n,t){return n^t},abs:s.abs,min:s.min,max:s.max,floor:s.floor,ceil:s.ceil,round:s.round,num:function(n){return"number"==typeof n?u.Math.floor(n):parseInt(n,10)},val:function(n){return u.Math.floor(n.valueOf())},sum:rn,product:function(n,t,e,r){return T(u.Arithmetic.mul,u.Arithmetic.I,n,t,e,r)},pow2:un,exp:function(n,t){var e=u.Arithmetic,r=e.N;return e.pow(r(n),r(t))},factorial:ln,stirling:sn,partitions:an,compositions:cn,bell:dn,catalan:hn,fibonacci:mn},u.Arithmetic={isDefault:function(){return 0===u.Arithmetic.O&&u.Arithmetic.add===en},O:0,I:1,J:-1,N:u.Math.N,V:u.Math.V,equ:u.Math.equ,gte:u.Math.gte,lte:u.Math.lte,gt:u.Math.gt,lt:u.Math.lt,inside:u.Math.inside,clamp:u.Math.clamp,wrap:u.Math.wrap,wrapR:u.Math.wrapR,add:u.Math.add,sub:u.Math.sub,mul:u.Math.mul,div:u.Math.div,mod:u.Math.mod,pow:u.Math.pow,shl:u.Math.shl,shr:u.Math.shr,bor:u.Math.bor,band:u.Math.band,xor:u.Math.xor,abs:u.Math.abs,min:u.Math.min,max:u.Math.max,floor:u.Math.floor,ceil:u.Math.ceil,round:u.Math.round,rnd:u.Math.rndInt,num:u.Math.num,val:u.Math.val},u.Util={array:j,operate:T,intersection:function(n,t,e,r,i,u,l,o){r=-1===r?-1:1,null==i&&(i=0),null==u&&(u=t.length-1),null==l&&(l=0),null==o&&(o=e.length-1);var a=i>u?-1:1,f=l>o?-1:1,c=a*(u-i)+1,h=f*(o-l)+1,d=i,m=l,p=0;if(null==n&&(n=new Array(s.min(c,h))),0===n.length)return n;for(;0<=a*(u-d)&&0<=f*(o-m);)1===r&&t[d]e[m]?d+=a:1===r&&t[d]>e[m]||-1===r&&t[d]u?-1:1,a=l>s?-1:1,f=o*(u-i)+1,c=i,h=l,d=0;if(!r||!r.length)return e?e.slice():e;for(null==n&&(n=new Array(f));0<=o*(u-c)&&0<=a*(s-h);)e[c]===r[h]?1r[h]?h+=a:(n[d++]=e[c],t[e[c]]--,c+=o);for(;0<=o*(u-c);)0>>1)]))return u;1===e&&nl?o=u-1:s=u+1}return-1},complementation:J,reflection:V,reversion:G,gray:F,finitedifference:W,partialsum:B,sort:Q,shuffle:nn,pick:tn,pluck:X},u.BitArray=v({constructor:function n(t){if(!(this instanceof n))return new n(t);this.length=t,this.bits=new Uint32Array(s.ceil(t/32))},length:0,bits:null,dispose:function(){return this.length=null,this.bits=null,this},clone:function(){var n=new u.BitArray(this.length);return n.bits=new Uint32Array(this.bits),n},fromArray:function(n){return this.bits=new Uint32Array(n),this},toArray:function(){return f.call(this.bits)},toString:function(){var n,t,e,r,i,u=this.toArray();for(n=0,t=u.length;n0?new Array(i+1).join("0")+r:r);return u.join("")},reset:function(){var n,t=this.bits,e=t.length;for(n=0;n>>5]&1<<(31&n))},set:function(n){return this.bits[n>>>5]|=1<<(31&n),this},unset:function(n){return this.bits[n>>>5]&=~(1<<(31&n)),this},toggle:function(n){return this.bits[n>>>5]^=1<<(31&n),this}}),t=u.CombinatorialIterator=v({constructor:function n(t,e,r,i){u.Arithmetic;if(!(this instanceof n))return new n(t,e,r,i);if(this[a],N(t)&&(t[0]instanceof n||t[t.length-1]instanceof n)){this.$=r=e||{},r.seq=t,t=null,this.n=e=r.seq.length,r.type="sequence",r.rand=r.rand||{},r.rand.sequence=1;var l=1/0,s=-1/0,o=1/0,f=-1/0;T(function(n,t){var e=t.base(),r=t.dimension();e>s&&(s=e),ef&&(f=r),r(e=-1===e?-1:1)||S&(t&&null!=t.order?t.order:L)?t.seq[t.seq.length-1].item0(e):t.seq[0].item0(e):null},succ:function(n,t,e,r,i,l){if(null==e||null==n)return null;var s,o,a,f,c,h=u.Arithmetic;if(i=-1===i?-1:1,r&&"sequence"===r.type){if(!(c=r.seq)||!c.length)return null;for(S&(r&&null!=r.order?r.order:L)?(s=-1,o=c.length-1):(s=1,o=0),f=s*r.seq_curr+o,a=s*i;0<=f&&fi?h.J:h.I),e,r)},rand:function(n,t){var e,r,i,l,s,o=u.Arithmetic,a=o.O;if(t&&"sequence"===t.type){if(!(i=t.seq)||!i.length)return null;for(e=null!=t.last?t.last:o.sub(this.count(n,t),o.I),r=o.rnd(a,e),l=0,s=i.length;o.gte(r,i[l].total())&&(r=o.sub(r,i[l].total()),!(++l>=s||o.lt(r,a))););return l=i||l.lt(n,s))););return ro&&(o=t[n]+1),t[n]):o+e[n-t.length]}:function(n){return nn?l.seq.length-1:0:-1),k&c?("gen"===u.Options.RANDOM||1===l.rand[l.type]||o.gt(e,u.Options.MAXMEM)||o.isDefault()&&0>e?(this.__item=i.rand(s,l),this.__index=f):(this._traversed&&this._traversed.dispose(),this._traversed=new u.BitArray(o.val(e)),t=this.random("index"),this._traversed.set(+t),this.__item=i.unrank(t,s,l),null!=this.__item&&(this.__index=t)),this._index=f):(this.__item=i.initial(s,l,n,!0),null!=this.__item&&(this.__index=0>n?r:f,this._update()),this._index=this.__index),this._item=i.output(this.__item,this.__index,s,l),this._prev=!(k&c||0n&&!(k&c))&&null!=this.__item,this},_update:function(){return this.item__=null,this},order:function(n,t){if(!arguments.length)return this._order;var e,r,i,l,s,o,f=this[a],c=u.Arithmetic,h=(c.O,c.I,!0===n);if(t=-1===t,this.n,i=this.$,this._traversed&&(this._traversed.dispose(),this._traversed=null),h?n=i.order:U(n)&&-1<(r=n.indexOf("|"))?(e=n.substr(r+1),n=pn(n.substr(0,r))):e=n=pn(n),l=t?-1:1,i.order=n,i.sub?(h?i.sub.rewind(l):i.sub.order(e,l),this.__subindex=i.sub.index(),this.__subitem=i.sub.next(l),this._subindex=null,this._subitem=null):(this.__subindex=null,this.__subitem=null,this._subindex=null,this._subitem=null),"sequence"===i.type&&i.seq&&i.seq.length)for(s=0,o=i.seq.length;s(n=-1===n?-1:1)&&k&b)return null;if(t=0>n?p:m,k&b)if(s=g.last,h.lt(this._index,s))if(e=this._traversed){for(r=this.random("index"),o=u.Math.rnd()>.5?p:m;e.isset(+r);)r=h.wrap(h.add(r,o),d,s);e.set(+r),this.__item=c.unrank(r,_,g),null!=this.__item&&(this.__index=r)}else this.__item=c.rand(_,g),this.__index=null;else this._item=this.__item=null,this._traversed&&(this._traversed.dispose(),this._traversed=null);else this.__item=c.succ(this.__item,this.__index,_,g,n,this.item__),null!=this.__item&&(this.__index=h.add(this.__index,t));if(f=null!=this.__item)this._index=h.add(this._index,t),null===this.__index&&(this.__index=this._index),0>n?(this._prev=f,this._next=x):(this._prev=x,this._next=f);else if(g.sub&&g.sub.hasNext(n)){if(this.__subindex=g.sub.index(),this.__subitem=g.sub.next(n),"sequence"===g.type&&g.seq&&g.seq.length)for(i=0,l=g.seq.length;in?(this._prev=f,this._next=x):(this._prev=x,this._next=f);return this._item=c.output(this.__item,this.__index,_,g),g.sub&&(f=f&&null!=this.__subitem,this._subindex=f?h.add(h.mul(this.__subindex,y),this._index):null,this._subitem=f?c.fusion(g.submethod,this._item,this.__subitem,this.dimension(),this.base(),g.subpos,g.subcascade):null,0>n?this._prev=f:this._next=f),v},range:function(n,t){var e,r,i,l,s=this,o=u.Arithmetic,a=o.N,f=o.O,c=o.I,h=s.$,d=h.sub?h.subcount:h.count,m=h.sub?o.sub(d,c):h.last,p=1,_=arguments.length,g=!(k&h.order);return _<1?(n=f,t=m):_<2?(n=a(n),t=m):(n=a(n),t=a(t)),n=o.wrapR(n,d),t=o.wrapR(t,d),o.gt(n,t)&&(e=n,n=t,t=e,p=-1),n=o.clamp(n,f,m),g&&(t=o.clamp(t,f,m)),o.lte(n,t)?(l=[s.$.order,s.__index,s._index,s.__item&&s.__item.slice(),s._item,s.__subindex,s._subindex,s.__subitem,s._subitem,s._prev,s._next],g&&s.index(n),i=o.val(o.sub(t,n)),r=T(function(n,t,e){return n[e]=s.next(),n},new Array(i+1),null,0>p?i:0,0>p?0:i,0>p?-1:1),s.$.order=l[0],s.__index=l[1],s._index=l[2],s.__item=l[3],s._item=l[4],s.__subindex=l[5],s._subindex=l[6],s.__subitem=l[7],s._subitem=l[8],s._prev=l[9],s._next=l[10],s._update()):r=[],r},__iter__:function(){return new t.Iterable(this)}}),"undefined"!=typeof Symbol&&void 0!==Symbol.iterator&&(t[o][Symbol.iterator]=t[o].__iter__),u.Tensor=v(t,{constructor:function n(){var e,r=null,i=f.call(arguments);if(e=!i.length||i[i.length-1]instanceof t||N(i[i.length-1])||i[i.length-1]===+i[i.length-1]?{}:i.pop()||{},i.length&&N(i[0])&&(i=i[0]),i&&i.length||(i=[]),!(this instanceof n))return new n(i,e);if(e.type=String(e.type||"tensor").toLowerCase(),e.order=e.order||L,e.rand=e.rand||{},"partial"===e.type){i=N(i)&&i.length?i[0]:i;var u=-1,l=e.data||[],s=e.position||null;i instanceof t?u=i=(r=i).base():r=e.sub,i=+(i||0)||0,l.length&&(U(l[0])||l[0].length&&(!0===l[0][0]||!1===l[0][0]))&&(u===i&&(i+=l.length,u=-1),l=n.generate(i,l,s,e.ordering||null)),u===i&&(i+=(l.length?l[0].length:0)||0,u=-1),e.data=l,e.position=s||j((l.length?l[0].length:0)||0,0,1),e.dimension=e.position.length,e.base=i,e.rand.partial=1}else if("tuple"===e.type)i[0]=i[0]||1,i[1]=i[1]||1,i[0]instanceof t?(r=i[0],i[0]=r.dimension()):i[1]instanceof t?(r=i[1],i[1]=r.base()):r=e.sub,e.base=i[1],e.dimension=i[0],"gray"===e.output&&(e.output=function(t,e){return n.gray(t,e[1])});else{var o=T(function(n,t){return tn[1]&&(n[1]=t),n},[1/0,0],i);if(e.base=i,e.minbase=o[0],e.maxbase=o[1],e.dimension=i.length,"gray"===e.output)e.output=function(t,e){return n.gray(t,e)};else if(N(e.output)){var a=e.output;e.output=function(t,e){return n.component(t,a)}}}t.call(this,"Tensor",i,e,r?{method:"partial"===e.type?e.submethod||"complete":e.submethod,iter:r,pos:"partial"===e.type?e.subpos||e.position:e.subpos,cascade:e.subcascade}:null)},__static__:{C:t.C,P:t.P,T:t.T,DUAL:t.DUAL,count:function(n,t){var e=u.Arithmetic.O,r=t&&t.type?t.type:"tensor";return"partial"===r?t.data&&t.data.length?u.Arithmetic.N(t.data.length):e:"tuple"===r?!n||0>=n[0]?e:u.Math.exp(n[1],n[0]):n&&n.length?u.Math.product(n):e},initial:function(n,t,e){var r,i=t&&t.type?t.type:"tensor",u=t&&t.order?t.order:L;return e=-1===e?-1:1,(!(C&u)&&S&u||C&u&&!(S&u))&&(e=-e),"partial"===i?r=t.data&&t.data.length?0>e?t.data[t.data.length-1]:t.data[0]:null:(r="tuple"===i?n[0]?j(n[0],0>e?n[1]-1:0,0):[]:n.length?0>e?j(n.length,function(t){return n[t]-1}):j(n.length,0,0):[],r=this.DUAL(r,n,t)),r},succ:function(n,t,e,r,i,l){if(!e||null==n)return null;var s=r&&r.type?r.type:"tensor",o=r&&null!=r.order?r.order:L;if(i=-1===i?-1:1,"partial"===s){if(!r.data||!r.data.length)return null;S&o&&(i=-i);var a=null==t?r.data.indexOf(n):u.Arithmetic.val(t);return 0>i?0<=a-1?r.data[a-1]:null:0<=a&&a+1=e[0]?null:function(n,t,e,r,i,u){var l,s,o,a,f,c,h,d,m,p=t;"tuple"===r?(l=p[0],p=p[1]):l=p.length;0,c=1,a=m=l-1,f=0,h=1,d=0,C&i&&(c=-c,a=m-a,f=m-f,h=-h,d=m-d);q&i&&(c=-c,a=m-a,f=m-f,h=-h,d=m-d);S&i&&(e=-e);if(0>e)if("tuple"===r){for(s=a;0<=s&&m>=s&&0===n[s];)s-=c;if(0<=s&&m>=s)for(p-=1,n[s]=n[s]-1,o=s+c;0<=o&&m>=o;o+=c)n[o]=p;else n=null}else{for(s=a;0<=s&&m>=s&&0===n[s];)s-=c;if(0<=s&&m>=s)for(n[s]=n[s]-1,o=s+c;0<=o&&m>=o;o+=c)n[o]=p[h*o+d]-1;else n=null}else if("tuple"===r){for(s=a;0<=s&&m>=s&&n[s]+1===p;)s-=c;if(0<=s&&m>=s)for(n[s]=n[s]+1,o=s+c;0<=o&&m>=o;o+=c)n[o]=0;else n=null}else{for(s=a;0<=s&&m>=s&&n[s]+1===p[h*s+d];)s-=c;if(0<=s&&m>=s)for(n[s]=n[s]+1,o=s+c;0<=o&&m>=o;o+=c)n[o]=0;else n=null}return n}(n,e,i,s,o)},rand:function(n,t){var e,r=u.Math.rndInt,i=t&&t.type?t.type:"tensor";return"partial"===i?e=t.data&&t.data.length?t.data[r(0,t.data.length-1)]:null:(e="tuple"===i?n[0]?j(n[0],function(t){return r(0,n[1]-1)}):[]:n.length?j(n.length,function(t){return r(0,n[t]-1)}):[],e=this.DUAL(e,n,t)),e},randu:t.rand,rank:function(n,t,e){var r,i,l=u.Arithmetic,s=e&&null!=e.order?e.order:L,o=e&&e.type?e.type:"tensor",a=l.add,f=l.sub,c=l.mul,h=l.O,d=l.J;if("partial"===o)h=l.N(e.data&&e.data.length?e.data.indexOf(n):-1);else if(n=this.DUAL(n,t,e),"tuple"===o){if(!(r=t[0]))return d;for(t=t[1],i=0;i=0;l--)s=m(r,i),r=p(r,i),o[l]=_(s)}else{if(!(a=t.length))return[];for(o=new Array(a),r=n,l=a-1;l>=0;l--)s=m(r,i=t[l]),r=p(r,i),o[l]=_(s)}o=this.DUAL(o,t,e)}return o},fusion:t.fusion,output:t.output,gray:function(n,t){return F(new Array(n.length),n,t)},product:_n,directsum:function(){var n,t,e=arguments,r=e.length,i=0;for(t=0;t=n+e[t].length&&(n+=e[t++].length),n+e[t][r-n]})},component:function(n,t){return null==n?null:null==t?n:j(n.length,function(e){return eu[1]?bn(n,j(u[0]-u[1]+1,u[1],1).filter(k)).reverse():bn(n,j(u[1]-u[0]+1,u[0],1).filter(k)):bn(n,u[1].split(",").map(Number).filter(k))).length){q=!0;break}t.push(o)}else if(u=l.match(_)){if(!(o=0u[1]?j(u[0]-u[1]+1,u[0],-1):j(u[1]-u[0]+1,u[0],1)).filter(k):u[1].split(",").map(Number).filter(k)).length){q=!0;break}t.push(o)}else{if(f=-1,c=null,h=null,l=l.replace(p,function(n,t){return null===c&&(f=parseInt(t,10),c="v"+String(f)),c}),!k(f)){e&&e.splice(a--,1);continue}try{h=new Function(c,"return Math.floor("+l+");")}catch(n){h=null}if(!P(h)){e&&e.splice(a--,1);continue}E[f]?E[f].push(h):E[f]=[h],0>e.indexOf(f)&&C.push(f),L.push([h,null,null,f,e[a]]),t.push(L[L.length-1])}else if(N(l)){if(!(o=!1===l[0]?bn(n,l.slice(1).filter(k)):l.slice(1).filter(k)).length){q=!0;break}t.push(o)}if(q&&(t=[]),C.length)for(s=0,S=C.length;st||t>b)return!1;return!0})).length){q=!0;break}t[u[1]]=o}return q?[]:(r="="===r?x:"!="===r||"<>"===r?A:"<"===r?w:"<="===r||"=<"===r?I:">"===r?O:(">="===r||"=>"===r)&&M,d=P(i.extra_conditions)?function(n,t,e){return m<=n[t]&&n[t]<=b&&i.extra_conditions(n,t,e)}:function(n,t,e){return m<=n[t]&&n[t]<=b},!0===i.lazy?t:gn(t,r,d))}}}),u.Permutation=v(t,{constructor:function n(e,r){var i=null;if(!(this instanceof n))return new n(e,r);(r=r||{}).type=String(r.type||"permutation").toLowerCase(),(e=e||0)instanceof t?e=(i=e).dimension():i=r.sub,r.base=r.dimension=e,r.rand=r.rand||{},r.rand.derangement=1,r.rand.involution=1,r.rand.connected=1,"multiset"===r.type&&(r.multiplicity=N(r.multiplicity)&&r.multiplicity.length?r.multiplicity.slice():j(e,1,0),r.multiplicity=r.multiplicity.concat(j(e-T(en,0,r.multiplicity),1,0)),r.base=r.multiplicity.length,r.multiset=On(r.multiplicity,e)),t.call(this,"Permutation",e,r,i?{method:r.submethod,iter:i,pos:r.subpos,cascade:r.subcascade}:null)},__static__:{C:t.C,P:t.P,T:t.T,DUAL:t.DUAL,count:function(n,t){var e=u.Arithmetic,r=e.O,i=u.Math.factorial,l=u.Math.stirling,s=t&&t.type?t.type:"permutation",o=t&&null!=t["cycles="]?0|t["cycles="]:null,a=t&&null!=t["fixed="]?0|t["fixed="]:null;return 0>n?r:"cyclic"===s?e.N(n):"multiset"===s?i(n,t.multiplicity):"derangement"===s?a?2>n-a?r:e.mul(i(n,a),i(n-a,!1)):2>n?r:i(n,!1):"involution"===s?i(n,!0):"connected"===s?i(n-1):o?l(n,o,1):i(n)},initial:function(n,t,e){var r,i=t&&t.type?t.type:"permutation",u=t&&null!=-t.order?t.order:L,l=t&&null!=t["cycles="]?0|t["cycles="]:null,o=t&&null!=t["fixed="]?0|t["fixed="]:null;if(0===n)return[];if(e=-1===e?-1:1,(!(C&u)&&S&u||C&u&&!(S&u))&&(e=-e),"cyclic"===i)r=0>e?[n-1].concat(j(n-1,0,1)):j(n,0,1);else if("derangement"===i){if(o||2>n)return null;if(1&n){var a=s.floor(n/2);r=0>e?j(n-a-1,n-1,-1).concat([a-1,a]).concat(j(a-1,a-2,-1)):j(n-3,function(n){return 1&n?n-1:n+1}).concat([n-2,n-1,n-3])}else r=0>e?j(n,n-1,-1):j(n,function(n){return 1&n?n-1:n+1})}else r="multiset"===i?0>e?t.multiset.slice().reverse():t.multiset.slice():"connected"===i?null:"involution"===i?0>e?j(n,n-1,-1):j(n,0,1):l?null:0>e?j(n,n-1,-1):j(n,0,1);return r=this.DUAL(r,n,t)},succ:function(n,t,e,r,i,u){if(!e||0>=e||null==n)return null;var l=r&&r.type?r.type:"permutation",s=r&&null!=r["cycles="]?0|r["cycles="]:null,o=r&&null!=r["fixed="]?0|r["fixed="]:null;return"derangement"===l&&o||"permutation"===l&&s?null:function(n,t,e,r,i,u,l){var s,o,a,f,c,h,d,m,p,_,g,b,y,v=t,x=null==u?v:u;0,m=1,d=y=v-1,p=1,_=0,g=1,b=0,C&i&&(m=-m,d=y-d,p=-p,_=x-1-_,e=-e);q&i&&(m=-m,d=y-d,g=-1,b=x-1);S&i&&(e=-e);if(0>e)if("cyclic"===r)if(p*n[s=y-d]+_>0)for(g=v-1,m=v+m,a=0;a0&&p*n[s]<=p*n[o];)o-=m;for(c=n[s],n[s]=n[o],n[o]=c,a=s+m,f=d;0<=a&&a<=y&&0<=f&&f<=y&&m*(f-a)>0;)c=n[a],n[a]=n[f],n[f]=c,h=h||g*a+b===n[a]||g*f+b===n[f],a+=m,f-=m;if("derangement"===r)for(0<=o&&o<=y&&(h=h||g*o+b===n[o]),0<=f&&f<=y&&(h=h||g*f+b===n[f]),h=h||g*s+b===n[s],a=s-m;!h&&0<=a&&a<=y;a-=m)h=g*a+b===n[a];else h=!1}else n=null}while(n&&h);else if("cyclic"===r)if(p*n[s=y-d]+_=p*n[s+m];)s-=m;if(0<=s&&s<=y){for(o=d;0<=o&&o<=y&&m*(o-s)>0&&p*n[s]>=p*n[o];)o-=m;for(c=n[s],n[s]=n[o],n[o]=c,a=s+m,f=d;0<=a&&a<=y&&0<=f&&f<=y&&m*(f-a)>0;)c=n[a],n[a]=n[f],n[f]=c,h=h||g*a+b===n[a]||g*f+b===n[f],a+=m,f-=m;if("derangement"===r)for(0<=o&&o<=y&&(h=h||g*o+b===n[o]),0<=f&&f<=y&&(h=h||g*f+b===n[f]),h=h||g*s+b===n[s],a=s-m;!h&&0<=a&&a<=y;a-=m)h=g*a+b===n[a];else h=!1}else n=null}while(n&&h);return n}(n,e,i=-1===i?-1:1,l,r&&null!=r.order?r.order:L,r&&null!=r.base?r.base:null)},rand:function(n,t){var e,r=u.Math.rndInt,i=t&&t.type?t.type:"permutation",l=t&&null!=t["cycles="]?0|t["cycles="]:null,s=t&&null!=t["fixed="]?0|t["fixed="]:null;if(0===n)return[];if("cyclic"===i){var o=r(0,n-1);e=0b&&(m=A[d=r(0,v-1)],--v,_=A[d],A[d]=A[v],A[v]=_,_=e[m],e[m]=e[h],e[h]=_)}else if("multiset"===i)e=nn(t.multiset.slice());else if("connected"===i)e=nn(j(n,0,1),!0);else{if(l)return null;e=nn(j(n,0,1))}return e=this.DUAL(e,n,t)},randu:t.rand,rank:function(n,t,e){var r,i,l,s,o,a=u.Arithmetic,f=e&&e.type?e.type:"permutation",c=e&&null!=e["cycles="]?0|e["cycles="]:null,h=(e&&null!=e["fixed="]&&e["fixed="],e&&null!=-e.order?e.order:L),d=a.sub,m=a.add,p=a.mul,_=a.div,g=a.O,b=a.I,y=a.J;if(!(t=t||n.length))return g;if(n=this.DUAL(n,t,e),"cyclic"===f)g=a.NUM(n[0]);else{if("derangement"===f||"involution"===f||"connected"===f)return R();if("multiset"===f)for(o=e.multiplicity.slice(),s=e&&null!=e.count?e.count:u.Math.factorial(t,o),l=t-1,r=0;r=0;l--)a=_(i,o=t-l),i=g(i,o),r[l]=v(a);Sn(r,r)}}return r=this.DUAL(r,t,e)},fusion:t.fusion,output:t.output,permute:Cn,shuffle:function(n,t){if("cyclic"===t){var e=n.length,r=u.Math.rndInt(0,e-1);return 0=t+r&&(t+=r,r=n[++e].length),t+n[e][i-t]})}(N(arguments[0])&&N(arguments[0][0])?arguments[0]:f.call(arguments)):null},cycles:function(n,t){return-1===t?cycles2permutation(n):Ln(n)},swaps:function(n,t){return-1===t?function(n,t){var e,r,i,u=n.length,l=new Array(t);for(e=0;e=l&&(i=0,r++);else for(r=0,i=0;r=l&&(i=0,r++);return n}(null,n,t):function(n,t,e){var r,i,u=t.length,l=u*u;for(n=n||new Array(l),r=0,i=0;r=u&&(i=0,r+=u);if(!0===e)for(r=0;r(r=n[e])||r>=t||0t)return!1;return!0!==e||r===t},is_involution:function(n){for(var t=n.length,e=0,r=n[e];er||t<=r||n[r]!==e)return!1;return!0},is_kthroot:function(n,t){if(1>(t=t||1))return!1;var e,r,i,u=n.length;for(e=0;e=t||0e&&(e=i),e<=r)return!1;return!0},is_kcycle:function(n,t,e,r){if(!n.length||0>=t)return!1;r=!1!==r;var i,u,l,s,o,a=n.length;for(i=0,l=0,o=0,s=new Array(a);o="===e?l>=t:l===t}},_update:function(){return this.item__=(this.__item,this.n,this.$.order,this.$.type,null),this}}),(e=u.Combination=v(t,{constructor:function n(e,r,i){var u=null;if(!(this instanceof n))return new n(e,r,i);if(N(e)?(i=r||{},r=e[1]||0,e=e[0]||0):(i=i||{},e=e||0,r=r||0),i.type=String(i.type||"unordered").toLowerCase(),-1e?n[0]-1:0,0):"ordered"===i?0>e?j(n[1],n[0]-1,-1):j(n[1],0,1):j(n[1],0>e?n[0]-n[1]:0,1),r=this.DUAL(r,n,t))},succ:function(n,t,e,r,i,u){return!e||!e[0]||0>=e[0]||0===e[1]||null==n?null:function(n,t,e,r,i,u){var l,s,o,a,f,c,h,d,m,p,_,g,b,y=t[1],v=t[0];0,c=1,f=h=y-1,d=1,m=0,p=1,_=0,C&i&&(d=-d,m=v-1-m,c=-c,f=h-f,p=-p,_=h-_);q&i&&(c=-c,f=h-f,p=-p,_=h-_);S&i&&(e=-e);if(0>e)if("ordered+repeated"===r){for(l=f;0<=l&&l<=h&&0===n[l];)l-=c;if(0<=l&&l<=h)for(v-=1,n[l]=n[l]-1,s=l+c;0<=s&&s<=h;s+=c)n[s]=v;else n=null}else if("ordered"===r){for(null==u&&(u=En(n,v,y,i,r)),l=f,o=-1;-1===o&&0<=l&&l<=h;){if(d*n[l]+m-d>=0)for(s=d*n[l]+m-d;0<=s&&s(c=-c)?0:h,!b&&n[s]+1>y||b&&n[s]>0){if(b)for(;0<=l&&l<=h&&0===n[l];)l+=c;else for(;0<=l&&l<=h&&p*l+_===n[l];)l+=c;for(n[l]-=1,l-=c;0<=l&&l<=h;)n[l]=n[l+c]-g,l-=c}else n=null;else{for(o=-1,l=f;0<=l-c&&l-c<=h;l-=c)if(n[l]>n[l-c]+g){o=l;break}if(!(0<=o&&o<=h)&&0c?h:0]&&(o=0>c?h:0),0<=o&&o<=h){for(a=v-1+g,l=f;0<=l&&l<=h&&0=0;a--)o=_(s,t),s=p(s,t),r[a]=v(o);else if("ordered"===A){for(s=n,a=x-1;a>=0;a--)o=_(s,l=t-a),s=p(s,l),r[a]=v(o);Sn(r,r,l)}else{l=(f="repeated"===A)?t+x-1:t,n=m(m(i=e&&e.count?e.count:u.Math.factorial(l,x),d),n),i=p(g(i,l-x),l),o=l-x+1,s=x,a=l-1;do{b(i,n)?(r[x-s]=f?l-o-x+1:l-o-s+1,y(i,h)&&(n=m(n,i),i=p(g(i,s),a)),s--,a--):(i=p(g(i,a-s),a),o--,a--)}while(s>0)}return r=this.DUAL(r,t,e)},fusion:t.fusion,output:t.output,complement:function(n,t,e){return!0===e?nn(bn(t,n,!0)):bn(t,n)},binary:function(n,t,e){return-1===e?vn(n,t):yn(n,t)},pick:function(n,t,e){return 0e&&T(function(n,t){r[t]=t},null,null,0,n,1):0>e&&(r[0]=n-1,r[n]=1),r=this.DUAL(r,n,t,1)},succ:function(n,e,r,i,u,l){return null==n?null:i&&"binary"===i.type?t.succ.call(this,n,e,r,i,u):function(n,t,e,r){var i,u,l,s,o,a,f=t,c=t-1;if(s=1,1,0,C&r)return null;q&r&&(s=-s);S&r&&(e=-e);0>s?(i=t-(n[f]||1),o=u=t-1,a=i):(i=0,u=n[f]-1,o=i,a=u);0>e?00?1===n[f]||l>n[a-s]+1?(n[a]-=1,n[a+s]=c,n[f]++):n[f]--:n[f]=0:n=null:0===n[f]?(n[i]=0,n[f]=1):1*n[o]+0=0;l--)r(0,1)&&(i={len:i?i.len+1:1,k:l,next:i});return e=(e=i?j(i.len,function(n){var t=i.k;return i=i.next,t}):[]).concat(e.length=n||a&&o&&(a+o>n+1||a*on||o&&o>n?null:(e=-1===e?-1:1,(!(C&f)&&S&f||C&f&&!(S&f))&&(e=-e),(r=new Array(c+1))[c]=0,a&&o?(r[c]=a,1===a&&(r[0]=o),h?(r=T(function(n,t,e){return n[e]=t,n},r,[(n-o)%(u=s.min(o,s.ceil((n-o)/(a-1))))||u].concat(j(a-2,u,0)).concat([o])),0>e&&V(r,r,0,0,a-1)):0>e?(a-=i=s.min(a,s.floor(n/o)||1),0===(n-=i*o)&&0e&&V(r,r,0,0,a-1)):(i=n%(u=s.ceil(n/a))||u,r=T(function(n,t,e){return n[e]=t,n},r,0>e?[n-a+1].concat(j(a-1,1,0)):j(a-1,u,0).concat([i])))):o?(i=s.floor(n/o),u=n%o,r=T(function(n,t,e){return n[e]=t,n[c]++,n},r,h?0>e?j(i,o,0).concat(u?[u]:[]):j(n-o,1,0).concat([o]):0>e?j(i,o,0).concat(u?[u]:[]):[o].concat(j(n-o,1,0)))):r=T(function(n,t,e){return n[e]=t,n[c]++,n},r,0>e?[n]:j(n,1,0)),r=this.DUAL(r,n,t,1))},succ:function(n,t,e,r,i,u){if(null==e||null==n)return null;var l=r&&r.type?r.type:"partition",o=r&&r["max="]?0|r["max="]:null,a=r&&r["parts="]?0|r["parts="]:null;return 0>=e||a&&o&&(a+o>e+1||a*oe||o&&o>e?null:(i=-1===i?-1:1,"composition"===l?function(n,t,e,r,i,u,l){var s,o,a,f,c,h,d,m,p=t,_=r||(i?p-i+1:p);h=1,q&u&&(h=-h);S&u&&(e=-e);0>h?(d=_-(n[_]||1),o=m=_-1,a=d):(d=0,m=n[_]-1,o=d,a=m);if(0>e)if(r)if(i)n=null;else if(C&u)if(f=n[o],p-r+1>f){for(n[o]=1,s=o+h;d<=s&&s<=m&&1===n[s];)s+=h;n[s]--,d<=s-h&&s-h<=m&&(n[s-h]=1+f)}else n=null;else if(f=n[a],p-r+1>f){for(n[a]=1,s=a;d<=s&&s<=m&&1===n[s];)s-=h;n[s]--,d<=s+h&&s+h<=m&&(n[s+h]=1+f)}else n=null;else if(i)n=null;else if(C&u)n=null;else if(p>n[_]){for(s=a,c=0;d<=s&&s<=m&&1===n[s];)s-=h,c++;if(f=n[s]-1,n[s]=f,0<++c)if(d<=s+h&&s+h<=m)n[s+=h]=c,c=0,n[_]=0>h?_-s:s+1;else for(;0n[a]){for(s=o;d<=s&&s<=m&&1===n[s];)s+=h;f=n[s],n[s]=1,n[o]=f-1,d<=s+h&&s+h<=m&&n[s+h]++}else n=null;else if(p-r+1>n[o]){for(s=a;d<=s&&s<=m&&1===n[s];)s-=h;f=n[s],n[s]=1,n[a]=f-1,d<=s-h&&s-h<=m&&n[s-h]++}else n=null;else if(i)n=null;else if(C&u)n=null;else if(p>n[o])for(c=n[a],n[a-h]++,n[_]--,s=a-h,c--;0p?(_=y-(n[y]||1),f=g=y-1,c=_):(_=0,g=n[y]-1,f=_,c=g);if(0>e)if(r)n=null;else if(C&u)n=null;else if(_<=(a=i?f+p:f)&&a<=g&&n[a]>1){for(o=c,m=0;_<=o&&o<=g&&p*(o-a)>=0&&1===n[o];)m+=n[o],o-=p;if(d=n[o]-1,m++,n[o]=d,n[y]=0>p?y-o:o+1,dh||n[f+(h-1)*p]0&&n[o]===n[o-p];)m+=n[o],o-=p;for(n[o]++,m--,n[y]=0>p?y-o:o+1;0=n||h&&c&&(h+c>n+1||h*cn||c&&c>n)return null;if(h&&c)return null;if(c&&!h&&(h=c,g=!0),h)1===h?(r=[n],m=n):n===h?(r=j(h,1,0),m=n):(e={},i=n-2,j(r=Q(j(h-1,function(){for(s=a(0,i);1===e[s];)s=(s+1)%(i+1);return e[s]=1,s+1})),function(n){return s=r[n],l=s-d,d=s,m+=l,l}));else{for(e=null,s=1;s(r=n[0].indexOf(i))||0(r=n[0].indexOf(i))||0>>1,c=new Array(u);for(t=0;tu||t!==c[r-1][0]||0!==c[r-1][1])return!1;if(l=r,c[(r=n[0][t])-1]||(c[r-1]=[0,t]),r<1||r>u||0!==c[r-1][0]||t!==c[r-1][1])return!1;for(s=r,o+=n[t][t],a+=n[t][i-1-t],e=1;eu||t!==c[r-1][0]||e!==c[r-1][1])return!1;if(l+=r,c[(r=n[e][t])-1]||(c[r-1]=[e,t]),r<1||r>u||e!==c[r-1][0]||t!==c[r-1][1])return!1;s+=r}if(l!==f||s!==f)return!1}return o===f&&a===f},make:function n(t){if(0>=t||2===t)return null;if(1===t)return[[t]];var e,r,i,u,l,s,o=1&t,a=1-o,f=0==t%4,c=t*t,h=t-o>>>1;for(s=new Array(t),e=0;e=t&&(e++,r=0),s[e][r]=(u+r-e)%t*t+(l+e+r)%t+1;else if(f)for(i=0,e=0,r=0;i=t&&(e++,r=0),s[e][r]=(e+1)%4>>>1==(r+1)%4>>>1?c-i:i+1;else if(a){var d,m,p,_,g=n(h),b=h*h,y=2*b,v=3*b;for(i=0,e=0,r=0;i=h&&(e++,r=0),d=g[e][r],s[e][r]=d,s[e+h][r+h]=d+b,s[e+h][r]=d+y,s[e][r+h]=d+v;for(p=m=h>>>1,r=0;rt-p||e===m&&r===m)||0===e&&r===m||(_=s[e][r],s[e][r]=s[e][r+h],s[e][r+h]=_)}return s},product:function(){if(1>=arguments.length)return arguments[0];for(var n,t,e,r,i,u,l,s,o,a,f,c,h,d=arguments,m=d.length,p=d[0],_=1;_=i&&(h=0,++f>=i&&(o+=i,f=0,h=0,++c>=r&&(a++,s+=i,c=0,o=0,f=0,h=0),a",position:[0,4,5]}).completeWith(Abacus.Permutation(6-3)) @@ -65,7 +65,7 @@ o.rewind(-1) [ 0, 1, 4, 5, 2, 3 ] [ 0, 3, 4, 5, 1, 2 ] o.random() -[ 0, 5, 4, 3, 1, 2 ] +[ 2, 5, 3, 4, 0, 1 ] o.dispose() o = Abacus.Tensor(7,{type:"partial",data:["{0..4}","[0]+1","[1]+1","[3]-1","[4]-1"],ordering:"<>",position:[0,1,2,4,5]}).completeWith(Abacus.Permutation(7-5)) @@ -96,5 +96,5 @@ o.rewind(-1) [ 0, 1, 2, 6, 5, 4, 3 ] [ 0, 1, 2, 5, 4, 3, 6 ] o.random() -[ 1, 2, 3, 6, 5, 4, 0 ] +[ 3, 4, 5, 2, 1, 0, 6 ] o.dispose() diff --git a/test/conditional_permutations_boolean.txt b/test/conditional_permutations_boolean.txt index a2858e6..24227db 100644 --- a/test/conditional_permutations_boolean.txt +++ b/test/conditional_permutations_boolean.txt @@ -1,4 +1,4 @@ -Abacus Algebraic Composition: Permutations w/ Partial Conditions (VERSION = 0.8.8) +Abacus Algebraic Composition: Permutations w/ Partial Conditions (VERSION = 0.9.0) --- 6-Permutations where (unique)values in 0th position ARE (0,1,2) and in 4th position ARE NOT (3,4) o = Abacus.Tensor(6,{type:"partial",data:[[true,0,1,2],[false,3,4]],ordering:"<>",position:[0,4]}).completeWith(Abacus.Permutation(6-2)) @@ -449,5 +449,5 @@ o.rewind(-1) [ 0, 1, 3, 4, 2, 5 ] [ 0, 2, 3, 4, 1, 5 ] o.random() -[ 1, 3, 0, 2, 5, 4 ] +[ 2, 3, 5, 4, 1, 0 ] o.dispose() diff --git a/test/connected_permutations.txt b/test/connected_permutations.txt index 8173d46..c0a2071 100644 --- a/test/connected_permutations.txt +++ b/test/connected_permutations.txt @@ -1,4 +1,4 @@ -Abacus.Permutations (VERSION = 0.8.8) +Abacus.Permutations (VERSION = 0.9.0) --- o = Abacus.Permutation(2,{type:"connected"}) o.total() @@ -9,19 +9,19 @@ o = Abacus.Permutation(3,{type:"connected"}) o.total() 2 o.random() -[ 2, 0, 1 ] +[ 1, 2, 0 ] o = Abacus.Permutation(4,{type:"connected"}) o.total() 6 o.random() -[ 2, 0, 3, 1 ] +[ 2, 3, 1, 0 ] o = Abacus.Permutation(5,{type:"connected"}) o.total() 24 o.random() -[ 4, 2, 3, 0, 1 ] +[ 3, 0, 4, 2, 1 ] o = Abacus.Permutation(6,{type:"connected"}) o.total() 120 o.random() -[ 2, 0, 3, 4, 5, 1 ] +[ 5, 4, 3, 1, 0, 2 ] diff --git a/test/cyclic_permutations.txt b/test/cyclic_permutations.txt index e0c0428..e92ed01 100644 --- a/test/cyclic_permutations.txt +++ b/test/cyclic_permutations.txt @@ -1,4 +1,4 @@ -Abacus.Permutations (VERSION = 0.8.8) +Abacus.Permutations (VERSION = 0.9.0) --- o = Abacus.Permutation(4,{type:"cyclic"}) o.total() @@ -47,12 +47,12 @@ o.order("colex,reversed") [ 2, 3, 0, 1 ] [ 1, 2, 3, 0 ] o.order("random") -[ 2, 3, 0, 1 ] -[ 1, 2, 3, 0 ] [ 0, 1, 2, 3 ] +[ 2, 3, 0, 1 ] [ 3, 0, 1, 2 ] +[ 1, 2, 3, 0 ] o.random() -[ 2, 3, 0, 1 ] +[ 1, 2, 3, 0 ] o.order("colex").range(-3, -1) [ 2, 3, 0, 1 ] [ 3, 0, 1, 2 ] diff --git a/test/derangements.txt b/test/derangements.txt index 517700e..d338c97 100644 --- a/test/derangements.txt +++ b/test/derangements.txt @@ -1,4 +1,4 @@ -Abacus.Permutations (VERSION = 0.8.8) +Abacus.Permutations (VERSION = 0.9.0) --- o = Abacus.Permutation(2,{type:"derangement"}) o.total() @@ -51,9 +51,9 @@ o.order("colex,reversed") [ 1, 2, 0 ] o.order("random") [ 1, 2, 0 ] -[ 2, 0, 1 ] +[ 1, 2, 0 ] o.random() -[ 2, 0, 1 ] +[ 1, 2, 0 ] o = Abacus.Permutation(4,{type:"derangement"}) o.total() 9 @@ -130,17 +130,17 @@ o.order("colex,reversed") [ 2, 3, 1, 0 ] [ 3, 2, 1, 0 ] o.order("random") -[ 3, 2, 0, 1 ] +[ 2, 0, 3, 1 ] +[ 3, 2, 1, 0 ] +[ 2, 0, 3, 1 ] [ 1, 0, 3, 2 ] [ 3, 2, 1, 0 ] -[ 2, 3, 1, 0 ] -[ 2, 3, 1, 0 ] +[ 1, 3, 0, 2 ] +[ 1, 3, 0, 2 ] +[ 1, 0, 3, 2 ] [ 3, 0, 1, 2 ] -[ 3, 2, 0, 1 ] -[ 2, 0, 3, 1 ] -[ 2, 0, 3, 1 ] o.random() -[ 3, 0, 1, 2 ] +[ 1, 2, 3, 0 ] o = Abacus.Permutation(5,{type:"derangement"}) o.total() 44 @@ -462,52 +462,52 @@ o.order("colex,reversed") [ 2, 4, 3, 1, 0 ] [ 4, 2, 3, 1, 0 ] o.order("random") -[ 2, 0, 4, 1, 3 ] -[ 4, 0, 1, 2, 3 ] -[ 4, 2, 3, 1, 0 ] -[ 3, 0, 4, 2, 1 ] -[ 3, 2, 0, 4, 1 ] -[ 2, 3, 4, 1, 0 ] -[ 4, 0, 1, 2, 3 ] -[ 4, 0, 1, 2, 3 ] +[ 2, 4, 0, 1, 3 ] +[ 4, 2, 1, 0, 3 ] [ 3, 0, 4, 2, 1 ] -[ 2, 3, 0, 4, 1 ] -[ 4, 0, 1, 2, 3 ] [ 3, 4, 1, 2, 0 ] [ 2, 0, 4, 1, 3 ] -[ 2, 4, 1, 0, 3 ] -[ 2, 0, 1, 4, 3 ] -[ 4, 0, 3, 1, 2 ] -[ 4, 3, 0, 2, 1 ] -[ 4, 0, 3, 2, 1 ] -[ 3, 2, 4, 0, 1 ] -[ 4, 2, 3, 1, 0 ] +[ 3, 4, 0, 2, 1 ] [ 4, 0, 1, 2, 3 ] -[ 3, 4, 0, 1, 2 ] -[ 4, 2, 3, 1, 0 ] [ 2, 4, 1, 0, 3 ] +[ 1, 4, 0, 2, 3 ] +[ 3, 2, 4, 1, 0 ] [ 4, 0, 3, 1, 2 ] -[ 3, 2, 1, 4, 0 ] -[ 3, 0, 1, 4, 2 ] +[ 2, 3, 1, 4, 0 ] [ 4, 3, 1, 2, 0 ] [ 2, 0, 1, 4, 3 ] -[ 1, 4, 3, 0, 2 ] -[ 1, 4, 0, 2, 3 ] -[ 3, 0, 4, 2, 1 ] -[ 1, 3, 0, 4, 2 ] +[ 2, 0, 3, 4, 1 ] +[ 4, 2, 0, 1, 3 ] +[ 3, 0, 1, 4, 2 ] +[ 1, 2, 0, 4, 3 ] +[ 2, 3, 0, 4, 1 ] [ 4, 0, 1, 2, 3 ] +[ 2, 0, 3, 4, 1 ] +[ 1, 0, 3, 4, 2 ] +[ 3, 4, 1, 2, 0 ] +[ 4, 3, 0, 1, 2 ] +[ 2, 3, 4, 0, 1 ] +[ 2, 4, 0, 1, 3 ] +[ 2, 0, 4, 1, 3 ] +[ 1, 2, 0, 4, 3 ] [ 3, 2, 0, 4, 1 ] +[ 3, 0, 4, 1, 2 ] +[ 2, 3, 1, 4, 0 ] +[ 3, 4, 0, 2, 1 ] [ 4, 0, 1, 2, 3 ] -[ 4, 0, 1, 2, 3 ] +[ 3, 2, 1, 4, 0 ] [ 4, 3, 1, 2, 0 ] -[ 4, 3, 0, 2, 1 ] -[ 1, 4, 0, 2, 3 ] -[ 4, 0, 3, 1, 2 ] -[ 2, 0, 3, 4, 1 ] +[ 2, 3, 4, 0, 1 ] [ 1, 3, 0, 4, 2 ] -[ 2, 4, 0, 1, 3 ] +[ 3, 0, 1, 4, 2 ] +[ 4, 0, 1, 2, 3 ] +[ 2, 0, 4, 1, 3 ] +[ 1, 0, 3, 4, 2 ] +[ 4, 2, 3, 1, 0 ] +[ 3, 2, 1, 4, 0 ] +[ 2, 4, 3, 1, 0 ] o.random() -[ 3, 4, 0, 2, 1 ] +[ 1, 3, 4, 0, 2 ] o = Abacus.Permutation(7,{type:"derangement"}) o.total() 1854 @@ -4224,5 +4224,5 @@ o.rewind(-1) [ 1, 0, 3, 2, 6, 4, 5 ] [ 1, 0, 3, 2, 5, 6, 4 ] o.random() -[ 2, 0, 1, 5, 3, 6, 4 ] +[ 6, 4, 3, 5, 0, 1, 2 ] o.dispose() diff --git a/test/involutions.txt b/test/involutions.txt index 48252c2..bbf6552 100644 --- a/test/involutions.txt +++ b/test/involutions.txt @@ -1,4 +1,4 @@ -Abacus.Permutations (VERSION = 0.8.8) +Abacus.Permutations (VERSION = 0.9.0) --- o = Abacus.Permutation(2,{type:"involution"}) o.total() @@ -8,7 +8,7 @@ o.rewind() [ 0, 1 ] [ 1, 0 ] o.random() -[ 0, 1 ] +[ 1, 0 ] o = Abacus.Permutation(3,{type:"involution"}) o.total() 4 @@ -19,7 +19,7 @@ o.rewind() [ 2, 1, 0 ] [ 1, 0, 2 ] o.random() -[ 0, 1, 2 ] +[ 2, 1, 0 ] o = Abacus.Permutation(4,{type:"involution"}) o.total() 10 @@ -36,7 +36,7 @@ o.rewind() [ 1, 0, 2, 3 ] [ 1, 0, 3, 2 ] o.random() -[ 0, 3, 2, 1 ] +[ 2, 3, 0, 1 ] o = Abacus.Permutation(5,{type:"involution"}) o.total() 26 @@ -69,7 +69,7 @@ o.rewind() [ 1, 0, 4, 3, 2 ] [ 1, 0, 3, 2, 4 ] o.random() -[ 3, 1, 4, 0, 2 ] +[ 0, 2, 1, 4, 3 ] o = Abacus.Permutation(6,{type:"involution"}) o.total() 76 @@ -152,4 +152,4 @@ o.rewind() [ 1, 0, 3, 2, 4, 5 ] [ 1, 0, 3, 2, 5, 4 ] o.random() -[ 1, 0, 5, 4, 3, 2 ] +[ 5, 1, 2, 3, 4, 0 ] diff --git a/test/k-derangements.txt b/test/k-derangements.txt index 57703fc..70646b5 100644 --- a/test/k-derangements.txt +++ b/test/k-derangements.txt @@ -1,4 +1,4 @@ -Abacus Algebraic Composition: k-Derangements (VERSION = 0.8.8) +Abacus Algebraic Composition: k-Derangements (VERSION = 0.9.0) --- o = Abacus.Combination(8,4).combineWith(Abacus.Permutation(8-4,{type:"derangement"})) o.dimension() @@ -1276,7 +1276,7 @@ o.rewind(-1) [ '0,1,2,4,4,4,7,6', 'ERROR' ] [ '0,1,2,3,5,4,7,6', 'ERROR' ] o.random() -[ 0, 1, 5, 3, 3, 5, 5, 6 ] +[ 2, 1, 2, 2, 5, 5, 6, 5 ] o.order("lex").range(-5, -1) [ 3, 2, 1, 3, 4, 5, 6, 4 ] [ 3, 2, 1, 3, 4, 5, 3, 7 ] diff --git a/test/latin_squares.js b/test/latin_squares.js new file mode 100644 index 0000000..08caee3 --- /dev/null +++ b/test/latin_squares.js @@ -0,0 +1,57 @@ +var isNode = 'undefined' !== typeof global && '[object global]' === {}.toString.call(global); +var Abacus = isNode ? require('../src/js/Abacus.js') : window.Abacus, echo = console.log; + +function format_num( n, L ) +{ + var s = String(n), l = s.length; + return l < L ? (new Array(L-l+1).join(' '))+s : s; +} +function print_square( s ) +{ + if ( null == s ) + { + echo('null'); + return; + } + var n = s.length, len = String(n).length, out = ''; + for(var i=0; i k-derangements.txt call node partially_fixed_permutations.js > partially_fixed_permutations.txt call node conditional_permutations_boolean.js > conditional_permutations_boolean.txt call node conditional_permutations_algebraic.js > conditional_permutations_algebraic.txt +call node latin_squares.js > latin_squares.txt +call node magic_squares.js > magic_squares.txt diff --git a/test/tuples.txt b/test/tuples.txt index 9acc394..3c1b6b6 100644 --- a/test/tuples.txt +++ b/test/tuples.txt @@ -1,4 +1,4 @@ -Abacus.Tuples (VERSION = 0.8.8) +Abacus.Tuples (VERSION = 0.9.0) --- o = Abacus.Tensor(3,3,{type:"tuple",output:"gray"}) o.total() @@ -208,35 +208,35 @@ o.order("colex,reversed") [ 1, 2, 0 ] [ 0, 0, 0 ] o.order("random") -[ 1, 0, 0 ] -[ 2, 1, 0 ] -[ 0, 1, 2 ] -[ 1, 0, 1 ] -[ 2, 1, 2 ] +[ 0, 2, 2 ] +[ 0, 2, 1 ] +[ 0, 2, 0 ] [ 2, 0, 1 ] -[ 2, 2, 1 ] +[ 1, 2, 1 ] [ 1, 2, 0 ] -[ 1, 0, 2 ] [ 1, 1, 1 ] +[ 0, 0, 0 ] +[ 1, 2, 2 ] [ 2, 0, 2 ] +[ 2, 2, 1 ] +[ 1, 0, 2 ] +[ 1, 0, 0 ] +[ 2, 1, 0 ] +[ 1, 0, 1 ] [ 1, 1, 2 ] -[ 2, 1, 1 ] -[ 2, 2, 0 ] +[ 0, 1, 1 ] [ 2, 0, 0 ] -[ 0, 2, 2 ] -[ 2, 2, 2 ] -[ 0, 0, 0 ] -[ 0, 2, 0 ] -[ 0, 0, 1 ] +[ 2, 2, 0 ] [ 0, 1, 0 ] -[ 0, 1, 1 ] +[ 2, 2, 2 ] [ 0, 0, 2 ] -[ 0, 2, 1 ] -[ 1, 2, 1 ] -[ 1, 2, 2 ] +[ 0, 1, 2 ] [ 1, 1, 0 ] +[ 0, 0, 1 ] +[ 2, 1, 2 ] +[ 2, 1, 1 ] o.random() -[ 1, 1, 2 ] +[ 2, 0, 0 ] o.order("colex").range(-5, -1) [ 1, 0, 1 ] [ 2, 2, 1 ]