<commit_before><commit_msg>Tested and certified 1<commit_after>#include "fastlib/fastlib.h"
#include "ocas.h"

#ifndef OCAS_LINE_SEARCH_H_
#define OCAS_LINE_SEARCH_H_


class Interval{

 public:

  double up;
  double low;

  // some useful functions

  bool CheckIfIntervalHasZero(){
    if(this->low<0&& this->up>0) return true;
    return false;

  }

  bool CheckIfIntervalIsPositive(){
    if(this->low>0&& this->up>0) return true;
    return false;

  }

  bool CheckIfIntervalIsNegative(){
    if(this->low<0&& this->up<0) return true;
    return false;

  }
 
};
class OCASLineSearch{

 public:

  
  // The dataset that will be used for optimization. This will have
  // points outside the range of the query point ALSO.
  Matrix train_data_appended_;

  // The train labels of all points
  Vector train_labels_;

  
  // The query point where we want to learn the local SVM.
  Vector query_point_appended_;
  
  
  // The regularization constant
  
  double lambda_reg_const_;


  // The indices that are in the bandwidth range
  
  ArrayList <index_t> indices_in_range_;


  // The kernel value of the points which are in the range.

  ArrayList <double> smoothing_kernel_values_in_range_;


  // This is the dimension of the appended dataset. Hence it will be 1 more 
  // than the actual dimensions.

  index_t num_dims_appended_;
  
  // The number of points in the range 
  
  index_t num_points_in_range_;

  // The total number of points
  index_t num_train_points_;


 
  // The thresholds

  Vector thresholds_vec_;

  // Initialize the derivatives of the line search objective

  ArrayList <Interval> derivative_line_search_objective_;

  //The vector $w_1$

  Vector w_1_vec_;

  // The vector $w_2$

  Vector w_2_vec_;

  // The optimal k. i.e we are trying to minimize the function
  // F[(1-k)w_1+kw_2] over k.

  double k_star_;

  // A constant that is used heavily while calculating derivatives

  double lambda_w_2_minus_w_1_sqd_;

  // Vector C_i and B_i. These will be used in gradient calculations as well as
  // threshold calculations

  Vector C_i_vec_;

  Vector B_i_vec_;
  
  void  CalculateThresholdsForPointsInRange_();
  void SortThresholds_();
  void Swap_(int *a,int *b);
  void Swap_(double *a,double *b);
  void CalculateDerivativesAtEachThreshold(int);
  void CalculateDerivatives_();  
  double CalculateOptimalK_();
  double ComputeGradientBeyondAndOptimalK_(int);
  double ComputeGradientBeforeAndOptimalK_(int index);
 public:
  void Init(Matrix &train_data_appended,Vector &train_labels, 
	    Vector &query_point_appended, double lambda_reg_const, 
	    ArrayList<index_t> &indices_in_range,
	    ArrayList <double> &smoothing_kernel_values_in_range, 
	    int num_points_in_range, int num_points, Vector &w_1,Vector &w_2);
  
  void PerformLineSearch();

    
  
};

#endif
{'fraction_non_alphanumeric': 0.07823008849557522, 'fraction_numerical': 0.006725663716814159, 'mean_word_length': 3.580226904376013, 'pattern_counts': {'":': 0, '<': 11, '<?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': '137823', 'n_tokens_mistral': 972, 'n_tokens_neox': 888, 'n_words': 276}