Skip to content

Commit

Permalink
math
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Apr 7, 2017
1 parent c13289e commit ecf7e2b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Jngen library provides several handy instruments for generating various kinds of
* [[options.h] Parsing command-line options](/doc/getopt.md)
* [[array.h] Array: wrapper for std::vector](/doc/array.md)
* [[repr.h, printers.h] Printers and output modifiers](/doc/printers.md)
* [[generic_graph.h] Graphs and trees: bacics](/doc/generic_graph.md)
* [[generic_graph.h] Graphs and trees: basics](/doc/generic_graph.md)
* [[graph.h] Graphs generation](/doc/graph.md)
* [[tree.h] Trees generation](/doc/tree.md)
* [[math.h] Math: primes and partitions](/doc/math.md)

### Examples
Generate a random tree on *n* vertices with a 3-letter string assigned to each edge:
Expand Down
31 changes: 31 additions & 0 deletions doc/math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## Math-ish primitives

Jngen provides several free functions and a generator class *MathRandom* to help generating numbers and combinatorial primitives. All interaction with *MathRandom* goes via its global instance called *rndm*. The source of randomness is *rnd*.

### Standalone functions

#### bool isPrime(long long n)
* Returns: true if *n* is prime, false otherwise.
* Supported for all *n* from 1 to 3.8e18.
* Implemented with deterministic variation of the Miller-Rabin primality test so should work relatively fast (exact benchmark here).

### MathRandom methods

#### long long randomPrime(long long n)
#### long long randomPrime(long long l, long long r)
* Returns: random prime in range *[2, n)* or *[l, r]* respectively.
* Throws if no prime is found on the interval.

#### Array partition(int n, int numParts)
#### Array partitionNonEmpty(int n, int numParts)
#### Array64 partition(long long n, int numParts)
#### Array64 partitionNonEmpty(long long n, int numParts)
* Returns: a random ordered partition of *n* into *numParts* parts. In case of *partitionNonEmpty* each element of the result is positive.

#### template&lt;typename T> <br> TArray&lt;TArray&lt;T>> partition(TArray&lt;T> elements, int numParts)
#### template&lt;typename T> <br> TArray&lt;TArray&lt;T>> partitionNonEmpty(TArray&lt;T> elements, int numParts)
* Returns: a random partition of the array *elements* into *numParts* parts.

#### template&lt;typename T> <br> TArray&lt;TArray&lt;T>> partitionNonEmpty(TArray&lt;T> elements, const Array& sizes)
* Returns: a random partition of the array *elements* into parts, where the size of each part is specified.
* Note: sum(*sizes*) must be equal to *elements.size()*.

0 comments on commit ecf7e2b

Please sign in to comment.