Skip to content

Commit

Permalink
move MpiPartitionGeneratorRunner to test scope, align method names
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Dec 31, 2024
1 parent e8ba884 commit 0a64556
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package de.tilman_neumann.jml.partitions;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
Expand All @@ -31,6 +32,8 @@ public class IntegerPartitionGenerator implements Generator<int[]> {

private static final Logger LOG = LogManager.getLogger(IntegerPartitionGenerator.class);

private static final boolean DEBUG = false;

/** Internal class for stack elements. */
private static class IntegerPartitionStackElem {
private int rest;
Expand All @@ -52,7 +55,7 @@ private IntegerPartitionStackElem(int[] prefix, int rest) {
* @param n
*/
public IntegerPartitionGenerator(int n) {
//LOG.debug("n = " + n);
if (DEBUG) LOG.debug("n = " + n);

IntegerPartitionStackElem firstStackElem = new IntegerPartitionStackElem(new int[0], n);
stack.push(firstStackElem);
Expand All @@ -78,7 +81,7 @@ public int[] next() {
maxStackSize = stack.size();
}
IntegerPartitionStackElem stackElem = stack.pop();
//LOG.debug("POP prefix=" + Arrays.asList(stackElem.partitionPrefix) + ", rest=" + stackElem.rest);
if (DEBUG) LOG.debug("POP prefix=" + Arrays.asList(stackElem.partitionPrefix) + ", rest=" + stackElem.rest);

// rest will be the biggest of all parts when the recursion ends
int rest = stackElem.rest;
Expand All @@ -96,14 +99,14 @@ public int[] next() {
System.arraycopy(prefix, 0, newPrefix, 0, prefixSize);
newPrefix[prefixSize] = part; // next part
// new rest is (rest-next part), the complement of next part
//LOG.debug("PUSH rest=" + rest + ", part=" + part + " -> newRest=" + (rest-part) + ", newPrefix=" + Arrays.toString(newPrefix));
if (DEBUG) LOG.debug("PUSH rest=" + rest + ", part=" + part + " -> newRest=" + (rest-part) + ", newPrefix=" + Arrays.toString(newPrefix));
stack.push(new IntegerPartitionStackElem(newPrefix, rest-part));
}
// prepare result: array is much, much faster than Multiset !
int[] result = new int[prefixSize+1];
result[0] = rest; // biggest part
System.arraycopy(prefix, 0, result, 1, prefixSize);
//LOG.debug("RETURN " + Arrays.toString(result));
if (DEBUG) LOG.debug("RETURN " + Arrays.toString(result));
return result;
}

Expand All @@ -129,11 +132,11 @@ public static SortedSet<IntegerPartition> partitionsOf(int n) {
IntegerPartition expPartition = new IntegerPartition(flatPartition);
partitions.add(expPartition);
}
LOG.debug("maxStackSize = " + partGen.maxStackSize);
if (DEBUG) LOG.debug("maxStackSize = " + partGen.maxStackSize);
return partitions;
}

public static long getNumberOfPartitions(int n) {
public static long numberOfPartitionsOf(int n) {
IntegerPartitionGenerator partGen = new IntegerPartitionGenerator(n);
long count = 0;
while (partGen.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
*/
package de.tilman_neumann.jml.partitions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.SortedSet;
Expand All @@ -26,8 +24,6 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import de.tilman_neumann.util.ConfigUtil;

/**
* A generator for the additive partitions of multipartite numbers.
*
Expand All @@ -53,6 +49,8 @@ public class MpiPartitionGenerator implements Generator<Mpi[]> {

private static final Logger LOG = LogManager.getLogger(MpiPartitionGenerator.class);

private static final boolean DEBUG = false;

/** Internal class for stack elements. */
private static class MpiPartitionStackElem {
private Mpi rest;
Expand All @@ -76,10 +74,12 @@ private MpiPartitionStackElem(Mpi[] prefix, Mpi rest) {
* @param q
*/
public MpiPartitionGenerator(Mpi q) {
//int dim = q.getDim(); // dimension of the multipartite numbers
//LOG.debug("q=" + q + ", dim = " + dim);
if (DEBUG) {
int dim = q.getDim(); // dimension of the multipartite numbers
LOG.debug("q=" + q + ", dim = " + dim);
}
subvalues = MpiPowerMap.create(q);
//LOG.info("power map has " + subvalues.size() + " elements!");
if (DEBUG) LOG.debug("power map has " + subvalues.size() + " elements!");
MpiPartitionStackElem firstStackElem = new MpiPartitionStackElem(new Mpi[0], q);
stack.push(firstStackElem);
}
Expand All @@ -102,7 +102,7 @@ public Mpi[] next() {
maxStackSize = stack.size();
}
MpiPartitionStackElem stackElem = stack.pop();
//LOG.debug("POP prefix=" + Arrays.asList(stackElem.partitionPrefix) + ", rest=" + stackElem.rest);
if (DEBUG) LOG.debug("POP prefix=" + Arrays.asList(stackElem.partitionPrefix) + ", rest=" + stackElem.rest);

// rest will be the biggest of all parts when the recursion ends
Mpi rest = stackElem.rest;
Expand All @@ -116,15 +116,15 @@ public Mpi[] next() {

// create next parts
if (maxNextPart!=null && maxNextPart.getCardinality()>0 && rest.getCardinality()>1) {
//LOG.debug("create nextPartsAndComplements of " + rest);
if (DEBUG) LOG.debug("create nextPartsAndComplements of " + rest);
Map<Mpi, Mpi> nextPartsAndComplements = subvalues.getSubvaluesLessOrEqual(rest, maxNextPart);
//LOG.debug("nextPartsAndComplements= " + nextPartsAndComplements);
if (DEBUG) LOG.debug("nextPartsAndComplements= " + nextPartsAndComplements);
for (Map.Entry<Mpi, Mpi> partAndComplement : nextPartsAndComplements.entrySet()) {
Mpi[] newPrefix = new Mpi[prefixSize+1];
System.arraycopy(prefix, 0, newPrefix, 0, prefixSize);
newPrefix[prefixSize] = partAndComplement.getKey(); // next part
// new rest is (rest-next part), the complement of next part
//LOG.debug("PUSH rest=" + rest + ", part=" + partAndComplement.getKey() + " -> newRest=" + partAndComplement.getValue() + ", newPrefix=" + Arrays.asList(newPrefix));
if (DEBUG) LOG.debug("PUSH rest=" + rest + ", part=" + partAndComplement.getKey() + " -> newRest=" + partAndComplement.getValue() + ", newPrefix=" + Arrays.asList(newPrefix));
stack.push(new MpiPartitionStackElem(newPrefix, partAndComplement.getValue()));
}
}
Expand All @@ -133,7 +133,7 @@ public Mpi[] next() {
Mpi[] result = new Mpi[prefixSize+1];
result[0] = rest; // biggest part
System.arraycopy(prefix, 0, result, 1, prefixSize);
//LOG.debug("RETURN " + Arrays.toString(result));
if (DEBUG) LOG.debug("RETURN " + Arrays.toString(result));
return result;
}

Expand All @@ -153,8 +153,10 @@ public static SortedSet<MpiPartition> partitionsOf(Mpi q) {
MpiPartition expPartition = new MpiPartition(flatPartition);
partitions.add(expPartition);
}
LOG.debug(partGen.subvalues.accessStats());
LOG.debug("maxStackSize = " + partGen.maxStackSize);
if (DEBUG) {
LOG.debug(partGen.subvalues.accessStats());
LOG.debug("maxStackSize = " + partGen.maxStackSize);
}
return partitions;
}

Expand All @@ -170,8 +172,10 @@ public static long numberOfPartitionsOf(Mpi q) {
partGen.next();
count++;
}
//LOG.debug(partGen.subvalues.accessStats());
//LOG.debug("maxStackSize = " + partGen.maxStackSize);
if (DEBUG) {
LOG.debug(partGen.subvalues.accessStats());
LOG.debug("maxStackSize = " + partGen.maxStackSize);
}
return count;
}

Expand All @@ -184,40 +188,4 @@ public static long numberOfFactorizationsOf(BigInteger n) {
PrimePowers mpiFromFactors = PrimePowers_DefaultImpl.valueOf(n);
return numberOfPartitionsOf(mpiFromFactors);
}

private static void printNumberOfMultipartitePartitions(Mpi q) {
long start = System.currentTimeMillis();
long count = numberOfPartitionsOf(q);
LOG.info(q + " has " + count + " partitions (computed in " + (System.currentTimeMillis()-start) + "ms)");
}

/**
* Test
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();

while(true) {
String input;
try {
LOG.info("\nPlease insert comma-separated parts of multipartite number:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
input = line.trim();
LOG.debug("multipartite number input = [" + input + "]");
} catch (IOException ioe) {
LOG.error("io-error occurring on input: " + ioe.getMessage());
continue;
}
try {
Mpi q = new Mpi_IntegerArrayImpl(input);
printNumberOfMultipartitePartitions(q);
//SortedSet<MpiPartition> partitions = partitionsOf(q);
//LOG.debug(q + " has " + partitions.size() + " partitions: " + partitions);
} catch (NumberFormatException nfe) {
LOG.error("input " + input + " is not a multipartite integer");
}
} // next input...
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import de.tilman_neumann.util.ConfigUtil;

/**
* Integer partition generator test runner.
* Integer partition generator runner.
* @author Tilman Neumann
*/
public class IntegerPartitionGeneratorRunner {
Expand Down Expand Up @@ -55,7 +55,7 @@ public static void main(String[] args) {
try {
int n = Integer.valueOf(input);
long start = System.currentTimeMillis();
long count = IntegerPartitionGenerator.getNumberOfPartitions(n);
long count = IntegerPartitionGenerator.numberOfPartitionsOf(n);
LOG.info(n + " has " + count + " partitions (computed in " + (System.currentTimeMillis()-start) + "ms)");
if (DEBUG) {
SortedSet<IntegerPartition> partitions = IntegerPartitionGenerator.partitionsOf(n);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
package de.tilman_neumann.jml.partitions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.SortedSet;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import de.tilman_neumann.util.ConfigUtil;

/**
* MPI partition generator runner.
* @author Tilman Neumann
*/
public class MpiPartitionGeneratorRunner {

private static final Logger LOG = LogManager.getLogger(MpiPartitionGeneratorRunner.class);

private static final boolean DEBUG = false;

/**
* Test
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();

while(true) {
String input;
try {
LOG.info("\nPlease insert comma-separated parts of multipartite number:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
input = line.trim();
LOG.debug("multipartite number input = [" + input + "]");
} catch (IOException ioe) {
LOG.error("io-error occurring on input: " + ioe.getMessage());
continue;
}
try {
Mpi q = new Mpi_IntegerArrayImpl(input);
long start = System.currentTimeMillis();
long count = MpiPartitionGenerator.numberOfPartitionsOf(q);
LOG.info(q + " has " + count + " partitions (computed in " + (System.currentTimeMillis()-start) + "ms)");
if (DEBUG) {
SortedSet<MpiPartition> partitions = MpiPartitionGenerator.partitionsOf(q);
LOG.debug(q + " has " + partitions.size() + " partitions: " + partitions);
}
} catch (NumberFormatException nfe) {
LOG.error("input " + input + " is not a multipartite integer");
}
} // next input...
}
}

0 comments on commit 0a64556

Please sign in to comment.