RealFieldElement.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.core;

  18. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;

  19. /**
  20.  * Interface representing a <a href="http://mathworld.wolfram.com/RealNumber.html">real</a>
  21.  * <a href="http://mathworld.wolfram.com/Field.html">field</a>.
  22.  * @param <T> the type of the field elements
  23.  * @see FieldElement
  24.  * @since 3.2
  25.  */
  26. public interface RealFieldElement<T> extends FieldElement<T> {

  27.     /** Get the real value of the number.
  28.      * @return real value
  29.      */
  30.     double getReal();

  31.     /** '+' operator.
  32.      * @param a right hand side parameter of the operator
  33.      * @return this+a
  34.      */
  35.     T add(double a);

  36.     /** '-' operator.
  37.      * @param a right hand side parameter of the operator
  38.      * @return this-a
  39.      */
  40.     T subtract(double a);

  41.     /** '&times;' operator.
  42.      * @param a right hand side parameter of the operator
  43.      * @return this&times;a
  44.      */
  45.     T multiply(double a);

  46.     /** '&divide;' operator.
  47.      * @param a right hand side parameter of the operator
  48.      * @return this&divide;a
  49.      */
  50.     T divide(double a);

  51.     /** IEEE remainder operator.
  52.      * @param a right hand side parameter of the operator
  53.      * @return this - n &times; a where n is the closest integer to this/a
  54.      * (the even integer is chosen for n if this/a is halfway between two integers)
  55.      */
  56.     T remainder(double a);

  57.     /** IEEE remainder operator.
  58.      * @param a right hand side parameter of the operator
  59.      * @return this - n &times; a where n is the closest integer to this/a
  60.      * (the even integer is chosen for n if this/a is halfway between two integers)
  61.      * @exception DimensionMismatchException if number of free parameters or orders are inconsistent
  62.      */
  63.     T remainder(T a)
  64.         throws DimensionMismatchException;

  65.     /** absolute value.
  66.      * @return abs(this)
  67.      */
  68.     T abs();

  69.     /** Get the smallest whole number larger than instance.
  70.      * @return ceil(this)
  71.      */
  72.     T ceil();

  73.     /** Get the largest whole number smaller than instance.
  74.      * @return floor(this)
  75.      */
  76.     T floor();

  77.     /** Get the whole number that is the nearest to the instance, or the even one if x is exactly
  78.      * half way between two integers.
  79.      * @return a double number r such that r is an integer r - 0.5 &le; this &le; r + 0.5
  80.      */
  81.     T rint();

  82.     /** Get the closest long to instance value.
  83.      * @return closest long to {@link #getReal()}
  84.      */
  85.     long round();

  86.     /** Compute the signum of the instance.
  87.      * The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise
  88.      * @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a
  89.      */
  90.     T signum();

  91.     /**
  92.      * Returns the instance with the sign of the argument.
  93.      * A NaN {@code sign} argument is treated as positive.
  94.      *
  95.      * @param sign the sign for the returned value
  96.      * @return the instance with the same sign as the {@code sign} argument
  97.      */
  98.     T copySign(T sign);

  99.     /**
  100.      * Returns the instance with the sign of the argument.
  101.      * A NaN {@code sign} argument is treated as positive.
  102.      *
  103.      * @param sign the sign for the returned value
  104.      * @return the instance with the same sign as the {@code sign} argument
  105.      */
  106.     T copySign(double sign);

  107.     /**
  108.      * Multiply the instance by a power of 2.
  109.      * @param n power of 2
  110.      * @return this &times; 2<sup>n</sup>
  111.      */
  112.     T scalb(int n);

  113.     /**
  114.      * Returns the hypotenuse of a triangle with sides {@code this} and {@code y}
  115.      * - sqrt(<i>this</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
  116.      * avoiding intermediate overflow or underflow.
  117.      *
  118.      * <ul>
  119.      * <li> If either argument is infinite, then the result is positive infinity.</li>
  120.      * <li> else, if either argument is NaN then the result is NaN.</li>
  121.      * </ul>
  122.      *
  123.      * @param y a value
  124.      * @return sqrt(<i>this</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
  125.      * @exception DimensionMismatchException if number of free parameters or orders are inconsistent
  126.      */
  127.     T hypot(T y)
  128.         throws DimensionMismatchException;

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     T reciprocal();

  132.     /** Square root.
  133.      * @return square root of the instance
  134.      */
  135.     T sqrt();

  136.     /** Cubic root.
  137.      * @return cubic root of the instance
  138.      */
  139.     T cbrt();

  140.     /** N<sup>th</sup> root.
  141.      * @param n order of the root
  142.      * @return n<sup>th</sup> root of the instance
  143.      */
  144.     T rootN(int n);

  145.     /** Power operation.
  146.      * @param p power to apply
  147.      * @return this<sup>p</sup>
  148.      */
  149.     T pow(double p);

  150.     /** Integer power operation.
  151.      * @param n power to apply
  152.      * @return this<sup>n</sup>
  153.      */
  154.     T pow(int n);

  155.     /** Power operation.
  156.      * @param e exponent
  157.      * @return this<sup>e</sup>
  158.      * @exception DimensionMismatchException if number of free parameters or orders are inconsistent
  159.      */
  160.     T pow(T e)
  161.         throws DimensionMismatchException;

  162.     /** Exponential.
  163.      * @return exponential of the instance
  164.      */
  165.     T exp();

  166.     /** Exponential minus 1.
  167.      * @return exponential minus one of the instance
  168.      */
  169.     T expm1();

  170.     /** Natural logarithm.
  171.      * @return logarithm of the instance
  172.      */
  173.     T log();

  174.     /** Shifted natural logarithm.
  175.      * @return logarithm of one plus the instance
  176.      */
  177.     T log1p();

  178.     /** Base 10 logarithm.
  179.      * @return base 10 logarithm of the instance
  180.      * @since 4.0
  181.      */
  182.     T log10();

  183.     /** Cosine operation.
  184.      * @return cos(this)
  185.      */
  186.     T cos();

  187.     /** Sine operation.
  188.      * @return sin(this)
  189.      */
  190.     T sin();

  191.     /** Tangent operation.
  192.      * @return tan(this)
  193.      */
  194.     T tan();

  195.     /** Arc cosine operation.
  196.      * @return acos(this)
  197.      */
  198.     T acos();

  199.     /** Arc sine operation.
  200.      * @return asin(this)
  201.      */
  202.     T asin();

  203.     /** Arc tangent operation.
  204.      * @return atan(this)
  205.      */
  206.     T atan();

  207.     /** Two arguments arc tangent operation.
  208.      * @param x second argument of the arc tangent
  209.      * @return atan2(this, x)
  210.      * @exception DimensionMismatchException if number of free parameters or orders are inconsistent
  211.      */
  212.     T atan2(T x)
  213.         throws DimensionMismatchException;

  214.     /** Hyperbolic cosine operation.
  215.      * @return cosh(this)
  216.      */
  217.     T cosh();

  218.     /** Hyperbolic sine operation.
  219.      * @return sinh(this)
  220.      */
  221.     T sinh();

  222.     /** Hyperbolic tangent operation.
  223.      * @return tanh(this)
  224.      */
  225.     T tanh();

  226.     /** Inverse hyperbolic cosine operation.
  227.      * @return acosh(this)
  228.      */
  229.     T acosh();

  230.     /** Inverse hyperbolic sine operation.
  231.      * @return asin(this)
  232.      */
  233.     T asinh();

  234.     /** Inverse hyperbolic  tangent operation.
  235.      * @return atanh(this)
  236.      */
  237.     T atanh();

  238.     /**
  239.      * Compute a linear combination.
  240.      * @param a Factors.
  241.      * @param b Factors.
  242.      * @return <code>&Sigma;<sub>i</sub> a<sub>i</sub> b<sub>i</sub></code>.
  243.      * @throws DimensionMismatchException if arrays dimensions don't match
  244.      * @since 3.2
  245.      */
  246.     T linearCombination(T[] a, T[] b)
  247.         throws DimensionMismatchException;

  248.     /**
  249.      * Compute a linear combination.
  250.      * @param a Factors.
  251.      * @param b Factors.
  252.      * @return <code>&Sigma;<sub>i</sub> a<sub>i</sub> b<sub>i</sub></code>.
  253.      * @throws DimensionMismatchException if arrays dimensions don't match
  254.      * @since 3.2
  255.      */
  256.     T linearCombination(double[] a, T[] b)
  257.         throws DimensionMismatchException;

  258.     /**
  259.      * Compute a linear combination.
  260.      * @param a1 first factor of the first term
  261.      * @param b1 second factor of the first term
  262.      * @param a2 first factor of the second term
  263.      * @param b2 second factor of the second term
  264.      * @return a<sub>1</sub>&times;b<sub>1</sub> +
  265.      * a<sub>2</sub>&times;b<sub>2</sub>
  266.      * @see #linearCombination(Object, Object, Object, Object, Object, Object)
  267.      * @see #linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)
  268.      * @since 3.2
  269.      */
  270.     T linearCombination(T a1, T b1, T a2, T b2);

  271.     /**
  272.      * Compute a linear combination.
  273.      * @param a1 first factor of the first term
  274.      * @param b1 second factor of the first term
  275.      * @param a2 first factor of the second term
  276.      * @param b2 second factor of the second term
  277.      * @return a<sub>1</sub>&times;b<sub>1</sub> +
  278.      * a<sub>2</sub>&times;b<sub>2</sub>
  279.      * @see #linearCombination(double, Object, double, Object, double, Object)
  280.      * @see #linearCombination(double, Object, double, Object, double, Object, double, Object)
  281.      * @since 3.2
  282.      */
  283.     T linearCombination(double a1, T b1, double a2, T b2);

  284.     /**
  285.      * Compute a linear combination.
  286.      * @param a1 first factor of the first term
  287.      * @param b1 second factor of the first term
  288.      * @param a2 first factor of the second term
  289.      * @param b2 second factor of the second term
  290.      * @param a3 first factor of the third term
  291.      * @param b3 second factor of the third term
  292.      * @return a<sub>1</sub>&times;b<sub>1</sub> +
  293.      * a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub>
  294.      * @see #linearCombination(Object, Object, Object, Object)
  295.      * @see #linearCombination(Object, Object, Object, Object, Object, Object, Object, Object)
  296.      * @since 3.2
  297.      */
  298.     T linearCombination(T a1, T b1, T a2, T b2, T a3, T b3);

  299.     /**
  300.      * Compute a linear combination.
  301.      * @param a1 first factor of the first term
  302.      * @param b1 second factor of the first term
  303.      * @param a2 first factor of the second term
  304.      * @param b2 second factor of the second term
  305.      * @param a3 first factor of the third term
  306.      * @param b3 second factor of the third term
  307.      * @return a<sub>1</sub>&times;b<sub>1</sub> +
  308.      * a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub>
  309.      * @see #linearCombination(double, Object, double, Object)
  310.      * @see #linearCombination(double, Object, double, Object, double, Object, double, Object)
  311.      * @since 3.2
  312.      */
  313.     T linearCombination(double a1, T b1,  double a2, T b2, double a3, T b3);

  314.     /**
  315.      * Compute a linear combination.
  316.      * @param a1 first factor of the first term
  317.      * @param b1 second factor of the first term
  318.      * @param a2 first factor of the second term
  319.      * @param b2 second factor of the second term
  320.      * @param a3 first factor of the third term
  321.      * @param b3 second factor of the third term
  322.      * @param a4 first factor of the third term
  323.      * @param b4 second factor of the third term
  324.      * @return a<sub>1</sub>&times;b<sub>1</sub> +
  325.      * a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub> +
  326.      * a<sub>4</sub>&times;b<sub>4</sub>
  327.      * @see #linearCombination(Object, Object, Object, Object)
  328.      * @see #linearCombination(Object, Object, Object, Object, Object, Object)
  329.      * @since 3.2
  330.      */
  331.     T linearCombination(T a1, T b1, T a2, T b2, T a3, T b3, T a4, T b4);

  332.     /**
  333.      * Compute a linear combination.
  334.      * @param a1 first factor of the first term
  335.      * @param b1 second factor of the first term
  336.      * @param a2 first factor of the second term
  337.      * @param b2 second factor of the second term
  338.      * @param a3 first factor of the third term
  339.      * @param b3 second factor of the third term
  340.      * @param a4 first factor of the third term
  341.      * @param b4 second factor of the third term
  342.      * @return a<sub>1</sub>&times;b<sub>1</sub> +
  343.      * a<sub>2</sub>&times;b<sub>2</sub> + a<sub>3</sub>&times;b<sub>3</sub> +
  344.      * a<sub>4</sub>&times;b<sub>4</sub>
  345.      * @see #linearCombination(double, Object, double, Object)
  346.      * @see #linearCombination(double, Object, double, Object, double, Object)
  347.      * @since 3.2
  348.      */
  349.     T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3, double a4, T b4);

  350.     /** Find the maximum of two field elements.
  351.      * @param <T> the type of the field elements
  352.      * @param e1 first element
  353.      * @param e2 second element
  354.      * @return max(a1, e2)
  355.      */
  356.     static <T extends RealFieldElement<T>> T max(final T e1, final T e2) {
  357.         return e1.subtract(e2).getReal() >= 0 ? e1 : e2;
  358.     }

  359.     /** Find the minimum of two field elements.
  360.      * @param <T> the type of the field elements
  361.      * @param e1 first element
  362.      * @param e2 second element
  363.      * @return min(a1, e2)
  364.      */
  365.     static <T extends RealFieldElement<T>> T min(final T e1, final T e2) {
  366.         return e1.subtract(e2).getReal() >= 0 ? e2 : e1;
  367.     }
  368. }