qfall_math/integer/z/arithmetic/exp.rs
1// Copyright © 2023 Marvin Beckmann
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! Implementations to call the exponential function on a [`Z`] integer.
10
11use crate::{
12 integer::Z,
13 rational::{PolyOverQ, Q},
14 traits::Evaluate,
15};
16
17impl Z {
18 /// Computes `e^self` using taylor series approximation of the exponential function.
19 ///
20 /// Parameters:
21 /// - `length_taylor_polynomial`: the length of the taylor series
22 /// approximation of the exponential function
23 ///
24 /// Returns `e^self`.
25 ///
26 /// # Examples
27 /// ```
28 /// use qfall_math::integer::Z;
29 ///
30 /// // sum_{k=0}^999 17^k/k!
31 /// let evaluation = Z::from(17).exp_taylor(1000_u32);
32 /// ```
33 pub fn exp_taylor(&self, length_taylor_polynomial: impl Into<u32>) -> Q {
34 let exp_taylor_series = PolyOverQ::exp_function_taylor(length_taylor_polynomial);
35 exp_taylor_series.evaluate(self)
36 }
37}
38
39#[cfg(test)]
40mod test_exp {
41 use crate::{integer::Z, rational::Q};
42
43 /// Ensure that `0` is returned if the length `0` is provided
44 #[test]
45 fn zero_length() {
46 let z = Z::from(17);
47
48 assert_eq!(Q::default(), z.exp_taylor(0_u32));
49 }
50
51 /// Test correct evaluation for some explicit values
52 #[test]
53 fn ten_length_value() {
54 assert_eq!(Q::from((98641, 36288)), Z::ONE.exp_taylor(10_u32));
55 assert_eq!(Q::from((22471, 1120)), Z::from(3).exp_taylor(10_u32));
56 assert_eq!(Q::from((83, 2240)), Z::from(-3).exp_taylor(10_u32));
57 }
58}