ConvertingRuleFactory.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.analysis.integration.gauss;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.FieldElement;
  20. import org.hipparchus.exception.MathIllegalArgumentException;
  21. import org.hipparchus.util.Pair;

  22. /**
  23.  * Factory converting {@link CalculusFieldElement field-based} {@link FieldRuleFactory} into {@link RuleFactory}.
  24.  * @param <T> Type of the number used to represent the points and weights of
  25.  * the quadrature rules.
  26.  * @since 2.0
  27.  */
  28. public class ConvertingRuleFactory<T extends FieldElement<T>> extends AbstractRuleFactory {

  29.     /** Underlying field-based factory. */
  30.     private final FieldRuleFactory<T> fieldFactory;

  31.     /** Simple constructor.
  32.      * @param fieldFactory field-based factory to convert
  33.      */
  34.     public ConvertingRuleFactory(final FieldRuleFactory<T> fieldFactory) {
  35.         this.fieldFactory = fieldFactory;
  36.     }

  37.     /** {@inheritDoc} */
  38.     @Override
  39.     protected Pair<double[], double[]> computeRule(final int numberOfPoints)
  40.         throws MathIllegalArgumentException {

  41.         // get the field-based rule
  42.         Pair<T[], T[]> rule = fieldFactory.getRule(numberOfPoints);

  43.         // convert the nodes and weights
  44.         final T[] pT = rule.getFirst();
  45.         final T[] wT = rule.getSecond();

  46.         final int len = pT.length;
  47.         final double[] pD = new double[len];
  48.         final double[] wD = new double[len];

  49.         for (int i = 0; i < len; i++) {
  50.             pD[i] = pT[i].getReal();
  51.             wD[i] = wT[i].getReal();
  52.         }

  53.         return new Pair<>(pD, wD);

  54.     }

  55. }