/* TA-LIB Copyright (c) 1999-2025, Mario Fortier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
*   notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
*   notice, this list of conditions and the following disclaimer in
*   the documentation and/or other materials provided with the
*   distribution.
*
* - Neither name of author nor the names of its contributors
*   may be used to endorse or promote products derived from this
*   software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* AUTO-GENERATED by ta_codegen — DO NOT EDIT.
* Source of truth: ta_codegen/input/<name>/  (regenerate: cd ta_codegen/generator && cargo run -- generate)
*/

#include <string.h>
#include <math.h>
#include "ta_func.h"
#include "ta_utility.h"
#include "ta_memory.h"
#include "ta_func_unguarded.h"

/* List of contributors:
 *
 *  Initial  Name/description
 *  -------------------------------------------------------------------
 *  MF       Mario Fortier
 *
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  120802 MF   Template creation.
 *  101003 MF   Initial Coding
 *  062804 MF   Resolve div by zero bug on limit case.
 */

TA_LIB_API int TA_CORREL_Lookback( int optInTimePeriod )
{
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return -1;
   return optInTimePeriod - 1;
}

TA_LIB_API TA_RetCode TA_CORREL( int    startIdx,
                                 int    endIdx,
                                 const double inReal0[],
                                 const double inReal1[],
                                 int optInTimePeriod,
                                 int          *outBegIdx,
                                 int          *outNBElement,
                                 double        outReal[] )
{
   double sumXY;
   double sumX;
   double sumY;
   double sumX2;
   double sumY2;
   double x;
   double y;
   double trailingX;
   double trailingY;
   double tempReal;
   int lookbackTotal;
   int today;
   int trailingIdx;
   int outIdx;

   if( startIdx < 0 )
      return TA_OUT_OF_RANGE_START_INDEX;
   if( (endIdx < 0) || (endIdx < startIdx) )
      return TA_OUT_OF_RANGE_END_INDEX;

   if( !inReal0 )
      return TA_BAD_PARAM;
   if( !inReal1 )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   /* Move up the start index if there is not
    * enough initial data.
    */
   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   /* Make sure there is still something to evaluate. */
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   trailingIdx = startIdx - lookbackTotal;
   /* Calculate the initial values. */
   sumY2 = 0.0;
   sumX2 = sumY2;
   sumY = sumX2;
   sumX = sumY;
   sumXY = sumX;
   for( today = trailingIdx; today <= startIdx; today += 1 )
   {
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
   }
   /* Write the first output.
    * Save first the trailing values since the input
    * and output might be the same array,
    */
   trailingX = inReal0[trailingIdx];
   trailingY = inReal1[trailingIdx++];
   tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
   if( !TA_IS_ZERO_OR_NEG(tempReal) )
   {
      outReal[0] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
   } else 
   {
      outReal[0] = 0.0;
   }
   /* Tight loop to do subsequent values. */
   outIdx = 1;
   while( today <= endIdx )
   {
      /* Remove trailing values */
      sumX -= trailingX;
      sumX2 -= trailingX * trailingX;
      sumXY -= trailingX * trailingY;
      sumY -= trailingY;
      sumY2 -= trailingY * trailingY;
      /* Add new values */
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today++];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
      /* Output new coefficient.
       * Save first the trailing values since the input
       * and output might be the same array,
       */
      trailingX = inReal0[trailingIdx];
      trailingY = inReal1[trailingIdx++];
      tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
      if( !TA_IS_ZERO_OR_NEG(tempReal) )
      {
         outReal[outIdx++] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_CORREL_Unguarded( int    startIdx,
                                           int    endIdx,
                                           const double inReal0[],
                                           const double inReal1[],
                                           int optInTimePeriod,
                                           int          *outBegIdx,
                                           int          *outNBElement,
                                           double        outReal[] )
{
   double sumXY;
   double sumX;
   double sumY;
   double sumX2;
   double sumY2;
   double x;
   double y;
   double trailingX;
   double trailingY;
   double tempReal;
   int lookbackTotal;
   int today;
   int trailingIdx;
   int outIdx;

   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   trailingIdx = startIdx - lookbackTotal;
   sumY2 = 0.0;
   sumX2 = sumY2;
   sumY = sumX2;
   sumX = sumY;
   sumXY = sumX;
   for( today = trailingIdx; today <= startIdx; today += 1 )
   {
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
   }
   trailingX = inReal0[trailingIdx];
   trailingY = inReal1[trailingIdx++];
   tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
   if( !TA_IS_ZERO_OR_NEG(tempReal) )
   {
      outReal[0] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
   } else 
   {
      outReal[0] = 0.0;
   }
   outIdx = 1;
   while( today <= endIdx )
   {
      sumX -= trailingX;
      sumX2 -= trailingX * trailingX;
      sumXY -= trailingX * trailingY;
      sumY -= trailingY;
      sumY2 -= trailingY * trailingY;
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today++];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
      trailingX = inReal0[trailingIdx];
      trailingY = inReal1[trailingIdx++];
      tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
      if( !TA_IS_ZERO_OR_NEG(tempReal) )
      {
         outReal[outIdx++] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_CORREL( int    startIdx,
                        int    endIdx,
                        const float inReal0[],
                        const float inReal1[],
                        int optInTimePeriod,
                        int          *outBegIdx,
                        int          *outNBElement,
                        double        outReal[] )
{
   double sumXY;
   double sumX;
   double sumY;
   double sumX2;
   double sumY2;
   double x;
   double y;
   double trailingX;
   double trailingY;
   double tempReal;
   int lookbackTotal;
   int today;
   int trailingIdx;
   int outIdx;

   if( startIdx < 0 )
      return TA_OUT_OF_RANGE_START_INDEX;
   if( (endIdx < 0) || (endIdx < startIdx) )
      return TA_OUT_OF_RANGE_END_INDEX;

   if( !inReal0 )
      return TA_BAD_PARAM;
   if( !inReal1 )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   trailingIdx = startIdx - lookbackTotal;
   sumY2 = 0.0;
   sumX2 = sumY2;
   sumY = sumX2;
   sumX = sumY;
   sumXY = sumX;
   for( today = trailingIdx; today <= startIdx; today += 1 )
   {
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
   }
   trailingX = inReal0[trailingIdx];
   trailingY = inReal1[trailingIdx++];
   tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
   if( !TA_IS_ZERO_OR_NEG(tempReal) )
   {
      outReal[0] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
   } else 
   {
      outReal[0] = 0.0;
   }
   outIdx = 1;
   while( today <= endIdx )
   {
      sumX -= trailingX;
      sumX2 -= trailingX * trailingX;
      sumXY -= trailingX * trailingY;
      sumY -= trailingY;
      sumY2 -= trailingY * trailingY;
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today++];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
      trailingX = inReal0[trailingIdx];
      trailingY = inReal1[trailingIdx++];
      tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
      if( !TA_IS_ZERO_OR_NEG(tempReal) )
      {
         outReal[outIdx++] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_CORREL_Unguarded( int    startIdx,
                                  int    endIdx,
                                  const float inReal0[],
                                  const float inReal1[],
                                  int optInTimePeriod,
                                  int          *outBegIdx,
                                  int          *outNBElement,
                                  double        outReal[] )
{
   double sumXY;
   double sumX;
   double sumY;
   double sumX2;
   double sumY2;
   double x;
   double y;
   double trailingX;
   double trailingY;
   double tempReal;
   int lookbackTotal;
   int today;
   int trailingIdx;
   int outIdx;

   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   trailingIdx = startIdx - lookbackTotal;
   sumY2 = 0.0;
   sumX2 = sumY2;
   sumY = sumX2;
   sumX = sumY;
   sumXY = sumX;
   for( today = trailingIdx; today <= startIdx; today += 1 )
   {
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
   }
   trailingX = inReal0[trailingIdx];
   trailingY = inReal1[trailingIdx++];
   tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
   if( !TA_IS_ZERO_OR_NEG(tempReal) )
   {
      outReal[0] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
   } else 
   {
      outReal[0] = 0.0;
   }
   outIdx = 1;
   while( today <= endIdx )
   {
      sumX -= trailingX;
      sumX2 -= trailingX * trailingX;
      sumXY -= trailingX * trailingY;
      sumY -= trailingY;
      sumY2 -= trailingY * trailingY;
      x = inReal0[today];
      sumX += x;
      sumX2 += x * x;
      y = inReal1[today++];
      sumXY += x * y;
      sumY += y;
      sumY2 += y * y;
      trailingX = inReal0[trailingIdx];
      trailingY = inReal1[trailingIdx++];
      tempReal = (sumX2 - sumX * sumX / optInTimePeriod) * (sumY2 - sumY * sumY / optInTimePeriod);
      if( !TA_IS_ZERO_OR_NEG(tempReal) )
      {
         outReal[outIdx++] = (sumXY - sumX * sumY / optInTimePeriod) / sqrt(tempReal);
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

