package br.com.starcode.math;

public class SoleBuilder {

	public static Sole generateRandom(int size, double minValue, double maxValue) {
		
		double range = maxValue - minValue;
		double[][] a = new double[size][size];
		double[] x = new double[size], b = new double[size];
		
		for (int i = 0; i < size; i++) {
			x[i] = Math.rint(10 * (Math.random() * range + minValue)) / 10;
			b[i] = 0;
			double max = minValue;
			for (int j = 0; j < size; j++) {
				if (j != i) {
					a[i][j] = Math.rint(10 * (Math.random() * range + minValue)) / 10;
					if (a[i][j] > max) {
						max = a[i][j];
					}
					b[i] += a[i][j] * x[i]; 
				}
			}
			a[i][i] = Math.rint(10 * (Math.random() * range + minValue)) / 10;
			if (a[i][i] < max) {
				a[i][i] = max + 1;
			}
			b[i] += a[i][i] * x[i]; 
		}
		
		return new Sole(a, x, b);
		
	}
	
	public static Sole generateRandomInt(int size, int minValue, int maxValue) {
		
		double range = maxValue - minValue;
		double[][] a = new double[size][size];
		double[] x = new double[size], b = new double[size];
		
		for (int i = 0; i < size; i++) {
			x[i] = Math.round(Math.random() * range + minValue);
			b[i] = 0;
			double max = minValue;
			for (int j = 0; j < size; j++) {
				if (j != i) {
					a[i][j] = Math.round(Math.random() * range + minValue);
					if (a[i][j] > max) {
						max = a[i][j];
					}
					b[i] += a[i][j] * x[i]; 
				}
			}
			a[i][i] = Math.round(Math.random() * range + minValue);
			if (a[i][i] < max) {
				a[i][i] = max + 1;
			}
			b[i] += a[i][i] * x[i]; 
		}
		
		return new Sole(a, x, b);
		
	}
	
	public static Sole generateRandom(int size) {
		
		return generateRandom(size, -10, 10);
		
	}
	
	public static Sole generateRandomInt(int size) {
		
		return generateRandomInt(size, -10, 10);
		
	}
	
	public static Sole generateRandomConsistent(int size) {
		
		Sole s = null;
		do {
			s = generateRandom(size);
		} while (!s.isConsistent());
		return s;
		
	}
	
	public static Sole generateRandomConsistent(int size, int minValue, int maxValue) {
		
		Sole s = null;
		do {
			s = generateRandom(size, minValue, maxValue);
		} while (!s.isConsistent());
		return s;
		
	}
	
	public static Sole generateRandomConsistentInt(int size) {
		
		Sole s = null;
		do {
			s = generateRandomInt(size);
		} while (!s.isConsistent());
		return s;
		
	}
	
	public static Sole generateRandomConsistentInt(int size, int minValue, int maxValue) {
		
		Sole s = null;
		//int t = 1;
		do {
			//System.out.println("trying " + t++);
			s = generateRandomInt(size, minValue, maxValue);
		} while (!s.isConsistent());
		return s;
		
	}
	
}

@@ -413,32 +413,25 @@
 ouble max = 
-minValue
+0
 ;%0D%0A%09%09%09for (i
@@ -554,32 +554,41 @@
 0;%0D%0A%09%09%09%09%09if 
+(Math.abs
 (a%5Bi%5D%5Bj%5D
  %3E max) %7B%0D%0A%09
@@ -567,39 +567,50 @@
 Math.abs(a%5Bi%5D%5Bj%5D
+)
  %3E 
+Math.abs(
 max)
+)
  %7B%0D%0A%09%09%09%09%09%09max = 
@@ -779,33 +779,50 @@
 a%5Bi%5D%5Bi%5D = max + 
-1
+(max %3E 0 ? 1 : -1)
 ;%0D%0A%09%09%09%7D%0D%0A%09%09%09b%5Bi%5D
@@ -1239,24 +1239,17 @@
 e max = 
-minValue
+0
 ;%0D%0A%09%09%09fo
@@ -1369,24 +1369,33 @@
 %09%09%09%09%09if 
+(Math.abs
 (a%5Bi%5D%5Bj%5D
  %3E max) 
@@ -1390,15 +1390,26 @@
 %5D%5Bj%5D
+)
  %3E 
+Math.abs(
 max)
+)
  %7B%0D%0A
@@ -1587,17 +1587,34 @@
 = max + 
-1
+(max %3E 0 ? 1 : -1)
 ;%0D%0A%09%09%09%7D%0D

generate diagonal dominant matrices