Transformer.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.ode.events;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.util.FastMath;
  24. import org.hipparchus.util.Precision;


  25. /** Transformer for {@link ODEEventDetector#g(org.hipparchus.ode.ODEStateAndDerivative) g functions}.
  26.  * @see EventSlopeFilter
  27.  * @see FilterType
  28.  */
  29. enum Transformer {

  30.     /** Transformer computing transformed = 0.
  31.      * <p>
  32.      * This transformer is used when we initialize the filter, until we get at
  33.      * least one non-zero value to select the proper transformer.
  34.      * </p>
  35.      */
  36.     UNINITIALIZED {

  37.         /**  {@inheritDoc} */
  38.         @Override
  39.         protected double transformed(final double g) {
  40.             return 0;
  41.         }

  42.         /**  {@inheritDoc} */
  43.         @Override
  44.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  45.             return g.getField().getZero();
  46.         }

  47.     },

  48.     /** Transformer computing transformed = g.
  49.      * <p>
  50.      * When this transformer is applied, the roots of the original function
  51.      * are preserved, with the same {@code increasing/decreasing} status.
  52.      * </p>
  53.      */
  54.     PLUS {

  55.         /**  {@inheritDoc} */
  56.         @Override
  57.         protected double transformed(final double g) {
  58.             return g;
  59.         }

  60.         /**  {@inheritDoc} */
  61.         @Override
  62.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  63.             return g;
  64.         }

  65.     },

  66.     /** Transformer computing transformed = -g.
  67.      * <p>
  68.      * When this transformer is applied, the roots of the original function
  69.      * are preserved, with reversed {@code increasing/decreasing} status.
  70.      * </p>
  71.      */
  72.     MINUS {

  73.         /**  {@inheritDoc} */
  74.         @Override
  75.         protected double transformed(final double g) {
  76.             return -g;
  77.         }

  78.         /**  {@inheritDoc} */
  79.         @Override
  80.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  81.             return g.negate();
  82.         }

  83.     },

  84.     /** Transformer computing transformed = min(-{@link Precision#SAFE_MIN}, -g, +g).
  85.      * <p>
  86.      * When this transformer is applied, the transformed function is
  87.      * guaranteed to be always strictly negative (i.e. there are no roots).
  88.      * </p>
  89.      */
  90.     MIN {

  91.         /**  {@inheritDoc} */
  92.         @Override
  93.         protected double transformed(final double g) {
  94.             return FastMath.min(FastMath.min(-g, +g), -Precision.SAFE_MIN);
  95.         }

  96.         /**  {@inheritDoc} */
  97.         @Override
  98.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  99.             return FastMath.min(FastMath.min(g.negate(), g), -Precision.SAFE_MIN);
  100.         }

  101.     },

  102.     /** Transformer computing transformed = max(+{@link Precision#SAFE_MIN}, -g, +g).
  103.      * <p>
  104.      * When this transformer is applied, the transformed function is
  105.      * guaranteed to be always strictly positive (i.e. there are no roots).
  106.      * </p>
  107.      */
  108.     MAX {

  109.         /**  {@inheritDoc} */
  110.         @Override
  111.         protected double transformed(final double g) {
  112.             return FastMath.max(FastMath.max(-g, +g), Precision.SAFE_MIN);
  113.         }

  114.         /**  {@inheritDoc} */
  115.         @Override
  116.         protected <T extends CalculusFieldElement<T>> T transformed(final T g) {
  117.             return FastMath.max(FastMath.max(g.negate(), g), Precision.SAFE_MIN);
  118.         }

  119.     };

  120.     /** Transform value of function g.
  121.      * @param g raw value of function g
  122.      * @return transformed value of function g
  123.      */
  124.     protected abstract double transformed(double g);

  125.     /** Transform value of function g.
  126.      * @param g raw value of function g
  127.      * @return transformed value of function g
  128.      * @param <T> the type of the field elements
  129.      * @since 2.0
  130.      */
  131.     protected abstract <T extends CalculusFieldElement<T>> T transformed(T g);

  132. }