RotationCountPhaseTracker.java

  1. package org.drip.numerical.fourier;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  *
  14.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  15.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  16.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  17.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  18.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  19.  *      and computational support.
  20.  *  
  21.  *      https://lakshmidrip.github.io/DROP/
  22.  *  
  23.  *  DROP is composed of three modules:
  24.  *  
  25.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  26.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  27.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  28.  *
  29.  *  DROP Product Core implements libraries for the following:
  30.  *  - Fixed Income Analytics
  31.  *  - Loan Analytics
  32.  *  - Transaction Cost Analytics
  33.  *
  34.  *  DROP Portfolio Core implements libraries for the following:
  35.  *  - Asset Allocation Analytics
  36.  *  - Asset Liability Management Analytics
  37.  *  - Capital Estimation Analytics
  38.  *  - Exposure Analytics
  39.  *  - Margin Analytics
  40.  *  - XVA Analytics
  41.  *
  42.  *  DROP Computational Core implements libraries for the following:
  43.  *  - Algorithm Support
  44.  *  - Computation Support
  45.  *  - Function Analysis
  46.  *  - Model Validation
  47.  *  - Numerical Analysis
  48.  *  - Numerical Optimizer
  49.  *  - Spline Builder
  50.  *  - Statistical Learning
  51.  *
  52.  *  Documentation for DROP is Spread Over:
  53.  *
  54.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  55.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  56.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  57.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  58.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  59.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  60.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  61.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  62.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  63.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  64.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  65.  *
  66.  *  Licensed under the Apache License, Version 2.0 (the "License");
  67.  *      you may not use this file except in compliance with the License.
  68.  *  
  69.  *  You may obtain a copy of the License at
  70.  *      http://www.apache.org/licenses/LICENSE-2.0
  71.  *  
  72.  *  Unless required by applicable law or agreed to in writing, software
  73.  *      distributed under the License is distributed on an "AS IS" BASIS,
  74.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  75.  *  
  76.  *  See the License for the specific language governing permissions and
  77.  *      limitations under the License.
  78.  */

  79. /**
  80.  * <i>RotationCountPhaseTracker</i> implements the standard technique to preserve the trajectory along the
  81.  * principal branch in multi-valued complex operations. This is most common in Fourier inversion quadrature
  82.  * runs.
  83.  *
  84.  * <br><br>
  85.  *  <ul>
  86.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  87.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  88.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/README.md">Numerical Quadrature, Differentiation, Eigenization, Linear Algebra, and Utilities</a></li>
  89.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/numerical/fourier/README.md">Specific Fourier Transform Functionality - Rotation Counter, Phase Adjuster</a></li>
  90.  *  </ul>
  91.  * <br><br>
  92.  *
  93.  * @author Lakshmi Krishnamurthy
  94.  */

  95. public class RotationCountPhaseTracker {

  96.     /**
  97.      * APPLY_NONE - Do not Apply Rotation Count
  98.      */

  99.     public static final int APPLY_NONE = 0;

  100.     /**
  101.      * APPLY_BACKWARD - Decrement Rotation Count
  102.      */

  103.     public static final int APPLY_BACKWARD = 1;

  104.     /**
  105.      * APPLY_FORWARD - Increment Rotation Count
  106.      */

  107.     public static final int APPLY_FORWARD = 2;

  108.     private int _iRotationDirection = APPLY_NONE;
  109.     private double _dblPreviousPhase = java.lang.Double.NaN;

  110.     /**
  111.      * Empty RotationCountPhaseTracker constructor - Initialize to "NO ROTATION COUNT"
  112.      */

  113.     public RotationCountPhaseTracker()
  114.     {
  115.         _iRotationDirection = APPLY_NONE;
  116.     }

  117.     /**
  118.      * Set the Direction on which the rotation count is to be applied
  119.      *
  120.      * @param iRotationDirection The Rotation Direction
  121.      *
  122.      * @return TRUE - Successfully set
  123.      */

  124.     public boolean setDirection (
  125.         final int iRotationDirection)
  126.     {
  127.         _iRotationDirection = iRotationDirection;
  128.         return true;
  129.     }

  130.     /**
  131.      * Get the Direction on which the rotation count is to be applied
  132.      *
  133.      * @return The Rotation Direction
  134.      */

  135.     public int getDirection()
  136.     {
  137.         return _iRotationDirection;
  138.     }

  139.     /**
  140.      * Set the Previous Phase
  141.      *
  142.      * @param dblPreviousPhase The Previous Phase
  143.      *
  144.      * @return TRUE - Previous Phase Successfully set
  145.      */

  146.     public boolean setPreviousPhase (
  147.         final double dblPreviousPhase)
  148.     {
  149.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblPreviousPhase)) return false;

  150.         _dblPreviousPhase = dblPreviousPhase;
  151.         return true;
  152.     }

  153.     /**
  154.      * Get the Previous Phase
  155.      *
  156.      * @return The Previous Phase
  157.      */

  158.     public double getPreviousPhase()
  159.     {
  160.         return _dblPreviousPhase;
  161.     }

  162.     /**
  163.      * Apply the Rotation Count Adjustment in accordance with the direction, (optionally) record the previous
  164.      *  phase.
  165.      *
  166.      * @param dblCurrentPhase The Phase to be Updated
  167.      * @param bApply TRUE - Record the Previous Phase
  168.      *
  169.      * @return The Updated Phase
  170.      *
  171.      * @throws java.lang.Exception Thrown if the Operation cannot be performed
  172.      */

  173.     public double updateAndApply (
  174.         final double dblCurrentPhase,
  175.         final boolean bApply)
  176.         throws java.lang.Exception
  177.     {
  178.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblCurrentPhase))
  179.             throw new java.lang.Exception ("RotationCountPhaseTracker::updateAndApply => Invalid Inputs");

  180.         double dblUpdatedPhase = dblCurrentPhase;

  181.         if (APPLY_FORWARD == _iRotationDirection) {
  182.             while (dblUpdatedPhase < _dblPreviousPhase)
  183.                 dblUpdatedPhase += 2. * java.lang.Math.PI;
  184.         } else if (APPLY_BACKWARD == _iRotationDirection) {
  185.             while (dblUpdatedPhase > _dblPreviousPhase)
  186.                 dblUpdatedPhase -= 2. * java.lang.Math.PI;
  187.         } else if (APPLY_NONE != _iRotationDirection)
  188.             throw new java.lang.Exception ("RotationCountPhaseTracker::updateAndApply => Invalid State");

  189.         if (bApply) _dblPreviousPhase = dblUpdatedPhase;

  190.         return dblUpdatedPhase;
  191.     }
  192. }