1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis;
18
19 import org.hipparchus.CalculusFieldElement;
20 import org.hipparchus.util.Binary64;
21 import org.hipparchus.util.Binary64Field;
22 import org.hipparchus.util.MathArrays;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 public class FieldFunctionsTest {
27
28 @Test
29 public void testScalarUnivariateFunctionConversion() {
30 FieldUnivariateFunction f1 = new FieldUnivariateFunction() {
31 public <T extends CalculusFieldElement<T>> T value(T x) {
32 return x.multiply(2);
33 }
34 };
35 CalculusFieldUnivariateFunction<Binary64> f1Converted = f1.toCalculusFieldUnivariateFunction(Binary64Field.getInstance());
36 CalculusFieldUnivariateFunction<Binary64> f2 = x -> x.multiply(2);
37
38 for (double x = 0; x < 1; x += 0.01) {
39 Assert.assertEquals(f2.value(new Binary64(x)).getReal(),
40 f1Converted.value(new Binary64(x)).getReal(),
41 1.0e-15);
42 }
43 }
44
45 @Test
46 public void testScalarMultivariateFunctionConversion() {
47 FieldMultivariateFunction f1 = new FieldMultivariateFunction() {
48 public <T extends CalculusFieldElement<T>> T value(@SuppressWarnings("unchecked") T... x) {
49 return x[0].multiply(2).add(x[1]);
50 }
51 };
52 CalculusFieldMultivariateFunction<Binary64> f1Converted = f1.toCalculusFieldMultivariateFunction(Binary64Field.getInstance());
53 CalculusFieldMultivariateFunction<Binary64> f2 = x -> x[0].multiply(2).add(x[1]);
54
55 for (double x0 = 0; x0 < 1; x0 += 0.01) {
56 for (double x1 = 0; x1 < 1; x1 += 0.01) {
57 Assert.assertEquals(f2.value(new Binary64(x0), new Binary64(x1)).getReal(),
58 f1Converted.value(new Binary64(x0), new Binary64(x1)).getReal(),
59 1.0e-15);
60 }
61 }
62 }
63
64 @Test
65 public void testVectorUnivariateFunctionConversion() {
66 FieldUnivariateVectorFunction f1 = new FieldUnivariateVectorFunction() {
67 public <T extends CalculusFieldElement<T>> T[] value(T x) {
68 T[] y = MathArrays.buildArray(x.getField(), 3);
69 y[0] = x.add(1);
70 y[1] = x.multiply(2);
71 y[2] = x.square();
72 return y;
73 }
74 };
75 CalculusFieldUnivariateVectorFunction<Binary64> f1Converted = f1.toCalculusFieldUnivariateVectorFunction(Binary64Field.getInstance());
76 CalculusFieldUnivariateVectorFunction<Binary64> f2 = x -> new Binary64[] {
77 x.add(1), x.multiply(2), x.square()
78 };
79
80 for (double x = 0; x < 1; x += 0.01) {
81 for (int i = 0; i < 3; ++i) {
82 Assert.assertEquals(f2.value(new Binary64(x))[i].getReal(),
83 f1Converted.value(new Binary64(x))[i].getReal(),
84 1.0e-15);
85 }
86 }
87 }
88
89 @Test
90 public void testVectorMultivariateFunctionConversion() {
91 FieldMultivariateVectorFunction f1 = new FieldMultivariateVectorFunction() {
92 public <T extends CalculusFieldElement<T>> T[] value(@SuppressWarnings("unchecked") T... x) {
93 T[] y = MathArrays.buildArray(x[0].getField(), 3);
94 y[0] = x[0].add(1);
95 y[1] = x[1].multiply(2);
96 y[2] = x[0].multiply(x[1]);
97 return y;
98 }
99 };
100 CalculusFieldMultivariateVectorFunction<Binary64> f1Converted = f1.toCalculusFieldMultivariateVectorFunction(Binary64Field.getInstance());
101 CalculusFieldMultivariateVectorFunction<Binary64> f2 = x -> new Binary64[] {
102 x[0].add(1), x[1].multiply(2), x[0].multiply(x[1])
103 };
104
105 for (double x0 = 0; x0 < 1; x0 += 0.01) {
106 for (double x1 = 0; x1 < 1; x1 += 0.01) {
107 for (int i = 0; i < 3; ++i) {
108 Assert.assertEquals(f2.value(new Binary64(x0), new Binary64(x1))[i].getReal(),
109 f1Converted.value(new Binary64(x0), new Binary64(x1))[i].getReal(),
110 1.0e-15);
111 }
112 }
113 }
114 }
115
116 @Test
117 public void testMatrixUnivariateFunctionConversion() {
118 FieldUnivariateMatrixFunction f1 = new FieldUnivariateMatrixFunction() {
119 public <T extends CalculusFieldElement<T>> T[][] value(T x) {
120 T[][] y = MathArrays.buildArray(x.getField(), 2, 2);
121 y[0][0] = x.add(1);
122 y[0][1] = x.multiply(2);
123 y[1][0] = x.square();
124 y[1][1] = x.sin();
125 return y;
126 }
127 };
128 CalculusFieldUnivariateMatrixFunction<Binary64> f1Converted = f1.toCalculusFieldUnivariateMatrixFunction(Binary64Field.getInstance());
129 CalculusFieldUnivariateMatrixFunction<Binary64> f2 = x -> new Binary64[][] {
130 { x.add(1), x.multiply(2) },
131 { x.square(), x.sin() }
132 };
133
134 for (double x = 0; x < 1; x += 0.01) {
135 for (int i = 0; i < 2; ++i) {
136 for (int j = 0; j < 2; ++j) {
137 Assert.assertEquals(f2.value(new Binary64(x))[i][j].getReal(),
138 f1Converted.value(new Binary64(x))[i][j].getReal(),
139 1.0e-15);
140 }
141 }
142 }
143 }
144
145 @Test
146 public void testMatrixMultivariateFunctionConversion() {
147 FieldMultivariateMatrixFunction f1 = new FieldMultivariateMatrixFunction() {
148 public <T extends CalculusFieldElement<T>> T[][] value(@SuppressWarnings("unchecked") T... x) {
149 T[][] y = MathArrays.buildArray(x[0].getField(), 2, 2);
150 y[0][0] = x[0].add(1);
151 y[0][1] = x[1].multiply(2);
152 y[1][0] = x[0].multiply(x[1]);
153 y[1][1] = x[1].sin();
154 return y;
155 }
156 };
157 CalculusFieldMultivariateMatrixFunction<Binary64> f1Converted = f1.toCalculusFieldMultivariateMatrixFunction(Binary64Field.getInstance());
158 CalculusFieldMultivariateMatrixFunction<Binary64> f2 = x -> new Binary64[][] {
159 { x[0].add(1), x[1].multiply(2) },
160 { x[0].multiply(x[1]), x[1].sin() }
161 };
162
163 for (double x0 = 0; x0 < 1; x0 += 0.01) {
164 for (double x1 = 0; x1 < 1; x1 += 0.01) {
165 for (int i = 0; i < 2; ++i) {
166 for (int j = 0; j < 2; ++j) {
167 Assert.assertEquals(f2.value(new Binary64(x0), new Binary64(x1))[i][j].getReal(),
168 f1Converted.value(new Binary64(x0), new Binary64(x1))[i][j].getReal(),
169 1.0e-15);
170 }
171 }
172 }
173 }
174 }
175
176 }