For example, you have the array:
origin = ['a', 'b', 'c']
and you are interested to find all possible combinations of this chars.
It can be
'a', 'b', 'ab', 'c', 'ac', 'bc' and finally
So you can attach the library and create binary vector:
vector = new DiscreteVector(origin)
It initializes array filled with zeros. This corresponds to the case when none of the chars are included in the set. You have to perform
vector.next() // [1,0,0]
each time you want to find next combination.
For the first time, you receive
[1, 0, 0].
It tells that the current set consists of only
To fill a new array with the proper amount of origin array items write down:
vector.fillWith(origin) // ["a"]
If you want to combine the vector with an exact number of indexes, you need to specify it explicitly:
vector.next(2).fillWith(origin) // ["a","b"], then ["a","c"], then ["b","c"]
To get all possible combinations in one array you have to run:
vector.allCombinations(origin) // [["a"],["b"],["a","b"],["c"],["a","c"],["b","c"],["a","b","c"],]
Be careful, this operation may take a lot of time for long arrays.
You can also specify an exact number of items you need by unnecessary second parameter
You can get the sum of vector items by running this method:
A number of possible combinations can be found with:
To fill the vector with random values (within defined range), write down:
To reset the vector, write down:
You can also specify an arity of the vector (default binary, max value is equal to 1). To do this you should specify explicitly the second parameter RANGE:
new DiscreteVector(origin, RANGE)
For example, consider the set of weights:
weights = [1, 4]
Imagine, you have two identical sets. What kind of total weight you can assemble with them? Well, define the trenary vector:
vector = new DiscreteVector(weights, 2)
To find all possible total weights, you can use this snippet:
totals = Array(vector.combinations).fill(). map(() => vector.next().fillWith(weights).reduce((sum, item) => sum + item, 0)). sort((a, b) => a - b);
The result will be
[0, 1, 2, 4, 5, 6, 8, 9, 10].
To use the library on pure front-end, load
move it to the project folder and include
in the html head. To use DiscreteVector on a server install it via npm:
$ npm install discrete-vector --save
and then import it into needed file:
DiscreteVector = require('discrete-vector')
or in ES6 way:
import DiscreteVector from 'discrete-vector'
To run tests, move into the npm folder and run
$ npm test.
Be aware! This library uses ES6 features, so you should use node version >= 6.x.x or babelify content manually.