<commit_before>//-*- c++ -*-
// A WLS class using Cholesky decomposition and sparse JtJ
// Also stores the sum squared error and can compute the residual

#ifndef __WLS_CHOLESKY_H
#define __WLS_CHOLESKY_H

#include <numerics.h>
#include <SVD.h>
#include <numhelpers.h>
#include <assert.h>
#include <cmath>

namespace CVD {

/// Performs weighted least squares using Cholesky decomposition and sparse JtJ.
/// Much faster (but less robust) than the standard WLS.
/// Also stores the sum squares error and can compute the residual.
/// @param The number of parameters in the system
/// @ingroup gMaths
template <int Size>
class WLSCholesky {
public:
  /// Default constructor
  WLSCholesky(){clear();}
  /// Construct using a given regularisation prior
  WLSCholesky(double prior){clear(prior);}
  /// Copy constructor
  /// @param w The decomposition object to copy
  WLSCholesky(const WLSCholesky &w) {
    my_C_inv=w.my_C_inv;
    my_err=w.my_err;
    my_extra=w.my_extra;
    my_vector=w.my_vector;
  }

  /// Clear all the measurements and apply a constant regularisation term. 
  /// Equates to a prior that says all the parameters are zero with \f$\sigma^2 = \frac{1}{\text{val}}\f$.
  /// @param prior The strength of the prior
  void clear(double prior=0){
    Identity(my_C_inv,prior);
    for(int i=0; i<Size; i++){
      my_vector[i]=0;
    }
    my_err=0;
    my_extra=0;
  }

  /// Applies a constant regularisation term. 
  /// Equates to a prior that says all the parameters are zero with \f$\sigma^2 = \frac{1}{\text{val}}\f$.
  /// @param val The strength of the prior
  void add_prior(double val){
    for(int i=0; i<Size; i++){
      my_C_inv(i,i)+=val;
    }
  }
  
	/// Applies a regularisation term with a different strength for each parameter value. 
	/// Equates to a prior that says all the parameters are zero with \f$\sigma_i^2 = \frac{1}{\text{v}_i}\f$.
	/// @param v The vector of priors
  template<class Accessor>
  void add_prior(const FixedVector<Size,Accessor>& v){
    for(int i=0; i<Size; i++){
      my_C_inv(i,i)+=v[i];
    }
  }

	/// Applies a whole-matrix regularisation term. 
	/// This is the same as adding the \f$m\f$ to the inverse covariance matrix.
	/// @param m The inverse covariance matrix to add
  template<class Accessor>
  void add_prior(const FixedMatrix<Size,Size,Accessor>& m){
    my_C_inv+=m;
  }

	/// Add a single measurement 
	/// @param m The value of the measurement
	/// @param J The Jacobian for the measurement \f$\frac{\partial\text{m}}{\partial\text{param}_i}\f$
	/// @param weight The inverse variance of the measurement (default = 1)
  template<class Accessor><commit_msg>Function to add a prior for a non-zero position<commit_after>//-*- c++ -*-
// A WLS class using Cholesky decomposition and sparse JtJ
// Also stores the sum squared error and can compute the residual

#ifndef __WLS_CHOLESKY_H
#define __WLS_CHOLESKY_H

#include <numerics.h>
#include <SVD.h>
#include <numhelpers.h>
#include <assert.h>
#include <cmath>

namespace CVD {

/// Performs weighted least squares using Cholesky decomposition and sparse JtJ.
/// Much faster (but less robust) than the standard WLS.
/// Also stores the sum squares error and can compute the residual.
/// @param The number of parameters in the system
/// @ingroup gMaths
template <int Size>
class WLSCholesky {
public:
  /// Default constructor
  WLSCholesky(){clear();}
  /// Construct using a given regularisation prior
  WLSCholesky(double prior){clear(prior);}
  /// Copy constructor
  /// @param w The decomposition object to copy
  WLSCholesky(const WLSCholesky &w) {
    my_C_inv=w.my_C_inv;
    my_err=w.my_err;
    my_extra=w.my_extra;
    my_vector=w.my_vector;
  }

  /// Clear all the measurements and apply a constant regularisation term. 
  /// Equates to a prior that says all the parameters are zero with \f$\sigma^2 = \frac{1}{\text{val}}\f$.
  /// @param prior The strength of the prior
  void clear(double prior=0){
    Identity(my_C_inv,prior);
    for(int i=0; i<Size; i++){
      my_vector[i]=0;
    }
    my_err=0;
    my_extra=0;
  }

  /// Applies a constant regularisation term. 
  /// Equates to a prior that says all the parameters are zero with \f$\sigma^2 = \frac{1}{\text{val}}\f$.
  /// @param val The strength of the prior
  void add_prior(double val){
    for(int i=0; i<Size; i++){
      my_C_inv(i,i)+=val;
    }
  }
  
  /// Applies a constant regularisation term about a non-zero postion
  /// Equates to a prior that says the parameters are equal to pos with \f$\sigma^2 = \frac{1}{\text{val}}\f$.
  /// @param val The strength of the prior
  /// @param pos The position
  template<class Accessor>
  void add_prior(double val, const FixedVector<Size,Accessor>& pos){
    for(int i=0; i<Size; i++){
      my_C_inv(i,i)+=val;
    }
    my_vector+=pos*val;
    my_err+=val*(pos*pos);
  }

	/// Applies a regularisation term with a different strength for each parameter value. 
	/// Equates to a prior that says all the parameters are zero with \f$\sigma_i^2 = \frac{1}{\text{v}_i}\f$.
	/// @param v The vector of priors
  template<class Accessor>
  void add_prior(const FixedVector<Size,Accessor>& v){
    for(int i=0; i<Size; i++){
      my_C_inv(i,i)+=v[i];
    }
  }

	/// Applies a whole-matrix regularisation term. 
	/// This is the same as adding the \f$m\f$ to the inverse covariance matrix.
	/// @param m The inverse covariance matrix to add
  template<class Accessor>
  void add_prior(const FixedMatrix<Size,Size,Accessor>& m){
    my_C_inv+=m;
  }

	/// Add a single measurement 
	/// @param m The value of the measurement
	/// @param J The Jacobian for the measurement \f$\frac{\partial\text{m}}{\partial\text{param}_i}\f$
	/// @param weight The inverse variance of the measurement (default = 1)
  template<class Accessor>
{'fraction_non_alphanumeric': 0.1272255834053587, 'fraction_numerical': 0.005358686257562662, 'mean_word_length': 3.2764227642276422, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 27, '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': '1156326', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1888, 'n_words': 645}