package com.hpctoday.jpip;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import com.hpctoday.jpip.math.LongFactory;
import com.hpctoday.jpip.math.LongInteger;
import com.hpctoday.jpip.math.RationalNumber;
import com.hpctoday.jpip.math.RationalVector;
public class Util {
public static List<List<RationalNumber<LongInteger>>> convertRationalNumber(long[][] m){
LongFactory factory = new LongFactory();
int height = m.length;
int width;
if(height == 0){
width = 0;
} else {
width = m[0].length;
}
ArrayList<List<RationalNumber<LongInteger>>> matrix = new ArrayList<List<RationalNumber<LongInteger>>>(height);
for(int i = 0; i < height; ++i){
ArrayList<RationalNumber<LongInteger>> vector = new ArrayList<RationalNumber<LongInteger>>();
for(int j = 0; j < width; ++j){
vector.add(new RationalNumber<LongInteger>(factory, m[i][j]));
}
matrix.add(vector);
}
return matrix;
}
public static RationalVector<LongInteger> getRationalVector(long[] m){
RationalVector<LongInteger> vector = new RationalVector<LongInteger>(m.length);
LongFactory factory = new LongFactory();
for(int i = 0; i < m.length; i++){
vector.set(i, new RationalNumber<LongInteger>(factory, m[i]));
}
return vector;
}
public static int EOF = -1;
public static Pip<LongInteger> readPip(String filename) throws IOException {
return readPip(filename, 0);
}
public static Pip<LongInteger> readPip(String filename, int verbose) throws IOException{
int nVar = 0, nParm = 0, nIneq = 0, nContext = 0, bigparm = 0;
boolean integerSolution = false;
List<List<RationalNumber<LongInteger>>> ineq = null, context = null;
PipInputReader reader = new PipInputReader(filename);
reader.read();
StringTokenizer tokens = new StringTokenizer(reader.getSettings());
if (tokens.hasMoreTokens()) {
nVar = Integer.parseInt(tokens.nextToken());
}
if (tokens.hasMoreTokens()) {
nParm = Integer.parseInt(tokens.nextToken()); // Np
}
if (tokens.hasMoreTokens()) {
nIneq = Integer.parseInt(tokens.nextToken()); // Nl
}
if (tokens.hasMoreTokens()) {
nContext = Integer.parseInt(tokens.nextToken()); // Nm
}
if (tokens.hasMoreTokens()) {
bigparm = Integer.parseInt(tokens.nextToken()); // Bg
}
if (tokens.hasMoreTokens()) {
int n = Integer.parseInt(tokens.nextToken()); // Nq
integerSolution = (n == 1);
}
if(verbose > 0){
System.out.println();
System.out.println("Number of unknown (nVar): " + nVar);
System.out.println("Number of parameters (nParm): " + nParm);
System.out.println("Number of inequalities (nIneq): " + nIneq);
System.out.println("Number of inequalities satisfied by parameter (Nm): " + nContext);
System.out.println("Bigparm: " + bigparm);
System.out.println("Integer solution (Nq): " + integerSolution);
System.out.println();
}
long [][] m = Util.readMatrix(reader.getTable(), nIneq, nVar + nParm + 1);
ineq = Util.convertRationalNumber(m);
m = Util.readMatrix(reader.getContext(), nContext, nParm + 1);
context = Util.convertRationalNumber(m);
//
//// if (Nm != 0) {
//// System.out.println("Treatment on context -> *********************************");
//// ctxt = pip.traiter.expanser(context, Np, Nm, Np + 1, Np, 0, 0);
//// pip.traiter.traiter(ctxt, null, Np, 0, Nm, 0, -1, Pip.TRAITER_INT);
//// non_vide = pip.solution.is_not_Nil(p);
//// pip.solution.sol_reset(p);
////
//// System.out.println("New Context:");
//// System.out.println(ctxt);
//// } else
// non_vide = true;
//
// if (non_vide) {
// //System.out.println("Treatment on ineq -> *********************************");
//
// //pip.traiter.traiter(ineq, context, Nn, Np, Nl, Nm, bigparm, nq ? Pip.TRAITER_INT : 0);
//
// // fputs(")\n",out);
// //if (simple) pip.solution.sol_simplify(xq);
// //q = pip.solution.sol_hwm();
//
// // while((xq = sol_edit(out, xq)) != q);
//
// } else {
// // fprintf(out, "void\n");
// }
// }
LongFactory lf = new LongFactory();
Tableau<LongInteger> table = new Tableau<LongInteger>(lf, nVar, nParm, nIneq, bigparm, ineq);
table.addUnitRows();
if(integerSolution){
table.simplify(nVar);
}
table.sortRows();
Tableau<LongInteger> tContext = new Tableau<LongInteger>(lf, nParm, 0, nContext, context);
Pip<LongInteger> pip = new Pip<LongInteger>(lf, table, tContext, nVar, nParm, bigparm);
pip.setIntegerSolution(integerSolution);
return pip;
}
public static long[][] readMatrix(String table, int h, int w){
long[][] matrix = new long[h][w];
int length = table.length();
int cIndex = 0;
char c;
for (int i = 0; i < h; i++) {
for(;cIndex < length; cIndex++){
c = table.charAt(cIndex);
if (c == '['){
cIndex++;
break;
}
}
StringBuilder lineBuffer = new StringBuilder();
for(;cIndex < length; cIndex++){
c = table.charAt(cIndex);
if (c == ']') {
if (lineBuffer.length() > 0) {
StringTokenizer tokens = new StringTokenizer(lineBuffer.toString());
for (int j = 0; j < w; j++) {
if (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
try {
matrix[i][j] = Long.parseLong(token);
} catch (NumberFormatException ex) {
throw new RuntimeException("readMatrix: token '" + token + "' not valid LONG");
}
} else {
throw new RuntimeException("readMatrix: expecting more tokens");
}
}
}
break;
} else if (c == '\t' || c == '\n') {
lineBuffer.append(' ');
} else if(c == ')'){
return matrix;
} else {
lineBuffer.append((char)c);
}
}
}
return matrix;
}
}