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   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  public final class Nan implements MathFunction
21  {
22      
23      public static final Nan NAN = new Nan();
24  
25      
26  
27  
28      private Nan()
29      {
30          
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; 
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  }