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