1 package org.opentrafficsim.core.value.vfloat.vector;
2
3 import java.io.Serializable;
4
5 import org.opentrafficsim.core.unit.SICoefficients;
6 import org.opentrafficsim.core.unit.SIUnit;
7 import org.opentrafficsim.core.unit.Unit;
8 import org.opentrafficsim.core.value.Absolute;
9 import org.opentrafficsim.core.value.AbstractValue;
10 import org.opentrafficsim.core.value.DenseData;
11 import org.opentrafficsim.core.value.Format;
12 import org.opentrafficsim.core.value.Relative;
13 import org.opentrafficsim.core.value.SparseData;
14 import org.opentrafficsim.core.value.ValueException;
15 import org.opentrafficsim.core.value.ValueUtil;
16 import org.opentrafficsim.core.value.vfloat.scalar.FloatScalar;
17
18 import cern.colt.matrix.tfloat.FloatMatrix1D;
19 import cern.colt.matrix.tfloat.impl.DenseFloatMatrix1D;
20 import cern.colt.matrix.tfloat.impl.SparseFloatMatrix1D;
21
22 /**
23 * Immutable FloatVector.
24 * <p>
25 * This file was generated by the OpenTrafficSim value classes generator, 09 mrt, 2015
26 * <p>
27 * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
29 * <p>
30 * @version 09 mrt, 2015 <br>
31 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33 * @param <U> Unit; the unit of this FloatVector
34 */
35 public abstract class FloatVector<U extends Unit<U>> extends AbstractValue<U> implements
36 Serializable,
37 ReadOnlyFloatVectorFunctions<U>
38 {
39 /** */
40 private static final long serialVersionUID = 20150309L;
41
42 /**
43 * The internal storage for the vector; internally the values are stored in standard SI unit; storage can be dense
44 * or sparse.
45 */
46 private FloatMatrix1D vectorSI;
47
48 /**
49 * Construct a new Immutable FloatVector.
50 * @param unit U; the unit of the new FloatVector
51 */
52 protected FloatVector(final U unit)
53 {
54 super(unit);
55 // System.out.println("Created FloatVector");
56 }
57
58 /**
59 * @param <U> Unit
60 */
61 public abstract static class Abs<U extends Unit<U>> extends FloatVector<U> implements Absolute
62 {
63 /** */
64 private static final long serialVersionUID = 20150309L;
65
66 /**
67 * Construct a new Absolute Immutable FloatVector.
68 * @param unit U; the unit of the new Absolute Immutable FloatVector
69 */
70 protected Abs(final U unit)
71 {
72 super(unit);
73 // System.out.println("Created Abs");
74 }
75
76 /**
77 * @param <U> Unit
78 */
79 public static class Dense<U extends Unit<U>> extends Abs<U> implements DenseData
80 {
81 /** */
82 private static final long serialVersionUID = 20150309L;
83
84 /**
85 * Construct a new Absolute Dense Immutable FloatVector.
86 * @param values float[]; the values of the entries in the new Absolute Dense Immutable FloatVector
87 * @param unit U; the unit of the new Absolute Dense Immutable FloatVector
88 * @throws ValueException when values is null
89 */
90 public Dense(final float[] values, final U unit) throws ValueException
91 {
92 super(unit);
93 // System.out.println("Created Dense");
94 initialize(values);
95 }
96
97 /**
98 * Construct a new Absolute Dense Immutable FloatVector.
99 * @param values FloatScalar.Abs<U>[]; the values of the entries in the new Absolute Dense Immutable
100 * FloatVector
101 * @throws ValueException when values has zero entries
102 */
103 public Dense(final FloatScalar.Abs<U>[] values) throws ValueException
104 {
105 super(checkNonEmpty(values)[0].getUnit());
106 // System.out.println("Created Dense");
107 initialize(values);
108 }
109
110 /**
111 * For package internal use only.
112 * @param values FloatMatrix1D; the values of the entries in the new Absolute Dense Immutable FloatVector
113 * @param unit U; the unit of the new Absolute Dense Immutable FloatVector
114 */
115 protected Dense(final FloatMatrix1D values, final U unit)
116 {
117 super(unit);
118 // System.out.println("Created Dense");
119 initialize(values); // shallow copy
120 }
121
122 /** {@inheritDoc} */
123 @Override
124 public final MutableFloatVector.Abs.Dense<U> mutable()
125 {
126 return new MutableFloatVector.Abs.Dense<U>(getVectorSI(), getUnit());
127 }
128
129 /** {@inheritDoc} */
130 @Override
131 protected final FloatMatrix1D createMatrix1D(final int size)
132 {
133 return new DenseFloatMatrix1D(size);
134 }
135
136 /** {@inheritDoc} */
137 @Override
138 public final FloatVector.Abs.Dense<U> copy()
139 {
140 return this; // That was easy...
141 }
142
143 }
144
145 /**
146 * @param <U> Unit
147 */
148 public static class Sparse<U extends Unit<U>> extends Abs<U> implements SparseData
149 {
150 /** */
151 private static final long serialVersionUID = 20150309L;
152
153 /**
154 * Construct a new Absolute Sparse Immutable FloatVector.
155 * @param values float[]; the values of the entries in the new Absolute Sparse Immutable FloatVector
156 * @param unit U; the unit of the new Absolute Sparse Immutable FloatVector
157 * @throws ValueException when values is null
158 */
159 public Sparse(final float[] values, final U unit) throws ValueException
160 {
161 super(unit);
162 // System.out.println("Created Sparse");
163 initialize(values);
164 }
165
166 /**
167 * Construct a new Absolute Sparse Immutable FloatVector.
168 * @param values FloatScalar.Abs<U>[]; the values of the entries in the new Absolute Sparse Immutable
169 * FloatVector
170 * @throws ValueException when values has zero entries
171 */
172 public Sparse(final FloatScalar.Abs<U>[] values) throws ValueException
173 {
174 super(checkNonEmpty(values)[0].getUnit());
175 // System.out.println("Created Sparse");
176 initialize(values);
177 }
178
179 /**
180 * For package internal use only.
181 * @param values FloatMatrix1D; the values of the entries in the new Absolute Sparse Immutable FloatVector
182 * @param unit U; the unit of the new Absolute Sparse Immutable FloatVector
183 */
184 protected Sparse(final FloatMatrix1D values, final U unit)
185 {
186 super(unit);
187 // System.out.println("Created Sparse");
188 initialize(values); // shallow copy
189 }
190
191 /** {@inheritDoc} */
192 @Override
193 public final MutableFloatVector.Abs.Sparse<U> mutable()
194 {
195 return new MutableFloatVector.Abs.Sparse<U>(getVectorSI(), getUnit());
196 }
197
198 /** {@inheritDoc} */
199 @Override
200 protected final FloatMatrix1D createMatrix1D(final int size)
201 {
202 return new SparseFloatMatrix1D(size);
203 }
204
205 /** {@inheritDoc} */
206 @Override
207 public final FloatVector.Abs.Sparse<U> copy()
208 {
209 return this; // That was easy...
210 }
211
212 }
213
214 /** {@inheritDoc} */
215 @Override
216 public final FloatScalar.Abs<U> get(final int index) throws ValueException
217 {
218 return new FloatScalar.Abs<U>(getInUnit(index, getUnit()), getUnit());
219 }
220
221 }
222
223 /**
224 * @param <U> Unit
225 */
226 public abstract static class Rel<U extends Unit<U>> extends FloatVector<U> implements Relative
227 {
228 /** */
229 private static final long serialVersionUID = 20150309L;
230
231 /**
232 * Construct a new Relative Immutable FloatVector.
233 * @param unit U; the unit of the new Relative Immutable FloatVector
234 */
235 protected Rel(final U unit)
236 {
237 super(unit);
238 // System.out.println("Created Rel");
239 }
240
241 /**
242 * @param <U> Unit
243 */
244 public static class Dense<U extends Unit<U>> extends Rel<U> implements DenseData
245 {
246 /** */
247 private static final long serialVersionUID = 20150309L;
248
249 /**
250 * Construct a new Relative Dense Immutable FloatVector.
251 * @param values float[]; the values of the entries in the new Relative Dense Immutable FloatVector
252 * @param unit U; the unit of the new Relative Dense Immutable FloatVector
253 * @throws ValueException when values is null
254 */
255 public Dense(final float[] values, final U unit) throws ValueException
256 {
257 super(unit);
258 // System.out.println("Created Dense");
259 initialize(values);
260 }
261
262 /**
263 * Construct a new Relative Dense Immutable FloatVector.
264 * @param values FloatScalar.Rel<U>[]; the values of the entries in the new Relative Dense Immutable
265 * FloatVector
266 * @throws ValueException when values has zero entries
267 */
268 public Dense(final FloatScalar.Rel<U>[] values) throws ValueException
269 {
270 super(checkNonEmpty(values)[0].getUnit());
271 // System.out.println("Created Dense");
272 initialize(values);
273 }
274
275 /**
276 * For package internal use only.
277 * @param values FloatMatrix1D; the values of the entries in the new Relative Dense Immutable FloatVector
278 * @param unit U; the unit of the new Relative Dense Immutable FloatVector
279 */
280 protected Dense(final FloatMatrix1D values, final U unit)
281 {
282 super(unit);
283 // System.out.println("Created Dense");
284 initialize(values); // shallow copy
285 }
286
287 /** {@inheritDoc} */
288 @Override
289 public final MutableFloatVector.Rel.Dense<U> mutable()
290 {
291 return new MutableFloatVector.Rel.Dense<U>(getVectorSI(), getUnit());
292 }
293
294 /** {@inheritDoc} */
295 @Override
296 protected final FloatMatrix1D createMatrix1D(final int size)
297 {
298 return new DenseFloatMatrix1D(size);
299 }
300
301 /** {@inheritDoc} */
302 @Override
303 public final FloatVector.Rel.Dense<U> copy()
304 {
305 return this; // That was easy...
306 }
307
308 }
309
310 /**
311 * @param <U> Unit
312 */
313 public static class Sparse<U extends Unit<U>> extends Rel<U> implements SparseData
314 {
315 /** */
316 private static final long serialVersionUID = 20150309L;
317
318 /**
319 * Construct a new Relative Sparse Immutable FloatVector.
320 * @param values float[]; the values of the entries in the new Relative Sparse Immutable FloatVector
321 * @param unit U; the unit of the new Relative Sparse Immutable FloatVector
322 * @throws ValueException when values is null
323 */
324 public Sparse(final float[] values, final U unit) throws ValueException
325 {
326 super(unit);
327 // System.out.println("Created Sparse");
328 initialize(values);
329 }
330
331 /**
332 * Construct a new Relative Sparse Immutable FloatVector.
333 * @param values FloatScalar.Rel<U>[]; the values of the entries in the new Relative Sparse Immutable
334 * FloatVector
335 * @throws ValueException when values has zero entries
336 */
337 public Sparse(final FloatScalar.Rel<U>[] values) throws ValueException
338 {
339 super(checkNonEmpty(values)[0].getUnit());
340 // System.out.println("Created Sparse");
341 initialize(values);
342 }
343
344 /**
345 * For package internal use only.
346 * @param values FloatMatrix1D; the values of the entries in the new Relative Sparse Immutable FloatVector
347 * @param unit U; the unit of the new Relative Sparse Immutable FloatVector
348 */
349 protected Sparse(final FloatMatrix1D values, final U unit)
350 {
351 super(unit);
352 // System.out.println("Created Sparse");
353 initialize(values); // shallow copy
354 }
355
356 /** {@inheritDoc} */
357 @Override
358 public final MutableFloatVector.Rel.Sparse<U> mutable()
359 {
360 return new MutableFloatVector.Rel.Sparse<U>(getVectorSI(), getUnit());
361 }
362
363 /** {@inheritDoc} */
364 @Override
365 protected final FloatMatrix1D createMatrix1D(final int size)
366 {
367 return new SparseFloatMatrix1D(size);
368 }
369
370 /** {@inheritDoc} */
371 @Override
372 public final FloatVector.Rel.Sparse<U> copy()
373 {
374 return this; // That was easy...
375 }
376
377 }
378
379 /** {@inheritDoc} */
380 @Override
381 public final FloatScalar.Rel<U> get(final int index) throws ValueException
382 {
383 return new FloatScalar.Rel<U>(getInUnit(index, getUnit()), getUnit());
384 }
385
386 }
387
388 /**
389 * Retrieve the internal data.
390 * @return FloatMatrix1D; the data in the internal format
391 */
392 protected final FloatMatrix1D getVectorSI()
393 {
394 return this.vectorSI;
395 }
396
397 /**
398 * Make a deep copy of the data (used ONLY in the MutableFloatVector sub class).
399 */
400 protected final void deepCopyData()
401 {
402 this.vectorSI = getVectorSI().copy(); // makes a deep copy, using multithreading
403 }
404
405 /**
406 * Create a mutable version of this FloatVector. <br>
407 * The mutable version is created with a shallow copy of the data and the internal copyOnWrite flag set. The first
408 * operation in the mutable version that modifies the data shall trigger a deep copy of the data.
409 * @return MutableFloatVector<U>; mutable version of this FloatVector
410 */
411 public abstract MutableFloatVector<U> mutable();
412
413 /**
414 * Import the values and convert them into the SI standard unit.
415 * @param values float[]; an array of values
416 * @throws ValueException when values is null
417 */
418 protected final void initialize(final float[] values) throws ValueException
419 {
420 if (null == values)
421 {
422 throw new ValueException("values is null");
423 }
424 this.vectorSI = createMatrix1D(values.length);
425 if (getUnit().equals(getUnit().getStandardUnit()))
426 {
427 this.vectorSI.assign(values);
428 }
429 else
430 {
431 for (int index = values.length; --index >= 0;)
432 {
433 safeSet(index, (float) expressAsSIUnit(values[index]));
434 }
435 }
436 }
437
438 /**
439 * Import the values from an existing FloatMatrix1D. This makes a shallow copy.
440 * @param values FloatMatrix1D; the values
441 */
442 protected final void initialize(final FloatMatrix1D values)
443 {
444 this.vectorSI = values;
445 }
446
447 /**
448 * Construct the vector and store the values in the standard SI unit.
449 * @param values FloatScalar<U>[]; an array of values
450 * @throws ValueException when values is null, or empty
451 */
452 protected final void initialize(final FloatScalar<U>[] values) throws ValueException
453 {
454 if (null == values)
455 {
456 throw new ValueException("values is null");
457 }
458 this.vectorSI = createMatrix1D(values.length);
459 for (int index = 0; index < values.length; index++)
460 {
461 safeSet(index, values[index].getSI());
462 }
463 }
464
465 /**
466 * Create storage for the data. <br/>
467 * This method must be implemented by each leaf class.
468 * @param size int; the number of cells in the vector
469 * @return FloatMatrix1D; an instance of the right type of FloatMatrix1D (absolute/relative, dense/sparse, etc.)
470 */
471 protected abstract FloatMatrix1D createMatrix1D(final int size);
472
473 /**
474 * Create a float[] array filled with the values in the standard SI unit.
475 * @return float[]; array of values in the standard SI unit
476 */
477 public final float[] getValuesSI()
478 {
479 return this.vectorSI.toArray(); // this makes a deep copy
480 }
481
482 /**
483 * Create a float[] array filled with the values in the original unit.
484 * @return float[]; the values in the original unit
485 */
486 public final float[] getValuesInUnit()
487 {
488 return getValuesInUnit(getUnit());
489 }
490
491 /**
492 * Create a float[] array filled with the values converted into a specified unit.
493 * @param targetUnit U; the unit into which the values are converted for use
494 * @return float[]; the values converted into the specified unit
495 */
496 public final float[] getValuesInUnit(final U targetUnit)
497 {
498 float[] values = this.vectorSI.toArray();
499 for (int i = values.length; --i >= 0;)
500 {
501 values[i] = (float) ValueUtil.expressAsUnit(values[i], targetUnit);
502 }
503 return values;
504 }
505
506 /** {@inheritDoc} */
507 @Override
508 public final int size()
509 {
510 return (int) this.vectorSI.size();
511 }
512
513 /** {@inheritDoc} */
514 @Override
515 public final float getSI(final int index) throws ValueException
516 {
517 checkIndex(index);
518 return safeGet(index);
519 }
520
521 /** {@inheritDoc} */
522 @Override
523 public final float getInUnit(final int index) throws ValueException
524 {
525 return (float) expressAsSpecifiedUnit(getSI(index));
526 }
527
528 /** {@inheritDoc} */
529 @Override
530 public final float getInUnit(final int index, final U targetUnit) throws ValueException
531 {
532 return (float) ValueUtil.expressAsUnit(getSI(index), targetUnit);
533 }
534
535 /** {@inheritDoc} */
536 @Override
537 public final float zSum()
538 {
539 return this.vectorSI.zSum();
540 }
541
542 /** {@inheritDoc} */
543 @Override
544 public final int cardinality()
545 {
546 return this.vectorSI.cardinality();
547 }
548
549 /** {@inheritDoc} */
550 @Override
551 public final String toString()
552 {
553 return toString(getUnit(), false, true);
554 }
555
556 /**
557 * Print this FloatVector with the values expressed in the specified unit.
558 * @param displayUnit U; the unit into which the values are converted for display
559 * @return String; printable string with the vector contents expressed in the specified unit
560 */
561 public final String toString(final U displayUnit)
562 {
563 return toString(displayUnit, false, true);
564 }
565
566 /**
567 * Print this FloatVector with optional type and unit information.
568 * @param verbose boolean; if true; include type info; if false; exclude type info
569 * @param withUnit boolean; if true; include the unit; of false; exclude the unit
570 * @return String; printable string with the vector contents
571 */
572 public final String toString(final boolean verbose, final boolean withUnit)
573 {
574 return toString(getUnit(), verbose, withUnit);
575 }
576
577 /**
578 * Print this FloatVector with the values expressed in the specified unit.
579 * @param displayUnit U; the unit into which the values are converted for display
580 * @param verbose boolean; if true; include type info; if false; exclude type info
581 * @param withUnit boolean; if true; include the unit; of false; exclude the unit
582 * @return String; printable string with the vector contents
583 */
584 public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
585 {
586 StringBuffer buf = new StringBuffer();
587 if (verbose)
588 {
589 if (this instanceof MutableFloatVector)
590 {
591 buf.append("Mutable ");
592 if (this instanceof MutableFloatVector.Abs.Dense)
593 {
594 buf.append("Abs Dense ");
595 }
596 else if (this instanceof MutableFloatVector.Rel.Dense)
597 {
598 buf.append("Rel Dense ");
599 }
600 else if (this instanceof MutableFloatVector.Abs.Sparse)
601 {
602 buf.append("Abs Sparse ");
603 }
604 else if (this instanceof MutableFloatVector.Rel.Sparse)
605 {
606 buf.append("Rel Sparse ");
607 }
608 else
609 {
610 buf.append("??? ");
611 }
612 }
613 else
614 {
615 buf.append("Immutable ");
616 if (this instanceof FloatVector.Abs.Dense)
617 {
618 buf.append("Abs Dense ");
619 }
620 else if (this instanceof FloatVector.Rel.Dense)
621 {
622 buf.append("Rel Dense ");
623 }
624 else if (this instanceof FloatVector.Abs.Sparse)
625 {
626 buf.append("Abs Sparse ");
627 }
628 else if (this instanceof FloatVector.Rel.Sparse)
629 {
630 buf.append("Rel Sparse ");
631 }
632 else
633 {
634 buf.append("??? ");
635 }
636 }
637 }
638 for (int i = 0; i < size(); i++)
639 {
640 float f = (float) ValueUtil.expressAsUnit(safeGet(i), displayUnit);
641 buf.append(" " + Format.format(f));
642 }
643 if (withUnit)
644 {
645 buf.append(displayUnit.getAbbreviation());
646 }
647 return buf.toString();
648 }
649
650 /**
651 * Centralized size equality check.
652 * @param other FloatVector<?>; other FloatVector
653 * @throws ValueException when other is null, or vectors have unequal size
654 */
655 protected final void checkSize(final FloatVector<?> other) throws ValueException
656 {
657 if (null == other)
658 {
659 throw new ValueException("other is null");
660 }
661 if (size() != other.size())
662 {
663 throw new ValueException("The vectors have different sizes: " + size() + " != " + other.size());
664 }
665 }
666
667 /**
668 * Centralized size equality check.
669 * @param other float[]; array of float
670 * @throws ValueException when vectors have unequal size
671 */
672 protected final void checkSize(final float[] other) throws ValueException
673 {
674 if (size() != other.length)
675 {
676 throw new ValueException("The vector and the array have different sizes: " + size() + " != " + other.length);
677 }
678 }
679
680 /**
681 * Check that a provided index is valid.
682 * @param index int; the value to check
683 * @throws ValueException when index is invalid
684 */
685 protected final void checkIndex(final int index) throws ValueException
686 {
687 if (index < 0 || index >= size())
688 {
689 throw new ValueException("index out of range (valid range is 0.." + (size() - 1) + ", got " + index + ")");
690 }
691 }
692
693 /**
694 * Retrieve a value in vectorSI without checking validity of the index.
695 * @param index int; the index
696 * @return float; the value stored at that index
697 */
698 protected final float safeGet(final int index)
699 {
700 return this.vectorSI.getQuick(index);
701 }
702
703 /**
704 * Modify a value in vectorSI without checking validity of the index.
705 * @param index int; the index
706 * @param valueSI float; the new value for the entry in vectorSI
707 */
708 protected final void safeSet(final int index, final float valueSI)
709 {
710 this.vectorSI.setQuick(index, valueSI);
711 }
712
713 /**
714 * Create a deep copy of the data.
715 * @return FloatMatrix1D; deep copy of the data
716 */
717 protected final FloatMatrix1D deepCopyOfData()
718 {
719 return this.vectorSI.copy();
720 }
721
722 /**
723 * Check that a provided array can be used to create some descendant of a FloatVector.
724 * @param fsArray FloatScalar<U>[]; the provided array
725 * @param <U> Unit; the unit of the FloatScalar array
726 * @return FloatScalar<U>[]; the provided array
727 * @throws ValueException when the array has length equal to 0
728 */
729 protected static <U extends Unit<U>> FloatScalar<U>[] checkNonEmpty(final FloatScalar<U>[] fsArray)
730 throws ValueException
731 {
732 if (0 == fsArray.length)
733 {
734 throw new ValueException(
735 "Cannot create a FloatVector or MutableFloatVector from an empty array of FloatScalar");
736 }
737 return fsArray;
738 }
739
740 /** {@inheritDoc} */
741 @Override
742 public final int hashCode()
743 {
744 final int prime = 31;
745 int result = 1;
746 result = prime * result + this.vectorSI.hashCode();
747 return result;
748 }
749
750 /** {@inheritDoc} */
751 @Override
752 public final boolean equals(final Object obj)
753 {
754 if (this == obj)
755 {
756 return true;
757 }
758 if (obj == null)
759 {
760 return false;
761 }
762 if (!(obj instanceof FloatVector))
763 {
764 return false;
765 }
766 FloatVector<?> other = (FloatVector<?>) obj;
767 // unequal if not both Absolute or both Relative
768 if (this.isAbsolute() != other.isAbsolute() || this.isRelative() != other.isRelative())
769 {
770 return false;
771 }
772 // unequal if the standard SI units differ
773 if (!this.getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
774 {
775 return false;
776 }
777 // Colt's equals also tests the size of the vector
778 if (!getVectorSI().equals(other.getVectorSI()))
779 {
780 return false;
781 }
782 return true;
783 }
784
785 /**********************************************************************************/
786 /********************************* STATIC METHODS *********************************/
787 /**********************************************************************************/
788
789 /**
790 * Add two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
791 * @param left FloatVector.Abs.Dense<U>; the left operand
792 * @param right FloatVector.Rel<U>; the right operand
793 * @param <U> Unit; the unit of the parameters and the result
794 * @return MutableFloatVector.Abs.Dense<U>
795 * @throws ValueException when the vectors do not have the same size
796 */
797 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> plus(final FloatVector.Abs.Dense<U> left,
798 final FloatVector.Rel<U> right) throws ValueException
799 {
800 return (MutableFloatVector.Abs.Dense<U>) left.mutable().incrementBy(right);
801 }
802
803 /**
804 * Add two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
805 * @param left FloatVector.Abs.Sparse<U>; the left operand
806 * @param right FloatVector.Rel.Dense<U>; the right operand
807 * @param <U> Unit; the unit of the parameters and the result
808 * @return MutableFloatVector.Abs.Dense<U>
809 * @throws ValueException when the vectors do not have the same size
810 */
811 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> plus(final FloatVector.Abs.Sparse<U> left,
812 final FloatVector.Rel.Dense<U> right) throws ValueException
813 {
814 return (MutableFloatVector.Abs.Dense<U>) sparseToDense(left).incrementBy(right);
815 }
816
817 /**
818 * Add two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Sparse<U>.
819 * @param left FloatVector.Abs.Sparse<U>; the left operand
820 * @param right FloatVector.Rel.Sparse<U>; the right operand
821 * @param <U> Unit; the unit of the parameters and the result
822 * @return MutableFloatVector.Abs.Sparse<U>
823 * @throws ValueException when the vectors do not have the same size
824 */
825 public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> plus(final FloatVector.Abs.Sparse<U> left,
826 final FloatVector.Rel.Sparse<U> right) throws ValueException
827 {
828 return (MutableFloatVector.Abs.Sparse<U>) left.mutable().incrementBy(right);
829 }
830
831 /**
832 * Add two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
833 * @param left FloatVector.Rel.Dense<U>; the left operand
834 * @param right FloatVector.Rel<U>; the right operand
835 * @param <U> Unit; the unit of the parameters and the result
836 * @return MutableFloatVector.Rel.Dense<U>
837 * @throws ValueException when the vectors do not have the same size
838 */
839 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> plus(final FloatVector.Rel.Dense<U> left,
840 final FloatVector.Rel<U> right) throws ValueException
841 {
842 return (MutableFloatVector.Rel.Dense<U>) left.mutable().incrementBy(right);
843 }
844
845 /**
846 * Add two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
847 * @param left FloatVector.Rel.Sparse<U>; the left operand
848 * @param right FloatVector.Rel.Dense<U>; the right operand
849 * @param <U> Unit; the unit of the parameters and the result
850 * @return MutableFloatVector.Rel.Dense<U>
851 * @throws ValueException when the vectors do not have the same size
852 */
853 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> plus(final FloatVector.Rel.Sparse<U> left,
854 final FloatVector.Rel.Dense<U> right) throws ValueException
855 {
856 return (MutableFloatVector.Rel.Dense<U>) sparseToDense(left).incrementBy(right);
857 }
858
859 /**
860 * Add two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Sparse<U>.
861 * @param left FloatVector.Rel.Sparse<U>; the left operand
862 * @param right FloatVector.Rel.Sparse<U>; the right operand
863 * @param <U> Unit; the unit of the parameters and the result
864 * @return MutableFloatVector.Rel.Sparse<U>
865 * @throws ValueException when the vectors do not have the same size
866 */
867 public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> plus(final FloatVector.Rel.Sparse<U> left,
868 final FloatVector.Rel.Sparse<U> right) throws ValueException
869 {
870 return (MutableFloatVector.Rel.Sparse<U>) left.mutable().incrementBy(right);
871 }
872
873 /**
874 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
875 * @param left FloatVector.Abs.Dense<U>; the left operand
876 * @param right FloatVector.Abs<U>; the right operand
877 * @param <U> Unit; the unit of the parameters and the result
878 * @return MutableFloatVector.Rel.Dense<U>
879 * @throws ValueException when the vectors do not have the same size
880 */
881 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Abs.Dense<U> left,
882 final FloatVector.Abs<U> right) throws ValueException
883 {
884 return (MutableFloatVector.Rel.Dense<U>) new MutableFloatVector.Rel.Dense<U>(left.deepCopyOfData(),
885 left.getUnit()).decrementBy(right);
886 }
887
888 /**
889 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Sparse<U>.
890 * @param left FloatVector.Abs.Sparse<U>; the left operand
891 * @param right FloatVector.Abs.Sparse<U>; the right operand
892 * @param <U> Unit; the unit of the parameters and the result
893 * @return MutableFloatVector.Rel.Sparse<U>
894 * @throws ValueException when the vectors do not have the same size
895 */
896 public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> minus(final FloatVector.Abs.Sparse<U> left,
897 final FloatVector.Abs.Sparse<U> right) throws ValueException
898 {
899 return (MutableFloatVector.Rel.Sparse<U>) new MutableFloatVector.Rel.Sparse<U>(left.deepCopyOfData(),
900 left.getUnit()).decrementBy(right);
901 }
902
903 /**
904 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
905 * @param left FloatVector.Abs.Sparse<U>; the left operand
906 * @param right FloatVector.Abs.Dense<U>; the right operand
907 * @param <U> Unit; the unit of the parameters and the result
908 * @return MutableFloatVector.Rel.Dense<U>
909 * @throws ValueException when the vectors do not have the same size
910 */
911 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Abs.Sparse<U> left,
912 final FloatVector.Abs.Dense<U> right) throws ValueException
913 {
914 return (MutableFloatVector.Rel.Dense<U>) new MutableFloatVector.Rel.Dense<U>(left.deepCopyOfData(),
915 left.getUnit()).decrementBy(right);
916 }
917
918 /**
919 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
920 * @param left FloatVector.Abs.Dense<U>; the left operand
921 * @param right FloatVector.Rel<U>; the right operand
922 * @param <U> Unit; the unit of the parameters and the result
923 * @return MutableFloatVector.Abs.Dense<U>
924 * @throws ValueException when the vectors do not have the same size
925 */
926 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> minus(final FloatVector.Abs.Dense<U> left,
927 final FloatVector.Rel<U> right) throws ValueException
928 {
929 return (MutableFloatVector.Abs.Dense<U>) left.mutable().decrementBy(right);
930 }
931
932 /**
933 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
934 * @param left FloatVector.Abs.Sparse<U>; the left operand
935 * @param right FloatVector.Rel.Dense<U>; the right operand
936 * @param <U> Unit; the unit of the parameters and the result
937 * @return MutableFloatVector.Abs.Dense<U>
938 * @throws ValueException when the vectors do not have the same size
939 */
940 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> minus(final FloatVector.Abs.Sparse<U> left,
941 final FloatVector.Rel.Dense<U> right) throws ValueException
942 {
943 return (MutableFloatVector.Abs.Dense<U>) sparseToDense(left).decrementBy(right);
944 }
945
946 /**
947 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Sparse<U>.
948 * @param left FloatVector.Abs.Sparse<U>; the left operand
949 * @param right FloatVector.Rel.Sparse<U>; the right operand
950 * @param <U> Unit; the unit of the parameters and the result
951 * @return MutableFloatVector.Abs.Sparse<U>
952 * @throws ValueException when the vectors do not have the same size
953 */
954 public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> minus(final FloatVector.Abs.Sparse<U> left,
955 final FloatVector.Rel.Sparse<U> right) throws ValueException
956 {
957 return (MutableFloatVector.Abs.Sparse<U>) left.mutable().decrementBy(right);
958 }
959
960 /**
961 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
962 * @param left FloatVector.Rel.Dense<U>; the left operand
963 * @param right FloatVector.Rel<U>; the right operand
964 * @param <U> Unit; the unit of the parameters and the result
965 * @return MutableFloatVector.Rel.Dense<U>
966 * @throws ValueException when the vectors do not have the same size
967 */
968 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Rel.Dense<U> left,
969 final FloatVector.Rel<U> right) throws ValueException
970 {
971 return (MutableFloatVector.Rel.Dense<U>) left.mutable().decrementBy(right);
972 }
973
974 /**
975 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
976 * @param left FloatVector.Rel.Sparse<U>; the left operand
977 * @param right FloatVector.Rel.Dense<U>; the right operand
978 * @param <U> Unit; the unit of the parameters and the result
979 * @return MutableFloatVector.Rel.Dense<U>
980 * @throws ValueException when the vectors do not have the same size
981 */
982 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Rel.Sparse<U> left,
983 final FloatVector.Rel.Dense<U> right) throws ValueException
984 {
985 return (MutableFloatVector.Rel.Dense<U>) sparseToDense(left).decrementBy(right);
986 }
987
988 /**
989 * Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Sparse<U>.
990 * @param left FloatVector.Rel.Sparse<U>; the left operand
991 * @param right FloatVector.Rel.Sparse<U>; the right operand
992 * @param <U> Unit; the unit of the parameters and the result
993 * @return MutableFloatVector.Rel.Sparse<U>
994 * @throws ValueException when the vectors do not have the same size
995 */
996 public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> minus(final FloatVector.Rel.Sparse<U> left,
997 final FloatVector.Rel.Sparse<U> right) throws ValueException
998 {
999 return (MutableFloatVector.Rel.Sparse<U>) left.mutable().decrementBy(right);
1000 }
1001
1002 // TODO Decide if you ever need multiply an Absolute with anything
1003 /**
1004 * Multiply two FloatVectors value by value and store the result in a new
1005 * MutableFloatVector.Abs.Dense<SIUnit>.
1006 * @param left FloatVector.Abs.Dense<?>; the left operand
1007 * @param right FloatVector.Abs.Dense<?>; the right operand
1008 * @return MutableFloatVector.Abs.Dense<SIUnit>
1009 * @throws ValueException when the vectors do not have the same size
1010 */
1011 public static MutableFloatVector.Abs.Dense<SIUnit> times(final FloatVector.Abs.Dense<?> left,
1012 final FloatVector.Abs.Dense<?> right) throws ValueException
1013 {
1014 SIUnit targetUnit =
1015 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1016 right.getUnit().getSICoefficients()).toString());
1017 MutableFloatVector.Abs.Dense<SIUnit> work =
1018 new MutableFloatVector.Abs.Dense<SIUnit>(left.deepCopyOfData(), targetUnit);
1019 work.scaleValueByValue(right);
1020 return work;
1021 }
1022
1023 /**
1024 * Multiply two FloatVectors value by value and store the result in a new
1025 * MutableFloatVector.Abs.Sparse<SIUnit>.
1026 * @param left FloatVector.Abs.Dense<?>; the left operand
1027 * @param right FloatVector.Abs.Sparse<?>; the right operand
1028 * @return MutableFloatVector.Abs.Sparse<SIUnit>
1029 * @throws ValueException when the vectors do not have the same size
1030 */
1031 public static MutableFloatVector.Abs.Sparse<SIUnit> times(final FloatVector.Abs.Dense<?> left,
1032 final FloatVector.Abs.Sparse<?> right) throws ValueException
1033 {
1034 SIUnit targetUnit =
1035 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1036 right.getUnit().getSICoefficients()).toString());
1037 MutableFloatVector.Abs.Sparse<SIUnit> work =
1038 new MutableFloatVector.Abs.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1039 work.scaleValueByValue(right);
1040 return work;
1041 }
1042
1043 /**
1044 * Multiply two FloatVectors value by value and store the result in a new
1045 * MutableFloatVector.Abs.Sparse<SIUnit>.
1046 * @param left FloatVector.Abs.Sparse<?>; the left operand
1047 * @param right FloatVector.Abs<?>; the right operand
1048 * @return MutableFloatVector.Abs.Sparse<SIUnit>
1049 * @throws ValueException when the vectors do not have the same size
1050 */
1051 public static MutableFloatVector.Abs.Sparse<SIUnit> times(final FloatVector.Abs.Sparse<?> left,
1052 final FloatVector.Abs<?> right) throws ValueException
1053 {
1054 SIUnit targetUnit =
1055 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1056 right.getUnit().getSICoefficients()).toString());
1057 MutableFloatVector.Abs.Sparse<SIUnit> work =
1058 new MutableFloatVector.Abs.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1059 work.scaleValueByValue(right);
1060 return work;
1061 }
1062
1063 /**
1064 * Multiply two FloatVectors value by value and store the result in a new
1065 * MutableFloatVector.Rel.Dense<SIUnit>.
1066 * @param left FloatVector.Rel.Dense<?>; the left operand
1067 * @param right FloatVector.Rel.Dense<?>; the right operand
1068 * @return MutableFloatVector.Rel.Dense<SIUnit>
1069 * @throws ValueException when the vectors do not have the same size
1070 */
1071 public static MutableFloatVector.Rel.Dense<SIUnit> times(final FloatVector.Rel.Dense<?> left,
1072 final FloatVector.Rel.Dense<?> right) throws ValueException
1073 {
1074 SIUnit targetUnit =
1075 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1076 right.getUnit().getSICoefficients()).toString());
1077 MutableFloatVector.Rel.Dense<SIUnit> work =
1078 new MutableFloatVector.Rel.Dense<SIUnit>(left.deepCopyOfData(), targetUnit);
1079 work.scaleValueByValue(right);
1080 return work;
1081 }
1082
1083 /**
1084 * Multiply two FloatVectors value by value and store the result in a new
1085 * MutableFloatVector.Rel.Sparse<SIUnit>.
1086 * @param left FloatVector.Rel.Dense<?>; the left operand
1087 * @param right FloatVector.Rel.Sparse<?>; the right operand
1088 * @return MutableFloatVector.Rel.Sparse<SIUnit>
1089 * @throws ValueException when the vectors do not have the same size
1090 */
1091 public static MutableFloatVector.Rel.Sparse<SIUnit> times(final FloatVector.Rel.Dense<?> left,
1092 final FloatVector.Rel.Sparse<?> right) throws ValueException
1093 {
1094 SIUnit targetUnit =
1095 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1096 right.getUnit().getSICoefficients()).toString());
1097 MutableFloatVector.Rel.Sparse<SIUnit> work =
1098 new MutableFloatVector.Rel.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1099 work.scaleValueByValue(right);
1100 return work;
1101 }
1102
1103 /**
1104 * Multiply two FloatVectors value by value and store the result in a new
1105 * MutableFloatVector.Rel.Sparse<SIUnit>.
1106 * @param left FloatVector.Rel.Sparse<?>; the left operand
1107 * @param right FloatVector.Rel<?>; the right operand
1108 * @return MutableFloatVector.Rel.Sparse<SIUnit>
1109 * @throws ValueException when the vectors do not have the same size
1110 */
1111 public static MutableFloatVector.Rel.Sparse<SIUnit> times(final FloatVector.Rel.Sparse<?> left,
1112 final FloatVector.Rel<?> right) throws ValueException
1113 {
1114 SIUnit targetUnit =
1115 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1116 right.getUnit().getSICoefficients()).toString());
1117 MutableFloatVector.Rel.Sparse<SIUnit> work =
1118 new MutableFloatVector.Rel.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1119 work.scaleValueByValue(right);
1120 return work;
1121 }
1122
1123 /**
1124 * Multiply the values in a FloatVector and a float array value by value and store the result in a new
1125 * MutableFloatVector.Abs.Dense<U>.
1126 * @param left FloatVector.Abs.Dense<U>; the FloatVector
1127 * @param right float[]; the float array
1128 * @param <U> Unit; the unit of the left parameter and the result
1129 * @return MutableFloatVector.Abs.Dense<U>
1130 * @throws ValueException when the FloatVector and the array do not have the same size
1131 */
1132 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> times(final FloatVector.Abs.Dense<U> left,
1133 final float[] right) throws ValueException
1134 {
1135 return (MutableFloatVector.Abs.Dense<U>) left.mutable().scaleValueByValue(right);
1136 }
1137
1138 /**
1139 * Multiply the values in a FloatVector and a float array value by value and store the result in a new
1140 * MutableFloatVector.Abs.Sparse<U>.
1141 * @param left FloatVector.Abs.Sparse<U>; the FloatVector
1142 * @param right float[]; the float array
1143 * @param <U> Unit; the unit of the left parameter and the result
1144 * @return MutableFloatVector.Abs.Sparse<U>
1145 * @throws ValueException when the FloatVector and the array do not have the same size
1146 */
1147 public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> times(final FloatVector.Abs.Sparse<U> left,
1148 final float[] right) throws ValueException
1149 {
1150 return (MutableFloatVector.Abs.Sparse<U>) left.mutable().scaleValueByValue(right);
1151 }
1152
1153 /**
1154 * Multiply the values in a FloatVector and a float array value by value and store the result in a new
1155 * MutableFloatVector.Rel.Dense<U>.
1156 * @param left FloatVector.Rel.Dense<U>; the FloatVector
1157 * @param right float[]; the float array
1158 * @param <U> Unit; the unit of the left parameter and the result
1159 * @return MutableFloatVector.Rel.Dense<U>
1160 * @throws ValueException when the FloatVector and the array do not have the same size
1161 */
1162 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> times(final FloatVector.Rel.Dense<U> left,
1163 final float[] right) throws ValueException
1164 {
1165 return (MutableFloatVector.Rel.Dense<U>) left.mutable().scaleValueByValue(right);
1166 }
1167
1168 /**
1169 * Multiply the values in a FloatVector and a float array value by value and store the result in a new
1170 * MutableFloatVector.Rel.Sparse<U>.
1171 * @param left FloatVector.Rel.Sparse<U>; the FloatVector
1172 * @param right float[]; the float array
1173 * @param <U> Unit; the unit of the left parameter and the result
1174 * @return MutableFloatVector.Rel.Sparse<U>
1175 * @throws ValueException when the FloatVector and the array do not have the same size
1176 */
1177 public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> times(final FloatVector.Rel.Sparse<U> left,
1178 final float[] right) throws ValueException
1179 {
1180 return (MutableFloatVector.Rel.Sparse<U>) left.mutable().scaleValueByValue(right);
1181 }
1182
1183 /**
1184 * Make the Sparse equivalent of a DenseFloatMatrix1D.
1185 * @param dense FloatMatrix1D; the Dense FloatMatrix1D
1186 * @return SparseFloatMatrix1D
1187 */
1188 private static SparseFloatMatrix1D makeSparse(final FloatMatrix1D dense)
1189 {
1190 SparseFloatMatrix1D result = new SparseFloatMatrix1D((int) dense.size());
1191 result.assign(dense);
1192 return result;
1193 }
1194
1195 /**
1196 * Create a Sparse version of a Dense FloatVector.
1197 * @param in FloatVector.Abs.Dense<U>; the Dense FloatVector
1198 * @param <U> Unit; the unit of the parameter and the result
1199 * @return MutableFloatVector.Abs.Sparse<U>
1200 */
1201 public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> denseToSparse(final FloatVector.Abs.Dense<U> in)
1202 {
1203 return new MutableFloatVector.Abs.Sparse<U>(makeSparse(in.getVectorSI()), in.getUnit());
1204 }
1205
1206 /**
1207 * Create a Sparse version of a Dense FloatVector.
1208 * @param in FloatVector.Rel.Dense<U>; the Dense FloatVector
1209 * @param <U> Unit; the unit of the parameter and the result
1210 * @return MutableFloatVector.Rel.Sparse<U>
1211 */
1212 public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> denseToSparse(final FloatVector.Rel.Dense<U> in)
1213 {
1214 return new MutableFloatVector.Rel.Sparse<U>(makeSparse(in.getVectorSI()), in.getUnit());
1215 }
1216
1217 /**
1218 * Make the Dense equivalent of a SparseFloatMatrix1D.
1219 * @param sparse FloatMatrix1D; the Sparse FloatMatrix1D
1220 * @return DenseFloatMatrix1D
1221 */
1222 private static DenseFloatMatrix1D makeDense(final FloatMatrix1D sparse)
1223 {
1224 DenseFloatMatrix1D result = new DenseFloatMatrix1D((int) sparse.size());
1225 result.assign(sparse);
1226 return result;
1227 }
1228
1229 /**
1230 * Create a Dense version of a Sparse FloatVector.
1231 * @param in FloatVector.Abs.Sparse<U>; the Sparse FloatVector
1232 * @param <U> Unit; the unit of the parameter and the result
1233 * @return MutableFloatVector.Abs.Dense<U>
1234 */
1235 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> sparseToDense(final FloatVector.Abs.Sparse<U> in)
1236 {
1237 return new MutableFloatVector.Abs.Dense<U>(makeDense(in.getVectorSI()), in.getUnit());
1238 }
1239
1240 /**
1241 * Create a Dense version of a Sparse FloatVector.
1242 * @param in FloatVector.Rel.Sparse<U>; the Sparse FloatVector
1243 * @param <U> Unit; the unit of the parameter and the result
1244 * @return MutableFloatVector.Rel.Dense<U>
1245 */
1246 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> sparseToDense(final FloatVector.Rel.Sparse<U> in)
1247 {
1248 return new MutableFloatVector.Rel.Dense<U>(makeDense(in.getVectorSI()), in.getUnit());
1249 }
1250
1251 /**
1252 * Interpolate between or extrapolate over two values.
1253 * @param zero FloatVector.Abs.Dense<U>; zero reference (returned when ratio == 0)
1254 * @param one FloatVector.Abs.Dense<U>; one reference (returned when ratio == 1)
1255 * @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
1256 * @param <U> Unit; the unit of the parameters and the result
1257 * @return MutableFloatVector.Abs.Dense<U>
1258 * @throws ValueException when zero and one do not have the same size
1259 */
1260 public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> interpolate(final FloatVector.Abs.Dense<U> zero,
1261 final FloatVector.Abs.Dense<U> one, final float ratio) throws ValueException
1262 {
1263 MutableFloatVector.Abs.Dense<U> result = zero.mutable();
1264 for (int index = result.size(); --index >= 0;)
1265 {
1266 result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
1267 }
1268 return result;
1269 }
1270
1271 /**
1272 * Interpolate between or extrapolate over two values.
1273 * @param zero FloatVector.Rel.Dense<U>; zero reference (returned when ratio == 0)
1274 * @param one FloatVector.Rel.Dense<U>; one reference (returned when ratio == 1)
1275 * @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
1276 * @param <U> Unit; the unit of the parameters and the result
1277 * @return MutableFloatVector.Rel.Dense<U>
1278 * @throws ValueException when zero and one do not have the same size
1279 */
1280 public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> interpolate(final FloatVector.Rel.Dense<U> zero,
1281 final FloatVector.Rel.Dense<U> one, final float ratio) throws ValueException
1282 {
1283 MutableFloatVector.Rel.Dense<U> result = zero.mutable();
1284 for (int index = result.size(); --index >= 0;)
1285 {
1286 result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
1287 }
1288 return result;
1289 }
1290
1291 /**
1292 * Interpolate between or extrapolate over two values.
1293 * @param zero FloatVector.Abs.Sparse<U>; zero reference (returned when ratio == 0)
1294 * @param one FloatVector.Abs.Sparse<U>; one reference (returned when ratio == 1)
1295 * @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
1296 * @param <U> Unit; the unit of the parameters and the result
1297 * @return MutableFloatVector.Abs.Sparse<U>
1298 * @throws ValueException when zero and one do not have the same size
1299 */
1300 public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> interpolate(final FloatVector.Abs.Sparse<U> zero,
1301 final FloatVector.Abs.Sparse<U> one, final float ratio) throws ValueException
1302 {
1303 MutableFloatVector.Abs.Sparse<U> result = zero.mutable();
1304 for (int index = result.size(); --index >= 0;)
1305 {
1306 result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
1307 }
1308 return result;
1309 }
1310
1311 /**
1312 * Interpolate between or extrapolate over two values.
1313 * @param zero FloatVector.Rel.Sparse<U>; zero reference (returned when ratio == 0)
1314 * @param one FloatVector.Rel.Sparse<U>; one reference (returned when ratio == 1)
1315 * @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
1316 * @param <U> Unit; the unit of the parameters and the result
1317 * @return MutableFloatVector.Rel.Sparse<U>
1318 * @throws ValueException when zero and one do not have the same size
1319 */
1320 public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> interpolate(final FloatVector.Rel.Sparse<U> zero,
1321 final FloatVector.Rel.Sparse<U> one, final float ratio) throws ValueException
1322 {
1323 MutableFloatVector.Rel.Sparse<U> result = zero.mutable();
1324 for (int index = result.size(); --index >= 0;)
1325 {
1326 result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
1327 }
1328 return result;
1329 }
1330
1331 }