1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.differentiation;
23
24 import java.io.Serializable;
25
26 import org.hipparchus.Field;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.exception.MathRuntimeException;
29 import org.hipparchus.util.FastMath;
30 import org.hipparchus.util.FieldSinCos;
31 import org.hipparchus.util.FieldSinhCosh;
32 import org.hipparchus.util.MathArrays;
33 import org.hipparchus.util.MathUtils;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class DerivativeStructure implements Derivative<DerivativeStructure>, Serializable {
68
69
70 private static final long serialVersionUID = 20161220L;
71
72
73 private final DSFactory factory;
74
75
76 private final double[] data;
77
78
79
80
81
82 DerivativeStructure(final DSFactory factory, final double[] data) {
83 this.factory = factory;
84 this.data = data.clone();
85 }
86
87
88
89
90
91 DerivativeStructure(final DSFactory factory) {
92 this.factory = factory;
93 this.data = new double[factory.getCompiler().getSize()];
94 }
95
96
97 @Override
98 public DerivativeStructure newInstance(final double value) {
99 return factory.constant(value);
100 }
101
102
103 @Override
104 public DerivativeStructure withValue(final double value) {
105 final DerivativeStructure ds = factory.build();
106 System.arraycopy(data, 1, ds.data, 1, data.length - 1);
107 ds.data[0] = value;
108 return ds;
109 }
110
111
112
113
114 public DSFactory getFactory() {
115 return factory;
116 }
117
118
119 @Override
120 public int getFreeParameters() {
121 return getFactory().getCompiler().getFreeParameters();
122 }
123
124
125 @Override
126 public int getOrder() {
127 return getFactory().getCompiler().getOrder();
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141 void setDerivativeComponent(final int index, final double value) {
142 data[index] = value;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156 double getDerivativeComponent(final int index) {
157 return data[index];
158 }
159
160
161 @Override
162 public DerivativeStructure getAddendum() {
163 final double[] addendum = data.clone();
164 addendum[0] = 0;
165 return new DerivativeStructure(factory, addendum);
166 }
167
168
169
170
171
172 @Override
173 public double getValue() {
174 return data[0];
175 }
176
177
178 @Override
179 public double getPartialDerivative(final int ... orders)
180 throws MathIllegalArgumentException {
181 return data[getFactory().getCompiler().getPartialDerivativeIndex(orders)];
182 }
183
184
185
186
187
188 public double[] getAllDerivatives() {
189 return data.clone();
190 }
191
192
193
194
195
196 @Override
197 public DerivativeStructure add(final DerivativeStructure a)
198 throws MathIllegalArgumentException {
199 factory.checkCompatibility(a.factory);
200 final DerivativeStructure ds = factory.build();
201 factory.getCompiler().add(data, 0, a.data, 0, ds.data, 0);
202 return ds;
203 }
204
205
206
207
208
209 @Override
210 public DerivativeStructure subtract(final DerivativeStructure a)
211 throws MathIllegalArgumentException {
212 factory.checkCompatibility(a.factory);
213 final DerivativeStructure ds = factory.build();
214 factory.getCompiler().subtract(data, 0, a.data, 0, ds.data, 0);
215 return ds;
216 }
217
218
219
220 @Override
221 public DerivativeStructure multiply(final double a) {
222 final DerivativeStructure ds = factory.build();
223 for (int i = 0; i < ds.data.length; ++i) {
224 ds.data[i] = data[i] * a;
225 }
226 return ds;
227 }
228
229
230
231
232
233 @Override
234 public DerivativeStructure multiply(final DerivativeStructure a)
235 throws MathIllegalArgumentException {
236 factory.checkCompatibility(a.factory);
237 final DerivativeStructure result = factory.build();
238 factory.getCompiler().multiply(data, 0, a.data, 0, result.data, 0);
239 return result;
240 }
241
242
243 @Override
244 public DerivativeStructure square() {
245 return multiply(this);
246 }
247
248
249
250 @Override
251 public DerivativeStructure divide(final double a) {
252 final DerivativeStructure ds = factory.build();
253 final double inv = 1.0 / a;
254 for (int i = 0; i < ds.data.length; ++i) {
255 ds.data[i] = data[i] * inv;
256 }
257 return ds;
258 }
259
260
261
262
263
264 @Override
265 public DerivativeStructure divide(final DerivativeStructure a)
266 throws MathIllegalArgumentException {
267 factory.checkCompatibility(a.factory);
268 final DerivativeStructure result = factory.build();
269 factory.getCompiler().divide(data, 0, a.data, 0, result.data, 0);
270 return result;
271 }
272
273
274
275
276
277 @Override
278 public DerivativeStructure remainder(final DerivativeStructure a)
279 throws MathIllegalArgumentException {
280 factory.checkCompatibility(a.factory);
281 final DerivativeStructure result = factory.build();
282 factory.getCompiler().remainder(data, 0, a.data, 0, result.data, 0);
283 return result;
284 }
285
286
287 @Override
288 public DerivativeStructure negate() {
289 final DerivativeStructure ds = factory.build();
290 for (int i = 0; i < ds.data.length; ++i) {
291 ds.data[i] = -data[i];
292 }
293 return ds;
294 }
295
296
297
298 @Override
299 public DerivativeStructure abs() {
300 if (Double.doubleToLongBits(data[0]) < 0) {
301
302 return negate();
303 } else {
304 return this;
305 }
306 }
307
308
309
310 @Override
311 public DerivativeStructure copySign(final DerivativeStructure sign) {
312 long m = Double.doubleToLongBits(data[0]);
313 long s = Double.doubleToLongBits(sign.data[0]);
314 if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) {
315 return this;
316 }
317 return negate();
318 }
319
320
321
322 @Override
323 public DerivativeStructure copySign(final double sign) {
324 long m = Double.doubleToLongBits(data[0]);
325 long s = Double.doubleToLongBits(sign);
326 if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) {
327 return this;
328 }
329 return negate();
330 }
331
332
333
334 @Override
335 public DerivativeStructure scalb(final int n) {
336 final DerivativeStructure ds = factory.build();
337 for (int i = 0; i < ds.data.length; ++i) {
338 ds.data[i] = FastMath.scalb(data[i], n);
339 }
340 return ds;
341 }
342
343
344
345
346
347 @Override
348 public DerivativeStructure hypot(final DerivativeStructure y)
349 throws MathIllegalArgumentException {
350
351 factory.checkCompatibility(y.factory);
352
353 if (Double.isInfinite(data[0]) || Double.isInfinite(y.data[0])) {
354 return factory.constant(Double.POSITIVE_INFINITY);
355 } else if (Double.isNaN(data[0]) || Double.isNaN(y.data[0])) {
356 return factory.constant(Double.NaN);
357 } else {
358
359 final int expX = getExponent();
360 final int expY = y.getExponent();
361 if (expX > expY + 27) {
362
363 return abs();
364 } else if (expY > expX + 27) {
365
366 return y.abs();
367 } else {
368
369
370 final int middleExp = (expX + expY) / 2;
371
372
373 final DerivativeStructure scaledX = scalb(-middleExp);
374 final DerivativeStructure scaledY = y.scalb(-middleExp);
375
376
377 final DerivativeStructure scaledH =
378 scaledX.multiply(scaledX).add(scaledY.multiply(scaledY)).sqrt();
379
380
381 return scaledH.scalb(middleExp);
382
383 }
384
385 }
386 }
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404 public static DerivativeStructure hypot(final DerivativeStructure x, final DerivativeStructure y)
405 throws MathIllegalArgumentException {
406 return x.hypot(y);
407 }
408
409
410
411
412
413
414
415
416
417 @Override
418 public DerivativeStructure compose(final double ... f)
419 throws MathIllegalArgumentException {
420
421 MathUtils.checkDimension(f.length, getOrder() + 1);
422 final DerivativeStructure result = factory.build();
423 factory.getCompiler().compose(data, 0, f, result.data, 0);
424 return result;
425 }
426
427
428 @Override
429 public DerivativeStructure reciprocal() {
430 final DerivativeStructure result = factory.build();
431 factory.getCompiler().reciprocal(data, 0, result.data, 0);
432 return result;
433 }
434
435
436
437 @Override
438 public DerivativeStructure sqrt() {
439 final DerivativeStructure result = factory.build();
440 factory.getCompiler().sqrt(data, 0, result.data, 0);
441 return result;
442 }
443
444
445
446 @Override
447 public DerivativeStructure rootN(final int n) {
448 final DerivativeStructure result = factory.build();
449 factory.getCompiler().rootN(data, 0, n, result.data, 0);
450 return result;
451 }
452
453
454 @Override
455 public Field<DerivativeStructure> getField() {
456 return factory.getDerivativeField();
457 }
458
459
460
461
462
463
464 public static DerivativeStructure pow(final double a, final DerivativeStructure x) {
465 final DerivativeStructure result = x.factory.build();
466 x.factory.getCompiler().pow(a, x.data, 0, result.data, 0);
467 return result;
468 }
469
470
471
472 @Override
473 public DerivativeStructure pow(final double p) {
474 final DerivativeStructure result = factory.build();
475 factory.getCompiler().pow(data, 0, p, result.data, 0);
476 return result;
477 }
478
479
480
481 @Override
482 public DerivativeStructure pow(final int n) {
483 final DerivativeStructure result = factory.build();
484 factory.getCompiler().pow(data, 0, n, result.data, 0);
485 return result;
486 }
487
488
489
490
491
492 @Override
493 public DerivativeStructure pow(final DerivativeStructure e)
494 throws MathIllegalArgumentException {
495 factory.checkCompatibility(e.factory);
496 final DerivativeStructure result = factory.build();
497 factory.getCompiler().pow(data, 0, e.data, 0, result.data, 0);
498 return result;
499 }
500
501
502
503 @Override
504 public DerivativeStructure exp() {
505 final DerivativeStructure result = factory.build();
506 factory.getCompiler().exp(data, 0, result.data, 0);
507 return result;
508 }
509
510
511
512 @Override
513 public DerivativeStructure expm1() {
514 final DerivativeStructure result = factory.build();
515 factory.getCompiler().expm1(data, 0, result.data, 0);
516 return result;
517 }
518
519
520
521 @Override
522 public DerivativeStructure log() {
523 final DerivativeStructure result = factory.build();
524 factory.getCompiler().log(data, 0, result.data, 0);
525 return result;
526 }
527
528
529
530 @Override
531 public DerivativeStructure log1p() {
532 final DerivativeStructure result = factory.build();
533 factory.getCompiler().log1p(data, 0, result.data, 0);
534 return result;
535 }
536
537
538
539
540 @Override
541 public DerivativeStructure log10() {
542 final DerivativeStructure result = factory.build();
543 factory.getCompiler().log10(data, 0, result.data, 0);
544 return result;
545 }
546
547
548
549 @Override
550 public DerivativeStructure cos() {
551 final DerivativeStructure result = factory.build();
552 factory.getCompiler().cos(data, 0, result.data, 0);
553 return result;
554 }
555
556
557
558 @Override
559 public DerivativeStructure sin() {
560 final DerivativeStructure result = factory.build();
561 factory.getCompiler().sin(data, 0, result.data, 0);
562 return result;
563 }
564
565
566
567 @Override
568 public FieldSinCos<DerivativeStructure> sinCos() {
569 final DerivativeStructure sin = factory.build();
570 final DerivativeStructure cos = factory.build();
571 factory.getCompiler().sinCos(data, 0, sin.data, 0, cos.data, 0);
572 return new FieldSinCos<>(sin, cos);
573 }
574
575
576
577 @Override
578 public DerivativeStructure tan() {
579 final DerivativeStructure result = factory.build();
580 factory.getCompiler().tan(data, 0, result.data, 0);
581 return result;
582 }
583
584
585
586 @Override
587 public DerivativeStructure acos() {
588 final DerivativeStructure result = factory.build();
589 factory.getCompiler().acos(data, 0, result.data, 0);
590 return result;
591 }
592
593
594
595 @Override
596 public DerivativeStructure asin() {
597 final DerivativeStructure result = factory.build();
598 factory.getCompiler().asin(data, 0, result.data, 0);
599 return result;
600 }
601
602
603
604 @Override
605 public DerivativeStructure atan() {
606 final DerivativeStructure result = factory.build();
607 factory.getCompiler().atan(data, 0, result.data, 0);
608 return result;
609 }
610
611
612
613 @Override
614 public DerivativeStructure atan2(final DerivativeStructure x)
615 throws MathIllegalArgumentException {
616 factory.checkCompatibility(x.factory);
617 final DerivativeStructure result = factory.build();
618 factory.getCompiler().atan2(data, 0, x.data, 0, result.data, 0);
619 return result;
620 }
621
622
623
624
625
626
627
628
629 public static DerivativeStructure atan2(final DerivativeStructure y, final DerivativeStructure x)
630 throws MathIllegalArgumentException {
631 return y.atan2(x);
632 }
633
634
635
636 @Override
637 public DerivativeStructure cosh() {
638 final DerivativeStructure result = factory.build();
639 factory.getCompiler().cosh(data, 0, result.data, 0);
640 return result;
641 }
642
643
644
645 @Override
646 public DerivativeStructure sinh() {
647 final DerivativeStructure result = factory.build();
648 factory.getCompiler().sinh(data, 0, result.data, 0);
649 return result;
650 }
651
652
653
654 @Override
655 public FieldSinhCosh<DerivativeStructure> sinhCosh() {
656 final DerivativeStructure sinh = factory.build();
657 final DerivativeStructure cosh = factory.build();
658 factory.getCompiler().sinhCosh(data, 0, sinh.data, 0, cosh.data, 0);
659 return new FieldSinhCosh<>(sinh, cosh);
660 }
661
662
663
664 @Override
665 public DerivativeStructure tanh() {
666 final DerivativeStructure result = factory.build();
667 factory.getCompiler().tanh(data, 0, result.data, 0);
668 return result;
669 }
670
671
672
673 @Override
674 public DerivativeStructure acosh() {
675 final DerivativeStructure result = factory.build();
676 factory.getCompiler().acosh(data, 0, result.data, 0);
677 return result;
678 }
679
680
681
682 @Override
683 public DerivativeStructure asinh() {
684 final DerivativeStructure result = factory.build();
685 factory.getCompiler().asinh(data, 0, result.data, 0);
686 return result;
687 }
688
689
690
691 @Override
692 public DerivativeStructure atanh() {
693 final DerivativeStructure result = factory.build();
694 factory.getCompiler().atanh(data, 0, result.data, 0);
695 return result;
696 }
697
698
699 @Override
700 public DerivativeStructure toDegrees() {
701 final DerivativeStructure ds = factory.build();
702 for (int i = 0; i < ds.data.length; ++i) {
703 ds.data[i] = FastMath.toDegrees(data[i]);
704 }
705 return ds;
706 }
707
708
709 @Override
710 public DerivativeStructure toRadians() {
711 final DerivativeStructure ds = factory.build();
712 for (int i = 0; i < ds.data.length; ++i) {
713 ds.data[i] = FastMath.toRadians(data[i]);
714 }
715 return ds;
716 }
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732 public DerivativeStructure integrate(final int varIndex, final int integrationOrder) {
733
734
735 if (integrationOrder > getOrder()) {
736 return factory.constant(0.);
737 } else if (integrationOrder == 0) {
738 return factory.build(data);
739 }
740
741
742 if (integrationOrder < 0) {
743 return differentiate(varIndex, -integrationOrder);
744 }
745
746 final double[] newData = new double[data.length];
747 final DSCompiler dsCompiler = factory.getCompiler();
748 for (int i = 0; i < newData.length; i++) {
749 if (data[i] != 0.) {
750 final int[] orders = dsCompiler.getPartialDerivativeOrders(i);
751 int sum = 0;
752 for (int order : orders) {
753 sum += order;
754 }
755 if (sum + integrationOrder <= getOrder()) {
756 final int saved = orders[varIndex];
757 orders[varIndex] += integrationOrder;
758 final int index = dsCompiler.getPartialDerivativeIndex(orders);
759 orders[varIndex] = saved;
760 newData[index] = data[i];
761 }
762 }
763 }
764
765 return factory.build(newData);
766 }
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782 public DerivativeStructure differentiate(final int varIndex, final int differentiationOrder) {
783
784
785 if (differentiationOrder > getOrder()) {
786 return factory.constant(0.);
787 } else if (differentiationOrder == 0) {
788 return factory.build(data);
789 }
790
791
792 if (differentiationOrder < 0) {
793 return integrate(varIndex, -differentiationOrder);
794 }
795
796 final double[] newData = new double[data.length];
797 final DSCompiler dsCompiler = factory.getCompiler();
798 for (int i = 0; i < newData.length; i++) {
799 if (data[i] != 0.) {
800 final int[] orders = dsCompiler.getPartialDerivativeOrders(i);
801 if (orders[varIndex] - differentiationOrder >= 0) {
802 final int saved = orders[varIndex];
803 orders[varIndex] -= differentiationOrder;
804 final int index = dsCompiler.getPartialDerivativeIndex(orders);
805 orders[varIndex] = saved;
806 newData[index] = data[i];
807 }
808 }
809 }
810
811 return factory.build(newData);
812 }
813
814
815
816
817
818
819 public double taylor(final double ... delta) throws MathRuntimeException {
820 return factory.getCompiler().taylor(data, 0, delta);
821 }
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858 public DerivativeStructure rebase(final DerivativeStructure... p) {
859
860 MathUtils.checkDimension(getFreeParameters(), p.length);
861
862
863 if (p.length == 0) {
864 return this;
865 }
866
867 final int pSize = p[0].getFactory().getCompiler().getSize();
868 final double[] pData = new double[p.length * pSize];
869 for (int i = 0; i < p.length; ++i) {
870 MathUtils.checkDimension(getOrder(), p[i].getOrder());
871 MathUtils.checkDimension(p[0].getFreeParameters(), p[i].getFreeParameters());
872 System.arraycopy(p[i].data, 0, pData, i * pSize, pSize);
873 }
874
875 final DerivativeStructure result = p[0].factory.build();
876 factory.getCompiler().rebase(data, 0, p[0].factory.getCompiler(), pData, result.data, 0);
877 return result;
878
879 }
880
881
882
883
884
885 @Override
886 public DerivativeStructure linearCombination(final DerivativeStructure[] a, final DerivativeStructure[] b)
887 throws MathIllegalArgumentException {
888
889
890 final double[] aDouble = new double[a.length];
891 for (int i = 0; i < a.length; ++i) {
892 aDouble[i] = a[i].getValue();
893 }
894 final double[] bDouble = new double[b.length];
895 for (int i = 0; i < b.length; ++i) {
896 bDouble[i] = b[i].getValue();
897 }
898 final double accurateValue = MathArrays.linearCombination(aDouble, bDouble);
899
900
901 DerivativeStructure simpleValue = a[0].getField().getZero();
902 for (int i = 0; i < a.length; ++i) {
903 simpleValue = simpleValue.add(a[i].multiply(b[i]));
904 }
905
906
907 final double[] all = simpleValue.getAllDerivatives();
908 all[0] = accurateValue;
909 return factory.build(all);
910
911 }
912
913
914
915
916
917 @Override
918 public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b)
919 throws MathIllegalArgumentException {
920
921
922 final double[] bDouble = new double[b.length];
923 for (int i = 0; i < b.length; ++i) {
924 bDouble[i] = b[i].getValue();
925 }
926 final double accurateValue = MathArrays.linearCombination(a, bDouble);
927
928
929 DerivativeStructure simpleValue = b[0].getField().getZero();
930 for (int i = 0; i < a.length; ++i) {
931 simpleValue = simpleValue.add(b[i].multiply(a[i]));
932 }
933
934
935 final double[] all = simpleValue.getAllDerivatives();
936 all[0] = accurateValue;
937 return factory.build(all);
938
939 }
940
941
942
943
944
945 @Override
946 public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1,
947 final DerivativeStructure a2, final DerivativeStructure b2)
948 throws MathIllegalArgumentException {
949
950
951 final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(),
952 a2.getValue(), b2.getValue());
953
954
955 final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2));
956
957
958 final double[] all = simpleValue.getAllDerivatives();
959 all[0] = accurateValue;
960 return factory.build(all);
961
962 }
963
964
965
966
967
968 @Override
969 public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
970 final double a2, final DerivativeStructure b2)
971 throws MathIllegalArgumentException {
972
973 factory.checkCompatibility(b1.factory);
974 factory.checkCompatibility(b2.factory);
975
976 final DerivativeStructure ds = factory.build();
977 factory.getCompiler().linearCombination(a1, b1.data, 0,
978 a2, b2.data, 0,
979 ds.data, 0);
980
981 return ds;
982
983 }
984
985
986
987
988
989 @Override
990 public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1,
991 final DerivativeStructure a2, final DerivativeStructure b2,
992 final DerivativeStructure a3, final DerivativeStructure b3)
993 throws MathIllegalArgumentException {
994
995
996 final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(),
997 a2.getValue(), b2.getValue(),
998 a3.getValue(), b3.getValue());
999
1000
1001 final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3));
1002
1003
1004 final double[] all = simpleValue.getAllDerivatives();
1005 all[0] = accurateValue;
1006 return factory.build(all);
1007
1008 }
1009
1010
1011
1012
1013
1014 @Override
1015 public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
1016 final double a2, final DerivativeStructure b2,
1017 final double a3, final DerivativeStructure b3)
1018 throws MathIllegalArgumentException {
1019
1020 factory.checkCompatibility(b1.factory);
1021 factory.checkCompatibility(b2.factory);
1022 factory.checkCompatibility(b3.factory);
1023
1024 final DerivativeStructure ds = factory.build();
1025 factory.getCompiler().linearCombination(a1, b1.data, 0,
1026 a2, b2.data, 0,
1027 a3, b3.data, 0,
1028 ds.data, 0);
1029
1030 return ds;
1031
1032 }
1033
1034
1035
1036
1037
1038 @Override
1039 public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1,
1040 final DerivativeStructure a2, final DerivativeStructure b2,
1041 final DerivativeStructure a3, final DerivativeStructure b3,
1042 final DerivativeStructure a4, final DerivativeStructure b4)
1043 throws MathIllegalArgumentException {
1044
1045
1046 final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(),
1047 a2.getValue(), b2.getValue(),
1048 a3.getValue(), b3.getValue(),
1049 a4.getValue(), b4.getValue());
1050
1051
1052 final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)).add(a4.multiply(b4));
1053
1054
1055 final double[] all = simpleValue.getAllDerivatives();
1056 all[0] = accurateValue;
1057 return factory.build(all);
1058
1059 }
1060
1061
1062
1063
1064
1065 @Override
1066 public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
1067 final double a2, final DerivativeStructure b2,
1068 final double a3, final DerivativeStructure b3,
1069 final double a4, final DerivativeStructure b4)
1070 throws MathIllegalArgumentException {
1071
1072 factory.checkCompatibility(b1.factory);
1073 factory.checkCompatibility(b2.factory);
1074 factory.checkCompatibility(b3.factory);
1075 factory.checkCompatibility(b4.factory);
1076
1077 final DerivativeStructure ds = factory.build();
1078 factory.getCompiler().linearCombination(a1, b1.data, 0,
1079 a2, b2.data, 0,
1080 a3, b3.data, 0,
1081 a4, b4.data, 0,
1082 ds.data, 0);
1083
1084 return ds;
1085
1086 }
1087
1088
1089
1090 @Override
1091 public DerivativeStructure getPi() {
1092 return factory.getDerivativeField().getPi();
1093 }
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 @Override
1105 public boolean equals(Object other) {
1106
1107 if (this == other) {
1108 return true;
1109 }
1110
1111 if (other instanceof DerivativeStructure) {
1112 final DerivativeStructure rhs = (DerivativeStructure)other;
1113 return (getFreeParameters() == rhs.getFreeParameters()) &&
1114 (getOrder() == rhs.getOrder()) &&
1115 MathArrays.equals(data, rhs.data);
1116 }
1117
1118 return false;
1119
1120 }
1121
1122
1123
1124
1125
1126 @Override
1127 public int hashCode() {
1128 return 227 + 229 * getFreeParameters() + 233 * getOrder() + 239 * MathUtils.hash(data);
1129 }
1130
1131 }