FieldSinCos.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.util;

  18. import org.hipparchus.CalculusFieldElement;

  19. /** Holder for both sine and cosine values.
  20.  * <p>
  21.  * This class is a simple container, it does not provide any computational method.
  22.  * </p>
  23.  * @see FastMath#sinCos(org.hipparchus.CalculusFieldElement)
  24.  * @param <T> the type of the field elements
  25.  * @since 1.4
  26.  */
  27. public class FieldSinCos<T> {

  28.     /** Value of the sine. */
  29.     private final T sin;

  30.     /** Value of the cosine. */
  31.     private final T cos;

  32.     /** Simple constructor.
  33.      * @param sin value of the sine
  34.      * @param cos value of the cosine
  35.      */
  36.     public FieldSinCos(final T sin, final T cos) {
  37.         this.sin = sin;
  38.         this.cos = cos;
  39.     }

  40.     /** Get the value of the sine.
  41.      * @return value of the sine
  42.      */
  43.     public T sin() {
  44.         return sin;
  45.     }

  46.     /** Get the value of the cosine.
  47.      * @return value of the cosine
  48.      */
  49.     public T cos() {
  50.         return cos;
  51.     }

  52.     /** Compute sine and cosine of angles sum.
  53.      * @param scAlpha \((\sin \alpha, \cos \alpha)\)
  54.      * @param scBeta \((\sin \beta, \cos \beta)\)
  55.      * @param <S> the type of the field elements
  56.      * @return \((\sin \alpha+\beta, \cos \alpha+\beta)\)
  57.      * @since 1.8
  58.      */
  59.     public static <S extends CalculusFieldElement<S>> FieldSinCos<S> sum(final FieldSinCos<S> scAlpha, final FieldSinCos<S> scBeta) {
  60.         return new FieldSinCos<>(scAlpha.sin.linearCombination(scAlpha.sin, scBeta.cos, scAlpha.cos,          scBeta.sin),
  61.                                  scAlpha.sin.linearCombination(scAlpha.cos, scBeta.cos, scAlpha.sin.negate(), scBeta.sin));
  62.     }

  63.     /** Compute sine and cosine of angles difference.
  64.      * @param scAlpha \((\sin \alpha, \cos \alpha)\)
  65.      * @param scBeta \((\sin \beta, \cos \beta)\)
  66.      * @param <S> the type of the field elements
  67.      * @return \((\sin \alpha+\beta, \cos \alpha-\beta)\)
  68.      * @since 1.8
  69.      */
  70.     public static <S extends CalculusFieldElement<S>> FieldSinCos<S> difference(final FieldSinCos<S> scAlpha, final FieldSinCos<S> scBeta) {
  71.         return new FieldSinCos<>(scAlpha.sin.linearCombination(scAlpha.sin, scBeta.cos, scAlpha.cos.negate(), scBeta.sin),
  72.                                  scAlpha.sin.linearCombination(scAlpha.cos, scBeta.cos, scAlpha.sin,          scBeta.sin));
  73.     }

  74. }