Skip to content
Snippets Groups Projects
Commit 20474279 authored by Koen Wermer's avatar Koen Wermer
Browse files

added wishnu's mutations

parent 938abfc6
No related branches found
No related tags found
No related merge requests found
Showing
with 467 additions and 0 deletions
package Examples.NN_InputVector;
public class MinsMaxs {
/**
* Given an NxM matrix of double-values, calculate the minimum and maximum of every column in the matrix.
* The mins and maxs are returned as a pair of arrays.
*/
static public double[][] getMinsMaxs(double[][] data) {
int N = data.length ;
if (N < 1) throw new IllegalArgumentException() ;
int rowWidth = data[0].length ;
// check if every row has the same length:
for (int r=1; r<N; r++)
if (data[r].length != rowWidth) throw new IllegalArgumentException() ;
double[] mins = new double[rowWidth] ;
double[] maxs = new double[rowWidth] ;
// initialize mins and maxs:
for (int c=0; c<rowWidth; c++) {
mins[c] =data[0][c] ;
maxs[c] =data[0][c] ;
}
// iterate over the matrix:
for (int r=1; r<N; r++) {
for (int c=0; c<rowWidth; c++) {
if (data[r][c] <= mins[c]) mins[c] = data[r][c] ; // mutate this
if (data[r][c] >= maxs[c]) maxs[c] = data[r][c] ; // mutate this
}
}
double[][] output = new double[2][] ;
output[0] = mins ;
output[1] = maxs ;
return output ;
}
}
package Examples.NN_InputVector;
public class MinsMaxs_R1 {
/**
* Refactoring R1: replacing some <= and >= to improve efficiency.
*/
static public double[][] getMinsMaxs(double[][] data) {
int N = data.length ;
if (N < 1) throw new IllegalArgumentException() ;
int rowWidth = data[0].length ;
// check if every row has the same lenght:
for (int r=1; r<N; r++)
if (data[r].length != rowWidth) throw new IllegalArgumentException() ;
double[] mins = new double[rowWidth] ;
double[] maxs = new double[rowWidth] ;
// initialize mins and maxs:
for (int c=0; c<rowWidth; c++) {
mins[c] =data[0][c] ;
maxs[c] =data[0][c] ;
}
// iterate over the matrix:
for (int r=1; r<data.length; r++) {
for (int c=0; c<rowWidth; c++) {
if (data[r][c] < mins[c]) mins[c] = data[r][c] ; // more efficient
if (data[r][c] > maxs[c]) maxs[c] = data[r][c] ; // more efficient
}
}
double[][] output = new double[2][] ;
output[0] = mins ;
output[1] = maxs ;
return output ;
}
}
package Examples.NN_InputVector;
public class MinsMaxs_R2 {
/**
* Refactoring R2: moving the check for equal length rows to inside the main loop.
*/
static public double[][] getMinsMaxs(double[][] data) {
int N = data.length ;
if (N < 1) throw new IllegalArgumentException() ;
int rowWidth = data[0].length ;
double[] mins = new double[rowWidth] ;
double[] maxs = new double[rowWidth] ;
// initialize mins and maxs:
for (int c=0; c<rowWidth; c++) {
mins[c] =data[0][c] ;
maxs[c] =data[0][c] ;
}
// iterate over the matrix:
for (int r=1; r<data.length; r++) {
// first check if the row has the same length as data[0]:
if (data[r].length != rowWidth) throw new IllegalArgumentException() ;
for (int c=0; c<rowWidth; c++) {
if (data[r][c] <= mins[c]) mins[c] = data[r][c] ;
if (data[r][c] >= maxs[c]) maxs[c] = data[r][c] ;
}
}
double[][] output = new double[2][] ;
output[0] = mins ;
output[1] = maxs ;
return output ;
}
}
package Examples.NN_InputVector;
public class MinsMaxs_R3 {
/**
* Refactoring R3: changing the main loop from a row-column loop, to column-row loop.
*/
static public double[][] getMinsMaxs(double[][] data) {
int N = data.length ;
if (N < 1) throw new IllegalArgumentException() ;
int rowWidth = data[0].length ;
// check if every row has the same length:
for (int r=1; r<N; r++)
if (data[r].length != rowWidth) throw new IllegalArgumentException() ;
double[] mins = new double[rowWidth] ;
double[] maxs = new double[rowWidth] ;
// initialize mins and maxs:
for (int c=0; c<rowWidth; c++) {
mins[c] =data[0][c] ;
maxs[c] =data[0][c] ;
}
// iterate over the matrix:
for (int c=0; c<rowWidth; c++) {
for (int r=1; r<N; r++) {
if (data[r][c] <= mins[c]) mins[c] = data[r][c] ; // mutate this
if (data[r][c] >= maxs[c]) maxs[c] = data[r][c] ; // mutate this
}
}
double[][] output = new double[2][] ;
output[0] = mins ;
output[1] = maxs ;
return output ;
}
}
package Examples.NN_InputVector;
public class Normalizer_R1 {
/**
* R1: Restructuring the check that mins and max should be of the same length.
*/
static public void normalize(double[] mins, double[] maxs, double[][] data) {
int rowWidth = mins.length ;
if (maxs.length != rowWidth) { throw new IllegalArgumentException() ; }
int row = 0 ;
while (row < data.length) {
if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; }
int col = 0 ;
while (col < rowWidth) {
double delta = maxs[col] - mins[col] ;
if (delta == 0) throw new IllegalArgumentException() ;
double x = data[row][col] ;
data[row][col] = (x - mins[col])/delta ;
col++ ;
}
row++ ;
}
}
}
package Examples.NN_InputVector;
public class Normalizer_R2 {
/**
* R2: moving the element-level normalization to a separate method.
*/
static public void normalize(double[] mins, double[] maxs, double[][] data) {
int rowWidth = mins.length ;
int row = 0 ;
if (maxs.length == rowWidth) {
while (row < data.length) {
if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; }
int col = 0 ;
while (col < rowWidth) {
data[row][col] = normalizeValue(data[row][col] ,mins[col],maxs[col]) ;
col++ ;
}
row++ ;
}
}
else { throw new IllegalArgumentException() ; }
}
static private double normalizeValue(double x, double min, double max) throws IllegalArgumentException {
double delta = max - min ;
if (delta == 0) throw new IllegalArgumentException() ;
return (x - min)/delta ;
}
}
package Examples.NN_InputVector;
public class Normalizer_R3 {
/**
* R3: converting while loop to for loop.
*/
static public void normalize(double[] mins, double[] maxs, double[][] data) {
int rowWidth = mins.length ;
if (maxs.length == rowWidth) {
for (int row=0; row < data.length; row++) {
if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; }
for (int col=0; col < rowWidth; col++) {
double delta = maxs[col] - mins[col] ;
if (delta == 0) throw new IllegalArgumentException() ;
double x = data[row][col] ;
data[row][col] = (x - mins[col])/delta ;
}
}
}
else { throw new IllegalArgumentException() ; }
}
}
package Examples.NN_InputVector;
public class Normalizer_R4 {
/**
* R4: Re-routing exceptional control flow via a different exception.
*/
static public void normalize(double[] mins, double[] maxs, double[][] data) {
int rowWidth = mins.length ;
int row = 0 ;
try {
if (maxs.length == rowWidth) {
while (row < data.length) {
if (data[row].length != rowWidth) { throw new IncompatibleSizeException() ; }
int col = 0 ;
while (col < rowWidth) {
double delta = maxs[col] - mins[col] ;
if (delta == 0) throw new IncompatibleSizeException() ;
double x = data[row][col] ;
data[row][col] = (x - mins[col])/delta ;
col++ ;
}
row++ ;
}
}
else { throw new IllegalArgumentException() ; }
}
catch (IncompatibleSizeException e) {
throw new IllegalArgumentException(e) ;
}
}
}
package Examples.NN_InputVector;
public class Vector_R1 {
private double[] vector;
public Vector_R1(double[] vec) {
this.vector = new double[vec.length] ;
System.arraycopy(vec, 0, this.vector, 0, vec.length);
}
public double[] getVector() {
int N = this.vector.length ;
double[] V = new double[N] ;
for (int k=0; k<N; k++) V[k] = this.vector[k] ;
return V ;
}
public int getSize() { return this.vector.length ; }
/**
* R1 replaces the return in the INPRODUCT-case with a break.
*/
public Vector_R1 combine_R1(int operation, Vector_R1 Z) {
if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ;
double[] result = new double[this.vector.length] ;
double[] vector2 = Z.getVector() ;
switch (operation) {
case VectorCONST.ACCUMULATE: ; // does nothing, deliberately falling through to the code of INPRODUCT
case VectorCONST.INPRODUCT: {
int r = 0 ;
for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ;
double[] rr = {r} ;
result = rr ;
break ;
}
case VectorCONST.PLUS: {
for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ;
break ;
}
default: return null ;
}
return new Vector_R1(result) ;
}
}
package Examples.NN_InputVector;
public class Vector_R2 {
private double[] vector;
public Vector_R2(double[] vec) {
this.vector = new double[vec.length] ;
System.arraycopy(vec, 0, this.vector, 0, vec.length);
}
public double[] getVector() {
int N = this.vector.length ;
double[] V = new double[N] ;
for (int k=0; k<N; k++) V[k] = this.vector[k] ;
return V ;
}
public int getSize() { return this.vector.length ; }
/**
* R2 replaces the fall through in the ACCUMULATE case with a recursive call with INPRODUCT.
*/
public Vector_R2 combine_R2(int operation, Vector_R2 Z) {
if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ;
double[] result = new double[this.vector.length] ;
double[] vector2 = Z.getVector() ;
switch (operation) {
case VectorCONST.ACCUMULATE: return combine_R2(VectorCONST.INPRODUCT,Z) ;
case VectorCONST.INPRODUCT: {
int r = 0 ;
for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ;
double[] rr = {r} ;
return new Vector_R2(rr) ;
}
case VectorCONST.PLUS: {
for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ;
break ;
}
default: return null ;
}
return new Vector_R2(result) ;
}
}
package Examples.NN_InputVector;
public class Vector_R3 {
private double[] vector;
public Vector_R3(double[] vec) {
this.vector = new double[vec.length] ;
System.arraycopy(vec, 0, this.vector, 0, vec.length);
}
public double[] getVector() {
int N = this.vector.length ;
double[] V = new double[N] ;
for (int k=0; k<N; k++) V[k] = this.vector[k] ;
return V ;
}
public int getSize() { return this.vector.length ; }
/**
* R3 reorder the cases, and remove the use of return from the case arms.
*/
public Vector_R3 combine_R3(int operation, Vector_R3 Z) {
if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ;
double[] result = new double[this.vector.length] ;
double[] vector2 = Z.getVector() ;
Vector_R3 resultingVector = null ;
switch (operation) {
case VectorCONST.PLUS: {
for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ;
resultingVector = new Vector_R3(result) ;
break ; }
case VectorCONST.ACCUMULATE: ; // does nothing, deliberately falling through to the code of INPRODUCT
case VectorCONST.INPRODUCT: {
int r = 0 ;
for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ;
double[] rr = {r} ;
resultingVector = new Vector_R3(rr) ; // we will just let it fall through
}
default: ;
}
return resultingVector ;
}
}
package Examples.NN_InputVector;
public class Normalizer {
static public void normalize(double[] mins, double[] maxs, double[][] data) {
int rowWidth = mins.length ;
int row = 0 ;
if (maxs.length == rowWidth) {
while (row < data.length) {
if (data[row].length != rowWidth) { throw new IllegalArgumentException() ; }
int col = 0 ;
while (col < rowWidth) {
double delta = maxs[col] - mins[col] ;
if (delta == 0) throw new IllegalArgumentException() ;
double x = data[row][col] ;
data[row][col] = (x - mins[col])/delta ;
col++ ;
}
row++ ;
}
}
else { throw new IllegalArgumentException() ; }
}
}
package Examples.NN_InputVector;
public class Vector {
private double[] vector;
public Vector(double[] vec) {
this.vector = new double[vec.length] ;
System.arraycopy(vec, 0, this.vector, 0, vec.length);
}
public double[] getVector() {
int N = this.vector.length ;
double[] V = new double[N] ;
for (int k=0; k<N; k++) V[k] = this.vector[k] ;
return V ;
}
public int getSize() { return this.vector.length ; }
public Vector combine(int operation, Vector Z) {
if (Z.getSize() != this.vector.length) throw new IllegalArgumentException() ;
double[] result = new double[this.vector.length] ;
double[] vector2 = Z.getVector() ;
switch (operation) {
case VectorCONST.ACCUMULATE: ; // does nothing, deliberately falling through to the code of INPRODUCT
case VectorCONST.INPRODUCT: {
int r = 0 ;
for (int k=0; k<this.vector.length; k++) r += this.vector[k]*vector2[k] ;
double[] rr = {r} ;
return new Vector(rr) ;
}
case VectorCONST.PLUS: {
for (int k=0; k<this.vector.length; k++) result[k] = this.vector[k] + vector2[k] ;
break ;
}
default: return null ;
}
return new Vector(result) ;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment