View Javadoc
1   package org.djutils.math.functions;
2   
3   import java.util.SortedSet;
4   import java.util.TreeSet;
5   
6   import org.djutils.exceptions.Throw;
7   
8   /**
9    * Nan; MathFunction that returns NaN.
10   * <p>
11   * Copyright (c) 2024-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
13   * distributed under a three-clause BSD-style license, which can be found at
14   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
15   * </p>
16   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
18   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
19   */
20  public final class Nan implements MathFunction
21  {
22      /** MathFunction that always return NaN (Not A Number). */
23      public static final Nan NAN = new Nan();
24  
25      /**
26       * Utility class; do not instantiate.
27       */
28      private Nan()
29      {
30          // Do not instantiate
31      }
32  
33      @Override
34      public Double apply(final Double x)
35      {
36          return Double.NaN;
37      }
38  
39      @Override
40      public MathFunction getDerivative()
41      {
42          return this; // same NaN value, same domain
43      }
44  
45      @Override
46      public MathFunction scaleBy(final double factor)
47      {
48          return this;
49      }
50  
51      @Override
52      public int sortPriority()
53      {
54          return 3;
55      }
56  
57      @Override
58      public int compareWithinSubType(final MathFunction other)
59      {
60          Throw.when(!(other instanceof Nan), IllegalArgumentException.class, "other is of wrong type");
61          return 0;
62      }
63  
64      @Override
65      public KnotReport getKnotReport(final Interval<?> interval)
66      {
67          return interval.low() < interval.high() ? KnotReport.KNOWN_INFINITE : KnotReport.KNOWN_FINITE;
68      }
69  
70      @Override
71      public SortedSet<Double> getKnots(final Interval<?> interval)
72      {
73          if (interval.low() == interval.high())
74          {
75              SortedSet<Double> result =  new TreeSet<Double>();
76              result.add(interval.low());
77              return result;
78          }
79          throw new UnsupportedOperationException("there are Infinitely many knots in " + interval);
80      }
81  
82      @Override
83      public String toString()
84      {
85          return "NaN";
86      }
87  
88  }