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 * https://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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22
23 package org.hipparchus.ode.events;
24
25 import org.hipparchus.CalculusFieldElement;
26 import org.hipparchus.util.FastMath;
27 import org.hipparchus.util.Precision;
28
29
30 /** Transformer for {@link ODEEventDetector#g(org.hipparchus.ode.ODEStateAndDerivative) g functions}.
31 * @see EventSlopeFilter
32 * @see FilterType
33 */
34 enum Transformer {
35
36 /** Transformer computing transformed = 0.
37 * <p>
38 * This transformer is used when we initialize the filter, until we get at
39 * least one non-zero value to select the proper transformer.
40 * </p>
41 */
42 UNINITIALIZED {
43
44 /** {@inheritDoc} */
45 @Override
46 protected double transformed(final double g) {
47 return 0;
48 }
49
50 /** {@inheritDoc} */
51 @Override
52 protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
53 return g.getField().getZero();
54 }
55
56 },
57
58 /** Transformer computing transformed = g.
59 * <p>
60 * When this transformer is applied, the roots of the original function
61 * are preserved, with the same {@code increasing/decreasing} status.
62 * </p>
63 */
64 PLUS {
65
66 /** {@inheritDoc} */
67 @Override
68 protected double transformed(final double g) {
69 return g;
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
75 return g;
76 }
77
78 },
79
80 /** Transformer computing transformed = -g.
81 * <p>
82 * When this transformer is applied, the roots of the original function
83 * are preserved, with reversed {@code increasing/decreasing} status.
84 * </p>
85 */
86 MINUS {
87
88 /** {@inheritDoc} */
89 @Override
90 protected double transformed(final double g) {
91 return -g;
92 }
93
94 /** {@inheritDoc} */
95 @Override
96 protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
97 return g.negate();
98 }
99
100 },
101
102 /** Transformer computing transformed = min(-{@link Precision#SAFE_MIN}, -g, +g).
103 * <p>
104 * When this transformer is applied, the transformed function is
105 * guaranteed to be always strictly negative (i.e. there are no roots).
106 * </p>
107 */
108 MIN {
109
110 /** {@inheritDoc} */
111 @Override
112 protected double transformed(final double g) {
113 return FastMath.min(FastMath.min(-g, +g), -Precision.SAFE_MIN);
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
119 return FastMath.min(FastMath.min(g.negate(), g), -Precision.SAFE_MIN);
120 }
121
122 },
123
124 /** Transformer computing transformed = max(+{@link Precision#SAFE_MIN}, -g, +g).
125 * <p>
126 * When this transformer is applied, the transformed function is
127 * guaranteed to be always strictly positive (i.e. there are no roots).
128 * </p>
129 */
130 MAX {
131
132 /** {@inheritDoc} */
133 @Override
134 protected double transformed(final double g) {
135 return FastMath.max(FastMath.max(-g, +g), Precision.SAFE_MIN);
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
141 return FastMath.max(FastMath.max(g.negate(), g), Precision.SAFE_MIN);
142 }
143
144 };
145
146 /** Transform value of function g.
147 * @param g raw value of function g
148 * @return transformed value of function g
149 */
150 protected abstract double transformed(double g);
151
152 /** Transform value of function g.
153 * @param g raw value of function g
154 * @return transformed value of function g
155 * @param <T> the type of the field elements
156 * @since 2.0
157 */
158 protected abstract <T extends CalculusFieldElement<T>> T transformed(T g);
159
160 }