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