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