1 31 package org.objectweb.proactive.examples.algebra; 32 33 import java.io.Serializable ; 34 35 import org.apache.log4j.Logger; 36 37 public class Vector extends Object implements Serializable , Cloneable { 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 |