/* 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
 *  -------------------------------------------------------------------
 *  JP       John Price <jp_talib@gcfl.net>
 *
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  070203 JP   Initial.
 */

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

TA_LIB_API TA_RetCode TA_LINEARREG_SLOPE( int    startIdx,
                                          int    endIdx,
                                          const double inReal[],
                                          int optInTimePeriod,
                                          int          *outBegIdx,
                                          int          *outNBElement,
                                          double        outReal[] )
{
   int outIdx;
   int today;
   int lookbackTotal;
   double SumX;
   double SumXY;
   double SumY;
   double SumXSqr;
   double Divisor;
   int i;
   double tempValue1;

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

   if( !inReal )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 14;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   /* Linear Regression is a concept also known as the
    * "least squares method" or "best fit." Linear
    * Regression attempts to fit a straight line between
    * several data points in such a way that distance
    * between each data point and the line is minimized.
    *
    * For each point, a straight line over the specified
    * previous bar period is determined in terms
    * of y = b + m*x:
    *
    * TA_LINEARREG          : Returns b+m*(period-1)
    * TA_LINEARREG_SLOPE    : Returns 'm'
    * TA_LINEARREG_ANGLE    : Returns 'm' in degree.
    * TA_LINEARREG_INTERCEPT: Returns 'b'
    * TA_TSF                : Returns b+m*(period)
    */
   /* Adjust startIdx to account for the lookback period. */
   lookbackTotal = TA_LINEARREG_SLOPE_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   /* Make sure there is still something to evaluate. */
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   /* Index into the output. */
   today = startIdx;
   SumX = optInTimePeriod * (optInTimePeriod - 1) * 0.5;
   SumXSqr = optInTimePeriod * (optInTimePeriod - 1) * (2 * optInTimePeriod - 1) / 6;
   Divisor = SumX * SumX - optInTimePeriod * SumXSqr;
   while( today <= endIdx )
   {
      SumXY = 0;
      SumY = 0;
      for( i = optInTimePeriod; i-- != 0;  )
      {
         tempValue1 = inReal[today - i];
         SumY += tempValue1;
         SumXY += (double)i * tempValue1;
      }
      outReal[outIdx++] = (optInTimePeriod * SumXY - SumX * SumY) / Divisor;
      today += 1;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_LINEARREG_SLOPE_Unguarded( int    startIdx,
                                                    int    endIdx,
                                                    const double inReal[],
                                                    int optInTimePeriod,
                                                    int          *outBegIdx,
                                                    int          *outNBElement,
                                                    double        outReal[] )
{
   int outIdx;
   int today;
   int lookbackTotal;
   double SumX;
   double SumXY;
   double SumY;
   double SumXSqr;
   double Divisor;
   int i;
   double tempValue1;

   lookbackTotal = TA_LINEARREG_SLOPE_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   today = startIdx;
   SumX = optInTimePeriod * (optInTimePeriod - 1) * 0.5;
   SumXSqr = optInTimePeriod * (optInTimePeriod - 1) * (2 * optInTimePeriod - 1) / 6;
   Divisor = SumX * SumX - optInTimePeriod * SumXSqr;
   while( today <= endIdx )
   {
      SumXY = 0;
      SumY = 0;
      for( i = optInTimePeriod; i-- != 0;  )
      {
         tempValue1 = inReal[today - i];
         SumY += tempValue1;
         SumXY += (double)i * tempValue1;
      }
      outReal[outIdx++] = (optInTimePeriod * SumXY - SumX * SumY) / Divisor;
      today += 1;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_LINEARREG_SLOPE( int    startIdx,
                                 int    endIdx,
                                 const float inReal[],
                                 int optInTimePeriod,
                                 int          *outBegIdx,
                                 int          *outNBElement,
                                 double        outReal[] )
{
   int outIdx;
   int today;
   int lookbackTotal;
   double SumX;
   double SumXY;
   double SumY;
   double SumXSqr;
   double Divisor;
   int i;
   double tempValue1;

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

   if( !inReal )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 14;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   lookbackTotal = TA_LINEARREG_SLOPE_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   today = startIdx;
   SumX = optInTimePeriod * (optInTimePeriod - 1) * 0.5;
   SumXSqr = optInTimePeriod * (optInTimePeriod - 1) * (2 * optInTimePeriod - 1) / 6;
   Divisor = SumX * SumX - optInTimePeriod * SumXSqr;
   while( today <= endIdx )
   {
      SumXY = 0;
      SumY = 0;
      for( i = optInTimePeriod; i-- != 0;  )
      {
         tempValue1 = inReal[today - i];
         SumY += tempValue1;
         SumXY += (double)i * tempValue1;
      }
      outReal[outIdx++] = (optInTimePeriod * SumXY - SumX * SumY) / Divisor;
      today += 1;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_LINEARREG_SLOPE_Unguarded( int    startIdx,
                                           int    endIdx,
                                           const float inReal[],
                                           int optInTimePeriod,
                                           int          *outBegIdx,
                                           int          *outNBElement,
                                           double        outReal[] )
{
   int outIdx;
   int today;
   int lookbackTotal;
   double SumX;
   double SumXY;
   double SumY;
   double SumXSqr;
   double Divisor;
   int i;
   double tempValue1;

   lookbackTotal = TA_LINEARREG_SLOPE_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outIdx = 0;
   today = startIdx;
   SumX = optInTimePeriod * (optInTimePeriod - 1) * 0.5;
   SumXSqr = optInTimePeriod * (optInTimePeriod - 1) * (2 * optInTimePeriod - 1) / 6;
   Divisor = SumX * SumX - optInTimePeriod * SumXSqr;
   while( today <= endIdx )
   {
      SumXY = 0;
      SumY = 0;
      for( i = optInTimePeriod; i-- != 0;  )
      {
         tempValue1 = inReal[today - i];
         SumY += tempValue1;
         SumXY += (double)i * tempValue1;
      }
      outReal[outIdx++] = (optInTimePeriod * SumXY - SumX * SumY) / Divisor;
      today += 1;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

