vecutils.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //! A module containing some utility functions useful for the runtime
  2. //! processing of vector operations.
  3. use core::iter::Sum;
  4. use std::ops::{Add, Mul, Sub};
  5. /// Add two vectors componentwise
  6. pub fn add_vecs<L, R, P>(left: &[L], right: &[R]) -> Vec<P>
  7. where
  8. L: Add<R, Output = P> + Clone,
  9. R: Clone,
  10. {
  11. left.iter()
  12. .cloned()
  13. .zip(right.iter().cloned())
  14. .map(|(l, r)| l + r)
  15. .collect()
  16. }
  17. /// Add components of a vector by a non-vector
  18. pub fn add_vec_nv<L, R, P>(left: &[L], right: &R) -> Vec<P>
  19. where
  20. L: Add<R, Output = P> + Clone,
  21. R: Clone,
  22. {
  23. left.iter().cloned().map(|l| l + right.clone()).collect()
  24. }
  25. /// Add a non-vector by components of a vector
  26. pub fn add_nv_vec<L, R, P>(left: &L, right: &[R]) -> Vec<P>
  27. where
  28. L: Add<R, Output = P> + Clone,
  29. R: Clone,
  30. {
  31. right.iter().cloned().map(|r| left.clone() + r).collect()
  32. }
  33. /// Subtract two vectors componentwise
  34. pub fn sub_vecs<L, R, P>(left: &[L], right: &[R]) -> Vec<P>
  35. where
  36. L: Sub<R, Output = P> + Clone,
  37. R: Clone,
  38. {
  39. left.iter()
  40. .cloned()
  41. .zip(right.iter().cloned())
  42. .map(|(l, r)| l - r)
  43. .collect()
  44. }
  45. /// Subtract components of a vector by a non-vector
  46. pub fn sub_vec_nv<L, R, P>(left: &[L], right: &R) -> Vec<P>
  47. where
  48. L: Sub<R, Output = P> + Clone,
  49. R: Clone,
  50. {
  51. left.iter().cloned().map(|l| l - right.clone()).collect()
  52. }
  53. /// Subtract a non-vector by components of a vector
  54. pub fn sub_nv_vec<L, R, P>(left: &L, right: &[R]) -> Vec<P>
  55. where
  56. L: Sub<R, Output = P> + Clone,
  57. R: Clone,
  58. {
  59. right.iter().cloned().map(|r| left.clone() - r).collect()
  60. }
  61. /// Multiply two vectors componentwise
  62. pub fn mul_vecs<L, R, P>(left: &[L], right: &[R]) -> Vec<P>
  63. where
  64. L: Mul<R, Output = P> + Clone,
  65. R: Clone,
  66. {
  67. left.iter()
  68. .cloned()
  69. .zip(right.iter().cloned())
  70. .map(|(l, r)| l * r)
  71. .collect()
  72. }
  73. /// Multiply components of a vector by a non-vector
  74. pub fn mul_vec_nv<L, R, P>(left: &[L], right: &R) -> Vec<P>
  75. where
  76. L: Mul<R, Output = P> + Clone,
  77. R: Clone,
  78. {
  79. left.iter().cloned().map(|l| l * right.clone()).collect()
  80. }
  81. /// Multiply a non-vector by components of a vector
  82. pub fn mul_nv_vec<L, R, P>(left: &L, right: &[R]) -> Vec<P>
  83. where
  84. L: Mul<R, Output = P> + Clone,
  85. R: Clone,
  86. {
  87. right.iter().cloned().map(|r| left.clone() * r).collect()
  88. }
  89. /// Add the elements of a vector.
  90. ///
  91. /// This wrapper avoids the problem of the standard
  92. /// [`sum`](Sum#tymethod.sum) function requiring you to explicitly
  93. /// specify the output type. This wrapper gives you whatever type you
  94. /// get by adding two values of type `T` together. `T` must be
  95. /// [`Clone`] because we're adding things of type `T` and not `&T`.
  96. pub fn sum_vec<T, S>(summable: &[T]) -> S
  97. where
  98. T: Add<T, Output = S> + Clone,
  99. S: Sum<T>,
  100. {
  101. summable.iter().cloned().sum()
  102. }