1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::common::consts::FOUR;
use crate::common::consts::ONE;
use crate::common::consts::THREE;
use crate::common::util::get_add_cost;
use crate::common::util::get_mul_cost;
use crate::num::BigFloatNumber;
use crate::defs::RoundingMode;
use crate::defs::Error;
use crate::ops::series::PolycoeffGen;
use crate::ops::series::ArgReductionEstimator;
use crate::ops::series::series_run;
use crate::ops::series::series_cost_optimize;
struct SinhPolycoeffGen {
one_full_p: BigFloatNumber,
inc: BigFloatNumber,
fct: BigFloatNumber,
iter_cost: usize,
}
impl SinhPolycoeffGen {
fn new(p: usize) -> Result<Self, Error> {
let inc = BigFloatNumber::from_word(1, 1)?;
let fct = BigFloatNumber::from_word(1, p)?;
let one_full_p = BigFloatNumber::from_word(1, p)?;
let iter_cost = (get_mul_cost(p) + get_add_cost(p)) << 1; Ok(SinhPolycoeffGen {
one_full_p,
inc,
fct,
iter_cost,
})
}
}
impl PolycoeffGen for SinhPolycoeffGen {
fn next(&mut self, rm: RoundingMode) -> Result<&BigFloatNumber, Error> {
self.inc = self.inc.add(&ONE, rm)?;
let inv_inc = self.one_full_p.div(&self.inc, rm)?;
self.fct = self.fct.mul(&inv_inc, rm)?;
self.inc = self.inc.add(&ONE, rm)?;
let inv_inc = self.one_full_p.div(&self.inc, rm)?;
self.fct = self.fct.mul(&inv_inc, rm)?;
Ok(&self.fct)
}
#[inline]
fn get_iter_cost(&self) -> usize {
self.iter_cost
}
}
struct SinhArgReductionEstimator {}
impl ArgReductionEstimator for SinhArgReductionEstimator {
fn get_reduction_cost(n: usize, p: usize) -> usize {
let cost_mul = get_mul_cost(p);
let cost_add = get_add_cost(p);
(n * ((cost_mul << 1) + cost_add )) << 1
}
#[inline]
fn reduction_effect(n: usize, m: isize) -> usize {
(n as isize*1000/631 + m) as usize
}
}
impl BigFloatNumber {
pub fn sinh(&self, rm: RoundingMode) -> Result<Self, Error> {
let arg = self.clone()?;
let mut ret = arg.sinh_series(RoundingMode::None)?;
ret.set_precision(self.get_mantissa_max_bit_len(), rm)?;
Ok(ret)
}
pub(super) fn sinh_series(mut self, rm: RoundingMode) -> Result<Self, Error> {
let p = self.get_mantissa_max_bit_len();
let mut polycoeff_gen = SinhPolycoeffGen::new(p)?;
let (reduction_times, niter) = series_cost_optimize::<SinhPolycoeffGen, SinhArgReductionEstimator>(
p, &polycoeff_gen, (-self.e) as isize, 2, false);
self.set_precision(self.get_mantissa_max_bit_len() + 1 + reduction_times * 3, rm)?;
let arg = if reduction_times > 0 {
self.sinh_arg_reduce(reduction_times, rm)?
} else {
self
};
let acc = arg.clone()?; let x_step = arg.mul(&arg, rm)?; let x_first = arg.mul(&x_step, rm)?; let ret = series_run(acc, x_first, x_step, niter, &mut polycoeff_gen, rm)?;
if reduction_times > 0 {
ret.sinh_arg_restore(reduction_times, rm)
} else {
Ok(ret)
}
}
fn sinh_arg_reduce(&self, n: usize, rm: RoundingMode) -> Result<Self, Error> {
let mut ret = self.clone()?;
for _ in 0..n {
ret = ret.div(&THREE, rm)?;
}
Ok(ret)
}
fn sinh_arg_restore(&self, n: usize, rm: RoundingMode) -> Result<Self, Error> {
let mut sinh = self.clone()?;
for _ in 0..n {
let mut sinh_cub = sinh.mul(&sinh, rm)?;
sinh_cub = sinh_cub.mul(&sinh, rm)?;
let p1 = sinh.mul(&THREE, rm)?;
let p2 = sinh_cub.mul(&FOUR, rm)?;
sinh = p1.add(&p2, rm)?;
}
Ok(sinh)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sinh() {
let rm = RoundingMode::ToEven;
let mut n1 = BigFloatNumber::from_word(1,32000).unwrap();
n1.set_exponent(0);
let _n2 = n1.sinh(rm).unwrap();
}
#[ignore]
#[test]
#[cfg(feature="std")]
fn sinh_perf() {
let mut n = vec![];
for _ in 0..100 {
n.push(BigFloatNumber::random_normal(32000, -0, -0).unwrap());
}
for _ in 0..5 {
let start_time = std::time::Instant::now();
for ni in n.drain(..) {
let _f = ni.sinh_series(RoundingMode::ToEven).unwrap();
}
let time = start_time.elapsed();
println!("{}", time.as_millis());
}
}
}