File tree Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Expand file tree Collapse file tree 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -101,10 +101,17 @@ inline double inv_Phi_lambda(double p) {
101
101
pre_mult = q < 0 ? -1 : 1 ;
102
102
}
103
103
104
- Eigen::VectorXd r_pow = pow (inner_r, Eigen::ArrayXd::LinSpaced (8 , 0 , 7 )) / 10.0 ;
105
- Eigen::Map<const Eigen::VectorXd> numerator_map (num_ptr, 8 );
106
- Eigen::Map<const Eigen::VectorXd> denonimator_map (den_ptr, 8 );
107
- return pre_mult * (numerator_map.dot (r_pow) * 10.0 ) / (denonimator_map.dot (r_pow) * 10.0 );
104
+ // As computation requires evaluating r^8, this causes a loss of precision,
105
+ // even when using log space. We can mitigate this by scaling the
106
+ // exponentiated result (dividing by 10), since the same scaling is applied
107
+ // to the numerator and denominator.
108
+ Eigen::VectorXd log_r_pow = Eigen::ArrayXd::LinSpaced (8 , 0 , 7 ) * log (inner_r)
109
+ - LOG_TEN;
110
+ Eigen::Map<const Eigen::VectorXd> num_map (num_ptr, 8 );
111
+ Eigen::Map<const Eigen::VectorXd> den_map (den_ptr, 8 );
112
+ double log_result = log_sum_exp (log_r_pow + log (num_map))
113
+ - log_sum_exp (log_r_pow + log (den_map));
114
+ return pre_mult * exp (log_result);
108
115
}
109
116
} // namespace internal
110
117
You can’t perform that action at this time.
0 commit comments