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