SinCos.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. /** Holder for both sine and cosine values.
  19.  * <p>
  20.  * This class is a simple container, it does not provide any computational method.
  21.  * </p>
  22.  * @see FastMath#sinCos(double)
  23.  * @since 1.3
  24.  */
  25. public class SinCos {

  26.     /** Value of the sine. */
  27.     private final double sin;

  28.     /** Value of the cosine. */
  29.     private final double cos;

  30.     /** Simple constructor.
  31.      * @param sin value of the sine
  32.      * @param cos value of the cosine
  33.      */
  34.     SinCos(final double sin, final double cos) {
  35.         this.sin = sin;
  36.         this.cos = cos;
  37.     }

  38.     /** Get the value of the sine.
  39.      * @return value of the sine
  40.      */
  41.     public double sin() {
  42.         return sin;
  43.     }

  44.     /** Get the value of the cosine.
  45.      * @return value of the cosine
  46.      */
  47.     public double cos() {
  48.         return cos;
  49.     }

  50.     /** Compute sine and cosine of angles sum.
  51.      * @param scAlpha \((\sin \alpha, \cos \alpha)\)
  52.      * @param scBeta \((\sin \beta, \cos \beta)\)
  53.      * @return \((\sin \alpha+\beta, \cos \alpha+\beta)\)
  54.      * @since 1.8
  55.      */
  56.     public static SinCos sum(final SinCos scAlpha, final SinCos scBeta) {
  57.         return new SinCos(MathArrays.linearCombination(scAlpha.sin, scBeta.cos,  scAlpha.cos, scBeta.sin),
  58.                           MathArrays.linearCombination(scAlpha.cos, scBeta.cos, -scAlpha.sin, scBeta.sin));
  59.     }

  60.     /** Compute sine and cosine of angles difference.
  61.      * @param scAlpha \((\sin \alpha, \cos \alpha)\)
  62.      * @param scBeta \((\sin \beta, \cos \beta)\)
  63.      * @return \((\sin \alpha+\beta, \cos \alpha-\beta)\)
  64.      * @since 1.8
  65.      */
  66.     public static SinCos difference(final SinCos scAlpha, final SinCos scBeta) {
  67.         return new SinCos(MathArrays.linearCombination(scAlpha.sin, scBeta.cos, -scAlpha.cos, scBeta.sin),
  68.                           MathArrays.linearCombination(scAlpha.cos, scBeta.cos,  scAlpha.sin, scBeta.sin));
  69.     }

  70. }