1 package org.opentrafficsim.core.value.vfloat.vector;
2
3 import org.opentrafficsim.core.unit.Unit;
4 import org.opentrafficsim.core.value.Absolute;
5 import org.opentrafficsim.core.value.DenseData;
6 import org.opentrafficsim.core.value.Relative;
7 import org.opentrafficsim.core.value.SparseData;
8 import org.opentrafficsim.core.value.ValueException;
9 import org.opentrafficsim.core.value.ValueUtil;
10 import org.opentrafficsim.core.value.vfloat.FloatMathFunctions;
11 import org.opentrafficsim.core.value.vfloat.FloatMathFunctionsImpl;
12 import org.opentrafficsim.core.value.vfloat.scalar.FloatScalar;
13
14 import cern.colt.matrix.tfloat.FloatMatrix1D;
15 import cern.colt.matrix.tfloat.impl.DenseFloatMatrix1D;
16 import cern.colt.matrix.tfloat.impl.SparseFloatMatrix1D;
17 import cern.jet.math.tfloat.FloatFunctions;
18
19 /**
20 * MutableFloatVector.
21 * <p>
22 * This file was generated by the OpenTrafficSim value classes generator, 09 mrt, 2015
23 * <p>
24 * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
26 * <p>
27 * @version 09 mrt, 2015 <br>
28 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
30 * @param <U> Unit; the unit of this MutableFloatVector
31 */
32 public abstract class MutableFloatVector<U extends Unit<U>> extends FloatVector<U> implements
33 WriteFloatVectorFunctions<U>, FloatMathFunctions<MutableFloatVector<U>>
34 {
35 /** */
36 private static final long serialVersionUID = 20150309L;
37
38 /**
39 * Construct a new MutableFloatVector.
40 * @param unit U; the unit of the new MutableFloatVector
41 */
42 protected MutableFloatVector(final U unit)
43 {
44 super(unit);
45 // System.out.println("Created MutableFloatVector");
46 }
47
48 /** If set, any modification of the data must be preceded by replacing the data with a local copy. */
49 private boolean copyOnWrite = false;
50
51 /**
52 * Retrieve the value of the copyOnWrite flag.
53 * @return boolean
54 */
55 private boolean isCopyOnWrite()
56 {
57 return this.copyOnWrite;
58 }
59
60 /**
61 * Change the copyOnWrite flag.
62 * @param copyOnWrite boolean; the new value for the copyOnWrite flag
63 */
64 final void setCopyOnWrite(final boolean copyOnWrite)
65 {
66 this.copyOnWrite = copyOnWrite;
67 }
68
69 /** {@inheritDoc} */
70 @Override
71 public final void normalize() throws ValueException
72 {
73 float sum = zSum();
74 if (0 == sum)
75 {
76 throw new ValueException("zSum is 0; cannot normalize");
77 }
78 checkCopyOnWrite();
79 for (int i = 0; i < size(); i++)
80 {
81 safeSet(i, safeGet(i) / sum);
82 }
83 }
84
85 /**
86 * @param <U> Unit
87 */
88 public abstract static class Abs<U extends Unit<U>> extends MutableFloatVector<U> implements Absolute
89 {
90 /** */
91 private static final long serialVersionUID = 20150309L;
92
93 /**
94 * Construct a new Absolute MutableFloatVector.
95 * @param unit U; the unit of the new Absolute MutableFloatVector
96 */
97 protected Abs(final U unit)
98 {
99 super(unit);
100 // System.out.println("Created Abs");
101 }
102
103 /**
104 * @param <U> Unit
105 */
106 public static class Dense<U extends Unit<U>> extends Abs<U> implements DenseData
107 {
108 /** */
109 private static final long serialVersionUID = 20150309L;
110
111 /**
112 * Construct a new Absolute Dense MutableFloatVector.
113 * @param values float[]; the initial values of the entries in the new Absolute Dense MutableFloatVector
114 * @param unit U; the unit of the new Absolute Dense MutableFloatVector
115 * @throws ValueException when values is null
116 */
117 public Dense(final float[] values, final U unit) throws ValueException
118 {
119 super(unit);
120 // System.out.println("Created Dense");
121 initialize(values);
122 }
123
124 /**
125 * Construct a new Absolute Dense MutableFloatVector.
126 * @param values FloatScalar.Abs<U>[]; the initial values of the entries in the new Absolute Dense
127 * MutableFloatVector
128 * @throws ValueException when values has zero entries
129 */
130 public Dense(final FloatScalar.Abs<U>[] values) throws ValueException
131 {
132 super(checkNonEmpty(values)[0].getUnit());
133 // System.out.println("Created Dense");
134 initialize(values);
135 }
136
137 /**
138 * For package internal use only.
139 * @param values FloatMatrix1D; the initial values of the entries in the new Absolute Dense
140 * MutableFloatVector
141 * @param unit U; the unit of the new Absolute Dense MutableFloatVector
142 */
143 protected Dense(final FloatMatrix1D values, final U unit)
144 {
145 super(unit);
146 // System.out.println("Created Dense");
147 setCopyOnWrite(true);
148 initialize(values); // shallow copy
149 }
150
151 /** {@inheritDoc} */
152 @Override
153 public final FloatVector.Abs.Dense<U> immutable()
154 {
155 setCopyOnWrite(true);
156 return new FloatVector.Abs.Dense<U>(getVectorSI(), getUnit());
157 }
158
159 /** {@inheritDoc} */
160 @Override
161 public final MutableFloatVector.Abs.Dense<U> mutable()
162 {
163 setCopyOnWrite(true);
164 final MutableFloatVector.Abs.Dense<U> result =
165 new MutableFloatVector.Abs.Dense<U>(getVectorSI(), getUnit());
166 result.setCopyOnWrite(true);
167 return result;
168 }
169
170 /** {@inheritDoc} */
171 @Override
172 protected final FloatMatrix1D createMatrix1D(final int size)
173 {
174 return new DenseFloatMatrix1D(size);
175 }
176
177 /** {@inheritDoc} */
178 @Override
179 public final MutableFloatVector.Abs.Dense<U> copy()
180 {
181 return mutable();
182 }
183
184 }
185
186 /**
187 * @param <U> Unit
188 */
189 public static class Sparse<U extends Unit<U>> extends Abs<U> implements SparseData
190 {
191 /** */
192 private static final long serialVersionUID = 20150309L;
193
194 /**
195 * Construct a new Absolute Sparse MutableFloatVector.
196 * @param values float[]; the initial values of the entries in the new Absolute Sparse MutableFloatVector
197 * @param unit U; the unit of the new Absolute Sparse MutableFloatVector
198 * @throws ValueException when values is null
199 */
200 public Sparse(final float[] values, final U unit) throws ValueException
201 {
202 super(unit);
203 // System.out.println("Created Sparse");
204 initialize(values);
205 }
206
207 /**
208 * Construct a new Absolute Sparse MutableFloatVector.
209 * @param values FloatScalar.Abs<U>[]; the initial values of the entries in the new Absolute Sparse
210 * MutableFloatVector
211 * @throws ValueException when values has zero entries
212 */
213 public Sparse(final FloatScalar.Abs<U>[] values) throws ValueException
214 {
215 super(checkNonEmpty(values)[0].getUnit());
216 // System.out.println("Created Sparse");
217 initialize(values);
218 }
219
220 /**
221 * For package internal use only.
222 * @param values FloatMatrix1D; the initial values of the entries in the new Absolute Sparse
223 * MutableFloatVector
224 * @param unit U; the unit of the new Absolute Sparse MutableFloatVector
225 */
226 protected Sparse(final FloatMatrix1D values, final U unit)
227 {
228 super(unit);
229 // System.out.println("Created Sparse");
230 setCopyOnWrite(true);
231 initialize(values); // shallow copy
232 }
233
234 /** {@inheritDoc} */
235 @Override
236 public final FloatVector.Abs.Sparse<U> immutable()
237 {
238 setCopyOnWrite(true);
239 return new FloatVector.Abs.Sparse<U>(getVectorSI(), getUnit());
240 }
241
242 /** {@inheritDoc} */
243 @Override
244 public final MutableFloatVector.Abs.Sparse<U> mutable()
245 {
246 setCopyOnWrite(true);
247 final MutableFloatVector.Abs.Sparse<U> result =
248 new MutableFloatVector.Abs.Sparse<U>(getVectorSI(), getUnit());
249 result.setCopyOnWrite(true);
250 return result;
251 }
252
253 /** {@inheritDoc} */
254 @Override
255 protected final FloatMatrix1D createMatrix1D(final int size)
256 {
257 return new SparseFloatMatrix1D(size);
258 }
259
260 /** {@inheritDoc} */
261 @Override
262 public final MutableFloatVector.Abs.Sparse<U> copy()
263 {
264 return mutable();
265 }
266
267 }
268
269 /** {@inheritDoc} */
270 @Override
271 public final FloatScalar.Abs<U> get(final int index) throws ValueException
272 {
273 return new FloatScalar.Abs<U>(getInUnit(index, getUnit()), getUnit());
274 }
275
276 /**
277 * Increment the value by the supplied value and return the result.
278 * @param increment FloatVector.Rel<U>; amount by which the value is incremented
279 * @return MutableFloatVector.Abs<U>
280 * @throws ValueException when the size of increment is not identical to the size of this
281 */
282 public final MutableFloatVector.Abs<U> incrementBy(final FloatVector.Rel<U> increment) throws ValueException
283 {
284 return (MutableFloatVector.Abs<U>) incrementByImpl(increment);
285 }
286
287 /**
288 * Decrement the value by the supplied value and return the result.
289 * @param decrement FloatVector.Rel<U>; amount by which the value is decremented
290 * @return MutableFloatVector.Abs<U>
291 * @throws ValueException when the size of increment is not identical to the size of this
292 */
293 public final MutableFloatVector.Abs<U> decrementBy(final FloatVector.Rel<U> decrement) throws ValueException
294 {
295 return (MutableFloatVector.Abs<U>) decrementByImpl(decrement);
296 }
297
298 /**********************************************************************************/
299 /********************************** MATH METHODS **********************************/
300 /**********************************************************************************/
301
302 /** {@inheritDoc} */
303 @Override
304 public final MutableFloatVector.Abs<U> abs()
305 {
306 assign(FloatFunctions.abs);
307 return this;
308 }
309
310 /** {@inheritDoc} */
311 @Override
312 public final MutableFloatVector.Abs<U> acos()
313 {
314 assign(FloatFunctions.acos);
315 return this;
316 }
317
318 /** {@inheritDoc} */
319 @Override
320 public final MutableFloatVector.Abs<U> asin()
321 {
322 assign(FloatFunctions.asin);
323 return this;
324 }
325
326 /** {@inheritDoc} */
327 @Override
328 public final MutableFloatVector.Abs<U> atan()
329 {
330 assign(FloatFunctions.atan);
331 return this;
332 }
333
334 /** {@inheritDoc} */
335 @Override
336 public final MutableFloatVector.Abs<U> cbrt()
337 {
338 assign(FloatMathFunctionsImpl.cbrt);
339 return this;
340 }
341
342 /** {@inheritDoc} */
343 @Override
344 public final MutableFloatVector.Abs<U> ceil()
345 {
346 assign(FloatFunctions.ceil);
347 return this;
348 }
349
350 /** {@inheritDoc} */
351 @Override
352 public final MutableFloatVector.Abs<U> cos()
353 {
354 assign(FloatFunctions.cos);
355 return this;
356 }
357
358 /** {@inheritDoc} */
359 @Override
360 public final MutableFloatVector.Abs<U> cosh()
361 {
362 assign(FloatMathFunctionsImpl.cosh);
363 return this;
364 }
365
366 /** {@inheritDoc} */
367 @Override
368 public final MutableFloatVector.Abs<U> exp()
369 {
370 assign(FloatFunctions.exp);
371 return this;
372 }
373
374 /** {@inheritDoc} */
375 @Override
376 public final MutableFloatVector.Abs<U> expm1()
377 {
378 assign(FloatMathFunctionsImpl.expm1);
379 return this;
380 }
381
382 /** {@inheritDoc} */
383 @Override
384 public final MutableFloatVector.Abs<U> floor()
385 {
386 assign(FloatFunctions.floor);
387 return this;
388 }
389
390 /** {@inheritDoc} */
391 @Override
392 public final MutableFloatVector.Abs<U> log()
393 {
394 assign(FloatFunctions.log);
395 return this;
396 }
397
398 /** {@inheritDoc} */
399 @Override
400 public final MutableFloatVector.Abs<U> log10()
401 {
402 assign(FloatMathFunctionsImpl.log10);
403 return this;
404 }
405
406 /** {@inheritDoc} */
407 @Override
408 public final MutableFloatVector.Abs<U> log1p()
409 {
410 assign(FloatMathFunctionsImpl.log1p);
411 return this;
412 }
413
414 /** {@inheritDoc} */
415 @Override
416 public final MutableFloatVector.Abs<U> pow(final double x)
417 {
418 assign(FloatFunctions.pow((float) x));
419 return this;
420 }
421
422 /** {@inheritDoc} */
423 @Override
424 public final MutableFloatVector.Abs<U> rint()
425 {
426 assign(FloatFunctions.rint);
427 return this;
428 }
429
430 /** {@inheritDoc} */
431 @Override
432 public final MutableFloatVector.Abs<U> round()
433 {
434 assign(FloatMathFunctionsImpl.round);
435 return this;
436 }
437
438 /** {@inheritDoc} */
439 @Override
440 public final MutableFloatVector.Abs<U> signum()
441 {
442 assign(FloatMathFunctionsImpl.signum);
443 return this;
444 }
445
446 /** {@inheritDoc} */
447 @Override
448 public final MutableFloatVector.Abs<U> sin()
449 {
450 assign(FloatFunctions.sin);
451 return this;
452 }
453
454 /** {@inheritDoc} */
455 @Override
456 public final MutableFloatVector.Abs<U> sinh()
457 {
458 assign(FloatMathFunctionsImpl.sinh);
459 return this;
460 }
461
462 /** {@inheritDoc} */
463 @Override
464 public final MutableFloatVector.Abs<U> sqrt()
465 {
466 assign(FloatFunctions.sqrt);
467 return this;
468 }
469
470 /** {@inheritDoc} */
471 @Override
472 public final MutableFloatVector.Abs<U> tan()
473 {
474 assign(FloatFunctions.tan);
475 return this;
476 }
477
478 /** {@inheritDoc} */
479 @Override
480 public final MutableFloatVector.Abs<U> tanh()
481 {
482 assign(FloatMathFunctionsImpl.tanh);
483 return this;
484 }
485
486 /** {@inheritDoc} */
487 @Override
488 public final MutableFloatVector.Abs<U> toDegrees()
489 {
490 assign(FloatMathFunctionsImpl.toDegrees);
491 return this;
492 }
493
494 /** {@inheritDoc} */
495 @Override
496 public final MutableFloatVector.Abs<U> toRadians()
497 {
498 assign(FloatMathFunctionsImpl.toRadians);
499 return this;
500 }
501
502 /** {@inheritDoc} */
503 @Override
504 public final MutableFloatVector.Abs<U> inv()
505 {
506 assign(FloatFunctions.inv);
507 return this;
508 }
509
510 }
511
512 /**
513 * @param <U> Unit
514 */
515 public abstract static class Rel<U extends Unit<U>> extends MutableFloatVector<U> implements Relative
516 {
517 /** */
518 private static final long serialVersionUID = 20150309L;
519
520 /**
521 * Construct a new Relative MutableFloatVector.
522 * @param unit U; the unit of the new Relative MutableFloatVector
523 */
524 protected Rel(final U unit)
525 {
526 super(unit);
527 // System.out.println("Created Rel");
528 }
529
530 /**
531 * @param <U> Unit
532 */
533 public static class Dense<U extends Unit<U>> extends Rel<U> implements DenseData
534 {
535 /** */
536 private static final long serialVersionUID = 20150309L;
537
538 /**
539 * Construct a new Relative Dense MutableFloatVector.
540 * @param values float[]; the initial values of the entries in the new Relative Dense MutableFloatVector
541 * @param unit U; the unit of the new Relative Dense MutableFloatVector
542 * @throws ValueException when values is null
543 */
544 public Dense(final float[] values, final U unit) throws ValueException
545 {
546 super(unit);
547 // System.out.println("Created Dense");
548 initialize(values);
549 }
550
551 /**
552 * Construct a new Relative Dense MutableFloatVector.
553 * @param values FloatScalar.Rel<U>[]; the initial values of the entries in the new Relative Dense
554 * MutableFloatVector
555 * @throws ValueException when values has zero entries
556 */
557 public Dense(final FloatScalar.Rel<U>[] values) throws ValueException
558 {
559 super(checkNonEmpty(values)[0].getUnit());
560 // System.out.println("Created Dense");
561 initialize(values);
562 }
563
564 /**
565 * For package internal use only.
566 * @param values FloatMatrix1D; the initial values of the entries in the new Relative Dense
567 * MutableFloatVector
568 * @param unit U; the unit of the new Relative Dense MutableFloatVector
569 */
570 protected Dense(final FloatMatrix1D values, final U unit)
571 {
572 super(unit);
573 // System.out.println("Created Dense");
574 setCopyOnWrite(true);
575 initialize(values); // shallow copy
576 }
577
578 /** {@inheritDoc} */
579 @Override
580 public final FloatVector.Rel.Dense<U> immutable()
581 {
582 setCopyOnWrite(true);
583 return new FloatVector.Rel.Dense<U>(getVectorSI(), getUnit());
584 }
585
586 /** {@inheritDoc} */
587 @Override
588 public final MutableFloatVector.Rel.Dense<U> mutable()
589 {
590 setCopyOnWrite(true);
591 final MutableFloatVector.Rel.Dense<U> result =
592 new MutableFloatVector.Rel.Dense<U>(getVectorSI(), getUnit());
593 result.setCopyOnWrite(true);
594 return result;
595 }
596
597 /** {@inheritDoc} */
598 @Override
599 protected final FloatMatrix1D createMatrix1D(final int size)
600 {
601 return new DenseFloatMatrix1D(size);
602 }
603
604 /** {@inheritDoc} */
605 @Override
606 public final MutableFloatVector.Rel.Dense<U> copy()
607 {
608 return mutable();
609 }
610
611 }
612
613 /**
614 * @param <U> Unit
615 */
616 public static class Sparse<U extends Unit<U>> extends Rel<U> implements SparseData
617 {
618 /** */
619 private static final long serialVersionUID = 20150309L;
620
621 /**
622 * Construct a new Relative Sparse MutableFloatVector.
623 * @param values float[]; the initial values of the entries in the new Relative Sparse MutableFloatVector
624 * @param unit U; the unit of the new Relative Sparse MutableFloatVector
625 * @throws ValueException when values is null
626 */
627 public Sparse(final float[] values, final U unit) throws ValueException
628 {
629 super(unit);
630 // System.out.println("Created Sparse");
631 initialize(values);
632 }
633
634 /**
635 * Construct a new Relative Sparse MutableFloatVector.
636 * @param values FloatScalar.Rel<U>[]; the initial values of the entries in the new Relative Sparse
637 * MutableFloatVector
638 * @throws ValueException when values has zero entries
639 */
640 public Sparse(final FloatScalar.Rel<U>[] values) throws ValueException
641 {
642 super(checkNonEmpty(values)[0].getUnit());
643 // System.out.println("Created Sparse");
644 initialize(values);
645 }
646
647 /**
648 * For package internal use only.
649 * @param values FloatMatrix1D; the initial values of the entries in the new Relative Sparse
650 * MutableFloatVector
651 * @param unit U; the unit of the new Relative Sparse MutableFloatVector
652 */
653 protected Sparse(final FloatMatrix1D values, final U unit)
654 {
655 super(unit);
656 // System.out.println("Created Sparse");
657 setCopyOnWrite(true);
658 initialize(values); // shallow copy
659 }
660
661 /** {@inheritDoc} */
662 @Override
663 public final FloatVector.Rel.Sparse<U> immutable()
664 {
665 setCopyOnWrite(true);
666 return new FloatVector.Rel.Sparse<U>(getVectorSI(), getUnit());
667 }
668
669 /** {@inheritDoc} */
670 @Override
671 public final MutableFloatVector.Rel.Sparse<U> mutable()
672 {
673 setCopyOnWrite(true);
674 final MutableFloatVector.Rel.Sparse<U> result =
675 new MutableFloatVector.Rel.Sparse<U>(getVectorSI(), getUnit());
676 result.setCopyOnWrite(true);
677 return result;
678 }
679
680 /** {@inheritDoc} */
681 @Override
682 protected final FloatMatrix1D createMatrix1D(final int size)
683 {
684 return new SparseFloatMatrix1D(size);
685 }
686
687 /** {@inheritDoc} */
688 @Override
689 public final MutableFloatVector.Rel.Sparse<U> copy()
690 {
691 return mutable();
692 }
693
694 }
695
696 /** {@inheritDoc} */
697 @Override
698 public final FloatScalar.Rel<U> get(final int index) throws ValueException
699 {
700 return new FloatScalar.Rel<U>(getInUnit(index, getUnit()), getUnit());
701 }
702
703 /**
704 * Increment the value by the supplied value and return the result.
705 * @param increment FloatVector.Rel<U>; amount by which the value is incremented
706 * @return MutableFloatVector.Rel<U>
707 * @throws ValueException when the size of increment is not identical to the size of this
708 */
709 public final MutableFloatVector.Rel<U> incrementBy(final FloatVector.Rel<U> increment) throws ValueException
710 {
711 return (MutableFloatVector.Rel<U>) incrementByImpl(increment);
712 }
713
714 /**
715 * Decrement the value by the supplied value and return the result.
716 * @param decrement FloatVector.Rel<U>; amount by which the value is decremented
717 * @return MutableFloatVector.Rel<U>
718 * @throws ValueException when the size of increment is not identical to the size of this
719 */
720 public final MutableFloatVector.Rel<U> decrementBy(final FloatVector.Rel<U> decrement) throws ValueException
721 {
722 return (MutableFloatVector.Rel<U>) decrementByImpl(decrement);
723 }
724
725 /**********************************************************************************/
726 /********************************** MATH METHODS **********************************/
727 /**********************************************************************************/
728
729 /** {@inheritDoc} */
730 @Override
731 public final MutableFloatVector.Rel<U> abs()
732 {
733 assign(FloatFunctions.abs);
734 return this;
735 }
736
737 /** {@inheritDoc} */
738 @Override
739 public final MutableFloatVector.Rel<U> acos()
740 {
741 assign(FloatFunctions.acos);
742 return this;
743 }
744
745 /** {@inheritDoc} */
746 @Override
747 public final MutableFloatVector.Rel<U> asin()
748 {
749 assign(FloatFunctions.asin);
750 return this;
751 }
752
753 /** {@inheritDoc} */
754 @Override
755 public final MutableFloatVector.Rel<U> atan()
756 {
757 assign(FloatFunctions.atan);
758 return this;
759 }
760
761 /** {@inheritDoc} */
762 @Override
763 public final MutableFloatVector.Rel<U> cbrt()
764 {
765 assign(FloatMathFunctionsImpl.cbrt);
766 return this;
767 }
768
769 /** {@inheritDoc} */
770 @Override
771 public final MutableFloatVector.Rel<U> ceil()
772 {
773 assign(FloatFunctions.ceil);
774 return this;
775 }
776
777 /** {@inheritDoc} */
778 @Override
779 public final MutableFloatVector.Rel<U> cos()
780 {
781 assign(FloatFunctions.cos);
782 return this;
783 }
784
785 /** {@inheritDoc} */
786 @Override
787 public final MutableFloatVector.Rel<U> cosh()
788 {
789 assign(FloatMathFunctionsImpl.cosh);
790 return this;
791 }
792
793 /** {@inheritDoc} */
794 @Override
795 public final MutableFloatVector.Rel<U> exp()
796 {
797 assign(FloatFunctions.exp);
798 return this;
799 }
800
801 /** {@inheritDoc} */
802 @Override
803 public final MutableFloatVector.Rel<U> expm1()
804 {
805 assign(FloatMathFunctionsImpl.expm1);
806 return this;
807 }
808
809 /** {@inheritDoc} */
810 @Override
811 public final MutableFloatVector.Rel<U> floor()
812 {
813 assign(FloatFunctions.floor);
814 return this;
815 }
816
817 /** {@inheritDoc} */
818 @Override
819 public final MutableFloatVector.Rel<U> log()
820 {
821 assign(FloatFunctions.log);
822 return this;
823 }
824
825 /** {@inheritDoc} */
826 @Override
827 public final MutableFloatVector.Rel<U> log10()
828 {
829 assign(FloatMathFunctionsImpl.log10);
830 return this;
831 }
832
833 /** {@inheritDoc} */
834 @Override
835 public final MutableFloatVector.Rel<U> log1p()
836 {
837 assign(FloatMathFunctionsImpl.log1p);
838 return this;
839 }
840
841 /** {@inheritDoc} */
842 @Override
843 public final MutableFloatVector.Rel<U> pow(final double x)
844 {
845 assign(FloatFunctions.pow((float) x));
846 return this;
847 }
848
849 /** {@inheritDoc} */
850 @Override
851 public final MutableFloatVector.Rel<U> rint()
852 {
853 assign(FloatFunctions.rint);
854 return this;
855 }
856
857 /** {@inheritDoc} */
858 @Override
859 public final MutableFloatVector.Rel<U> round()
860 {
861 assign(FloatMathFunctionsImpl.round);
862 return this;
863 }
864
865 /** {@inheritDoc} */
866 @Override
867 public final MutableFloatVector.Rel<U> signum()
868 {
869 assign(FloatMathFunctionsImpl.signum);
870 return this;
871 }
872
873 /** {@inheritDoc} */
874 @Override
875 public final MutableFloatVector.Rel<U> sin()
876 {
877 assign(FloatFunctions.sin);
878 return this;
879 }
880
881 /** {@inheritDoc} */
882 @Override
883 public final MutableFloatVector.Rel<U> sinh()
884 {
885 assign(FloatMathFunctionsImpl.sinh);
886 return this;
887 }
888
889 /** {@inheritDoc} */
890 @Override
891 public final MutableFloatVector.Rel<U> sqrt()
892 {
893 assign(FloatFunctions.sqrt);
894 return this;
895 }
896
897 /** {@inheritDoc} */
898 @Override
899 public final MutableFloatVector.Rel<U> tan()
900 {
901 assign(FloatFunctions.tan);
902 return this;
903 }
904
905 /** {@inheritDoc} */
906 @Override
907 public final MutableFloatVector.Rel<U> tanh()
908 {
909 assign(FloatMathFunctionsImpl.tanh);
910 return this;
911 }
912
913 /** {@inheritDoc} */
914 @Override
915 public final MutableFloatVector.Rel<U> toDegrees()
916 {
917 assign(FloatMathFunctionsImpl.toDegrees);
918 return this;
919 }
920
921 /** {@inheritDoc} */
922 @Override
923 public final MutableFloatVector.Rel<U> toRadians()
924 {
925 assign(FloatMathFunctionsImpl.toRadians);
926 return this;
927 }
928
929 /** {@inheritDoc} */
930 @Override
931 public final MutableFloatVector.Rel<U> inv()
932 {
933 assign(FloatFunctions.inv);
934 return this;
935 }
936
937 }
938
939 /**
940 * Make (immutable) FloatVector equivalent for any type of MutableFloatVector.
941 * @return FloatVector<U>; immutable version of this FloatVector
942 */
943 public abstract FloatVector<U> immutable();
944
945 /**
946 * Check the copyOnWrite flag and, if it is set, make a deep copy of the data and clear the flag.
947 */
948 protected final void checkCopyOnWrite()
949 {
950 if (isCopyOnWrite())
951 {
952 // System.out.println("copyOnWrite is set: Copying data");
953 deepCopyData();
954 setCopyOnWrite(false);
955 }
956 }
957
958 /** {@inheritDoc} */
959 @Override
960 public final void setSI(final int index, final float valueSI) throws ValueException
961 {
962 checkIndex(index);
963 checkCopyOnWrite();
964 safeSet(index, valueSI);
965 }
966
967 /** {@inheritDoc} */
968 @Override
969 public final void set(final int index, final FloatScalar<U> value) throws ValueException
970 {
971 setSI(index, value.getSI());
972 }
973
974 /** {@inheritDoc} */
975 @Override
976 public final void setInUnit(final int index, final float value, final U valueUnit) throws ValueException
977 {
978 setSI(index, (float) ValueUtil.expressAsSIUnit(value, valueUnit));
979 }
980
981 /**
982 * Execute a function on a cell by cell basis.
983 * @param f cern.colt.function.tfloat.FloatFunction; the function to apply
984 */
985 public final void assign(final cern.colt.function.tfloat.FloatFunction f)
986 {
987 checkCopyOnWrite();
988 getVectorSI().assign(f);
989 }
990
991 /** {@inheritDoc} */
992 @Override
993 public final MutableFloatVector<U> multiply(final float constant)
994 {
995 assign(FloatFunctions.mult(constant));
996 return this;
997 }
998
999 /** {@inheritDoc} */
1000 @Override
1001 public final MutableFloatVector<U> divide(final float constant)
1002 {
1003 assign(FloatFunctions.div(constant));
1004 return this;
1005 }
1006
1007 /**********************************************************************************/
1008 /******************************* NON-STATIC METHODS *******************************/
1009 /**********************************************************************************/
1010
1011 /**
1012 * Increment the values in this MutableFloatVector by the corresponding values in a FloatVector.
1013 * @param increment FloatVector<U>; the values by which to increment the corresponding values in this
1014 * MutableFloatVector
1015 * @return MutableFloatVector<U>; this modified MutableFloatVector
1016 * @throws ValueException when the vectors do not have the same size
1017 */
1018 private MutableFloatVector<U> incrementValueByValue(final FloatVector<U> increment) throws ValueException
1019 {
1020 checkSizeAndCopyOnWrite(increment);
1021 for (int index = size(); --index >= 0;)
1022 {
1023 safeSet(index, safeGet(index) + increment.safeGet(index));
1024 }
1025 return this;
1026 }
1027
1028 /**
1029 * Decrement the values in this MutableFloatVector by the corresponding values in a FloatVector.
1030 * @param decrement FloatVector<U>; the values by which to decrement the corresponding values in this
1031 * MutableFloatVector
1032 * @return MutableFloatVector<U>; this modified MutableFloatVector
1033 * @throws ValueException when the vectors do not have the same size
1034 */
1035 private MutableFloatVector<U> decrementValueByValue(final FloatVector<U> decrement) throws ValueException
1036 {
1037 checkSizeAndCopyOnWrite(decrement);
1038 for (int index = size(); --index >= 0;)
1039 {
1040 safeSet(index, safeGet(index) - decrement.safeGet(index));
1041 }
1042 return this;
1043 }
1044
1045 /**
1046 * Increment the values in this MutableFloatVector by the corresponding values in a Relative FloatVector. <br>
1047 * Only Relative values are allowed; adding an Absolute value to an Absolute value is not allowed. Adding an
1048 * Absolute value to an existing Relative value would require the result to become Absolute, which is a type change
1049 * that is impossible. For that operation use a static method.
1050 * @param rel FloatVector.Rel<U>; the Relative FloatVector
1051 * @return MutableFloatVector<U>; this modified MutableFloatVector
1052 * @throws ValueException when the vectors do not have the same size
1053 */
1054 protected final MutableFloatVector<U> incrementByImpl(final FloatVector.Rel<U> rel) throws ValueException
1055 {
1056 return incrementValueByValue(rel);
1057 }
1058
1059 /**
1060 * Decrement the corresponding values of this Relative FloatVector from the values of this MutableFloatVector. <br>
1061 * Only Relative values are allowed; subtracting an Absolute value from a Relative value is not allowed. Subtracting
1062 * an Absolute value from an existing Absolute value would require the result to become Relative, which is a type
1063 * change that is impossible. For that operation use a static method.
1064 * @param rel FloatVector.Rel<U>; the Relative FloatVector
1065 * @return MutableFloatVector<U>; this modified MutableFloatVector
1066 * @throws ValueException when the vectors do not have the same size
1067 */
1068 protected final MutableFloatVector<U> decrementByImpl(final FloatVector.Rel<U> rel) throws ValueException
1069 {
1070 return decrementValueByValue(rel);
1071 }
1072
1073 // FIXME It makes no sense to subtract an Absolute from a Relative
1074 /**
1075 * Decrement the values in this Relative MutableFloatVector by the corresponding values in an Absolute FloatVector.
1076 * @param abs FloatVector.Abs<U>; the Absolute FloatVector
1077 * @return MutableFloatVector.Rel<U>; this modified Relative MutableFloatVector
1078 * @throws ValueException when the vectors do not have the same size
1079 */
1080 protected final MutableFloatVector.Rel<U> decrementBy(final FloatVector.Abs<U> abs) throws ValueException
1081 {
1082 return (MutableFloatVector.Rel<U>) decrementValueByValue(abs);
1083 }
1084
1085 /**
1086 * Scale the values in this MutableFloatVector by the corresponding values in a FloatVector.
1087 * @param factor FloatVector<?>; contains the values by which to scale the corresponding values in this
1088 * MutableFloatVector
1089 * @throws ValueException when the vectors do not have the same size
1090 */
1091 protected final void scaleValueByValue(final FloatVector<?> factor) throws ValueException
1092 {
1093 checkSizeAndCopyOnWrite(factor);
1094 for (int index = size(); --index >= 0;)
1095 {
1096 safeSet(index, safeGet(index) * factor.safeGet(index));
1097 }
1098 }
1099
1100 /**
1101 * Scale the values in this MutableFloatVector by the corresponding values in a float array.
1102 * @param factor float[]; contains the values by which to scale the corresponding values in this MutableFloatVector
1103 * @return MutableFloatVector<U>; this modified MutableFloatVector
1104 * @throws ValueException when the vector and the array do not have the same size
1105 */
1106 protected final MutableFloatVector<U> scaleValueByValue(final float[] factor) throws ValueException
1107 {
1108 checkSizeAndCopyOnWrite(factor);
1109 for (int index = size(); --index >= 0;)
1110 {
1111 safeSet(index, safeGet(index) * factor[index]);
1112 }
1113 return this;
1114 }
1115
1116 /**
1117 * Check sizes and copy the data if the copyOnWrite flag is set.
1118 * @param other FloatVector<?>; partner for the size check
1119 * @throws ValueException when the vectors do not have the same size
1120 */
1121 private void checkSizeAndCopyOnWrite(final FloatVector<?> other) throws ValueException
1122 {
1123 checkSize(other);
1124 checkCopyOnWrite();
1125 }
1126
1127 /**
1128 * Check sizes and copy the data if the copyOnWrite flag is set.
1129 * @param other float[]; partner for the size check
1130 * @throws ValueException when the vectors do not have the same size
1131 */
1132 private void checkSizeAndCopyOnWrite(final float[] other) throws ValueException
1133 {
1134 checkSize(other);
1135 checkCopyOnWrite();
1136 }
1137
1138 }