Comments (1)
Thanks for accepting this issue so quickly. I have already adapted the R code to Java. If you would like to take a look at the implementation, this may be valuable as a reference:
/** adaption from pseudo / R code of http://stackoverflow.com/a/14758057 **/
private static <T> List<List<List<T>>> groups(List<T> x,int n) {
//nx <- length(x); ning <- nx/n
int nx = x.size(), ning = nx/n;
//group1 <- rbind( matrix(rep(x[1],choose(nx-1,ning-1)),nrow=1), combn(x[-1],ning-1) )
List<List<T>> group1 = new ArrayList<List<T>>();
T kfst = x.get(0);
int knum = (int)choose(nx-1,ning-1);
ICombinatoricsVector<T> kvec = Factory.createVector(x.subList(1,nx));
Generator<T> kgen = Factory.createSimpleCombinationGenerator(kvec, ning-1);
List<ICombinatoricsVector<T>> kcoms = kgen.generateAllObjects();
for(int kidx=0;kidx<knum;kidx++) {
ICombinatoricsVector<T> kcom = kcoms.get(kidx);
group1.add(new ArrayList<T>(knum){ private static final long serialVersionUID = 1L; {
add(kfst); addAll(kcom.getVector());
}});
}
//ng <- ncol(group1)
int ng = group1.size();
//if(n > 2){ ... } else { ... }
if(n > 2){
//out <- vector('list',ng)
List<List<List<T>>> out = new ArrayList<List<List<T>>>(ng);
//for(i in seq_len(ng)){
for(int i=0;i<ng;i++) {
//other <- perm.groups(setdiff(x,group1[,i]),n=n-1)
List<T> kdif = new ArrayList<>(x), kgri = group1.get(i);
kdif.removeAll(kgri);
List<List<List<T>>> other = groups(kdif,n-1);
//out[[i]] <- lapply( seq_along(other),function(j) cbind(group1[,i],other[[j]]) )
for(int j=0;j<other.size();j++) {
List<List<T>> kotj = other.get(j);
out.add(new ArrayList<List<T>>(){ private static final long serialVersionUID = 1L; {
add(kgri); addAll(kotj);
}});
}
}
//unnecesarry: out <- unlist(out,recursive=FALSE)
return out;
} else {
//other <- lapply(seq_len(ng),function(i) matrix(setdiff(x,group1[,i]),ncol=1))
//out <- lapply(seq_len(ng), function(i) cbind(group1[,i],other[[i]]) )
List<List<List<T>>> out = new ArrayList<List<List<T>>>(ng);
for(int i=0;i<ng;i++) {
List<T> kdif = new ArrayList<>(x), kgri = group1.get(i);
kdif.removeAll(kgri);
out.add(Arrays.asList(group1.get(i), kdif));
}
return out;
}
}
private static long choose(int n,int k) {
if(k>n-k)
k = n-k;
long b = 1;
for(int i = 1,m = n;i<=k;i++,m--)
b = b*m/i;
return b;
}
Regards, Kristian
from combinatoricslib.
Related Issues (18)
- Compositions HOT 2
- factorial function should change HOT 2
- Name of Factory class should change HOT 1
- getNumberOfGeneratedObjects() returns long; ints only accepted by generateObjectsRange() HOT 1
- Use the Visistor pattern for ComplexCombinationIterator to avoid unnecessary creation of objects
- Add List#addAll(int index, Collection c) based Factory method
- License Missing HOT 1
- Friendly suggestion: Combinations with Repetition HOT 3
- permutation without repetition with selected object r from n things HOT 5
- Change factorial function in `combination` HOT 1
- Treat repeated elements of sinple permutation as identical HOT 2
- Publish to a public maven repo. HOT 2
- Using an iterator rather than a vector? HOT 2
- Need method to randomly sample SimpleCombination HOT 2
- The Simple combinations generator does not return other then {0,1,2,3} when asked to make combinations with those numbers HOT 1
- Maven Central Repo is weird HOT 2
- Runtime exception in getNumberOfGeneratedObjects when using createPermutationGenerator with treatAsIdentical=false HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from combinatoricslib.