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