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