Skip to content

add fixed power unary operation

Reference issue

#1425

What does this implement/fix?

Adds a unary expression for performing a coefficientwise real-valued power operation on an array. If the exponent argument is an integer, the operation is performed using repeated squaring. Otherwise, the operation defers to Eigen's existing vectorized pow routine with a fixed exponent. Both cases are IEEE compliant with respect to error handling.

This MR replaces !1022 (closed) as the functionality is quite different, though the result is the same.

Additional information

Some benchmark measurements are here: $2388067. The speedups for integer exponents are very impressive.

The benchmarks measure the following simple functions for float and double arrays of varying sizes:

template<typename T>                                                                                                                                                                                                                                     
void eigen_powquarter(const Eigen::Matrix<T, Eigen::Dynamic, 1>& v,                                                                                                                                                                                      
             Eigen::Matrix<T, Eigen::Dynamic, 1>* u) {                                                                                                                                                                                                   
  *u = v.array().pow(T(0.25));                                                                                                                                                                                                                           
}                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                         
template<typename T>                                                                                                                                                                                                                                     
void eigen_pow4(const Eigen::Matrix<T, Eigen::Dynamic, 1>& v,                                                                                                                                                                                            
             Eigen::Matrix<T, Eigen::Dynamic, 1>* u) {                                                                                                                                                                                                   
  *u = v.array().pow(int(4));                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                         
template<typename T>                                                                                                                                                                                                                                     
void eigen_pow4float(const Eigen::Matrix<T, Eigen::Dynamic, 1>& v,                                                                                                                                                                                       
             Eigen::Matrix<T, Eigen::Dynamic, 1>* u) {                                                                                                                                                                                                   
  *u = v.array().pow(T(4));                                                                                                                                                                                                                              
}                                       
Edited by Rasmus Munk Larsen

Merge request reports

Loading