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