sunscreen_math 0.10.0

This crate contains GPU implementations that support the Sunscreen compiler.
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
 * This module contains code for iterating over all combinations of t elements
 * from a set of n elements.
 */
use thiserror::Error;

/**
 * Returns the number of combinations of n choose k.
 * This is the binomial coefficient.
 * https://en.wikipedia.org/wiki/Binomial_coefficient
 *
 * # Arguments
 *
 * * `n` - The number of options to choose from.
 * * `k` - The number of options to choose.
 */
fn number_combinations(n: usize, mut k: usize) -> usize {
    // If we are choosing more options than we have, then there are no
    // combinations.
    if n < k {
        return 0;
    }

    // If we are choosing 0 options, then there is only one combination (the
    // empty set).
    if k == 0 {
        return 1;
    }

    // If we are choosing all the options, then there is only one combination.
    if k == n {
        return 1;
    }

    // The binomial coefficient is symmetric, so we can choose the smaller of
    // the two halves.
    if k > n / 2 {
        k = n - k;
    }

    let mut result = 1;
    for i in 1..=k {
        result = result * (n - k + i) / i;
    }

    result
}

/// Errors that can occur when getting the index of a combination.
#[derive(Debug, Error, PartialEq)]
pub enum CombinationGetIndexError {
    /// The combination provided has the wrong length for the given combination
    /// set.
    #[error("combination must have length {0}, but has length {1}")]
    IncorrectLength(usize, usize),

    /// The combination provided has an element that is too large for the given
    #[error("combination must have elements in the range 0..{0}, but has element {1}")]
    ValueTooLarge(usize, usize),
}

/// Errors that can occur when creating a new combination iterator.
#[derive(Debug, Error, PartialEq)]
pub enum CombinationNewError {
    /// The number of options must be greater than 0.
    #[error("N must be greater than 0")]
    ZeroOptions,

    /// The number of options must be greater than or equal to the number of
    /// options to choose.
    #[error("Combinations must N >= t, but N = {0} and t = {1}")]
    CombinationTooLarge(usize, usize),
}

/**
 * An iterator over all combinations of t elements from a set of n elements.
 */
#[derive(Clone, Debug)]
pub struct Combinations {
    n: usize,
    t: usize,

    // Current combination. We use an Option here so that a user can make a new Combinations
    // to access the non-iterator functions without using the space for the current combination.
    current: Option<Vec<usize>>,
}

impl Combinations {
    /**
     * Creates a new combination iterator.
     */
    pub fn new(n: usize, t: usize) -> Result<Self, CombinationNewError> {
        if n == 0 {
            return Err(CombinationNewError::ZeroOptions);
        }

        if t > n {
            return Err(CombinationNewError::CombinationTooLarge(n, t));
        }

        Ok(Self {
            n,
            t,
            current: None,
        })
    }

    /**
     * Returns the number of combinations in this combination set.
     */
    pub fn number_combinations(&self) -> usize {
        number_combinations(self.n, self.t)
    }

    /**
     * Returns the index of the given combination, where the combinations are
     * ordered lexicographically.
     *
     * # Arguments
     *
     * * `combination` - The combination to get the index of.
     */
    pub fn get_index(&self, combination: &[usize]) -> Result<usize, CombinationGetIndexError> {
        let k = combination.len();

        if k != self.t {
            return Err(CombinationGetIndexError::IncorrectLength(self.t, k));
        }

        let mut index = 0;
        let mut item_in_check = 0;

        let n = self.n - 1;

        // We iterate over the combination, and for each element, we count the
        // number of combinations that come before it.
        for (offset, item) in combination.iter().enumerate() {
            if *item > self.n - 1 {
                return Err(CombinationGetIndexError::ValueTooLarge(self.n - 1, *item));
            }

            let offset = offset + 1;

            while item_in_check < *item {
                index += number_combinations(n - item_in_check, k - offset);
                item_in_check += 1
            }
            item_in_check += 1
        }

        Ok(index)
    }

    /**
     * Returns the combination at the given index, where the combinations are
     * ordered lexicographically.
     *
     * # Arguments
     *
     * * `index` - The index of the combination to get.
     *
     * # Returns
     *
     * * `Some(combination)` - The combination at the given index.
     * * `None` - If the index is out of bounds.
     *
     * See
     * <https://jamesmccaffrey.wordpress.com/2022/06/28/generating-the-mth-lexicographical-element-of-a-combination-using-the-combinadic/>
     * for more details
     */
    pub fn at_index(&self, index: usize) -> Option<Vec<usize>> {
        if index >= number_combinations(self.n, self.t) {
            return None;
        }

        let mut result = Vec::new();
        let mut a = self.n;
        let mut b = self.t;
        let mut x = number_combinations(self.n, self.t) - 1 - index;

        for _ in 0..self.t {
            a -= 1;
            while number_combinations(a, b) > x {
                a -= 1;
            }
            result.push(self.n - 1 - a);
            x -= number_combinations(a, b);
            b -= 1;
        }

        Some(result)
    }
}

impl Iterator for Combinations {
    type Item = Vec<usize>;

    fn next(&mut self) -> Option<Self::Item> {
        // If we have not generated the first combination yet, then we generate
        // it and return.
        if self.current.is_none() {
            let current: Vec<usize> = (0..self.t).collect();

            self.current = Some(current.clone());
            return Some(current);
        }

        // Otherwise, we modify the current combination to get the next one.
        let current = self.current.as_mut().unwrap();

        // We find the first element that is not at its maximum value.
        // The maximum value depends on the position: for example, if we are
        // generating combinations of size 3 from 5 elements, then the maximums
        // are [2, 3, 4].
        let mut i = self.t;
        while i > 0 {
            i -= 1;

            // Is the ith element not at its maximum value?
            if current[i] != self.n - self.t + i {
                break;
            }
        }

        // If the first element is its highest possible value, then we have
        // generated all the combinations.
        if self.t == 0 || i == 0 && current[i] == self.n - self.t {
            return None;
        }

        // Otherwise, we increment the first element that is not at its maximum
        // value, and set all the elements after it to be one more than the
        // previous element.
        current[i] += 1;
        for j in i + 1..self.t {
            current[j] = current[j - 1] + 1;
        }

        Some(current.clone())
    }
}

/**
 * Adds the provided element to the given combination, and increments all elements
 * greater than or equal to the provided element by one. This is useful for generating
 * all the combinations of size t + 1 with the given element from combinations of size t.
 *
 * For example, we can generate all combinations containing the element 1 in lexicographic order
 * with the following code:
 *
 * ```rust
 * use sunscreen_math::combination::{Combinations, insert_element_into_reduced_combination};
 *
 * let n = 4;
 * let t = 3;
 * let chosen_element = 1;
 *
 * // Generate all combinations in n choose t containing 1
 * let mut combinations_with_chosen_element = Vec::new();
 * for combination in Combinations::new(n - 1, t - 1).unwrap() {
 *   let result = insert_element_into_reduced_combination(chosen_element, &combination);
 *   combinations_with_chosen_element.push(result);
 * }
 *
 * assert_eq!(
 *  combinations_with_chosen_element,
 *
 *  // combination missing is vec![0, 2, 3], as it does not contain 1.
 *  vec![
 *   (1, vec![0, 1, 2]),
 *   (1, vec![0, 1, 3]),
 *   (0, vec![1, 2, 3]),
 *  ]);
 * ```
 */
pub fn insert_element_into_reduced_combination(
    element_to_insert: usize,
    combination_without_element: &[usize],
) -> (usize, Vec<usize>) {
    let mut result = Vec::with_capacity(combination_without_element.len() + 1);
    let mut element_has_been_inserted = false;
    let mut index = combination_without_element.len();

    for (i, element) in combination_without_element.iter().enumerate() {
        if !element_has_been_inserted {
            // If we have not inserted the desired element yet, then we need to
            // check if we need to insert them here.
            if *element >= element_to_insert {
                element_has_been_inserted = true;
                index = i;

                result.push(element_to_insert);
                result.push(*element + 1);
            } else {
                // We have not inserted the desired element yet, so we copy over
                // the current element.
                result.push(*element);
            }
        } else {
            // We have already inserted the element, so we just need to
            // increment the rest of the elements.
            result.push(*element + 1)
        }
    }

    // If we never inserted the element, then we need to insert it at the end.
    if !element_has_been_inserted {
        result.push(element_to_insert);
    }

    (index, result)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_combinations_5_choose_3() {
        let combinations = Combinations::new(5, 3).unwrap();
        let result: Vec<_> = combinations.collect();
        assert_eq!(
            result,
            vec![
                vec![0, 1, 2],
                vec![0, 1, 3],
                vec![0, 1, 4],
                vec![0, 2, 3],
                vec![0, 2, 4],
                vec![0, 3, 4],
                vec![1, 2, 3],
                vec![1, 2, 4],
                vec![1, 3, 4],
                vec![2, 3, 4],
            ]
        );
    }

    #[test]
    fn test_combinations_4_choose_2() {
        let combinations = Combinations::new(4, 2).unwrap();
        let result: Vec<_> = combinations.collect();
        assert_eq!(
            result,
            vec![
                vec![0, 1],
                vec![0, 2],
                vec![0, 3],
                vec![1, 2],
                vec![1, 3],
                vec![2, 3],
            ]
        );
    }

    #[test]
    fn test_combinations_are_lexicographically_sorted() {
        let combinations = Combinations::new(5, 3).unwrap();
        let result: Vec<_> = combinations.collect();
        let mut sorted_result = result.clone();
        sorted_result.sort();
        assert_eq!(result, sorted_result);
    }

    #[test]
    fn test_combinations_number_combinations() {
        let combinations = Combinations::new(5, 3).unwrap();
        assert_eq!(combinations.number_combinations(), 10);
    }

    #[test]
    fn test_combinations_get_index() {
        let combinations = Combinations::new(5, 3).unwrap();

        for (index, combination) in combinations.clone().enumerate() {
            assert_eq!(combinations.get_index(&combination).unwrap(), index);
        }
    }

    #[test]
    fn test_combinations_at_index() {
        let combinations = Combinations::new(5, 3).unwrap();

        for (index, combination) in combinations.clone().enumerate() {
            assert_eq!(combinations.at_index(index).unwrap(), combination);
        }
    }

    #[test]
    fn test_combinations_at_index_out_of_bounds() {
        let combinations = Combinations::new(5, 3).unwrap();
        assert_eq!(combinations.at_index(10), None);
    }

    #[test]
    fn test_combinations_get_index_incorrect_length() {
        let combinations = Combinations::new(5, 3).unwrap();
        assert_eq!(
            combinations.get_index(&[0, 1]),
            Err(CombinationGetIndexError::IncorrectLength(3, 2))
        );
    }

    #[test]
    fn test_combinations_get_index_value_too_large() {
        let combinations = Combinations::new(5, 3).unwrap();
        assert_eq!(
            combinations.get_index(&[0, 1, 5]),
            Err(CombinationGetIndexError::ValueTooLarge(4, 5))
        );
    }

    #[test]
    fn test_combinations_new_combination_too_large() {
        let combinations = Combinations::new(5, 6);
        assert_eq!(
            combinations.unwrap_err(),
            CombinationNewError::CombinationTooLarge(5, 6)
        );
    }

    #[test]
    fn test_combinations_new_zero_options() {
        let combinations = Combinations::new(0, 3);
        assert_eq!(combinations.unwrap_err(), CombinationNewError::ZeroOptions);
    }

    #[test]
    fn test_combinations_new_zero_combination_size() {
        let _combinations = Combinations::new(5, 0);
    }

    #[test]
    fn test_insert_element_into_reduced_combination() {
        assert_eq!(
            insert_element_into_reduced_combination(1, &[0, 2, 3]),
            (1, vec![0, 1, 3, 4])
        );

        assert_eq!(
            insert_element_into_reduced_combination(0, &[1, 2, 3]),
            (0, vec![0, 2, 3, 4])
        );

        assert_eq!(
            insert_element_into_reduced_combination(3, &[0, 1, 2]),
            (3, vec![0, 1, 2, 3])
        );
    }
}