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.math3.geometry;
018
019import java.text.NumberFormat;
020
021import org.apache.commons.math3.exception.MathArithmeticException;
022
023/** This interface represents a generic vector in a vectorial space or a point in an affine space.
024 * @param <S> Type of the space.
025 * @see Space
026 * @see Point
027 * @since 3.0
028 */
029public interface Vector<S extends Space> extends Point<S> {
030
031    /** Get the null vector of the vectorial space or origin point of the affine space.
032     * @return null vector of the vectorial space or origin point of the affine space
033     */
034    Vector<S> getZero();
035
036    /** Get the L<sub>1</sub> norm for the vector.
037     * @return L<sub>1</sub> norm for the vector
038     */
039    double getNorm1();
040
041    /** Get the L<sub>2</sub> norm for the vector.
042     * @return Euclidean norm for the vector
043     */
044    double getNorm();
045
046    /** Get the square of the norm for the vector.
047     * @return square of the Euclidean norm for the vector
048     */
049    double getNormSq();
050
051    /** Get the L<sub>&infin;</sub> norm for the vector.
052     * @return L<sub>&infin;</sub> norm for the vector
053     */
054    double getNormInf();
055
056    /** Add a vector to the instance.
057     * @param v vector to add
058     * @return a new vector
059     */
060    Vector<S> add(Vector<S> v);
061
062    /** Add a scaled vector to the instance.
063     * @param factor scale factor to apply to v before adding it
064     * @param v vector to add
065     * @return a new vector
066     */
067    Vector<S> add(double factor, Vector<S> v);
068
069    /** Subtract a vector from the instance.
070     * @param v vector to subtract
071     * @return a new vector
072     */
073    Vector<S> subtract(Vector<S> v);
074
075    /** Subtract a scaled vector from the instance.
076     * @param factor scale factor to apply to v before subtracting it
077     * @param v vector to subtract
078     * @return a new vector
079     */
080    Vector<S> subtract(double factor, Vector<S> v);
081
082    /** Get the opposite of the instance.
083     * @return a new vector which is opposite to the instance
084     */
085    Vector<S> negate();
086
087    /** Get a normalized vector aligned with the instance.
088     * @return a new normalized vector
089     * @exception MathArithmeticException if the norm is zero
090     */
091    Vector<S> normalize() throws MathArithmeticException;
092
093    /** Multiply the instance by a scalar.
094     * @param a scalar
095     * @return a new vector
096     */
097    Vector<S> scalarMultiply(double a);
098
099    /**
100     * Returns true if any coordinate of this vector is infinite and none are NaN;
101     * false otherwise
102     * @return  true if any coordinate of this vector is infinite and none are NaN;
103     * false otherwise
104     */
105    boolean isInfinite();
106
107    /** Compute the distance between the instance and another vector according to the L<sub>1</sub> norm.
108     * <p>Calling this method is equivalent to calling:
109     * <code>q.subtract(p).getNorm1()</code> except that no intermediate
110     * vector is built</p>
111     * @param v second vector
112     * @return the distance between the instance and p according to the L<sub>1</sub> norm
113     */
114    double distance1(Vector<S> v);
115
116    /** Compute the distance between the instance and another vector according to the L<sub>2</sub> norm.
117     * <p>Calling this method is equivalent to calling:
118     * <code>q.subtract(p).getNorm()</code> except that no intermediate
119     * vector is built</p>
120     * @param v second vector
121     * @return the distance between the instance and p according to the L<sub>2</sub> norm
122     */
123    double distance(Vector<S> v);
124
125    /** Compute the distance between the instance and another vector according to the L<sub>&infin;</sub> norm.
126     * <p>Calling this method is equivalent to calling:
127     * <code>q.subtract(p).getNormInf()</code> except that no intermediate
128     * vector is built</p>
129     * @param v second vector
130     * @return the distance between the instance and p according to the L<sub>&infin;</sub> norm
131     */
132    double distanceInf(Vector<S> v);
133
134    /** Compute the square of the distance between the instance and another vector.
135     * <p>Calling this method is equivalent to calling:
136     * <code>q.subtract(p).getNormSq()</code> except that no intermediate
137     * vector is built</p>
138     * @param v second vector
139     * @return the square of the distance between the instance and p
140     */
141    double distanceSq(Vector<S> v);
142
143    /** Compute the dot-product of the instance and another vector.
144     * @param v second vector
145     * @return the dot product this.v
146     */
147    double dotProduct(Vector<S> v);
148
149    /** Get a string representation of this vector.
150     * @param format the custom format for components
151     * @return a string representation of this vector
152     */
153    String toString(final NumberFormat format);
154
155}