<commit_before>/**
 * \file SVFSD1OverdriveFilter.cpp
 */

#include "SVFSD1OverdriveFilter.h"

#include <stdexcept>

#include <boost/math/special_functions/sign.hpp>

#include <ATK/Utility/ScalarNewtonRaphson.h>

namespace ATK
{
  template<typename DataType_>
  class SVFSD1OverdriveFilter<DataType_>::SD1OverdriveFunction
  {
  public:
    typedef DataType_ DataType;
  protected:
    const DataType dt;
    const DataType R;
    const DataType R1;
    const DataType C;
    const DataType Q;
    DataType drive;
    const DataType is;
    const DataType vt;

    DataType ieq;
    
  public:
    SD1OverdriveFunction(DataType dt, DataType R, DataType C, DataType R1, DataType Q, DataType is, DataType vt)
    :dt(dt), R(R), R1(R1), C(C), Q(Q), drive(0.5), is(is), vt(vt), ieq(0)
    {
    }
    
    void set_drive(DataType drive)
    {
      this->drive = (R1 + drive * Q);
    }
    
    std::pair<DataType, DataType> operator()(const DataType* ATK_RESTRICT input, DataType* ATK_RESTRICT output, DataType y1)
    {
      auto x1 = input[0];
      y1 -= x1;
      DataType expdiode_y1_p = std::exp(y1 / vt);
      DataType expdiode_y1_m = 1 / expdiode_y1_p;
      
      DataType diode1 = is * (expdiode_y1_p - 2 * expdiode_y1_m + 1);
      DataType diode1_derivative = is * (expdiode_y1_p + 2 * expdiode_y1_m) / vt;

      DataType i = (2 * C * x1 / dt - ieq) / (2 * R * C / dt);
      
      return std::make_pair(y1 / drive + diode1 - i, 1 / drive + diode1_derivative);
    }
    
    void update_state(const DataType* ATK_RESTRICT input, DataType* ATK_RESTRICT output)
    {
      auto x1 = input[0];
      DataType i = (2 * C * x1 / dt - ieq) / (2 * R * C / dt);

      ieq = 4 / dt * C * (x1 - i * R) - ieq;
    }

    DataType estimate(const DataType* ATK_RESTRICT input, DataType* ATK_RESTRICT output)
    {
      return output[-1];
    }
    
    /*
    DataType id_estimate(DataType x0, DataType x1, DataType y0)
    {
      return y0;
    }
    
    DataType linear_estimate(DataType x0, DataType x1, DataType y0)
    {
      y0 -= x0;
      if(y0 == 0)
        return 0;
      auto sinh = is * (oldexpy1 - oldinvexpy1);
      return (x1 - x0 - y0 * (B / drive) - B * sinh) / (A * sinh / y0 + (A / drive)) + x1;
    }
    
    DataType affine_estimate(DataType x0, DataType x1, DataType y0)
    {
      y0 -= x0;
      auto sinh = is * (oldexpy1 - oldinvexpy1);
      auto cosh = is * (oldexpy1 + oldinvexpy1);<commit_msg>Fix SVF. Now I need to make it efficient.<commit_after>/**
 * \file SVFSD1OverdriveFilter.cpp
 */

#include "SVFSD1OverdriveFilter.h"

#include <stdexcept>

#include <boost/math/special_functions/sign.hpp>

#include <ATK/Utility/ScalarNewtonRaphson.h>

namespace ATK
{
  template<typename DataType_>
  class SVFSD1OverdriveFilter<DataType_>::SD1OverdriveFunction
  {
  public:
    typedef DataType_ DataType;
  protected:
    const DataType dt;
    const DataType R;
    const DataType R1;
    const DataType C;
    const DataType Q;
    DataType drive;
    const DataType is;
    const DataType vt;

    DataType ieq;
    
  public:
    SD1OverdriveFunction(DataType dt, DataType R, DataType C, DataType R1, DataType Q, DataType is, DataType vt)
    :dt(dt), R(R), R1(R1), C(C), Q(Q), drive(0.5), is(is), vt(vt), ieq(0)
    {
    }
    
    void set_drive(DataType drive)
    {
      this->drive = (R1 + drive * Q);
    }
    
    std::pair<DataType, DataType> operator()(const DataType* ATK_RESTRICT input, DataType* ATK_RESTRICT output, DataType y1)
    {
      auto x1 = input[0];
      y1 -= x1;
      DataType expdiode_y1_p = std::exp(y1 / vt);
      DataType expdiode_y1_m = 1 / expdiode_y1_p;
      
      DataType diode1 = is * (expdiode_y1_p - 2 * expdiode_y1_m + 1);
      DataType diode1_derivative = is * (expdiode_y1_p + 2 * expdiode_y1_m) / vt;

      DataType i = (2 * C * x1 / dt - ieq) / (1 + 2 * R * C / dt);
      
      return std::make_pair(y1 / drive + diode1 - i, 1 / drive + diode1_derivative);
    }
    
    void update_state(const DataType* ATK_RESTRICT input, DataType* ATK_RESTRICT output)
    {
      auto x1 = input[0];
      DataType i = (2 * C * x1 / dt - ieq) / (1 + 2 * R * C / dt);

      ieq = 4 / dt * C * (x1 - i * R) - ieq;
    }

    DataType estimate(const DataType* ATK_RESTRICT input, DataType* ATK_RESTRICT output)
    {
      return output[-1];
    }
    
    /*
    DataType id_estimate(DataType x0, DataType x1, DataType y0)
    {
      return y0;
    }
    
    DataType linear_estimate(DataType x0, DataType x1, DataType y0)
    {
      y0 -= x0;
      if(y0 == 0)
        return 0;
      auto sinh = is * (oldexpy1 - oldinvexpy1);
      return (x1 - x0 - y0 * (B / drive) - B * sinh) / (A * sinh / y0 + (A / drive)) + x1;
    }
    
    DataType affine_estimate(DataType x0, DataType x1, DataType y0)
    {
      y0 -= x0;
      auto sinh = is * (oldexpy1 - oldinvexpy1);
      auto cosh = is * (oldexpy1 + oldinvexpy1);
{'fraction_non_alphanumeric': 0.11939690301548492, 'fraction_numerical': 0.030969845150774247, 'mean_word_length': 2.3738831615120275, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2125477', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1912, 'n_words': 479}