예제 #1
0
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    operator/(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& v1,
              const var<AutodiffOrder, StrictSmoothness, ValidateIO>& v2) {

    if (ValidateIO) {
      validate_input(v1.first_val(), "operator/");
      validate_input(v2.first_val(), "operator/");
    }
      
    const short partials_order = 3;
    const unsigned int n_inputs = 2;
    
    create_node<binary_var_node<AutodiffOrder, partials_order>>(n_inputs);
    
    double x = v1.first_val();
    double y_inv = 1.0 / v2.first_val();
    double val = x * y_inv;
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(val);
    } catch (nomad_error) {
      throw nomad_output_value_error("operator/");
    }
      
    push_inputs(v1.dual_numbers());
    push_inputs(v2.dual_numbers());
    
    double y_inv_n = y_inv * y_inv;
    
    if (AutodiffOrder >= 1) {
      push_partials<ValidateIO>(y_inv);
      push_partials<ValidateIO>(- val * y_inv);
    }
    if (AutodiffOrder >= 2) {
      push_partials<ValidateIO>(0);
      push_partials<ValidateIO>(-y_inv_n);
      push_partials<ValidateIO>(2 * val * y_inv_n);
    }
    if (AutodiffOrder >= 3) {
      y_inv_n *= y_inv;
      push_partials<ValidateIO>(0);
      push_partials<ValidateIO>(0);
      push_partials<ValidateIO>(2 * y_inv_n);
      push_partials<ValidateIO>(-6 * val * y_inv_n);
    }
    
    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }
예제 #2
0
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    binary_prod_cubes(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& v1,
                      const var<AutodiffOrder, StrictSmoothness, ValidateIO>& v2) {

    if (ValidateIO) {
      validate_input(v1.first_val(), "binary_prod_cubes");
      validate_input(v2.first_val(), "binary_prod_cubes");
    }
      
    const short partials_order = 3;
    const unsigned int n_inputs = 2;
    
    create_node<binary_var_node<AutodiffOrder, partials_order>>(n_inputs);

    double x = v1.first_val();
    double y = v2.first_val();
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(binary_prod_cubes(x, y));
    } catch (nomad_error) {
      throw nomad_output_value_error("binary_prod_cubes");
    }
      
    push_inputs(v1.dual_numbers());
    push_inputs(v2.dual_numbers());
    
    try {
      if (AutodiffOrder >= 1) {
        push_partials<ValidateIO>(3 * x * x * y * y * y);
        push_partials<ValidateIO>(3 * x * x * x * y * y);
      }
      if (AutodiffOrder >= 2) {
        push_partials<ValidateIO>(6 * x * y * y * y);
        push_partials<ValidateIO>(9 * x * x * y * y);
        push_partials<ValidateIO>(6 * x * x * x * y);
      }
      if (AutodiffOrder >= 3) {
        push_partials<ValidateIO>(6 * y * y * y);
        push_partials<ValidateIO>(18 * x * y * y);
        push_partials<ValidateIO>(18 * x * x * y);
        push_partials<ValidateIO>(6 * x * x * x);
      }
    } catch (nomad_error) {
      throw nomad_output_partial_error("binary_prod_cubes");
    }
      
    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }
예제 #3
0
 inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
   operator/(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& v1,
             double y) {
   
   if (ValidateIO) {
     validate_input(v1.first_val(), "operator/");
     validate_input(y, "operator/");
   }
     
   const short partials_order = 1;
   const unsigned int n_inputs = 1;
   
   create_node<unary_var_node<AutodiffOrder, partials_order>>(n_inputs);
   
   double x = v1.first_val();
   double y_inv = 1.0 / y;
   
   try {
     push_dual_numbers<AutodiffOrder, ValidateIO>(x * y_inv);
   } catch (nomad_error) {
     throw nomad_output_value_error("operator/");
   }
     
   push_inputs(v1.dual_numbers());
   
   if (AutodiffOrder >= 1) push_partials<ValidateIO>(y_inv);
   
   return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
   
 }
예제 #4
0
파일: lgamma.hpp 프로젝트: stan-dev/nomad
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    lgamma(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& input) {
    
    if (ValidateIO) validate_input(input.first_val(), "lgamma");
      
    const short partials_order = 3;
    const unsigned int n_inputs = 1;
    
    create_node<unary_var_node<AutodiffOrder, partials_order>>(n_inputs);

    double val = input.first_val();
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(lgamma(val));
    } catch (nomad_error) {
      throw nomad_output_value_error("lgamma");
    }
      
    push_inputs(input.dual_numbers());
    
    try {
      if (AutodiffOrder >= 1) push_partials<ValidateIO>(digamma(val));
      if (AutodiffOrder >= 2) push_partials<ValidateIO>(trigamma(val));
      if (AutodiffOrder >= 3) push_partials<ValidateIO>(quadrigamma(val));
    } catch (nomad_error) {
      throw nomad_output_partial_error("lgamma");
    }
      
    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }
예제 #5
0
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    inv_logit(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& input) {
    
    if (ValidateIO) validate_input(input.first_val(), "inv_logit");
      
    const short partials_order = 3;
    const unsigned int n_inputs = 1;
    
    create_node<unary_var_node<AutodiffOrder, partials_order>>(n_inputs);

    double s = inv_logit(input.first_val());
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(s);
    } catch (nomad_error) {
      throw nomad_output_value_error("inv_logit");
    }
      
    push_inputs(input.dual_numbers());
    
    double ds = s * (1 - s);
    
    try {
      if (AutodiffOrder >= 1) push_partials<ValidateIO>(ds);
      if (AutodiffOrder >= 2) push_partials<ValidateIO>(ds * (1 - 2 * s) );
      if (AutodiffOrder >= 3) push_partials<ValidateIO>(ds * (1 - 6 * s * (1 - s)) );
    } catch (nomad_error) {
      throw nomad_output_partial_error("inv_logit");
    }
      
    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }
예제 #6
0
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    operator++(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& v1, int /* dummy */) {

    if (ValidateIO) validate_input(v1.first_val(), "operator++");
      
    create_node<unary_plus_var_node<AutodiffOrder>>(1);
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(v1.first_val() + 1.0);
    } catch (nomad_error) {
      throw nomad_output_value_error("operator++");
    }
      
    push_inputs(v1.dual_numbers());
    
    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }
예제 #7
0
파일: log2.hpp 프로젝트: stan-dev/nomad
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    log2(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& input) {
    
    if (ValidateIO) {
      double val = input.first_val();
      validate_input(val, "log2");
      validate_lower_bound(val, 0, "log2");
    }
      
    const short partials_order = 3;
    const unsigned int n_inputs = 1;
    
    create_node<unary_var_node<AutodiffOrder, partials_order>>(n_inputs);

    double val = input.first_val();
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(log2(val));
    } catch (nomad_error) {
      throw nomad_output_value_error("log2");
    }
      
    push_inputs(input.dual_numbers());
    
    double val_inv = 1.0 / val;
    
    try {
      if (AutodiffOrder >= 1) push_partials<ValidateIO>(val = val_inv * 1.44269504088896);
      if (AutodiffOrder >= 2) push_partials<ValidateIO>(val *= - val_inv);
      if (AutodiffOrder >= 3) push_partials<ValidateIO>(val *= - 2.0 * val_inv);
    } catch (nomad_error) {
      throw nomad_output_partial_error("log2");
    }
      
    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }
예제 #8
0
파일: sqrt.hpp 프로젝트: stan-dev/nomad
  inline var<AutodiffOrder, StrictSmoothness, ValidateIO>
    sqrt(const var<AutodiffOrder, StrictSmoothness, ValidateIO>& input) {
    
    if (ValidateIO) {
      double val = input.first_val();
      validate_input(val, "sqrt");
      validate_lower_bound(val, 0, "sqrt");
    }
      
    const short partials_order = 3;
    const unsigned int n_inputs = 1;
    
    create_node<unary_var_node<AutodiffOrder, partials_order>>(n_inputs);

    double val = std::sqrt(input.first_val());
    
    try {
      push_dual_numbers<AutodiffOrder, ValidateIO>(val);
    } catch (nomad_error) {
      throw nomad_output_value_error("sqrt");
    }
    
    push_inputs(input.dual_numbers());
    
    double d2 = 1.0 / input.first_val();
    
    try {
      if (AutodiffOrder >= 1) push_partials<ValidateIO>(val *= 0.5 * d2);
      if (AutodiffOrder >= 2) push_partials<ValidateIO>(val *= - 0.5 * d2);
      if (AutodiffOrder >= 3) push_partials<ValidateIO>(val *= - 1.5 * d2);
    } catch (nomad_error) {
      throw nomad_output_partial_error("sqrt");
    }

    return var<AutodiffOrder, StrictSmoothness, ValidateIO>(next_node_idx_ - 1);
    
  }