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
use crate::log_polynomial::log_polynomial::LogPolynomial;
use anyhow::Result;
pub trait Log {
/// Returns the 2-logarithm of the given argument: log_2(argument).
/// Returns an error if the argument is not positive.
///
/// This is a potentially expensive operation, as the prime factors of the argument may be computed.
/// May return an error if the argument is too large, that is, for now, cannot be represented by an u128.
///
/// If a multiplication with argument is foreseen, then the n_log_n function is more efficient.
fn log(&self) -> Result<LogPolynomial>
where
Self: Sized;
/// Returns the value argument * log_2(argument).
/// Returns an error if the argument is not positive.
///
/// This is a potentially expensive operation, as the prime factors of the argument may be computed.
/// May return an error if the argument is too large, that is, for now, cannot be represented by an u128.
fn n_log_n(&self) -> Result<LogPolynomial>
where
Self: Sized;
}
impl<T> Log for T
where
LogPolynomial: for<'a> LogOf<&'a T>,
{
fn log(&self) -> Result<LogPolynomial>
where
Self: Sized,
{
LogPolynomial::log_of(self)
}
fn n_log_n(&self) -> Result<LogPolynomial>
where
Self: Sized,
{
LogPolynomial::n_log_n_of(self)
}
}
pub trait LogOf<T> {
/// Returns the 2-logarithm of the given argument: log_2(argument).
/// Returns an error if the argument is not positive.
///
/// This is a potentially expensive operation, as the prime factors of the argument may be computed.
/// May return an error if the argument is too large, that is, for now, cannot be represented by an u128.
///
/// If a multiplication with argument is foreseen, then the n_log_n function is more efficient.
fn log_of(argument: T) -> Result<Self>
where
Self: Sized;
/// Returns the value argument * log_2(argument).
/// Returns an error if the argument is not positive.
///
/// This is a potentially expensive operation, as the prime factors of the argument may be computed.
/// May return an error if the argument is too large, that is, for now, cannot be represented by an u128.
fn n_log_n_of(argument: T) -> Result<Self>
where
Self: Sized;
}