Guest User

Untitled

a guest
Feb 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. extern crate rand;
  2.  
  3. use rand::Rng;
  4. use rand::chacha::ChaChaRng;
  5.  
  6. const PI: f64 = std::f64::consts::PI;
  7.  
  8. fn sin(x: f64) -> f64 {
  9. x.sin()
  10. }
  11.  
  12. fn cos(x: f64) -> f64 {
  13. x.cos()
  14. }
  15.  
  16. pub struct ExpressionBuilder {
  17. rng: ChaChaRng,
  18. }
  19.  
  20. impl ExpressionBuilder {
  21. fn new() -> ExpressionBuilder {
  22. ExpressionBuilder {
  23. rng: ChaChaRng::new_unseeded(),
  24. }
  25. }
  26.  
  27. fn build_expression<'a, T>(&mut self, x: &'a T, y: &'a T, probability: f64) -> &'a T
  28. where &'a T: Evaluate + New
  29. {
  30. self.rng.choose(&[x, y]).unwrap()
  31. }
  32. }
  33.  
  34. pub trait Evaluate {
  35. fn eval(&self, x: f64, y: f64) -> f64;
  36. }
  37.  
  38. pub trait New {
  39. fn new() -> Self;
  40. }
  41.  
  42. pub struct X {
  43. }
  44.  
  45. impl New for X {
  46. fn new() -> X {
  47. X { }
  48. }
  49. }
  50.  
  51. impl Evaluate for X {
  52. fn eval(&self, x: f64, y: f64) -> f64 {
  53. assert!(x >= -1.0 && y <= 1.0);
  54. x
  55. }
  56. }
  57.  
  58. pub struct Y {
  59. }
  60.  
  61. impl New for Y {
  62. fn new() -> Y {
  63. Y { }
  64. }
  65. }
  66.  
  67. impl Evaluate for Y {
  68. fn eval(&self, x: f64, y: f64) -> f64 {
  69. assert!(x >= -1.0 && y <= 1.0);
  70. y
  71. }
  72. }
  73.  
  74. pub struct SinPi {
  75. expression: Box<Evaluate>
  76. }
  77.  
  78. impl SinPi {
  79. pub fn new(expression: Box<Evaluate>) -> SinPi {
  80. SinPi {
  81. expression
  82. }
  83. }
  84. }
  85.  
  86. impl Evaluate for SinPi {
  87. fn eval(&self, x: f64, y: f64) -> f64 {
  88. assert!(x >= -1.0 && y <= 1.0);
  89.  
  90. sin(PI * self.expression.eval(x, y))
  91. }
  92. }
  93.  
  94. pub struct CosPi {
  95. expression: Box<Evaluate>
  96. }
  97.  
  98. impl CosPi {
  99. pub fn new(expression: Box<Evaluate>) -> CosPi {
  100. CosPi {
  101. expression
  102. }
  103. }
  104. }
  105.  
  106. impl Evaluate for CosPi {
  107. fn eval(&self, x: f64, y: f64) -> f64 {
  108. assert!(x >= -1.0 && y <= 1.0);
  109.  
  110. cos(PI * self.expression.eval(x, y))
  111. }
  112. }
  113.  
  114. pub struct Product {
  115. a: Box<Evaluate>,
  116. b: Box<Evaluate>,
  117. }
  118.  
  119. impl Product {
  120. pub fn new(a: Box<Evaluate>, b: Box<Evaluate>) -> Product {
  121. Product {
  122. a,
  123. b,
  124. }
  125. }
  126. }
  127.  
  128. impl Evaluate for Product {
  129. fn eval(&self, x: f64, y: f64) -> f64 {
  130. assert!(x >= -1.0 && y <= 1.0);
  131.  
  132. self.a.eval(x, y) * self.b.eval(x, y)
  133. }
  134. }
  135.  
  136. #[cfg(test)]
  137. mod tests {
  138. use super::*;
  139.  
  140. #[test]
  141. fn eval_x() {
  142. let x = X::new();
  143. assert_eq!(x.eval(-1.0, 1.0), -1.0);
  144. }
  145.  
  146. #[test]
  147. fn eval_y() {
  148. let y = Y::new();
  149. assert_eq!(y.eval(-1.0, 1.0), 1.0);
  150. }
  151. }
Add Comment
Please, Sign In to add comment