Skip to content

Commit

Permalink
support pluggable arithmetic routines, eg bigInteger arithmetic if ne…
Browse files Browse the repository at this point in the history
…eded as needed
  • Loading branch information
Nikos M committed Jun 19, 2016
1 parent 653ed10 commit 6eb1969
Show file tree
Hide file tree
Showing 5 changed files with 758 additions and 303 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ most algorithms:
* are **statisticaly unbiased** (e.g uniform sampling methods)
* use **efficient sucessor methods** (e.g loopless methods / constant delay methods) to generate objects
* **avoid big-integer arithmetic and computational overhead** (except if explicit ranking / unranking is needed and objects are large)
* arithmetic routines are **pluggable** so biginteger arithmetic can be used via external implementations. **Note** that the lib can generate very large (and in most cases also randomised) combinatorial objects **without ever using** biginteger arithmetic due to design and implementation except if arbitrary random, ranking and unranking have to be used (see above)



Expand All @@ -233,10 +234,10 @@ most algorithms:
* support **efficient ranking / unranking algorithms** and associated methods (preferably of `O(n)` or `O(nlgn)` complexity) for supported orderings [DONE]
* support **unique and uniform random ordering traversals** for all combinatorial objects, so that the space of a combinatorial object can be traversed in any random ordering uniquely and unbiasedly (useful in some applications, eg backtracking) [DONE, see reference, used as custom iterator ordering, see above]
* 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 `biginteger` computations e.g factorials?? [DONE, the lib does not support biginteger arithmetic, but arithmetic routines have been made dynamicaly pluggable and one can use an external implementation to support combinatorics with bigintegers where needed as needed, see test examples for an example]
* add `Combinadic`, `Factoradic` transformations [DONE]
* add magic squares algorithms [IN PROGRESS, NEW FEATURE]
* add `Derangement`, `RestrictedPartition` [IN PROGRESS]
* support generation of combinatorial objects based on *templates of constraints* to satisfy e.g "only combinations with `ab{i}{i+1}`" strtucture and so on.. (TODO)
* support generation of combinatorial objects based on *patterns/templates of constraints* to satisfy e.g "only combinations with `xx(n)(n+1)x`" pattern and so on.. (TODO)
* add generic *rule-based* `Combinatorial` objects like `Grammar` (TODO)
* add `Fibonacci`, `Catalan`, `Bell` number computations (TODO?)
* support `biginteger` computations e.g factorials??
704 changes: 403 additions & 301 deletions src/js/Abacus.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/BigInteger.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions test/permutations-bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var isNode = 'undefined' !== typeof global && '[object global]' === {}.toString.call(global);
var Abacus = isNode ? require('../src/js/Abacus.js') : window.Abacus, echo = console.log;
var bigInt = require('./BigInteger.js');

// plug-in bigInteger arithmetic routines
Abacus.ARITHMETIC.equ = function(a, b){ return bigInt(a).eq(bigInt(b)); };
Abacus.ARITHMETIC.gte = function(a, b){ return bigInt(a).geq(bigInt(b)); };
Abacus.ARITHMETIC.lte = function(a, b){ return bigInt(a).leq(bigInt(b)); };
Abacus.ARITHMETIC.gt = function(a, b){ return bigInt(a).gt(bigInt(b)); };
Abacus.ARITHMETIC.lt = function(a, b){ return bigInt(a).lt(bigInt(b)); };
Abacus.ARITHMETIC.add = function(a, b){ return bigInt(a).plus(bigInt(b)); };
Abacus.ARITHMETIC.sub = function(a, b){ return bigInt(a).minus(bigInt(b)); };
Abacus.ARITHMETIC.mul = function(a, b){ return bigInt(a).times(bigInt(b)); };
Abacus.ARITHMETIC.div = function(a, b){ return bigInt(a).over(bigInt(b)); };
Abacus.ARITHMETIC.mod = function(a, b){ return bigInt(a).mod(bigInt(b)); };
Abacus.ARITHMETIC.shl = function(a, b){ return bigInt(a).shiftLeft(bigInt(b)); };
Abacus.ARITHMETIC.shr = function(a, b){ return bigInt(a).shiftRight(bigInt(b)); };
Abacus.ARITHMETIC.pow = function(a, b){ return bigInt(a).pow(bigInt(b)); };
Abacus.ARITHMETIC.rnd = function(a, b){ return bigInt.randBetween(bigInt(a),bigInt(b)); };
Abacus.ARITHMETIC.min = bigInt.min;
Abacus.ARITHMETIC.max = bigInt.max;

// Note: Due to the large number of combinatorial samples,
// Abacus combinatorics use an Iterator pattern to succesively and consistently
// generate all combinatorial objects without storing all of them in memory at once
var o, i;

echo('Abacus.Permutations (VERSION = '+Abacus.VERSION+')');
echo('---');

// Permutations
echo('o = Abacus.Permutation(50)');
o = Abacus.Permutation(50);

echo('o.total()');
echo(String(o.total()));
/*
echo('default order is "lex", lexicographic-order');
echo('o.next()');
echo(o.next());
echo('o.hasNext()');
echo(o.hasNext());
echo('o.next()');
echo(o.next());
*/
/*
echo('o.rewind()');
o.rewind();
for(i=0; i<10&&o.hasNext(); i++) echo (o.next());
echo('o.forward()');
o.forward();
for(i=0; i<10&&o.hasNext(); i++) echo (o.prev());
echo('o.order("revlex")');
o.order("revlex");
for(i=0; i<10&&o.hasNext(); i++) echo (o.next());
echo('o.order("colex")');
o.order("colex");
for(i=0; i<10&&o.hasNext(); i++) echo (o.next());
echo('o.order("revcolex")');
o.order("revcolex");
for(i=0; i<10&&o.hasNext(); i++) echo (o.next());
*/

echo('o.random()');
echo(o.random());

echo('o.item(78043612608166064768844377641568960512000000000000,"lex")');
echo(o.item(bigInt("78043612608166064768844377641568960512000000000000"),"lex"));

echo('o.item(78043612608166064768844377641568960512000000000000,"colex")');
echo(o.item(bigInt("78043612608166064768844377641568960512000000000000"),"colex"));

echo('o.item(78043612608166064768844377641568960512000000000000,"revlex")');
echo(o.item(bigInt("78043612608166064768844377641568960512000000000000"),"revlex"));

echo('o.item(78043612608166064768844377641568960512000000000000,"revcolex")');
echo(o.item(bigInt("78043612608166064768844377641568960512000000000000"),"revcolex"));

// dispose
echo('o.dispose()');
o.dispose();


261 changes: 261 additions & 0 deletions test/permutations-bigint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
Abacus.Permutations (VERSION = 0.1.0)
---
o = Abacus.Permutation(50)
o.total()
30414093201713378043612608166064768844377641568960512000000000000
o.random()
[ 44,
10,
42,
26,
0,
24,
21,
45,
38,
1,
5,
34,
20,
31,
32,
40,
28,
19,
41,
25,
29,
4,
14,
46,
18,
15,
30,
47,
9,
33,
48,
12,
7,
22,
6,
3,
43,
13,
37,
35,
39,
49,
8,
11,
23,
36,
27,
17,
2,
16 ]
o.item(78043612608166064768844377641568960512000000000000,"lex")
[ 0,
1,
2,
3,
4,
5,
6,
7,
10,
22,
36,
11,
30,
34,
12,
27,
9,
31,
26,
20,
48,
19,
18,
47,
13,
24,
14,
21,
17,
38,
16,
15,
41,
40,
43,
23,
28,
39,
46,
37,
35,
45,
8,
33,
42,
29,
44,
49,
25,
32 ]
o.item(78043612608166064768844377641568960512000000000000,"colex")
[ 32,
25,
49,
44,
29,
42,
33,
8,
45,
35,
37,
46,
39,
28,
23,
43,
40,
41,
15,
16,
38,
17,
21,
14,
24,
13,
47,
18,
19,
48,
20,
26,
31,
9,
27,
12,
34,
30,
11,
36,
22,
10,
7,
6,
5,
4,
3,
2,
1,
0 ]
o.item(78043612608166064768844377641568960512000000000000,"revlex")
[ 49,
48,
47,
46,
45,
44,
43,
42,
39,
27,
13,
38,
19,
15,
37,
22,
40,
18,
23,
29,
1,
30,
31,
2,
36,
25,
35,
28,
32,
11,
33,
34,
8,
9,
6,
26,
21,
10,
3,
12,
14,
4,
41,
16,
7,
20,
5,
0,
24,
17 ]
o.item(78043612608166064768844377641568960512000000000000,"revcolex")
[ 17,
24,
0,
5,
20,
7,
16,
41,
4,
14,
12,
3,
10,
21,
26,
6,
9,
8,
34,
33,
11,
32,
28,
35,
25,
36,
2,
31,
30,
1,
29,
23,
18,
40,
22,
37,
15,
19,
38,
13,
27,
39,
42,
43,
44,
45,
46,
47,
48,
49 ]
o.dispose()

0 comments on commit 6eb1969

Please sign in to comment.