Simplify the `power` function by the relevant rules: 1. Power(a, 0) ===> 1 2. Power(a, 1) ===> a 3. Power(a, Log(a, b)) ===> b
(
&self,
args: Vec<Expr>,
info: &SimplifyContext,
)
| 566 | /// 2. Power(a, 1) ===> a |
| 567 | /// 3. Power(a, Log(a, b)) ===> b |
| 568 | fn simplify( |
| 569 | &self, |
| 570 | args: Vec<Expr>, |
| 571 | info: &SimplifyContext, |
| 572 | ) -> Result<ExprSimplifyResult> { |
| 573 | let [base, exponent] = take_function_args("power", args)?; |
| 574 | let base_type = info.get_data_type(&base)?; |
| 575 | let exponent_type = info.get_data_type(&exponent)?; |
| 576 | |
| 577 | // Null propagation |
| 578 | if base_type.is_null() || exponent_type.is_null() { |
| 579 | let return_type = self.return_type(&[base_type, exponent_type])?; |
| 580 | return Ok(ExprSimplifyResult::Simplified(lit( |
| 581 | ScalarValue::Null.cast_to(&return_type)? |
| 582 | ))); |
| 583 | } |
| 584 | |
| 585 | match exponent { |
| 586 | Expr::Literal(value, _) |
| 587 | if value == ScalarValue::new_zero(&exponent_type)? => |
| 588 | { |
| 589 | Ok(ExprSimplifyResult::Simplified(lit(ScalarValue::new_one( |
| 590 | &base_type, |
| 591 | )?))) |
| 592 | } |
| 593 | Expr::Literal(value, _) if value == ScalarValue::new_one(&exponent_type)? => { |
| 594 | Ok(ExprSimplifyResult::Simplified(base)) |
| 595 | } |
| 596 | Expr::ScalarFunction(ScalarFunction { func, mut args }) |
| 597 | if is_log(&func) && args.len() == 2 && base == args[0] => |
| 598 | { |
| 599 | let b = args.pop().unwrap(); // length checked above |
| 600 | Ok(ExprSimplifyResult::Simplified(b)) |
| 601 | } |
| 602 | _ => Ok(ExprSimplifyResult::Original(vec![base, exponent])), |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | fn documentation(&self) -> Option<&Documentation> { |
| 607 | self.doc() |
nothing calls this directly
no test coverage detected