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