001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.core;
018
019import org.apache.commons.math4.legacy.exception.MathArithmeticException;
020import org.apache.commons.math4.legacy.exception.NullArgumentException;
021
022
023/**
024 * Interface representing <a href="http://mathworld.wolfram.com/Field.html">field</a> elements.
025 * @param <T> the type of the field elements
026 * @see Field
027 * @since 2.0
028 */
029public interface FieldElement<T> {
030
031    /** Compute this + a.
032     * @param a element to add
033     * @return a new element representing this + a
034     * @throws NullArgumentException if {@code a} is {@code null}.
035     */
036    T add(T a) throws NullArgumentException;
037
038    /** Compute this - a.
039     * @param a element to subtract
040     * @return a new element representing this - a
041     * @throws NullArgumentException if {@code a} is {@code null}.
042     */
043    T subtract(T a) throws NullArgumentException;
044
045    /**
046     * Returns the additive inverse of {@code this} element.
047     * @return the opposite of {@code this}.
048     */
049    T negate();
050
051    /** Compute n &times; this. Multiplication by an integer number is defined
052     * as the following sum:
053     * <p>n &times; this = &sum;<sub>i=1</sub><sup>n</sup> this.
054     * @param n Number of times {@code this} must be added to itself.
055     * @return A new element representing n &times; this.
056     */
057    T multiply(int n);
058
059    /** Compute this &times; a.
060     * @param a element to multiply
061     * @return a new element representing this &times; a
062     * @throws NullArgumentException if {@code a} is {@code null}.
063     */
064    T multiply(T a) throws NullArgumentException;
065
066    /** Compute this &divide; a.
067     * @param a element to divide by
068     * @return a new element representing this &divide; a
069     * @throws NullArgumentException if {@code a} is {@code null}.
070     * @throws MathArithmeticException if {@code a} is zero
071     */
072    T divide(T a) throws NullArgumentException, MathArithmeticException;
073
074    /**
075     * Returns the multiplicative inverse of {@code this} element.
076     * @return the inverse of {@code this}.
077     * @throws MathArithmeticException if {@code this} is zero
078     */
079    T reciprocal() throws MathArithmeticException;
080
081    /** Get the {@link Field} to which the instance belongs.
082     * @return {@link Field} to which the instance belongs
083     */
084    Field<T> getField();
085}