View Javadoc
1   package org.opentrafficsim.core.value.vdouble.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.vdouble.scalar.DoubleScalar;
17  
18  import cern.colt.matrix.tdouble.DoubleMatrix1D;
19  import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D;
20  import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix1D;
21  
22  /**
23   * Immutable DoubleVector.
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 DoubleVector
34   */
35  public abstract class DoubleVector<U extends Unit<U>> extends AbstractValue<U> implements
36          Serializable,
37      ReadOnlyDoubleVectorFunctions<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 DoubleMatrix1D vectorSI;
47  
48      /**
49       * Construct a new Immutable DoubleVector.
50       * @param unit U; the unit of the new DoubleVector
51       */
52      protected  DoubleVector(final U unit)
53      {
54          super(unit);
55          // System.out.println("Created DoubleVector");
56      }
57  
58      /**
59       * @param <U> Unit
60       */
61      public abstract static class Abs<U extends Unit<U>> extends DoubleVector<U> implements Absolute
62      {
63          /**  */
64          private static final long serialVersionUID = 20150309L;
65  
66          /**
67           * Construct a new Absolute Immutable DoubleVector.
68           * @param unit U; the unit of the new Absolute Immutable DoubleVector
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 DoubleVector.
86               * @param values double[]; the values of the entries in the new Absolute Dense Immutable DoubleVector
87               * @param unit U; the unit of the new Absolute Dense Immutable DoubleVector
88               * @throws ValueException when values is null
89               */
90              public Dense(final double[] 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 DoubleVector.
99               * @param values DoubleScalar.Abs&lt;U&gt;[]; the values of the entries in the new Absolute Dense Immutable
100              *            DoubleVector
101              * @throws ValueException when values has zero entries
102              */
103             public Dense(final DoubleScalar.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 DoubleMatrix1D; the values of the entries in the new Absolute Dense Immutable DoubleVector
113              * @param unit U; the unit of the new Absolute Dense Immutable DoubleVector
114              */
115             protected Dense(final DoubleMatrix1D 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 MutableDoubleVector.Abs.Dense<U> mutable()
125             {
126                 return new MutableDoubleVector.Abs.Dense<U>(getVectorSI(), getUnit());
127             }
128 
129             /** {@inheritDoc} */
130             @Override
131             protected final DoubleMatrix1D createMatrix1D(final int size)
132             {
133                 return new DenseDoubleMatrix1D(size);
134             }
135 
136             /** {@inheritDoc} */
137             @Override
138             public final DoubleVector.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 DoubleVector.
155              * @param values double[]; the values of the entries in the new Absolute Sparse Immutable DoubleVector
156              * @param unit U; the unit of the new Absolute Sparse Immutable DoubleVector
157              * @throws ValueException when values is null
158              */
159             public Sparse(final double[] 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 DoubleVector.
168              * @param values DoubleScalar.Abs&lt;U&gt;[]; the values of the entries in the new Absolute Sparse Immutable
169              *            DoubleVector
170              * @throws ValueException when values has zero entries
171              */
172             public Sparse(final DoubleScalar.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 DoubleMatrix1D; the values of the entries in the new Absolute Sparse Immutable DoubleVector
182              * @param unit U; the unit of the new Absolute Sparse Immutable DoubleVector
183              */
184             protected Sparse(final DoubleMatrix1D 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 MutableDoubleVector.Abs.Sparse<U> mutable()
194             {
195                 return new MutableDoubleVector.Abs.Sparse<U>(getVectorSI(), getUnit());
196             }
197 
198             /** {@inheritDoc} */
199             @Override
200             protected final DoubleMatrix1D createMatrix1D(final int size)
201             {
202                 return new SparseDoubleMatrix1D(size);
203             }
204 
205             /** {@inheritDoc} */
206             @Override
207             public final DoubleVector.Abs.Sparse<U> copy()
208             {
209                 return this; // That was easy...
210             }
211 
212         }
213 
214         /** {@inheritDoc} */
215         @Override
216         public final DoubleScalar.Abs<U> get(final int index) throws ValueException
217         {
218             return new DoubleScalar.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 DoubleVector<U> implements Relative
227     {
228         /**  */
229         private static final long serialVersionUID = 20150309L;
230 
231         /**
232          * Construct a new Relative Immutable DoubleVector.
233          * @param unit U; the unit of the new Relative Immutable DoubleVector
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 DoubleVector.
251              * @param values double[]; the values of the entries in the new Relative Dense Immutable DoubleVector
252              * @param unit U; the unit of the new Relative Dense Immutable DoubleVector
253              * @throws ValueException when values is null
254              */
255             public Dense(final double[] 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 DoubleVector.
264              * @param values DoubleScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Dense Immutable
265              *            DoubleVector
266              * @throws ValueException when values has zero entries
267              */
268             public Dense(final DoubleScalar.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 DoubleMatrix1D; the values of the entries in the new Relative Dense Immutable DoubleVector
278              * @param unit U; the unit of the new Relative Dense Immutable DoubleVector
279              */
280             protected Dense(final DoubleMatrix1D 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 MutableDoubleVector.Rel.Dense<U> mutable()
290             {
291                 return new MutableDoubleVector.Rel.Dense<U>(getVectorSI(), getUnit());
292             }
293 
294             /** {@inheritDoc} */
295             @Override
296             protected final DoubleMatrix1D createMatrix1D(final int size)
297             {
298                 return new DenseDoubleMatrix1D(size);
299             }
300 
301             /** {@inheritDoc} */
302             @Override
303             public final DoubleVector.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 DoubleVector.
320              * @param values double[]; the values of the entries in the new Relative Sparse Immutable DoubleVector
321              * @param unit U; the unit of the new Relative Sparse Immutable DoubleVector
322              * @throws ValueException when values is null
323              */
324             public Sparse(final double[] 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 DoubleVector.
333              * @param values DoubleScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Sparse Immutable
334              *            DoubleVector
335              * @throws ValueException when values has zero entries
336              */
337             public Sparse(final DoubleScalar.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 DoubleMatrix1D; the values of the entries in the new Relative Sparse Immutable DoubleVector
347              * @param unit U; the unit of the new Relative Sparse Immutable DoubleVector
348              */
349             protected Sparse(final DoubleMatrix1D 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 MutableDoubleVector.Rel.Sparse<U> mutable()
359             {
360                 return new MutableDoubleVector.Rel.Sparse<U>(getVectorSI(), getUnit());
361             }
362 
363             /** {@inheritDoc} */
364             @Override
365             protected final DoubleMatrix1D createMatrix1D(final int size)
366             {
367                 return new SparseDoubleMatrix1D(size);
368             }
369 
370             /** {@inheritDoc} */
371             @Override
372             public final DoubleVector.Rel.Sparse<U> copy()
373             {
374                 return this; // That was easy...
375             }
376 
377         }
378 
379         /** {@inheritDoc} */
380         @Override
381         public final DoubleScalar.Rel<U> get(final int index) throws ValueException
382         {
383             return new DoubleScalar.Rel<U>(getInUnit(index, getUnit()), getUnit());
384         }
385 
386     }
387 
388     /**
389      * Retrieve the internal data.
390      * @return DoubleMatrix1D; the data in the internal format
391      */
392     protected final DoubleMatrix1D getVectorSI()
393     {
394         return this.vectorSI;
395     }
396 
397     /**
398      * Make a deep copy of the data (used ONLY in the MutableDoubleVector 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 DoubleVector. <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 MutableDoubleVector&lt;U&gt;; mutable version of this DoubleVector
410      */
411     public abstract MutableDoubleVector<U> mutable();
412 
413     /**
414      * Import the values and convert them into the SI standard unit.
415      * @param values double[]; an array of values
416      * @throws ValueException when values is null
417      */
418     protected final void initialize(final double[] 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, expressAsSIUnit(values[index]));
434             }
435         }
436     }
437 
438     /**
439      * Import the values from an existing DoubleMatrix1D. This makes a shallow copy.
440      * @param values DoubleMatrix1D; the values
441      */
442     protected final void initialize(final DoubleMatrix1D 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 DoubleScalar&lt;U&gt;[]; an array of values
450      * @throws ValueException when values is null, or empty
451      */
452     protected final void initialize(final DoubleScalar<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 DoubleMatrix1D; an instance of the right type of DoubleMatrix1D (absolute/relative, dense/sparse, etc.)
470      */
471     protected abstract DoubleMatrix1D createMatrix1D(final int size);
472 
473     /**
474      * Create a double[] array filled with the values in the standard SI unit.
475      * @return double[]; array of values in the standard SI unit
476      */
477     public final double[] getValuesSI()
478     {
479         return this.vectorSI.toArray(); // this makes a deep copy
480     }
481 
482     /**
483      * Create a double[] array filled with the values in the original unit.
484      * @return double[]; the values in the original unit
485      */
486     public final double[] getValuesInUnit()
487     {
488         return getValuesInUnit(getUnit());
489     }
490 
491     /**
492      * Create a double[] 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 double[]; the values converted into the specified unit
495      */
496     public final double[] getValuesInUnit(final U targetUnit)
497     {
498         double[] values = this.vectorSI.toArray();
499         for (int i = values.length; --i >= 0;)
500         {
501             values[i] = 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 double getSI(final int index) throws ValueException
516     {
517         checkIndex(index);
518         return safeGet(index);
519     }
520 
521     /** {@inheritDoc} */
522     @Override
523     public final double getInUnit(final int index) throws ValueException
524     {
525         return expressAsSpecifiedUnit(getSI(index));
526     }
527 
528     /** {@inheritDoc} */
529     @Override
530     public final double getInUnit(final int index, final U targetUnit) throws ValueException
531     {
532         return ValueUtil.expressAsUnit(getSI(index), targetUnit);
533     }
534 
535     /** {@inheritDoc} */
536     @Override
537     public final double 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 DoubleVector 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 DoubleVector 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 DoubleVector 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 MutableDoubleVector)
590             {
591                 buf.append("Mutable   ");
592                 if (this instanceof MutableDoubleVector.Abs.Dense)
593                 {
594                     buf.append("Abs Dense  ");
595                 }
596                 else if (this instanceof MutableDoubleVector.Rel.Dense)
597                 {
598                     buf.append("Rel Dense  ");
599                 }
600                 else if (this instanceof MutableDoubleVector.Abs.Sparse)
601                 {
602                     buf.append("Abs Sparse ");
603                 }
604                 else if (this instanceof MutableDoubleVector.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 DoubleVector.Abs.Dense)
617                 {
618                     buf.append("Abs Dense  ");
619                 }
620                 else if (this instanceof DoubleVector.Rel.Dense)
621                 {
622                     buf.append("Rel Dense  ");
623                 }
624                 else if (this instanceof DoubleVector.Abs.Sparse)
625                 {
626                     buf.append("Abs Sparse ");
627                 }
628                 else if (this instanceof DoubleVector.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             double d = ValueUtil.expressAsUnit(safeGet(i), displayUnit);
641             buf.append(" " + Format.format(d));
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 DoubleVector&lt;?&gt;; other DoubleVector
653      * @throws ValueException when other is null, or vectors have unequal size
654      */
655     protected final void checkSize(final DoubleVector<?> 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 double[]; array of double
670      * @throws ValueException when vectors have unequal size
671      */
672     protected final void checkSize(final double[] 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 double; the value stored at that index
697      */
698     protected final double 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 double; the new value for the entry in vectorSI
707      */
708     protected final void safeSet(final int index, final double valueSI)
709     {
710         this.vectorSI.setQuick(index, valueSI);
711     }
712 
713     /**
714      * Create a deep copy of the data.
715      * @return DoubleMatrix1D; deep copy of the data
716      */
717     protected final DoubleMatrix1D 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 DoubleVector.
724      * @param dsArray DoubleScalar&lt;U&gt;[]; the provided array
725      * @param <U> Unit; the unit of the DoubleScalar array
726      * @return DoubleScalar&lt;U&gt;[]; the provided array
727      * @throws ValueException when the array has length equal to 0
728      */
729     protected static <U extends Unit<U>> DoubleScalar<U>[] checkNonEmpty(final DoubleScalar<U>[] dsArray)
730             throws ValueException
731     {
732         if (0 == dsArray.length)
733         {
734             throw new ValueException(
735                     "Cannot create a DoubleVector or MutableDoubleVector from an empty array of DoubleScalar");
736         }
737         return dsArray;
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 DoubleVector))
763         {
764             return false;
765         }
766         DoubleVector<?> other = (DoubleVector<?>) 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 DoubleVectors value by value and store the result in a new MutableDoubleVector.Abs.Dense&lt;U&gt;.
791      * @param left DoubleVector.Abs.Dense&lt;U&gt;; the left operand
792      * @param right DoubleVector.Rel&lt;U&gt;; the right operand
793      * @param <U> Unit; the unit of the parameters and the result
794      * @return MutableDoubleVector.Abs.Dense&lt;U&gt;
795      * @throws ValueException when the vectors do not have the same size
796      */
797     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> plus(final DoubleVector.Abs.Dense<U> left,
798             final DoubleVector.Rel<U> right) throws ValueException
799     {
800         return (MutableDoubleVector.Abs.Dense<U>) left.mutable().incrementBy(right);
801     }
802 
803     /**
804      * Add two DoubleVectors value by value and store the result in a new MutableDoubleVector.Abs.Dense&lt;U&gt;.
805      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the left operand
806      * @param right DoubleVector.Rel.Dense&lt;U&gt;; the right operand
807      * @param <U> Unit; the unit of the parameters and the result
808      * @return MutableDoubleVector.Abs.Dense&lt;U&gt;
809      * @throws ValueException when the vectors do not have the same size
810      */
811     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> plus(final DoubleVector.Abs.Sparse<U> left,
812             final DoubleVector.Rel.Dense<U> right) throws ValueException
813     {
814         return (MutableDoubleVector.Abs.Dense<U>) sparseToDense(left).incrementBy(right);
815     }
816 
817     /**
818      * Add two DoubleVectors value by value and store the result in a new MutableDoubleVector.Abs.Sparse&lt;U&gt;.
819      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the left operand
820      * @param right DoubleVector.Rel.Sparse&lt;U&gt;; the right operand
821      * @param <U> Unit; the unit of the parameters and the result
822      * @return MutableDoubleVector.Abs.Sparse&lt;U&gt;
823      * @throws ValueException when the vectors do not have the same size
824      */
825     public static <U extends Unit<U>> MutableDoubleVector.Abs.Sparse<U> plus(final DoubleVector.Abs.Sparse<U> left,
826             final DoubleVector.Rel.Sparse<U> right) throws ValueException
827     {
828         return (MutableDoubleVector.Abs.Sparse<U>) left.mutable().incrementBy(right);
829     }
830 
831     /**
832      * Add two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Dense&lt;U&gt;.
833      * @param left DoubleVector.Rel.Dense&lt;U&gt;; the left operand
834      * @param right DoubleVector.Rel&lt;U&gt;; the right operand
835      * @param <U> Unit; the unit of the parameters and the result
836      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
837      * @throws ValueException when the vectors do not have the same size
838      */
839     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> plus(final DoubleVector.Rel.Dense<U> left,
840             final DoubleVector.Rel<U> right) throws ValueException
841     {
842         return (MutableDoubleVector.Rel.Dense<U>) left.mutable().incrementBy(right);
843     }
844 
845     /**
846      * Add two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Dense&lt;U&gt;.
847      * @param left DoubleVector.Rel.Sparse&lt;U&gt;; the left operand
848      * @param right DoubleVector.Rel.Dense&lt;U&gt;; the right operand
849      * @param <U> Unit; the unit of the parameters and the result
850      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
851      * @throws ValueException when the vectors do not have the same size
852      */
853     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> plus(final DoubleVector.Rel.Sparse<U> left,
854             final DoubleVector.Rel.Dense<U> right) throws ValueException
855     {
856         return (MutableDoubleVector.Rel.Dense<U>) sparseToDense(left).incrementBy(right);
857     }
858 
859     /**
860      * Add two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Sparse&lt;U&gt;.
861      * @param left DoubleVector.Rel.Sparse&lt;U&gt;; the left operand
862      * @param right DoubleVector.Rel.Sparse&lt;U&gt;; the right operand
863      * @param <U> Unit; the unit of the parameters and the result
864      * @return MutableDoubleVector.Rel.Sparse&lt;U&gt;
865      * @throws ValueException when the vectors do not have the same size
866      */
867     public static <U extends Unit<U>> MutableDoubleVector.Rel.Sparse<U> plus(final DoubleVector.Rel.Sparse<U> left,
868             final DoubleVector.Rel.Sparse<U> right) throws ValueException
869     {
870         return (MutableDoubleVector.Rel.Sparse<U>) left.mutable().incrementBy(right);
871     }
872 
873     /**
874      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Dense&lt;U&gt;.
875      * @param left DoubleVector.Abs.Dense&lt;U&gt;; the left operand
876      * @param right DoubleVector.Abs&lt;U&gt;; the right operand
877      * @param <U> Unit; the unit of the parameters and the result
878      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
879      * @throws ValueException when the vectors do not have the same size
880      */
881     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> minus(final DoubleVector.Abs.Dense<U> left,
882             final DoubleVector.Abs<U> right) throws ValueException
883     {
884         return (MutableDoubleVector.Rel.Dense<U>) new MutableDoubleVector.Rel.Dense<U>(left.deepCopyOfData(),
885                     left.getUnit()).decrementBy(right);
886     }
887 
888     /**
889      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Sparse&lt;U&gt;.
890      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the left operand
891      * @param right DoubleVector.Abs.Sparse&lt;U&gt;; the right operand
892      * @param <U> Unit; the unit of the parameters and the result
893      * @return MutableDoubleVector.Rel.Sparse&lt;U&gt;
894      * @throws ValueException when the vectors do not have the same size
895      */
896     public static <U extends Unit<U>> MutableDoubleVector.Rel.Sparse<U> minus(final DoubleVector.Abs.Sparse<U> left,
897             final DoubleVector.Abs.Sparse<U> right) throws ValueException
898     {
899         return (MutableDoubleVector.Rel.Sparse<U>) new MutableDoubleVector.Rel.Sparse<U>(left.deepCopyOfData(),
900                     left.getUnit()).decrementBy(right);
901     }
902 
903     /**
904      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Dense&lt;U&gt;.
905      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the left operand
906      * @param right DoubleVector.Abs.Dense&lt;U&gt;; the right operand
907      * @param <U> Unit; the unit of the parameters and the result
908      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
909      * @throws ValueException when the vectors do not have the same size
910      */
911     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> minus(final DoubleVector.Abs.Sparse<U> left,
912             final DoubleVector.Abs.Dense<U> right) throws ValueException
913     {
914         return (MutableDoubleVector.Rel.Dense<U>) new MutableDoubleVector.Rel.Dense<U>(left.deepCopyOfData(),
915                     left.getUnit()).decrementBy(right);
916     }
917 
918     /**
919      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Abs.Dense&lt;U&gt;.
920      * @param left DoubleVector.Abs.Dense&lt;U&gt;; the left operand
921      * @param right DoubleVector.Rel&lt;U&gt;; the right operand
922      * @param <U> Unit; the unit of the parameters and the result
923      * @return MutableDoubleVector.Abs.Dense&lt;U&gt;
924      * @throws ValueException when the vectors do not have the same size
925      */
926     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> minus(final DoubleVector.Abs.Dense<U> left,
927             final DoubleVector.Rel<U> right) throws ValueException
928     {
929         return (MutableDoubleVector.Abs.Dense<U>) left.mutable().decrementBy(right);
930     }
931 
932     /**
933      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Abs.Dense&lt;U&gt;.
934      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the left operand
935      * @param right DoubleVector.Rel.Dense&lt;U&gt;; the right operand
936      * @param <U> Unit; the unit of the parameters and the result
937      * @return MutableDoubleVector.Abs.Dense&lt;U&gt;
938      * @throws ValueException when the vectors do not have the same size
939      */
940     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> minus(final DoubleVector.Abs.Sparse<U> left,
941             final DoubleVector.Rel.Dense<U> right) throws ValueException
942     {
943         return (MutableDoubleVector.Abs.Dense<U>) sparseToDense(left).decrementBy(right);
944     }
945 
946     /**
947      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Abs.Sparse&lt;U&gt;.
948      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the left operand
949      * @param right DoubleVector.Rel.Sparse&lt;U&gt;; the right operand
950      * @param <U> Unit; the unit of the parameters and the result
951      * @return MutableDoubleVector.Abs.Sparse&lt;U&gt;
952      * @throws ValueException when the vectors do not have the same size
953      */
954     public static <U extends Unit<U>> MutableDoubleVector.Abs.Sparse<U> minus(final DoubleVector.Abs.Sparse<U> left,
955             final DoubleVector.Rel.Sparse<U> right) throws ValueException
956     {
957         return (MutableDoubleVector.Abs.Sparse<U>) left.mutable().decrementBy(right);
958     }
959 
960     /**
961      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Dense&lt;U&gt;.
962      * @param left DoubleVector.Rel.Dense&lt;U&gt;; the left operand
963      * @param right DoubleVector.Rel&lt;U&gt;; the right operand
964      * @param <U> Unit; the unit of the parameters and the result
965      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
966      * @throws ValueException when the vectors do not have the same size
967      */
968     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> minus(final DoubleVector.Rel.Dense<U> left,
969             final DoubleVector.Rel<U> right) throws ValueException
970     {
971         return (MutableDoubleVector.Rel.Dense<U>) left.mutable().decrementBy(right);
972     }
973 
974     /**
975      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Dense&lt;U&gt;.
976      * @param left DoubleVector.Rel.Sparse&lt;U&gt;; the left operand
977      * @param right DoubleVector.Rel.Dense&lt;U&gt;; the right operand
978      * @param <U> Unit; the unit of the parameters and the result
979      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
980      * @throws ValueException when the vectors do not have the same size
981      */
982     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> minus(final DoubleVector.Rel.Sparse<U> left,
983             final DoubleVector.Rel.Dense<U> right) throws ValueException
984     {
985         return (MutableDoubleVector.Rel.Dense<U>) sparseToDense(left).decrementBy(right);
986     }
987 
988     /**
989      * Subtract two DoubleVectors value by value and store the result in a new MutableDoubleVector.Rel.Sparse&lt;U&gt;.
990      * @param left DoubleVector.Rel.Sparse&lt;U&gt;; the left operand
991      * @param right DoubleVector.Rel.Sparse&lt;U&gt;; the right operand
992      * @param <U> Unit; the unit of the parameters and the result
993      * @return MutableDoubleVector.Rel.Sparse&lt;U&gt;
994      * @throws ValueException when the vectors do not have the same size
995      */
996     public static <U extends Unit<U>> MutableDoubleVector.Rel.Sparse<U> minus(final DoubleVector.Rel.Sparse<U> left,
997             final DoubleVector.Rel.Sparse<U> right) throws ValueException
998     {
999         return (MutableDoubleVector.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 DoubleVectors value by value and store the result in a new
1005      * MutableDoubleVector.Abs.Dense&lt;SIUnit&gt;.
1006      * @param left DoubleVector.Abs.Dense&lt;?&gt;; the left operand
1007      * @param right DoubleVector.Abs.Dense&lt;?&gt;; the right operand
1008      * @return MutableDoubleVector.Abs.Dense&lt;SIUnit&gt;
1009      * @throws ValueException when the vectors do not have the same size
1010      */
1011     public static MutableDoubleVector.Abs.Dense<SIUnit> times(final DoubleVector.Abs.Dense<?> left,
1012             final DoubleVector.Abs.Dense<?> right) throws ValueException
1013     {
1014         SIUnit targetUnit =
1015                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1016                         right.getUnit().getSICoefficients()).toString());
1017         MutableDoubleVector.Abs.Dense<SIUnit> work =
1018                 new MutableDoubleVector.Abs.Dense<SIUnit>(left.deepCopyOfData(), targetUnit);
1019         work.scaleValueByValue(right);
1020         return work;
1021     }
1022 
1023     /**
1024      * Multiply two DoubleVectors value by value and store the result in a new
1025      * MutableDoubleVector.Abs.Sparse&lt;SIUnit&gt;.
1026      * @param left DoubleVector.Abs.Dense&lt;?&gt;; the left operand
1027      * @param right DoubleVector.Abs.Sparse&lt;?&gt;; the right operand
1028      * @return MutableDoubleVector.Abs.Sparse&lt;SIUnit&gt;
1029      * @throws ValueException when the vectors do not have the same size
1030      */
1031     public static MutableDoubleVector.Abs.Sparse<SIUnit> times(final DoubleVector.Abs.Dense<?> left,
1032             final DoubleVector.Abs.Sparse<?> right) throws ValueException
1033     {
1034         SIUnit targetUnit =
1035                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1036                         right.getUnit().getSICoefficients()).toString());
1037         MutableDoubleVector.Abs.Sparse<SIUnit> work =
1038                 new MutableDoubleVector.Abs.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1039         work.scaleValueByValue(right);
1040         return work;
1041     }
1042 
1043     /**
1044      * Multiply two DoubleVectors value by value and store the result in a new
1045      * MutableDoubleVector.Abs.Sparse&lt;SIUnit&gt;.
1046      * @param left DoubleVector.Abs.Sparse&lt;?&gt;; the left operand
1047      * @param right DoubleVector.Abs&lt;?&gt;; the right operand
1048      * @return MutableDoubleVector.Abs.Sparse&lt;SIUnit&gt;
1049      * @throws ValueException when the vectors do not have the same size
1050      */
1051     public static MutableDoubleVector.Abs.Sparse<SIUnit> times(final DoubleVector.Abs.Sparse<?> left,
1052             final DoubleVector.Abs<?> right) throws ValueException
1053     {
1054         SIUnit targetUnit =
1055                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1056                         right.getUnit().getSICoefficients()).toString());
1057         MutableDoubleVector.Abs.Sparse<SIUnit> work =
1058                 new MutableDoubleVector.Abs.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1059         work.scaleValueByValue(right);
1060         return work;
1061     }
1062 
1063     /**
1064      * Multiply two DoubleVectors value by value and store the result in a new
1065      * MutableDoubleVector.Rel.Dense&lt;SIUnit&gt;.
1066      * @param left DoubleVector.Rel.Dense&lt;?&gt;; the left operand
1067      * @param right DoubleVector.Rel.Dense&lt;?&gt;; the right operand
1068      * @return MutableDoubleVector.Rel.Dense&lt;SIUnit&gt;
1069      * @throws ValueException when the vectors do not have the same size
1070      */
1071     public static MutableDoubleVector.Rel.Dense<SIUnit> times(final DoubleVector.Rel.Dense<?> left,
1072             final DoubleVector.Rel.Dense<?> right) throws ValueException
1073     {
1074         SIUnit targetUnit =
1075                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1076                         right.getUnit().getSICoefficients()).toString());
1077         MutableDoubleVector.Rel.Dense<SIUnit> work =
1078                 new MutableDoubleVector.Rel.Dense<SIUnit>(left.deepCopyOfData(), targetUnit);
1079         work.scaleValueByValue(right);
1080         return work;
1081     }
1082 
1083     /**
1084      * Multiply two DoubleVectors value by value and store the result in a new
1085      * MutableDoubleVector.Rel.Sparse&lt;SIUnit&gt;.
1086      * @param left DoubleVector.Rel.Dense&lt;?&gt;; the left operand
1087      * @param right DoubleVector.Rel.Sparse&lt;?&gt;; the right operand
1088      * @return MutableDoubleVector.Rel.Sparse&lt;SIUnit&gt;
1089      * @throws ValueException when the vectors do not have the same size
1090      */
1091     public static MutableDoubleVector.Rel.Sparse<SIUnit> times(final DoubleVector.Rel.Dense<?> left,
1092             final DoubleVector.Rel.Sparse<?> right) throws ValueException
1093     {
1094         SIUnit targetUnit =
1095                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1096                         right.getUnit().getSICoefficients()).toString());
1097         MutableDoubleVector.Rel.Sparse<SIUnit> work =
1098                 new MutableDoubleVector.Rel.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1099         work.scaleValueByValue(right);
1100         return work;
1101     }
1102 
1103     /**
1104      * Multiply two DoubleVectors value by value and store the result in a new
1105      * MutableDoubleVector.Rel.Sparse&lt;SIUnit&gt;.
1106      * @param left DoubleVector.Rel.Sparse&lt;?&gt;; the left operand
1107      * @param right DoubleVector.Rel&lt;?&gt;; the right operand
1108      * @return MutableDoubleVector.Rel.Sparse&lt;SIUnit&gt;
1109      * @throws ValueException when the vectors do not have the same size
1110      */
1111     public static MutableDoubleVector.Rel.Sparse<SIUnit> times(final DoubleVector.Rel.Sparse<?> left,
1112             final DoubleVector.Rel<?> right) throws ValueException
1113     {
1114         SIUnit targetUnit =
1115                 Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
1116                         right.getUnit().getSICoefficients()).toString());
1117         MutableDoubleVector.Rel.Sparse<SIUnit> work =
1118                 new MutableDoubleVector.Rel.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
1119         work.scaleValueByValue(right);
1120         return work;
1121     }
1122 
1123     /**
1124      * Multiply the values in a DoubleVector and a double array value by value and store the result in a new
1125      * MutableDoubleVector.Abs.Dense&lt;U&gt;.
1126      * @param left DoubleVector.Abs.Dense&lt;U&gt;; the DoubleVector
1127      * @param right double[]; the double array
1128      * @param <U> Unit; the unit of the left parameter and the result
1129      * @return MutableDoubleVector.Abs.Dense&lt;U&gt;
1130      * @throws ValueException when the DoubleVector and the array do not have the same size
1131      */
1132     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> times(final DoubleVector.Abs.Dense<U> left,
1133             final double[] right) throws ValueException
1134     {
1135         return (MutableDoubleVector.Abs.Dense<U>) left.mutable().scaleValueByValue(right);
1136     }
1137 
1138     /**
1139      * Multiply the values in a DoubleVector and a double array value by value and store the result in a new
1140      * MutableDoubleVector.Abs.Sparse&lt;U&gt;.
1141      * @param left DoubleVector.Abs.Sparse&lt;U&gt;; the DoubleVector
1142      * @param right double[]; the double array
1143      * @param <U> Unit; the unit of the left parameter and the result
1144      * @return MutableDoubleVector.Abs.Sparse&lt;U&gt;
1145      * @throws ValueException when the DoubleVector and the array do not have the same size
1146      */
1147     public static <U extends Unit<U>> MutableDoubleVector.Abs.Sparse<U> times(final DoubleVector.Abs.Sparse<U> left,
1148             final double[] right) throws ValueException
1149     {
1150         return (MutableDoubleVector.Abs.Sparse<U>) left.mutable().scaleValueByValue(right);
1151     }
1152 
1153     /**
1154      * Multiply the values in a DoubleVector and a double array value by value and store the result in a new
1155      * MutableDoubleVector.Rel.Dense&lt;U&gt;.
1156      * @param left DoubleVector.Rel.Dense&lt;U&gt;; the DoubleVector
1157      * @param right double[]; the double array
1158      * @param <U> Unit; the unit of the left parameter and the result
1159      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
1160      * @throws ValueException when the DoubleVector and the array do not have the same size
1161      */
1162     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> times(final DoubleVector.Rel.Dense<U> left,
1163             final double[] right) throws ValueException
1164     {
1165         return (MutableDoubleVector.Rel.Dense<U>) left.mutable().scaleValueByValue(right);
1166     }
1167 
1168     /**
1169      * Multiply the values in a DoubleVector and a double array value by value and store the result in a new
1170      * MutableDoubleVector.Rel.Sparse&lt;U&gt;.
1171      * @param left DoubleVector.Rel.Sparse&lt;U&gt;; the DoubleVector
1172      * @param right double[]; the double array
1173      * @param <U> Unit; the unit of the left parameter and the result
1174      * @return MutableDoubleVector.Rel.Sparse&lt;U&gt;
1175      * @throws ValueException when the DoubleVector and the array do not have the same size
1176      */
1177     public static <U extends Unit<U>> MutableDoubleVector.Rel.Sparse<U> times(final DoubleVector.Rel.Sparse<U> left,
1178             final double[] right) throws ValueException
1179     {
1180         return (MutableDoubleVector.Rel.Sparse<U>) left.mutable().scaleValueByValue(right);
1181     }
1182 
1183     /**
1184      * Make the Sparse equivalent of a DenseDoubleMatrix1D.
1185      * @param dense DoubleMatrix1D; the Dense DoubleMatrix1D
1186      * @return SparseDoubleMatrix1D
1187      */
1188     private static SparseDoubleMatrix1D makeSparse(final DoubleMatrix1D dense)
1189     {
1190         SparseDoubleMatrix1D result = new SparseDoubleMatrix1D((int) dense.size());
1191         result.assign(dense);
1192         return result;
1193     }
1194 
1195     /**
1196      * Create a Sparse version of a Dense DoubleVector.
1197      * @param in DoubleVector.Abs.Dense&lt;U&gt;; the Dense DoubleVector
1198      * @param <U> Unit; the unit of the parameter and the result
1199      * @return MutableDoubleVector.Abs.Sparse&lt;U&gt;
1200      */
1201     public static <U extends Unit<U>> MutableDoubleVector.Abs.Sparse<U> denseToSparse(final DoubleVector.Abs.Dense<U> in)
1202     {
1203         return new MutableDoubleVector.Abs.Sparse<U>(makeSparse(in.getVectorSI()), in.getUnit());
1204     }
1205 
1206     /**
1207      * Create a Sparse version of a Dense DoubleVector.
1208      * @param in DoubleVector.Rel.Dense&lt;U&gt;; the Dense DoubleVector
1209      * @param <U> Unit; the unit of the parameter and the result
1210      * @return MutableDoubleVector.Rel.Sparse&lt;U&gt;
1211      */
1212     public static <U extends Unit<U>> MutableDoubleVector.Rel.Sparse<U> denseToSparse(final DoubleVector.Rel.Dense<U> in)
1213     {
1214         return new MutableDoubleVector.Rel.Sparse<U>(makeSparse(in.getVectorSI()), in.getUnit());
1215     }
1216 
1217     /**
1218      * Make the Dense equivalent of a SparseDoubleMatrix1D.
1219      * @param sparse DoubleMatrix1D; the Sparse DoubleMatrix1D
1220      * @return DenseDoubleMatrix1D
1221      */
1222     private static DenseDoubleMatrix1D makeDense(final DoubleMatrix1D sparse)
1223     {
1224         DenseDoubleMatrix1D result = new DenseDoubleMatrix1D((int) sparse.size());
1225         result.assign(sparse);
1226         return result;
1227     }
1228 
1229     /**
1230      * Create a Dense version of a Sparse DoubleVector.
1231      * @param in DoubleVector.Abs.Sparse&lt;U&gt;; the Sparse DoubleVector
1232      * @param <U> Unit; the unit of the parameter and the result
1233      * @return MutableDoubleVector.Abs.Dense&lt;U&gt;
1234      */
1235     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> sparseToDense(final DoubleVector.Abs.Sparse<U> in)
1236     {
1237         return new MutableDoubleVector.Abs.Dense<U>(makeDense(in.getVectorSI()), in.getUnit());
1238     }
1239 
1240     /**
1241      * Create a Dense version of a Sparse DoubleVector.
1242      * @param in DoubleVector.Rel.Sparse&lt;U&gt;; the Sparse DoubleVector
1243      * @param <U> Unit; the unit of the parameter and the result
1244      * @return MutableDoubleVector.Rel.Dense&lt;U&gt;
1245      */
1246     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> sparseToDense(final DoubleVector.Rel.Sparse<U> in)
1247     {
1248         return new MutableDoubleVector.Rel.Dense<U>(makeDense(in.getVectorSI()), in.getUnit());
1249     }
1250 
1251     /**
1252      * Interpolate between or extrapolate over two values.
1253      * @param zero DoubleVector.Abs.Dense&lt;U&gt;; zero reference (returned when ratio == 0)
1254      * @param one DoubleVector.Abs.Dense&lt;U&gt;; one reference (returned when ratio == 1)
1255      * @param ratio double; 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 MutableDoubleVector.Abs.Dense&lt;U&gt;
1258      * @throws ValueException when zero and one do not have the same size
1259      */
1260     public static <U extends Unit<U>> MutableDoubleVector.Abs.Dense<U> interpolate(final DoubleVector.Abs.Dense<U> zero,
1261             final DoubleVector.Abs.Dense<U> one, final double ratio) throws ValueException
1262     {
1263         MutableDoubleVector.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 DoubleVector.Rel.Dense&lt;U&gt;; zero reference (returned when ratio == 0)
1274      * @param one DoubleVector.Rel.Dense&lt;U&gt;; one reference (returned when ratio == 1)
1275      * @param ratio double; 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 MutableDoubleVector.Rel.Dense&lt;U&gt;
1278      * @throws ValueException when zero and one do not have the same size
1279      */
1280     public static <U extends Unit<U>> MutableDoubleVector.Rel.Dense<U> interpolate(final DoubleVector.Rel.Dense<U> zero,
1281             final DoubleVector.Rel.Dense<U> one, final double ratio) throws ValueException
1282     {
1283         MutableDoubleVector.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 DoubleVector.Abs.Sparse&lt;U&gt;; zero reference (returned when ratio == 0)
1294      * @param one DoubleVector.Abs.Sparse&lt;U&gt;; one reference (returned when ratio == 1)
1295      * @param ratio double; 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 MutableDoubleVector.Abs.Sparse&lt;U&gt;
1298      * @throws ValueException when zero and one do not have the same size
1299      */
1300     public static <U extends Unit<U>> MutableDoubleVector.Abs.Sparse<U> interpolate(
1301             final DoubleVector.Abs.Sparse<U> zero, final DoubleVector.Abs.Sparse<U> one, final double ratio)
1302             throws ValueException
1303     {
1304         MutableDoubleVector.Abs.Sparse<U> result = zero.mutable();
1305         for (int index = result.size(); --index >= 0;)
1306         {
1307             result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
1308         }
1309         return result;
1310     }
1311 
1312     /**
1313      * Interpolate between or extrapolate over two values.
1314      * @param zero DoubleVector.Rel.Sparse&lt;U&gt;; zero reference (returned when ratio == 0)
1315      * @param one DoubleVector.Rel.Sparse&lt;U&gt;; one reference (returned when ratio == 1)
1316      * @param ratio double; the ratio that determines where between (or outside) zero and one the result lies
1317      * @param <U> Unit; the unit of the parameters and the result
1318      * @return MutableDoubleVector.Rel.Sparse&lt;U&gt;
1319      * @throws ValueException when zero and one do not have the same size
1320      */
1321     public static <U extends Unit<U>> MutableDoubleVector.Rel.Sparse<U> interpolate(
1322             final DoubleVector.Rel.Sparse<U> zero, final DoubleVector.Rel.Sparse<U> one, final double ratio)
1323             throws ValueException
1324     {
1325         MutableDoubleVector.Rel.Sparse<U> result = zero.mutable();
1326         for (int index = result.size(); --index >= 0;)
1327         {
1328             result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
1329         }
1330         return result;
1331     }
1332 
1333 }