FieldElement.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;

  22. import org.hipparchus.exception.MathRuntimeException;
  23. import org.hipparchus.exception.NullArgumentException;


  24. /**
  25.  * Interface representing <a href="http://mathworld.wolfram.com/Field.html">field</a> elements.
  26.  * @param <T> the type of the field elements
  27.  * @see Field
  28.  */
  29. public interface FieldElement<T extends FieldElement<T>> {

  30.     /** Get the real value of the number.
  31.      * @return real value
  32.      */
  33.     double getReal();

  34.     /** Compute this + a.
  35.      * @param a element to add
  36.      * @return a new element representing this + a
  37.      * @throws NullArgumentException if {@code a} is {@code null}.
  38.      */
  39.     T add(T a) throws NullArgumentException;

  40.     /** Compute this - a.
  41.      * @param a element to subtract
  42.      * @return a new element representing this - a
  43.      * @throws NullArgumentException if {@code a} is {@code null}.
  44.      */
  45.     T subtract(T a) throws NullArgumentException;

  46.     /**
  47.      * Returns the additive inverse of {@code this} element.
  48.      * @return the opposite of {@code this}.
  49.      */
  50.     T negate();

  51.     /** Compute n &times; this. Multiplication by an integer number is defined
  52.      * as the following sum
  53.      * \[
  54.      * n \times \mathrm{this} = \sum_{i=1}^n \mathrm{this}
  55.      * \]
  56.      * @param n Number of times {@code this} must be added to itself.
  57.      * @return A new element representing n &times; this.
  58.      */
  59.     T multiply(int n);

  60.     /** Compute this &times; a.
  61.      * @param a element to multiply
  62.      * @return a new element representing this &times; a
  63.      * @throws NullArgumentException if {@code a} is {@code null}.
  64.      */
  65.     T multiply(T a) throws NullArgumentException;

  66.     /** Compute this &divide; a.
  67.      * @param a element to divide by
  68.      * @return a new element representing this &divide; a
  69.      * @throws NullArgumentException if {@code a} is {@code null}.
  70.      * @throws MathRuntimeException if {@code a} is zero
  71.      */
  72.     T divide(T a) throws NullArgumentException, MathRuntimeException;

  73.     /**
  74.      * Returns the multiplicative inverse of {@code this} element.
  75.      * @return the inverse of {@code this}.
  76.      * @throws MathRuntimeException if {@code this} is zero
  77.      */
  78.     T reciprocal() throws MathRuntimeException;

  79.     /** Get the {@link Field} to which the instance belongs.
  80.      * @return {@link Field} to which the instance belongs
  81.      */
  82.     Field<T> getField();

  83.     /** Check if an element is semantically equal to zero.
  84.      * <p>
  85.      * The default implementation simply calls {@code equals(getField().getZero())}.
  86.      * However, this may need to be overridden in some cases as due to
  87.      * compatibility with {@code hashCode()} some classes implements
  88.      * {@code equals(Object)} in such a way that -0.0 and +0.0 are different,
  89.      * which may be a problem. It prevents for example identifying a diagonal
  90.      * element is zero and should be avoided when doing partial pivoting in
  91.      * LU decomposition.
  92.      * </p>
  93.      * @return true if the element is semantically equal to zero
  94.      * @since 1.8
  95.      */
  96.     default boolean isZero() {
  97.         return equals(getField().getZero());
  98.     }

  99. }