KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > algebra > Vector


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.algebra;
32
33 import java.io.Serializable JavaDoc;
34
35 import org.apache.log4j.Logger;
36
37 public class Vector extends Object JavaDoc implements Serializable JavaDoc, Cloneable JavaDoc {
38     
39     static Logger logger = Logger.getLogger(Vector.class.getName());
40   int size;
41   double[] elements;
42
43
44   public Vector() {
45     super();
46   }
47
48
49   public Vector(int _size) {
50     super();
51     this.elements = new double[_size];
52     this.size = _size;
53   }
54
55
56   public Vector(double[] tab) {
57     this(tab.length);
58     int index;
59     for (index = 0; index < tab.length; index++) {
60       this.setElement(index, tab[index]);
61     }
62   }
63
64
65   public void randomizeFillIn() {
66     int i;
67
68     for (i = 0; i < this.size; i++) {
69       this.setElement(i, Math.random());
70     }
71     return;
72   }
73
74
75   public int getSize() {
76     return this.size;
77   }
78
79
80   public synchronized void setElement(int index, double x) {
81     this.elements[index] = x;
82     return;
83   }
84
85
86   public synchronized double getElement(int index) {
87     return this.elements[index];
88   }
89
90
91   public synchronized Vector multiplicate(double a) {
92     int index;
93     Vector v;
94
95     v = new Vector(this.size);
96
97     for (index = 0; index < this.size; index++) {
98       v.setElement(index, a * this.getElement(index));
99     }
100
101     return v;
102   }
103
104
105   public double distance(Vector _v) {
106     if (_v.getSize() != this.getSize())
107       return -1;
108
109     int i;
110     double max = 0, current = 0;
111     for (i = 0; i < _v.getSize(); i++) {
112       current = Math.abs(_v.getElement(i) - this.getElement(i));
113       if (current > max)
114         max = current;
115     }
116     return max;
117   }
118
119
120   public Vector concat(Vector _v) {
121     int i;
122     Vector result = new Vector(this.getSize() + _v.getSize());
123
124     for (i = 0; i < this.getSize(); i++) {
125       result.setElement(i, this.getElement(i));
126     }
127     for (i = 0; i < _v.getSize(); i++) {
128       result.setElement(i + this.getSize(), _v.getElement(i));
129     }
130     return result;
131   }
132
133
134   public void display() {
135     int i;
136     logger.info("___");
137     for (i = 0; i < this.size; i++) {
138       logger.info("|" + this.getElement(i) + "|");
139     }
140     logger.info("---");
141     return;
142   }
143
144
145   public synchronized Vector add(Vector a) {
146     int index;
147     Vector v;
148
149     v = new Vector(this.size);
150
151     for (index = 0; index < size; index++) {
152       v.setElement(index, a.getElement(index) + this.getElement(index));
153     }
154
155     return v;
156   }
157 }
158
Popular Tags