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