001    
002    /**
003     * Performs a subtraction operation between two vectors sequentially.
004     * @author Heinz Kredel.
005     */
006    public class VSMult {
007    
008    /** 
009     * Performs a subtraction operation between two vectors sequentially.
010     * @param C double array.
011     * @param A double array.
012     * @param B double array.
013     */
014      public void seqdiff(double[] C, double[] A, double[] B) {
015          for (int i=0; i < C.length; i++) {
016              C[i] = A[i] - B[i];
017          }
018      }
019    
020    
021    /**
022     * Generates a vector.
023     * The method <CODE>random</CODE> returns a 
024     * random number within the intervall [0,1].
025     * @param n dimension of vector.
026     */
027      public double[] vecgen(int n) {
028    
029    /** 
030     * Creates a 1 dimensional array <CODE>A</CODE> with the dimension n.
031     */
032          double A[] = new double[n];
033          for (int i=0; i < n; i++) {
034              A[i] = Math.random();
035          }
036          return A;
037      }
038    
039    /** 
040     * Generates the null-vector.
041     * @param n dimension of vector.
042     */
043      public double[] vecgen0(int n) {
044          double A[] = new double[n];
045          for (int i=0; i < n; i++) {
046              A[i] = 0.0;
047          }
048          return A;
049      }
050    
051    
052    /** 
053     * Prints out the vector.
054     * @param A vector. 
055     */
056      public void vecprint(double[] A) {
057          for (int i=0; i < A.length; i++) {
058              System.out.print(A[i] + " ");
059          }
060          System.out.println();
061      }
062    
063    /** 
064     * Checks if the vector is approximatly zero.
065     * Checks if the absolut value of the vectors's elements are less than
066     * the production of the smallest positive value and 1000.
067     * @param A double vector.
068     */ 
069      public boolean veccheck0(double[] A) {
070          double eps = Double.MIN_VALUE*1000.0;
071          for (int i=0; i < A.length; i++) {
072             if ( Math.abs(A[i]) > eps) return false;
073          }
074          return true;
075      }
076    
077    
078    /**
079     * main.
080     */
081      public static void main(String[] args) {
082    
083            int n = 2;
084            boolean prnt = false;
085    
086            try { n = Integer.parseInt(args[0]); }
087            catch (Exception e) { }
088            try { prnt = Boolean.valueOf(args[1]).booleanValue(); }
089            catch (Exception e) { }
090    
091            VSMult x = new VSMult();
092    
093            VSMInf seq =  new SeqVSMult();
094    
095            VSMInf par =  new ParVSMult(4);
096    
097            double[] A = x.vecgen(n);
098            double[] B = null; //x.vecgen(n);
099            double C = 0.0;
100            double D = 0.0;
101     
102            System.out.println("A = ");
103            if (prnt) x.vecprint(A);
104            System.out.println("");
105    
106            System.out.println("B = ");
107            if (prnt) x.vecprint(B);
108            System.out.println("");
109    
110            System.out.println("C = ");
111            long tm;
112            tm = System.currentTimeMillis();
113            C = seq.norm(A);
114            tm = System.currentTimeMillis() - tm; 
115            System.out.println(tm + "ms");
116    
117            tm = System.currentTimeMillis();
118            D = par.norm(A);
119            tm = System.currentTimeMillis() - tm; 
120            System.out.println(tm + "ms");
121    
122            if (prnt) System.out.println(""+C);
123            if (prnt) System.out.println(""+D);
124    
125            double eps = Double.MIN_VALUE*1000.0;
126            if ( Math.abs(C-D) > 10.0e-7) {
127               System.out.println("C-D != 0.0: " + (C-D) );
128            } else {
129               System.out.println("C-D == 0.0: " + (C-D) );
130            }
131        }
132    
133    
134    /**
135     * main1.
136     */
137      public static void main1(String[] args) {
138    
139            int n = 2;
140            boolean prnt = false;
141    
142            try { n = Integer.parseInt(args[0]); }
143            catch (Exception e) { }
144            try { prnt = Boolean.valueOf(args[1]).booleanValue(); }
145            catch (Exception e) { }
146    
147            VSMult x = new VSMult();
148    
149            VSMInf seq =  new SeqVSMult();
150    
151            VSMInf par =  new ParVSMult(4);
152    
153            double[] A = x.vecgen(n);
154            double[] B = x.vecgen(n);
155            double C = 0.0;
156            double D = 0.0;
157     
158            System.out.println("A = ");
159            if (prnt) x.vecprint(A);
160            System.out.println("");
161    
162            System.out.println("B = ");
163            if (prnt) x.vecprint(B);
164            System.out.println("");
165    
166            System.out.println("C = ");
167            long tm;
168            tm = System.currentTimeMillis();
169            C = seq.multiply(A,B);
170            tm = System.currentTimeMillis() - tm; 
171            System.out.println(tm + "ms");
172    
173            tm = System.currentTimeMillis();
174            D = par.multiply(A,B);
175            tm = System.currentTimeMillis() - tm; 
176            System.out.println(tm + "ms");
177    
178            if (prnt) System.out.println(""+C);
179            if (prnt) System.out.println(""+D);
180    
181            double eps = Double.MIN_VALUE*1000.0;
182            if ( Math.abs(C-D) > 10.0e-7) {
183               System.out.println("C-D != 0.0: " + (C-D) );
184            } else {
185               System.out.println("C-D == 0.0: " + (C-D) );
186            }
187        }
188    
189    }