Vector.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.geometry;

  22. import org.hipparchus.analysis.polynomials.SmoothStepFactory;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.MathRuntimeException;
  25. import org.hipparchus.util.Blendable;

  26. import java.text.NumberFormat;

  27. /** This interface represents a generic vector in a vectorial space or a point in an affine space.
  28.  * @param <S> Type of the space.
  29.  * @param <V> Type of vector implementing this interface.
  30.  * @see Space
  31.  * @see Point
  32.  */
  33. public interface Vector<S extends Space, V extends Vector<S, V>> extends Point<S, V>, Blendable<V> {

  34.     /** Get the null vector of the vectorial space or origin point of the affine space.
  35.      * @return null vector of the vectorial space or origin point of the affine space
  36.      */
  37.     V getZero();

  38.     /** Get the L<sub>1</sub> norm for the vector.
  39.      * @return L<sub>1</sub> norm for the vector
  40.      */
  41.     double getNorm1();

  42.     /** Get the L<sub>2</sub> norm for the vector.
  43.      * @return Euclidean norm for the vector
  44.      */
  45.     double getNorm();

  46.     /** Get the square of the norm for the vector.
  47.      * @return square of the Euclidean norm for the vector
  48.      */
  49.     double getNormSq();

  50.     /** Get the L<sub>&infin;</sub> norm for the vector.
  51.      * @return L<sub>&infin;</sub> norm for the vector
  52.      */
  53.     double getNormInf();

  54.     /** Add a vector to the instance.
  55.      * @param v vector to add
  56.      * @return a new vector
  57.      */
  58.     V add(V v);

  59.     /** Add a scaled vector to the instance.
  60.      * @param factor scale factor to apply to v before adding it
  61.      * @param v vector to add
  62.      * @return a new vector
  63.      */
  64.     V add(double factor, V v);

  65.     /** Subtract a vector from the instance.
  66.      * @param v vector to subtract
  67.      * @return a new vector
  68.      */
  69.     V subtract(V v);

  70.     /** Subtract a scaled vector from the instance.
  71.      * @param factor scale factor to apply to v before subtracting it
  72.      * @param v vector to subtract
  73.      * @return a new vector
  74.      */
  75.     V subtract(double factor, V v);

  76.     /** Get the opposite of the instance.
  77.      * @return a new vector which is opposite to the instance
  78.      */
  79.     V negate();

  80.     /** Get a normalized vector aligned with the instance.
  81.      * @return a new normalized vector
  82.      * @exception MathRuntimeException if the norm is zero
  83.      */
  84.     default V normalize() throws MathRuntimeException{
  85.         double s = getNorm();
  86.         if (s == 0) {
  87.             throw new MathRuntimeException(LocalizedGeometryFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR);
  88.         }
  89.         return scalarMultiply(1 / s);
  90.     }

  91.     /** Multiply the instance by a scalar.
  92.      * @param a scalar
  93.      * @return a new vector
  94.      */
  95.     V scalarMultiply(double a);

  96.     /**
  97.      * Returns true if any coordinate of this vector is infinite and none are NaN;
  98.      * false otherwise
  99.      * @return  true if any coordinate of this vector is infinite and none are NaN;
  100.      * false otherwise
  101.      */
  102.     boolean isInfinite();

  103.     /** Compute the distance between the instance and another vector according to the L<sub>1</sub> norm.
  104.      * <p>Calling this method is equivalent to calling:
  105.      * <code>q.subtract(p).getNorm1()</code> except that no intermediate
  106.      * vector is built</p>
  107.      * @param v second vector
  108.      * @return the distance between the instance and p according to the L<sub>1</sub> norm
  109.      */
  110.     double distance1(V v);

  111.     /** Compute the distance between the instance and another vector according to the L<sub>&infin;</sub> norm.
  112.      * <p>Calling this method is equivalent to calling:
  113.      * <code>q.subtract(p).getNormInf()</code> except that no intermediate
  114.      * vector is built</p>
  115.      * @param v second vector
  116.      * @return the distance between the instance and p according to the L<sub>&infin;</sub> norm
  117.      */
  118.     double distanceInf(V v);

  119.     /** Compute the square of the distance between the instance and another vector.
  120.      * <p>Calling this method is equivalent to calling:
  121.      * <code>q.subtract(p).getNormSq()</code> except that no intermediate
  122.      * vector is built</p>
  123.      * @param v second vector
  124.      * @return the square of the distance between the instance and p
  125.      */
  126.     double distanceSq(V v);

  127.     /** Compute the dot-product of the instance and another vector.
  128.      * @param v second vector
  129.      * @return the dot product this.v
  130.      */
  131.     double dotProduct(V v);

  132.     /** Get a string representation of this vector.
  133.      * @param format the custom format for components
  134.      * @return a string representation of this vector
  135.      */
  136.     String toString(NumberFormat format);

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     default V blendArithmeticallyWith(V other, double blendingValue)
  140.             throws MathIllegalArgumentException {
  141.         SmoothStepFactory.checkBetweenZeroAndOneIncluded(blendingValue);
  142.         return this.scalarMultiply(1 - blendingValue).add(other.scalarMultiply(blendingValue));
  143.     }
  144. }