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.  *      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. package org.apache.commons.math4.legacy.ode.events;

  18. import org.apache.commons.math4.core.jdkmath.JdkMath;
  19. import org.apache.commons.numbers.core.Precision;


  20. /** Transformer for {@link EventHandler#g(double, double[]) g functions}.
  21.  * @see EventFilter
  22.  * @see FilterType
  23.  * @since 3.2
  24.  */
  25. enum Transformer {

  26.     /** Transformer computing transformed = 0.
  27.      * <p>
  28.      * This transformer is used when we initialize the filter, until we get at
  29.      * least one non-zero value to select the proper transformer.
  30.      * </p>
  31.      */
  32.     UNINITIALIZED {
  33.         /**  {@inheritDoc} */
  34.         @Override
  35.         protected double transformed(final double g) {
  36.             return 0;
  37.         }
  38.     },

  39.     /** Transformer computing transformed = g.
  40.      * <p>
  41.      * When this transformer is applied, the roots of the original function
  42.      * are preserved, with the same {@code increasing/decreasing} status.
  43.      * </p>
  44.      */
  45.     PLUS {
  46.         /**  {@inheritDoc} */
  47.         @Override
  48.         protected double transformed(final double g) {
  49.             return g;
  50.         }
  51.     },

  52.     /** Transformer computing transformed = -g.
  53.      * <p>
  54.      * When this transformer is applied, the roots of the original function
  55.      * are preserved, with reversed {@code increasing/decreasing} status.
  56.      * </p>
  57.      */
  58.     MINUS {
  59.         /**  {@inheritDoc} */
  60.         @Override
  61.         protected double transformed(final double g) {
  62.             return -g;
  63.         }
  64.     },

  65.     /** Transformer computing transformed = min(-{@link Precision#SAFE_MIN}, -g, +g).
  66.      * <p>
  67.      * When this transformer is applied, the transformed function is
  68.      * guaranteed to be always strictly negative (i.e. there are no roots).
  69.      * </p>
  70.      */
  71.     MIN {
  72.         /**  {@inheritDoc} */
  73.         @Override
  74.         protected double transformed(final double g) {
  75.             return JdkMath.min(-Precision.SAFE_MIN, JdkMath.min(-g, +g));
  76.         }
  77.     },

  78.     /** Transformer computing transformed = max(+{@link Precision#SAFE_MIN}, -g, +g).
  79.      * <p>
  80.      * When this transformer is applied, the transformed function is
  81.      * guaranteed to be always strictly positive (i.e. there are no roots).
  82.      * </p>
  83.      */
  84.     MAX {
  85.         /**  {@inheritDoc} */
  86.         @Override
  87.         protected double transformed(final double g) {
  88.             return JdkMath.max(+Precision.SAFE_MIN, JdkMath.max(-g, +g));
  89.         }
  90.     };

  91.     /** Transform value of function g.
  92.      * @param g raw value of function g
  93.      * @return transformed value of function g
  94.      */
  95.     protected abstract double transformed(double g);
  96. }