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