Skip to content

Commit 3cb51ee

Browse files
committed
Functional interface tests
1 parent 7a16d96 commit 3cb51ee

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1.8.0_191 amd64
2+
Direct: ...........-8069730.430666992........373
3+
Direct: ...........-8069730.430666992........269
4+
Direct: ...........-8069730.430666992........267
5+
Unwrapped: ........-8069730.430666992................ 248
6+
Unwrapped: ........-8069730.430666992................ 432
7+
Unwrapped: ........-8069730.430666992................ 430
8+
Wrapped: ..........-8069730.430666992........................263
9+
Wrapped: ..........-8069730.430666992........................248
10+
Wrapped: ..........-8069730.430666992........................247
11+
Direct: ...........-8069730.430666992........271
12+
Unwrapped: ........-8069730.430666992................ 430
13+
Wrapped: ..........-8069730.430666992........................244
14+
Direct: ...........-8069730.430666992........271
15+
Unwrapped: ........-8069730.430666992................ 429
16+
Wrapped: ..........-8069730.430666992........................241
17+
Direct: ...........-8069730.430666992........270
18+
Unwrapped: ........-8069730.430666992................ 426
19+
Wrapped: ..........-8069730.430666992........................248
20+
Wrapped: ..........-8069730.430666992........................245
21+
Wrapped: ..........-8069730.430666992........................242
22+
Wrapped: ..........-8069730.430666992........................244
23+
Unwrapped: ........-8069730.430666992................ 429
24+
Unwrapped: ........-8069730.430666992................ 422
25+
Unwrapped: ........-8069730.430666992................ 423
26+
Direct: ...........-8069730.430666992........264
27+
Direct: ...........-8069730.430666992........265
28+
Direct: ...........-8069730.430666992........269
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1.8 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
2+
Direct: ...........-8069730.43066709........188
3+
Direct: ...........-8069730.43066709........187
4+
Direct: ...........-8069730.43066709........183
5+
Unwrapped: ........-8069730.43066709................ 189
6+
Unwrapped: ........-8069730.43066709................ 186
7+
Unwrapped: ........-8069730.43066709................ 183
8+
Wrapped: ..........-8069730.43066709........................189
9+
Wrapped: ..........-8069730.43066709........................188
10+
Wrapped: ..........-8069730.43066709........................189
11+
Direct: ...........-8069730.43066709........185
12+
Unwrapped: ........-8069730.43066709................ 186
13+
Wrapped: ..........-8069730.43066709........................189
14+
Direct: ...........-8069730.43066709........185
15+
Unwrapped: ........-8069730.43066709................ 183
16+
Wrapped: ..........-8069730.43066709........................187
17+
Direct: ...........-8069730.43066709........185
18+
Unwrapped: ........-8069730.43066709................ 184
19+
Wrapped: ..........-8069730.43066709........................186
20+
Wrapped: ..........-8069730.43066709........................189
21+
Wrapped: ..........-8069730.43066709........................190
22+
Wrapped: ..........-8069730.43066709........................190
23+
Unwrapped: ........-8069730.43066709................ 196
24+
Unwrapped: ........-8069730.43066709................ 223
25+
Unwrapped: ........-8069730.43066709................ 185
26+
Direct: ...........-8069730.43066709........186
27+
Direct: ...........-8069730.43066709........184
28+
Direct: ...........-8069730.43066709........182
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1.8 Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0
2+
Direct: ...........-8069730.430666382........148
3+
Direct: ...........-8069730.430666382........149
4+
Direct: ...........-8069730.430666382........151
5+
Unwrapped: ........-7908873.121916154................ 155
6+
Unwrapped: ........-7908873.121916154................ 170
7+
Unwrapped: ........-7908873.121916154................ 195
8+
Wrapped: ..........-8069730.430666382........................166
9+
Wrapped: ..........-8069730.430666382........................148
10+
Wrapped: ..........-8069730.430666382........................141
11+
Direct: ...........-8069730.430666382........139
12+
Unwrapped: ........-7908873.121916154................ 138
13+
Wrapped: ..........-8069730.430666382........................140
14+
Direct: ...........-8069730.430666382........165
15+
Unwrapped: ........-7908873.121916154................ 200
16+
Wrapped: ..........-8069730.430666382........................142
17+
Direct: ...........-8069730.430666382........141
18+
Unwrapped: ........-7908873.121916154................ 140
19+
Wrapped: ..........-8069730.430666382........................147
20+
Wrapped: ..........-8069730.430666382........................143
21+
Wrapped: ..........-8069730.430666382........................140
22+
Wrapped: ..........-8069730.430666382........................134
23+
Unwrapped: ........-7908873.121916154................ 124
24+
Unwrapped: ........-7908873.121916154................ 121
25+
Unwrapped: ........-7908873.121916154................ 124
26+
Direct: ...........-8069730.430666382........122
27+
Direct: ...........-8069730.430666382........121
28+
Direct: ...........-8069730.430666382........124
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Open Source Physics software is free software as described near the bottom of this code file.
3+
*
4+
* For additional information and documentation on Open Source Physics please see:
5+
* <http://www.opensourcephysics.org/>
6+
*/
7+
8+
package test; //org.opensourcephysics.numerics;
9+
10+
import java.util.function.DoubleUnaryOperator;
11+
12+
/**
13+
* Derivative defines various derivative algorithms. This class cannot be
14+
* subclassed or instantiated because all methods are static.
15+
*
16+
* For testing, using java.util.function.DoubleUnaryOperator
17+
*
18+
* @author Wolfgang Christian
19+
*/
20+
public class Derivative {
21+
22+
/**
23+
* The function.
24+
*
25+
*/
26+
public static double math(double x) {
27+
return x * Math.log(x) * Math.sin(x) * Math.cos(x);
28+
}
29+
30+
/**
31+
* As a functional interface.
32+
*
33+
*/
34+
static DoubleUnaryOperator f = new DoubleUnaryOperator() {
35+
36+
@Override
37+
public double applyAsDouble(double x) {
38+
return x * Math.log(x) * Math.sin(x) * Math.cos(x);
39+
}
40+
41+
};
42+
43+
/**
44+
* First derivative wrapped to re-assign one of the two parameters.
45+
*
46+
* @param f
47+
* @param h
48+
* @return
49+
*/
50+
static public DoubleUnaryOperator getFirstWrapped(final DoubleUnaryOperator f, final double h) {
51+
return new DoubleUnaryOperator() {
52+
@Override
53+
public double applyAsDouble(double x) {
54+
return (f.applyAsDouble(x + h) - f.applyAsDouble(x - h)) / h / 2.0;
55+
}
56+
57+
};
58+
}
59+
60+
static DoubleUnaryOperator wrappedFirst = getFirstWrapped(f, 0.1);
61+
62+
private static void directCalc(boolean report) {
63+
System.gc();
64+
double result = 0;
65+
double h = 0.1;
66+
long t0 = System.currentTimeMillis();
67+
for (int i = 1; i < 1000000; i++) {
68+
result += (math(i + h) - math(i - h)) / h / 2;
69+
}
70+
if (report)
71+
System.out.println("Direct: ..........."+ result + "........" + (System.currentTimeMillis() - t0));
72+
73+
}
74+
75+
private static void unwrappedFI(DoubleUnaryOperator f, boolean report) {
76+
System.gc();
77+
double result = 0;
78+
long t0 = System.currentTimeMillis();
79+
double h = 0.1;
80+
for (int i = 1; i < 1000000; i++)
81+
result += (f.applyAsDouble(i + h) - f.applyAsDouble(i - h)) / h / 2.0;
82+
if (report)
83+
System.out.println("Unwrapped: ........"+ result + "................ " + (System.currentTimeMillis() - t0));
84+
}
85+
86+
private static void wrappedFI(DoubleUnaryOperator wrappedFirst, boolean report) {
87+
System.gc();
88+
double result = 0;
89+
long t0 = System.currentTimeMillis();
90+
for (int i = 1; i < 1000000; i++)
91+
result += wrappedFirst.applyAsDouble(i);
92+
if (report)
93+
System.out.println("Wrapped: .........." + result + "........................" + (System.currentTimeMillis() - t0));
94+
95+
}
96+
97+
public static void main(String[] args) {
98+
99+
System.out.println(System.getProperty("java.version") + " " + System.getProperty("os.arch"));
100+
unwrappedFI(f, false);
101+
wrappedFI(wrappedFirst, false);
102+
directCalc(false);
103+
104+
directCalc(true);
105+
directCalc(true);
106+
directCalc(true);
107+
unwrappedFI(f, true);
108+
unwrappedFI(f, true);
109+
unwrappedFI(f, true);
110+
wrappedFI(wrappedFirst, true);
111+
wrappedFI(wrappedFirst, true);
112+
wrappedFI(wrappedFirst, true);
113+
114+
directCalc(true);
115+
unwrappedFI(f, true);
116+
wrappedFI(wrappedFirst, true);
117+
directCalc(true);
118+
unwrappedFI(f, true);
119+
wrappedFI(wrappedFirst, true);
120+
directCalc(true);
121+
unwrappedFI(f, true);
122+
wrappedFI(wrappedFirst, true);
123+
124+
wrappedFI(wrappedFirst, true);
125+
wrappedFI(wrappedFirst, true);
126+
wrappedFI(wrappedFirst, true);
127+
unwrappedFI(f, true);
128+
unwrappedFI(f, true);
129+
unwrappedFI(f, true);
130+
directCalc(true);
131+
directCalc(true);
132+
directCalc(true);
133+
134+
wrappedFI(wrappedFirst, false);
135+
unwrappedFI(f, false);
136+
directCalc(false);
137+
138+
}
139+
140+
}

0 commit comments

Comments
 (0)