001 /*
002 * $Id: GenVector.java 3571 2011-03-18 22:02:51Z kredel $
003 */
004
005 package edu.jas.vector;
006
007
008 import java.util.ArrayList;
009 import java.util.List;
010
011 import org.apache.log4j.Logger;
012
013 import edu.jas.kern.PrettyPrint;
014 import edu.jas.structure.ModulElem;
015 import edu.jas.structure.RingElem;
016
017
018 /**
019 * GenVector implements generic vectors with RingElem entries. Vectors of n
020 * columns over C.
021 * @author Heinz Kredel
022 */
023
024 public class GenVector<C extends RingElem<C>> implements ModulElem<GenVector<C>, C> {
025
026
027 private static final Logger logger = Logger.getLogger(GenVector.class);
028
029
030 public final GenVectorModul<C> modul;
031
032
033 public final List<C> val;
034
035
036 /**
037 * Constructor for GenVector.
038 */
039 public GenVector(GenVectorModul<C> m) {
040 this(m, m.getZERO().val);
041 }
042
043
044 /**
045 * Constructor for GenVector.
046 */
047 public GenVector(GenVectorModul<C> m, List<C> v) {
048 if (m == null || v == null) {
049 throw new IllegalArgumentException("Empty m or v not allowed, m = " + m + ", v = " + v);
050 }
051 modul = m;
052 val = v;
053 }
054
055
056 /**
057 * Get the String representation as RingElem.
058 * @see java.lang.Object#toString()
059 */
060 @Override
061 public String toString() {
062 StringBuffer s = new StringBuffer();
063 s.append("[ ");
064 boolean first = true;
065 for (C c : val) {
066 if (first) {
067 first = false;
068 } else {
069 s.append(", ");
070 }
071 s.append(c.toString());
072 }
073 s.append(" ]");
074 if (!PrettyPrint.isTrue()) {
075 s.append(" :: " + modul.toString());
076 s.append("\n");
077 }
078 return s.toString();
079 }
080
081
082 /**
083 * Get a scripting compatible string representation.
084 * @return script compatible representation for this Element.
085 * @see edu.jas.structure.Element#toScript()
086 */
087 //JAVA6only: @Override
088 public String toScript() {
089 // Python case
090 StringBuffer s = new StringBuffer();
091 s.append("( ");
092 boolean first = true;
093 for (C c : val) {
094 if (first) {
095 first = false;
096 } else {
097 s.append(", ");
098 }
099 s.append(c.toScript());
100 }
101 s.append(" )");
102 return s.toString();
103 }
104
105
106 /**
107 * Get a scripting compatible string representation of the factory.
108 * @return script compatible representation for this ElemFactory.
109 * @see edu.jas.structure.Element#toScriptFactory()
110 */
111 //JAVA6only: @Override
112 public String toScriptFactory() {
113 // Python case
114 return factory().toScript();
115 }
116
117
118 /**
119 * Get the corresponding element factory.
120 * @return factory for this Element.
121 * @see edu.jas.structure.Element#factory()
122 */
123 public GenVectorModul<C> factory() {
124 return modul;
125 }
126
127
128 /**
129 * clone method.
130 * @see java.lang.Object#clone()
131 */
132 @Override
133 @SuppressWarnings("unchecked")
134 public GenVector<C> clone() {
135 //return modul.copy(this);
136 ArrayList<C> av = (ArrayList<C>) val;
137 return new GenVector<C>(modul, (List<C>) av.clone());
138 }
139
140
141 /**
142 * test if this is equal to a zero vector.
143 */
144 public boolean isZERO() {
145 return (0 == this.compareTo(modul.getZERO()));
146 }
147
148
149 /**
150 * equals method.
151 */
152 @Override
153 public boolean equals(Object other) {
154 if (!(other instanceof GenVector)) {
155 return false;
156 }
157 GenVector ovec = (GenVector) other;
158 if (!modul.equals(ovec.modul)) {
159 return false;
160 }
161 if (!val.equals(ovec.val)) {
162 return false;
163 }
164 return true;
165 }
166
167
168 /**
169 * Hash code for this GenVector.
170 * @see java.lang.Object#hashCode()
171 */
172 @Override
173 public int hashCode() {
174 return 37 * val.hashCode() + modul.hashCode();
175 }
176
177
178 /**
179 * compareTo, lexicogaphical comparison.
180 * @param b other
181 * @return 1 if (this < b), 0 if (this == b) or -1 if (this > b).
182 */
183 //JAVA6only: @Override
184 public int compareTo(GenVector<C> b) {
185 if (!modul.equals(b.modul)) {
186 return -1;
187 }
188 List<C> oval = b.val;
189 int i = 0;
190 for (C c : val) {
191 int s = c.compareTo(oval.get(i++));
192 if (s != 0) {
193 return s;
194 }
195 }
196 return 0;
197 }
198
199
200 /**
201 * sign of vector.
202 * @return 1 if (this < 0), 0 if (this == 0) or -1 if (this > 0).
203 */
204 public int signum() {
205 return compareTo(modul.getZERO());
206 }
207
208
209 /**
210 * Sum of vectors.
211 * @return this+b
212 */
213 public GenVector<C> sum(GenVector<C> b) {
214 List<C> oval = b.val;
215 ArrayList<C> a = new ArrayList<C>(modul.cols);
216 int i = 0;
217 for (C c : val) {
218 C e = c.sum(oval.get(i++));
219 a.add(e);
220 }
221 return new GenVector<C>(modul, a);
222 }
223
224
225 /**
226 * Difference of vectors.
227 * @return this-b
228 */
229 public GenVector<C> subtract(GenVector<C> b) {
230 List<C> oval = b.val;
231 ArrayList<C> a = new ArrayList<C>(modul.cols);
232 int i = 0;
233 for (C c : val) {
234 C e = c.subtract(oval.get(i++));
235 a.add(e);
236 }
237 return new GenVector<C>(modul, a);
238 }
239
240
241 /**
242 * Negative of this vector.
243 * @return -this
244 */
245 public GenVector<C> negate() {
246 ArrayList<C> a = new ArrayList<C>(modul.cols);
247 for (C c : val) {
248 C e = c.negate();
249 a.add(e);
250 }
251 return new GenVector<C>(modul, a);
252 }
253
254
255 /**
256 * Absolute value of this vector.
257 * @return abs(this)
258 */
259 public GenVector<C> abs() {
260 if (signum() < 0) {
261 return negate();
262 }
263 return this;
264 }
265
266
267 /**
268 * Product of this vector with scalar.
269 * @return this*s
270 */
271 public GenVector<C> scalarMultiply(C s) {
272 ArrayList<C> a = new ArrayList<C>(modul.cols);
273 for (C c : val) {
274 C e = c.multiply(s);
275 a.add(e);
276 }
277 return new GenVector<C>(modul, a);
278 }
279
280
281 /**
282 * Left product of this vector with scalar.
283 * @return s*this
284 */
285 public GenVector<C> leftScalarMultiply(C s) {
286 ArrayList<C> a = new ArrayList<C>(modul.cols);
287 for (C c : val) {
288 C e = s.multiply(c);
289 a.add(e);
290 }
291 return new GenVector<C>(modul, a);
292 }
293
294
295 /**
296 * Linear compination of this vector with scalar multiple of other vector.
297 * @return this*s+b*t
298 */
299 public GenVector<C> linearCombination(C s, GenVector<C> b, C t) {
300 List<C> oval = b.val;
301 ArrayList<C> a = new ArrayList<C>(modul.cols);
302 int i = 0;
303 for (C c : val) {
304 C c1 = c.multiply(s);
305 C c2 = oval.get(i++).multiply(t);
306 C e = c1.sum(c2);
307 a.add(e);
308 }
309 return new GenVector<C>(modul, a);
310 }
311
312
313 /**
314 * Linear compination of this vector with scalar multiple of other vector.
315 * @return this+b*t
316 */
317 public GenVector<C> linearCombination(GenVector<C> b, C t) {
318 List<C> oval = b.val;
319 ArrayList<C> a = new ArrayList<C>(modul.cols);
320 int i = 0;
321 for (C c : val) {
322 C c2 = oval.get(i++).multiply(t);
323 C e = c.sum(c2);
324 a.add(e);
325 }
326 return new GenVector<C>(modul, a);
327 }
328
329
330 /**
331 * Left linear compination of this vector with scalar multiple of other
332 * vector.
333 * @return this+t*b
334 */
335 public GenVector<C> linearCombination(C t, GenVector<C> b) {
336 List<C> oval = b.val;
337 ArrayList<C> a = new ArrayList<C>(modul.cols);
338 int i = 0;
339 for (C c : val) {
340 C c2 = t.multiply(oval.get(i++));
341 C e = c.sum(c2);
342 a.add(e);
343 }
344 return new GenVector<C>(modul, a);
345 }
346
347
348 /**
349 * left linear compination of this vector with scalar multiple of other
350 * vector.
351 * @return s*this+t*b
352 */
353 public GenVector<C> leftLinearCombination(C s, C t, GenVector<C> b) {
354 List<C> oval = b.val;
355 ArrayList<C> a = new ArrayList<C>(modul.cols);
356 int i = 0;
357 for (C c : val) {
358 C c1 = s.multiply(c);
359 C c2 = t.multiply(oval.get(i++));
360 C e = c1.sum(c2);
361 a.add(e);
362 }
363 return new GenVector<C>(modul, a);
364 }
365
366
367 /**
368 * scalar / dot product of this vector with other vector.
369 * @return this . b
370 */
371 public C scalarProduct(GenVector<C> b) {
372 C a = modul.coFac.getZERO();
373 List<C> oval = b.val;
374 int i = 0;
375 for (C c : val) {
376 C c2 = c.multiply(oval.get(i++));
377 a = a.sum(c2);
378 }
379 return a;
380 }
381
382
383 /**
384 * scalar / dot product of this vector with list of other vectors.
385 * @return this * b
386 */
387 public GenVector<C> scalarProduct(List<GenVector<C>> B) {
388 GenVector<C> A = modul.getZERO();
389 int i = 0;
390 for (C c : val) {
391 GenVector<C> b = B.get(i++);
392 GenVector<C> a = b.leftScalarMultiply(c);
393 A = A.sum(a);
394 }
395 return A;
396 }
397
398
399 /**
400 * right scalar / dot product of this vector with list of other vectors.
401 * @return b * this
402 */
403 public GenVector<C> rightScalarProduct(List<GenVector<C>> B) {
404 GenVector<C> A = modul.getZERO();
405 int i = 0;
406 for (C c : val) {
407 GenVector<C> b = B.get(i++);
408 GenVector<C> a = b.scalarMultiply(c);
409 A = A.sum(a);
410 }
411 return A;
412 }
413
414 }