View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math4.legacy.ode.events;
19  
20  import org.apache.commons.math4.core.jdkmath.JdkMath;
21  import org.apache.commons.numbers.core.Precision;
22  
23  
24  /** Transformer for {@link EventHandler#g(double, double[]) g functions}.
25   * @see EventFilter
26   * @see FilterType
27   * @since 3.2
28   */
29  enum Transformer {
30  
31      /** Transformer computing transformed = 0.
32       * <p>
33       * This transformer is used when we initialize the filter, until we get at
34       * least one non-zero value to select the proper transformer.
35       * </p>
36       */
37      UNINITIALIZED {
38          /**  {@inheritDoc} */
39          @Override
40          protected double transformed(final double g) {
41              return 0;
42          }
43      },
44  
45      /** Transformer computing transformed = g.
46       * <p>
47       * When this transformer is applied, the roots of the original function
48       * are preserved, with the same {@code increasing/decreasing} status.
49       * </p>
50       */
51      PLUS {
52          /**  {@inheritDoc} */
53          @Override
54          protected double transformed(final double g) {
55              return g;
56          }
57      },
58  
59      /** Transformer computing transformed = -g.
60       * <p>
61       * When this transformer is applied, the roots of the original function
62       * are preserved, with reversed {@code increasing/decreasing} status.
63       * </p>
64       */
65      MINUS {
66          /**  {@inheritDoc} */
67          @Override
68          protected double transformed(final double g) {
69              return -g;
70          }
71      },
72  
73      /** Transformer computing transformed = min(-{@link Precision#SAFE_MIN}, -g, +g).
74       * <p>
75       * When this transformer is applied, the transformed function is
76       * guaranteed to be always strictly negative (i.e. there are no roots).
77       * </p>
78       */
79      MIN {
80          /**  {@inheritDoc} */
81          @Override
82          protected double transformed(final double g) {
83              return JdkMath.min(-Precision.SAFE_MIN, JdkMath.min(-g, +g));
84          }
85      },
86  
87      /** Transformer computing transformed = max(+{@link Precision#SAFE_MIN}, -g, +g).
88       * <p>
89       * When this transformer is applied, the transformed function is
90       * guaranteed to be always strictly positive (i.e. there are no roots).
91       * </p>
92       */
93      MAX {
94          /**  {@inheritDoc} */
95          @Override
96          protected double transformed(final double g) {
97              return JdkMath.max(+Precision.SAFE_MIN, JdkMath.max(-g, +g));
98          }
99      };
100 
101     /** Transform value of function g.
102      * @param g raw value of function g
103      * @return transformed value of function g
104      */
105     protected abstract double transformed(double g);
106 }