-
Notifications
You must be signed in to change notification settings - Fork 291
/
logspace.rs
168 lines (144 loc) · 4.04 KB
/
logspace.rs
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
// Copyright 2014-2016 bluss and ndarray developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use num_traits::Float;
/// An iterator of a sequence of logarithmically evenly spaced floats.
///
/// Iterator element type is `F`.
pub struct Logspace<F> {
current: F,
last: F,
step: F,
index: usize,
len: usize,
}
impl<F> Iterator for Logspace<F>
where
F: Float,
{
type Item = F;
#[inline]
fn next(&mut self) -> Option<F> {
if self.index >= self.len {
None
} else {
self.index += 1;
let v = self.current;
self.current = self.current * self.step;
Some(v)
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let n = self.len - self.index;
(n, Some(n))
}
}
impl<F> DoubleEndedIterator for Logspace<F>
where
F: Float,
{
#[inline]
fn next_back(&mut self) -> Option<F> {
if self.index >= self.len {
None
} else {
self.len -= 1;
let v = self.last;
self.last = self.last / self.step;
Some(v)
}
}
}
impl<F> ExactSizeIterator for Logspace<F> where Logspace<F>: Iterator {}
/// Return an iterator of logarithmically evenly spaced floats.
///
/// The `Logspace` has `n` elements, where the first element is `a` and the last
/// element is `b`.
///
/// The sign of `a` and `b` must be the same so that the interval does not
/// include 0.
///
/// Iterator element type is `F`, where `F` must be either `f32` or `f64`.
#[inline]
pub fn logspace<F>(a: F, b: F, n: usize) -> Logspace<F>
where
F: Float,
{
assert!(
a != F::zero() && b != F::zero(),
"Start and/or end of logspace cannot be zero.",
);
assert!(
a.is_sign_negative() == b.is_sign_negative(),
"Logarithmic interval cannot cross 0."
);
let log_a = a.abs().ln();
let log_b = b.abs().ln();
let step = if n > 1 {
let nf = F::from(n).unwrap();
((log_b - log_a) / (nf - F::one())).exp()
} else {
F::one()
};
Logspace {
current: a,
last: b,
step: step,
index: 0,
len: n,
}
}
#[cfg(test)]
mod tests {
use super::logspace;
use crate::{arr1, Array1};
#[test]
fn valid() {
let array: Array1<_> = logspace(1e0, 1e3, 4).collect();
assert!(array.all_close(&arr1(&[1e0, 1e1, 1e2, 1e3]), 1e-5));
let array: Array1<_> = logspace(-1e3, -1e0, 4).collect();
assert!(array.all_close(&arr1(&[-1e3, -1e2, -1e1, -1e0]), 1e-5));
}
#[test]
fn iter_forward() {
let mut iter = logspace(1.0f64, 1e3, 4);
assert!(iter.size_hint() == (4, Some(4)));
assert!((iter.next().unwrap() - 1e0).abs() < 1e-5);
assert!((iter.next().unwrap() - 1e1).abs() < 1e-5);
assert!((iter.next().unwrap() - 1e2).abs() < 1e-5);
assert!((iter.next().unwrap() - 1e3).abs() < 1e-5);
assert!(iter.next().is_none());
assert!(iter.size_hint() == (0, Some(0)));
}
#[test]
fn iter_backward() {
let mut iter = logspace(1.0f64, 1e3, 4);
assert!(iter.size_hint() == (4, Some(4)));
assert!((iter.next_back().unwrap() - 1e3).abs() < 1e-5);
assert!((iter.next_back().unwrap() - 1e2).abs() < 1e-5);
assert!((iter.next_back().unwrap() - 1e1).abs() < 1e-5);
assert!((iter.next_back().unwrap() - 1e0).abs() < 1e-5);
assert!(iter.next_back().is_none());
assert!(iter.size_hint() == (0, Some(0)));
}
#[test]
#[should_panic]
fn zero_lower() {
logspace(0.0, 1.0, 4);
}
#[test]
#[should_panic]
fn zero_upper() {
logspace(1.0, 0.0, 4);
}
#[test]
#[should_panic]
fn zero_included() {
logspace(-1.0, 1.0, 4);
}
}