View Javadoc
1   package org.djutils.polynomialroots;
2   
3   /**
4    * BenchmarkPolynomialRoots tests the speed of different algorithms.
5    * <p>
6    * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
8    * </p>
9    * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
10   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
11   */
12  public final class BenchmarkPolynomialRoots
13  {
14      /** utility class. */
15      private BenchmarkPolynomialRoots()
16      {
17          // utility class
18      }
19  
20      /**
21       * Test the solver for quadratic (and simpler) cases and ensure that the cubic solver falls back to the quadratic solver
22       * when appropriate.
23       */
24      public static void quadraticTest()
25      {
26          long time = System.currentTimeMillis();
27          double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.0001, 10000};
28          for (double a : paramValues)
29          {
30              for (double b : paramValues)
31              {
32                  for (double c : paramValues)
33                  {
34                      PolynomialRoots2.quadraticRoots(a, b, c);
35                  }
36              }
37          }
38          System.out.println("quadratic F77: " + (System.currentTimeMillis() - time) + "ms");
39      }
40  
41      /**
42       * Test the cubic solver.
43       */
44      public static void cubicTest()
45      {
46          long time = System.currentTimeMillis();
47          double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.001, 1000};
48          for (double a : paramValues)
49          {
50              for (double b : paramValues)
51              {
52                  for (double c : paramValues)
53                  {
54                      for (double d : paramValues)
55                      {
56                          PolynomialRoots.cubicRoots(a, b, c, d);
57                      }
58                  }
59              }
60          }
61          System.out.println("cubic F77: " + (System.currentTimeMillis() - time) + "ms");
62      }
63  
64      /**
65       * Test the quartic solver.
66       */
67      public static void quarticTest()
68      {
69          long time = System.currentTimeMillis();
70          double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.01, 100};
71          for (double a : paramValues)
72          {
73              for (double b : paramValues)
74              {
75                  for (double c : paramValues)
76                  {
77                      for (double d : paramValues)
78                      {
79                          for (double e : paramValues)
80                          {
81                              PolynomialRoots.quarticRoots(a, b, c, d, e);
82                          }
83                      }
84                  }
85              }
86          }
87          System.out.println("quartic F77: " + (System.currentTimeMillis() - time) + "ms");
88      }
89  
90      /**
91       * Test the cubic solver.
92       */
93      public static void cubicTestDurandKerner()
94      {
95          long time = System.currentTimeMillis();
96          double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.001, 1000};
97          for (double a : paramValues)
98          {
99              for (double b : paramValues)
100             {
101                 for (double c : paramValues)
102                 {
103                     for (double d : paramValues)
104                     {
105                         PolynomialRoots2.cubicRootsDurandKerner(a, b, c, d);
106                     }
107                 }
108             }
109         }
110         System.out.println("cubic Durand-Kerner: " + (System.currentTimeMillis() - time) + "ms");
111     }
112 
113     /**
114      * Test the quartic solver.
115      */
116     public static void quarticTestDurandKerner()
117     {
118         long time = System.currentTimeMillis();
119         double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.01, 100};
120         for (double a : paramValues)
121         {
122             for (double b : paramValues)
123             {
124                 for (double c : paramValues)
125                 {
126                     for (double d : paramValues)
127                     {
128                         for (double e : paramValues)
129                         {
130                             PolynomialRoots2.quarticRootsDurandKerner(a, b, c, d, e);
131                         }
132                     }
133                 }
134             }
135         }
136         System.out.println("quartic Durand-Kerner: " + (System.currentTimeMillis() - time) + "ms");
137     }
138 
139     /**
140      * Test the cubic solver.
141      */
142     public static void cubicTestAberthEhrlich()
143     {
144         long time = System.currentTimeMillis();
145         double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.001, 1000};
146         for (double a : paramValues)
147         {
148             for (double b : paramValues)
149             {
150                 for (double c : paramValues)
151                 {
152                     for (double d : paramValues)
153                     {
154                         PolynomialRoots2.cubicRootsAberthEhrlich(a, b, c, d);
155                     }
156                 }
157             }
158         }
159         System.out.println("cubic Aberth-Ehrlich: " + (System.currentTimeMillis() - time) + "ms");
160     }
161 
162     /**
163      * Test the quartic solver.
164      */
165     public static void quarticTestAberthEhrlich()
166     {
167         long time = System.currentTimeMillis();
168         double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.01, 100};
169         for (double a : paramValues)
170         {
171             for (double b : paramValues)
172             {
173                 for (double c : paramValues)
174                 {
175                     for (double d : paramValues)
176                     {
177                         for (double e : paramValues)
178                         {
179                             PolynomialRoots2.quarticRootsAberthEhrlich(a, b, c, d, e);
180                         }
181                     }
182                 }
183             }
184         }
185         System.out.println("quartic Aberth-Ehrlich: " + (System.currentTimeMillis() - time) + "ms");
186     }
187 
188     /**
189      * Test the cubic solver.
190      */
191     public static void cubicTestNewtonFactor()
192     {
193         long time = System.currentTimeMillis();
194         double[] paramValues = new double[] {0, 1, 2, 3, 4, Math.PI, -Math.E, 0.001, 1000};
195         for (double a : paramValues)
196         {
197             for (double b : paramValues)
198             {
199                 for (double c : paramValues)
200                 {
201                     for (double d : paramValues)
202                     {
203                         PolynomialRoots2.cubicRootsNewtonFactor(a, b, c, d);
204                     }
205                 }
206             }
207         }
208         System.out.println("cubic Cardano: " + (System.currentTimeMillis() - time) + "ms");
209     }
210 
211     /**
212      * @param args String[] not used
213      */
214     public static void main(final String[] args)
215     {
216         quadraticTest();
217         cubicTest();
218         cubicTestDurandKerner();
219         cubicTestAberthEhrlich();
220         cubicTestNewtonFactor();
221         quarticTest();
222         quarticTestDurandKerner();
223         quarticTestAberthEhrlich();
224     }
225 
226 }