DependentVectorsHandler.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.linear;

  18. import java.util.List;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.Field;
  21. import org.hipparchus.exception.LocalizedCoreFormats;
  22. import org.hipparchus.exception.MathIllegalArgumentException;

  23. /** Enumerate to specify how dependent vectors should be handled in
  24.  * {@link MatrixUtils#orthonormalize(List, double, DependentVectorsHandler)} and
  25.  * {@link MatrixUtils#orthonormalize(Field, List, CalculusFieldElement, DependentVectorsHandler)}.
  26.  * @since 2.1
  27.  */
  28. public enum DependentVectorsHandler {

  29.     /** Generate a {@link MathIllegalArgumentException} if dependent vectors are found. */
  30.     GENERATE_EXCEPTION {

  31.         /** {@inheritDoc} */
  32.         @Override
  33.         public int manageDependent(final int index, final List<RealVector> basis) {
  34.             // generate exception, dependent vectors are forbidden with this settings
  35.             throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_NORM);
  36.         }


  37.         /** {@inheritDoc} */
  38.         @Override
  39.         public <T extends CalculusFieldElement<T>> int manageDependent(final Field<T> field,
  40.                                                                        final int index,
  41.                                                                        final List<FieldVector<T>> basis) {
  42.             // generate exception, dependent vectors are forbidden with this settings
  43.             throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_NORM);
  44.         }

  45.     },

  46.     /** Replace dependent vectors by vectors with norm 0.
  47.      * <p>
  48.      * This behavior matches the Wolfram language API. It keeps the
  49.      * number of output vectors equal to the number of input vectors.
  50.      * The only two norms output vectors can have are 0 and 1.
  51.      * </p>
  52.      */
  53.     ADD_ZERO_VECTOR {

  54.         /** {@inheritDoc} */
  55.         @Override
  56.         public int manageDependent(final int index, final List<RealVector> basis) {
  57.             // add a zero vector, preserving output vector size (and dropping its normalization property)
  58.             basis.set(index, MatrixUtils.createRealVector(basis.get(index).getDimension()));
  59.             return index + 1;
  60.         }


  61.         /** {@inheritDoc} */
  62.         @Override
  63.         public <T extends CalculusFieldElement<T>> int manageDependent(final Field<T> field,
  64.                                                                        final int index,
  65.                                                                        final List<FieldVector<T>> basis) {
  66.             // add a zero vector, preserving output vector size (and dropping its normalization property)
  67.             basis.set(index, MatrixUtils.createFieldVector(field, basis.get(index).getDimension()));
  68.             return index + 1;
  69.         }

  70.     },

  71.     /** Ignore dependent vectors.
  72.      * <p>
  73.      * This behavior ensures the output vectors form an orthonormal
  74.      * basis, i.e. all vectors are independent and they all have norm 1.
  75.      * The number of output vectors may be smaller than the number of
  76.      * input vectors, this number corresponds to the dimension of the
  77.      * span of the input vectors.
  78.      * </p>
  79.      */
  80.     REDUCE_BASE_TO_SPAN {

  81.         /** {@inheritDoc} */
  82.         @Override
  83.         public int manageDependent(final int index, final List<RealVector> basis) {
  84.             // remove dependent vector
  85.             basis.remove(index);
  86.             return index;
  87.         }


  88.         /** {@inheritDoc} */
  89.         @Override
  90.         public <T extends CalculusFieldElement<T>> int manageDependent(final Field<T> field,
  91.                                                                        final int index,
  92.                                                                        final List<FieldVector<T>> basis) {
  93.             // remove dependent vector
  94.             basis.remove(index);
  95.             return index;
  96.         }

  97.     };

  98.     /** Manage a dependent vector.
  99.      * @param index of the vector in the basis
  100.      * @param basis placeholder for basis vectors
  101.      * @return next index to manage
  102.      */
  103.     public abstract int manageDependent(int index, List<RealVector> basis);

  104.     /** Manage a dependent vector.
  105.      * @param <T> type of the vectors components
  106.      * @param field field to which the vectors belong
  107.      * @param index of the vector in the basis
  108.      * @param basis placeholder for basis vectors
  109.      * @return next index to manage
  110.      */
  111.     public abstract <T extends CalculusFieldElement<T>> int manageDependent(Field<T> field, int index, List<FieldVector<T>> basis);

  112. }