/**
* Performs a subtraction operation between two vectors sequentially.
* @author Heinz Kredel.
*/
public class VSMult {
/**
* Performs a subtraction operation between two vectors sequentially.
* @param C double array.
* @param A double array.
* @param B double array.
*/
public void seqdiff(double[] C, double[] A, double[] B) {
for (int i=0; i < C.length; i++) {
C[i] = A[i] - B[i];
}
}
/**
* Generates a vector.
* The method random
returns a
* random number within the intervall [0,1].
* @param n dimension of vector.
*/
public double[] vecgen(int n) {
/**
* Creates a 1 dimensional array A
with the dimension n.
*/
double A[] = new double[n];
for (int i=0; i < n; i++) {
A[i] = Math.random();
}
return A;
}
/**
* Generates the null-vector.
* @param n dimension of vector.
*/
public double[] vecgen0(int n) {
double A[] = new double[n];
for (int i=0; i < n; i++) {
A[i] = 0.0;
}
return A;
}
/**
* Prints out the vector.
* @param A vector.
*/
public void vecprint(double[] A) {
for (int i=0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
System.out.println();
}
/**
* Checks if the vector is approximatly zero.
* Checks if the absolut value of the vectors's elements are less than
* the production of the smallest positive value and 1000.
* @param A double vector.
*/
public boolean veccheck0(double[] A) {
double eps = Double.MIN_VALUE*1000.0;
for (int i=0; i < A.length; i++) {
if ( Math.abs(A[i]) > eps) return false;
}
return true;
}
/**
* main.
*/
public static void main(String[] args) {
int n = 2;
boolean prnt = false;
try { n = Integer.parseInt(args[0]); }
catch (Exception e) { }
try { prnt = Boolean.valueOf(args[1]).booleanValue(); }
catch (Exception e) { }
VSMult x = new VSMult();
VSMInf seq = new SeqVSMult();
VSMInf par = new ParVSMult(4);
double[] A = x.vecgen(n);
double[] B = null; //x.vecgen(n);
double C = 0.0;
double D = 0.0;
System.out.println("A = ");
if (prnt) x.vecprint(A);
System.out.println("");
System.out.println("B = ");
if (prnt) x.vecprint(B);
System.out.println("");
System.out.println("C = ");
long tm;
tm = System.currentTimeMillis();
C = seq.norm(A);
tm = System.currentTimeMillis() - tm;
System.out.println(tm + "ms");
tm = System.currentTimeMillis();
D = par.norm(A);
tm = System.currentTimeMillis() - tm;
System.out.println(tm + "ms");
if (prnt) System.out.println(""+C);
if (prnt) System.out.println(""+D);
double eps = Double.MIN_VALUE*1000.0;
if ( Math.abs(C-D) > 10.0e-7) {
System.out.println("C-D != 0.0: " + (C-D) );
} else {
System.out.println("C-D == 0.0: " + (C-D) );
}
}
/**
* main1.
*/
public static void main1(String[] args) {
int n = 2;
boolean prnt = false;
try { n = Integer.parseInt(args[0]); }
catch (Exception e) { }
try { prnt = Boolean.valueOf(args[1]).booleanValue(); }
catch (Exception e) { }
VSMult x = new VSMult();
VSMInf seq = new SeqVSMult();
VSMInf par = new ParVSMult(4);
double[] A = x.vecgen(n);
double[] B = x.vecgen(n);
double C = 0.0;
double D = 0.0;
System.out.println("A = ");
if (prnt) x.vecprint(A);
System.out.println("");
System.out.println("B = ");
if (prnt) x.vecprint(B);
System.out.println("");
System.out.println("C = ");
long tm;
tm = System.currentTimeMillis();
C = seq.multiply(A,B);
tm = System.currentTimeMillis() - tm;
System.out.println(tm + "ms");
tm = System.currentTimeMillis();
D = par.multiply(A,B);
tm = System.currentTimeMillis() - tm;
System.out.println(tm + "ms");
if (prnt) System.out.println(""+C);
if (prnt) System.out.println(""+D);
double eps = Double.MIN_VALUE*1000.0;
if ( Math.abs(C-D) > 10.0e-7) {
System.out.println("C-D != 0.0: " + (C-D) );
} else {
System.out.println("C-D == 0.0: " + (C-D) );
}
}
}