BigR1Array.java

  1. package org.drip.spaces.big;

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

  78. /**
  79.  * <i>BigR1Array</i> contains an Implementation of a variety of in-place Sorting Algorithms for Big Double
  80.  * Arrays.
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li>
  85.  *          C A R Hoare's Quick Sort
  86.  *      </li>
  87.  *      <li>
  88.  *          J von Neumann's Merge Sort
  89.  *      </li>
  90.  *      <li>
  91.  *          R W Floyd's Heap Sort
  92.  *      </li>
  93.  *      <li>
  94.  *          Insertion Sort
  95.  *      </li>
  96.  *  </ul>
  97.  *
  98.  * <br><br>
  99.  *  <ul>
  100.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  101.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  102.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  103.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/big/README.md">Big-data In-place Manipulator</a></li>
  104.  *  </ul>
  105.  * <br><br>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. public class BigR1Array {
  110.     private int _iLength = -1;
  111.     private double[] _adblA = null;

  112.     private void swapLocations (
  113.         final int i1,
  114.         final int i2)
  115.     {
  116.         double dblTemp = _adblA[i1];
  117.         _adblA[i1] = _adblA[i2];
  118.         _adblA[i2] = dblTemp;
  119.     }

  120.     private int dropOffIndex (
  121.         final int iPickupIndex)
  122.     {
  123.         for (int i = 0; i < iPickupIndex; ++i) {
  124.             if (_adblA[i] > _adblA[iPickupIndex]) return i;
  125.         }

  126.         return iPickupIndex;
  127.     }

  128.     /**
  129.      * BigR1Array Constructor
  130.      *
  131.      * @param adblA The Array to be Sorted
  132.      *
  133.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  134.      */

  135.     public BigR1Array (
  136.         final double[] adblA)
  137.         throws java.lang.Exception
  138.     {
  139.         if (null == (_adblA = adblA) || 0 == (_iLength = _adblA.length))
  140.             throw new java.lang.Exception ("BigR1Array Constructor => Invalid Inputs");
  141.     }

  142.     /**
  143.      * Retrieve the Array Contents
  144.      *
  145.      * @return The Array Contents
  146.      */

  147.     public double[] array()
  148.     {
  149.         return _adblA;
  150.     }

  151.     /**
  152.      * Transfer all Elements from the Pickup Index to the Drop Off Index, and contiguously Shift the
  153.      *  Intermediate Array
  154.      *
  155.      * @param iPickupIndex The Pickup Index
  156.      * @param iDropOffIndex The Drop off Index
  157.      *
  158.      * @return TRUE - Successfully transferred the Pickup to the Drop off Locations and the related
  159.      *  Translations
  160.      */

  161.     public boolean transfer (
  162.         final int iPickupIndex,
  163.         final int iDropOffIndex)
  164.     {
  165.         if (iPickupIndex < 0 || iPickupIndex >= _iLength || iDropOffIndex < 0 || iDropOffIndex >= _iLength)
  166.             return false;

  167.         if (iPickupIndex == iDropOffIndex) return true;

  168.         double dblTemp = _adblA[iPickupIndex];

  169.         if (iPickupIndex > iDropOffIndex) {
  170.             for (int i = iPickupIndex; i > iDropOffIndex; --i)
  171.                 _adblA[i] = _adblA[i - 1];
  172.         } else {
  173.             for (int i = iPickupIndex; i > iDropOffIndex; ++i)
  174.                 _adblA[i] = _adblA[i + 1];
  175.         }

  176.         _adblA[iDropOffIndex] = dblTemp;
  177.         return true;
  178.     }

  179.     /**
  180.      * Sort the Specified Range in the Array using Quick Sort
  181.      *
  182.      * @param iLow Lower Index of the Range (Inclusive)
  183.      * @param iHigh Upper Index of the Range (Inclusive)
  184.      *
  185.      * @return TRUE - The Range has been successfully sorted
  186.      */

  187.     public boolean quickSort (
  188.         final int iLow,
  189.         final int iHigh)
  190.     {
  191.         if (iLow < 0 || iLow >= iHigh || iLow >= _iLength)
  192.         {
  193.             return false;
  194.         }

  195.         int iLeft = iLow;
  196.         int iRight = iHigh;
  197.         double dblPivot = _adblA[(iLow + iHigh) / 2];

  198.         while (iLeft <= iRight)
  199.         {
  200.             while (_adblA[iLeft] < dblPivot)
  201.             {
  202.                 ++iLeft;
  203.             }

  204.             while (_adblA[iRight] > dblPivot)
  205.             {
  206.                 --iRight;
  207.             }

  208.             if (iLeft <= iRight)
  209.             {
  210.                 swapLocations (iLeft, iRight);

  211.                 ++iLeft;
  212.                 --iRight;
  213.             }
  214.         }

  215.         if (iLow < iRight && !quickSort (iLow, iRight)) return false;

  216.         if (iLeft < iHigh && !quickSort (iLeft, iHigh)) return false;

  217.         return true;
  218.     }

  219.     /**
  220.      * Sort the Full Array using Quick Sort
  221.      *
  222.      * @return TRUE - The Full Array has been successfully sorted
  223.      */

  224.     public boolean quickSort()
  225.     {
  226.         return quickSort (0, _iLength - 1);
  227.     }

  228.     /**
  229.      * Merge the Sorted Sub Array Pair
  230.      *
  231.      * @param i1Start The Left Starting Index (Inclusive)
  232.      * @param i1End The Left Ending Index (Inclusive)
  233.      * @param i2Start The Right Starting Index (Inclusive)
  234.      * @param i2End The Right End Index (Inclusive)
  235.      *
  236.      * @return TRUE - Successfully Merged the Sorted Array Pair and Re-assigned Back to the Master
  237.      */

  238.     public boolean mergeSort (
  239.         final int i1Start,
  240.         final int i1End,
  241.         final int i2Start,
  242.         final int i2End)
  243.     {
  244.         if (i1Start >= _iLength || i1Start > i1End || i2Start > i2End || i1End >= i2Start) return false;

  245.         if (i1Start == i1End && i2Start == i2End) {
  246.             if (_adblA[i1Start] > _adblA[i2Start]) swapLocations (i1Start, i2Start);
  247.         } else {
  248.             if (i1Start == i1End) {
  249.                 int i2Mid = (i2Start + i2End) / 2;

  250.                 if (!mergeSort (i2Start, i2Mid, i2Mid + 1, i2End)) return false;
  251.             }
  252.         }

  253.         int i = 0;
  254.         int i1 = i1Start;
  255.         int i2 = i2Start;
  256.         double[] adblMerged = new double[i1End - i1Start + i2End - i2Start + 2];

  257.         while (i1 <= i1End && i2 <= i2End) {
  258.             if (_adblA[i1] < _adblA[i2])
  259.                 adblMerged[i++] = _adblA[i1++];
  260.             else if (_adblA[i1] > _adblA[i2])
  261.                 adblMerged[i++] = _adblA[i2++];
  262.             else {
  263.                 adblMerged[i++] = _adblA[i1++];
  264.                 adblMerged[i++] = _adblA[i2++];
  265.             }
  266.         }

  267.         while (i1 <= i1End)
  268.             adblMerged[i++] = _adblA[i1++];

  269.         while (i2 <= i2End)
  270.             adblMerged[i++] = _adblA[i2++];

  271.         i = 0;

  272.         for (int j = i1Start; j <= i1End; ++j)
  273.             _adblA[j] = adblMerged[i++];

  274.         for (int j = i2Start; j <= i2End; ++j)
  275.             _adblA[j] = adblMerged[i++];

  276.         return true;
  277.     }

  278.     /**
  279.      * Contiguous Stretch Merge Sort
  280.      *
  281.      * @param iStart The Master Starting Index (Inclusive)
  282.      * @param iEnd The Master Ending Index (Inclusive)
  283.      *
  284.      * @return TRUE - Successfully Merged the Sorted Array Stretch and Re-assigned Back to the Master
  285.      */

  286.     public boolean mergeSort (
  287.         final int iStart,
  288.         final int iEnd)
  289.     {
  290.         if (iStart < 0 || iStart > iEnd || iStart >= _iLength) return false;

  291.         if (iStart == iEnd) return true;

  292.         if (iStart == iEnd - 1) {
  293.             if (_adblA[iStart] > _adblA[iEnd]) swapLocations (iStart, iEnd);

  294.             return true;
  295.         }

  296.         double[] adblMerged = new double[iEnd - iStart + 1];
  297.         int iMid = (iStart + iEnd) / 2;
  298.         int iRightStart = iMid + 1;
  299.         int iRight = iRightStart;
  300.         int iLeftStart = iStart;
  301.         int iLeft = iLeftStart;
  302.         int iRightEnd = iEnd;
  303.         int iLeftEnd = iMid;
  304.         int i = 0;

  305.         if (!mergeSort (iLeftStart, iLeftEnd) || !mergeSort (iRightStart, iRightEnd)) return false;

  306.         while (iLeft <= iLeftEnd && iRight <= iRightEnd) {
  307.             if (_adblA[iLeft] < _adblA[iRight])
  308.                 adblMerged[i++] = _adblA[iLeft++];
  309.             else if (_adblA[iLeft] > _adblA[iRight])
  310.                 adblMerged[i++] = _adblA[iRight++];
  311.             else {
  312.                 adblMerged[i++] = _adblA[iLeft++];
  313.                 adblMerged[i++] = _adblA[iRight++];
  314.             }
  315.         }

  316.         while (iLeft <= iLeftEnd)
  317.             adblMerged[i++] = _adblA[iLeft++];

  318.         while (iRight <= iRightEnd)
  319.             adblMerged[i++] = _adblA[iRight++];

  320.         i = 0;

  321.         for (int j = iLeftStart; j <= iLeftEnd; ++j)
  322.             _adblA[j] = adblMerged[i++];

  323.         for (int j = iRightStart; j <= iRightEnd; ++j)
  324.             _adblA[j] = adblMerged[i++];

  325.         return true;
  326.     }

  327.     /**
  328.      * In-place Big Array Merge Sort
  329.      *
  330.      * @return TRUE - Successfully Merged the Big Double Array and Re-assigned Back to the Master
  331.      */

  332.     public boolean mergeSort()
  333.     {
  334.         return mergeSort (0, _iLength - 1);
  335.     }

  336.     /**
  337.      * Heap Sort the Big Array
  338.      *
  339.      * @return TRUE - Successfully Merged the Big Double Array and Re-assigned Back to the Master
  340.      */

  341.     public boolean heapSort()
  342.     {
  343.         org.drip.spaces.big.BinaryTree bt = null;

  344.         try {
  345.             bt = new org.drip.spaces.big.BinaryTree (_adblA[0], null);
  346.         } catch (java.lang.Exception e) {
  347.             e.printStackTrace();

  348.             return false;
  349.         }

  350.         for (int i = 1; i < _iLength; ++i) {
  351.             if (null == bt.insert (_adblA[i])) return false;
  352.         }

  353.         try {
  354.             return _iLength == bt.ascendingNodeArray (_adblA, 0);
  355.         } catch (java.lang.Exception e) {
  356.             e.printStackTrace();
  357.         }

  358.         return false;
  359.     }

  360.     /**
  361.      * Insertion Sort the Big Array
  362.      *
  363.      * @return TRUE - Successfully Merged the Big Double Array and Re-assigned Back to the Master
  364.      */

  365.     public boolean insertionSort()
  366.     {
  367.         for (int iPickupIndex = 1; iPickupIndex < _iLength; ++iPickupIndex) {
  368.             if (!transfer (iPickupIndex, dropOffIndex (iPickupIndex))) return false;
  369.         }

  370.         return true;
  371.     }
  372. }