Advertisement
mwchase

LC:NN upload 3 - node_defs.rs

Mar 19th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 6.37 KB | None | 0 0
  1. use std::collections::hash_map::DefaultHasher;
  2. use std::hash::{Hash, Hasher};
  3. use std::cmp::Ordering;
  4. use std::sync::Arc;
  5.  
  6. use num::{Integer, Signed};
  7.  
  8. pub type Container<T> = Arc<T>;
  9.  
  10. pub fn container<T>(data: T) -> Container<T> {
  11.     Arc::new(data)
  12. }
  13.  
  14. pub trait NodeData {
  15.     type Constant: Hash + Ord + Clone + Signed;
  16.     type Exponent: Hash + Clone + Integer;
  17.     type Input: Hash + Ord + Clone;
  18.     type SystemVariable: Hash + Ord + Clone;
  19.     type Parameter: Hash + Ord + Clone;
  20. }
  21.  
  22. pub struct Sum<T: NodeData> {
  23.     pub pre_hash: u64, //I would like these fields to be private if possible.
  24.     pub minus: bool,
  25.     pub constant: T::Constant,
  26.     pub terms: Vec<Container<Product<T>>>,
  27. }
  28.  
  29. pub struct Product<T: NodeData> {
  30.     pub pre_hash: u64, //I would like these fields to be private if possible.
  31.     pub coefficient: T::Constant,
  32.     pub powers: Vec<Container<Power<T>>>,
  33. }
  34.  
  35. pub struct Power<T: NodeData> {
  36.     pub exponent: T::Exponent,
  37.     pub primitive: Container<Primitive<T>>,
  38. }
  39.  
  40. pub enum Primitive<T: NodeData> {
  41.     Input(T::Input),
  42.     SystemVariable(T::SystemVariable),
  43.     Parameter(T::Parameter),
  44.     Sigmoid(bool, Container<Sum<T>>),
  45. }
  46.  
  47. impl<T: NodeData> PartialOrd for Sum<T> {
  48.     fn partial_cmp(&self, other: &Sum<T>) -> Option<Ordering> {
  49.         Some(self.cmp(other))
  50.     }
  51. }
  52. impl<T: NodeData> PartialOrd for Product<T> {
  53.     fn partial_cmp(&self, other: &Product<T>) -> Option<Ordering> {
  54.         Some(self.cmp(other))
  55.     }
  56. }
  57. impl<T: NodeData> PartialOrd for Power<T> {
  58.     fn partial_cmp(&self, other: &Power<T>) -> Option<Ordering> {
  59.         Some(self.cmp(other))
  60.     }
  61. }
  62. impl<T: NodeData> PartialOrd for Primitive<T> {
  63.     fn partial_cmp(&self, other: &Primitive<T>) -> Option<Ordering> {
  64.         Some(self.cmp(other))
  65.     }
  66. }
  67.  
  68. impl<T: NodeData> PartialEq for Sum<T> {
  69.     fn eq(&self, other: &Sum<T>) -> bool {
  70.         self.minus == other.minus && self.constant == other.constant && self.terms == other.terms
  71.     }
  72. }
  73.  
  74. impl<T: NodeData> PartialEq for Product<T> {
  75.     fn eq(&self, other: &Product<T>) -> bool {
  76.         self.coefficient == other.coefficient && self.powers == other.powers
  77.     }
  78. }
  79.  
  80. impl<T: NodeData> PartialEq for Power<T> {
  81.     fn eq(&self, other: &Power<T>) -> bool {
  82.         self.exponent == other.exponent && self.primitive == other.primitive
  83.     }
  84. }
  85.  
  86. impl<T: NodeData> PartialEq for Primitive<T> {
  87.     fn eq(&self, other: &Primitive<T>) -> bool {
  88.         match (self, other) {
  89.             (&Primitive::Input(ref a), &Primitive::Input(ref b)) => a == b,
  90.             (&Primitive::SystemVariable(ref a), &Primitive::SystemVariable(ref b)) => a == b,
  91.             (&Primitive::Parameter(ref a), &Primitive::Parameter(ref b)) => a == b,
  92.             (&Primitive::Sigmoid(minus_a, ref a), &Primitive::Sigmoid(minus_b, ref b)) => {
  93.                 minus_a == minus_b && a == b
  94.             }
  95.             _ => false,
  96.         }
  97.     }
  98. }
  99.  
  100. impl<T: NodeData> Eq for Sum<T> {}
  101. impl<T: NodeData> Eq for Product<T> {}
  102. impl<T: NodeData> Eq for Power<T> {}
  103. impl<T: NodeData> Eq for Primitive<T> {}
  104.  
  105. impl<T: NodeData> Hash for Sum<T> {
  106.     fn hash<H: Hasher>(&self, state: &mut H) {
  107.         self.pre_hash.hash(state);
  108.     }
  109. }
  110.  
  111. impl<T: NodeData> Hash for Product<T> {
  112.     fn hash<H: Hasher>(&self, state: &mut H) {
  113.         self.pre_hash.hash(state);
  114.     }
  115. }
  116.  
  117. impl<T: NodeData> Hash for Power<T> {
  118.     fn hash<H: Hasher>(&self, state: &mut H) {
  119.         self.exponent.hash(state);
  120.         self.primitive.hash(state);
  121.     }
  122. }
  123.  
  124. impl<T: NodeData> Hash for Primitive<T> {
  125.     fn hash<H: Hasher>(&self, state: &mut H) {
  126.         match self {
  127.             &Primitive::Input(ref a) => {
  128.                 "Input".hash(state);
  129.                 a.hash(state)
  130.             }
  131.             &Primitive::SystemVariable(ref a) => {
  132.                 "SystemVariable".hash(state);
  133.                 a.hash(state)
  134.             }
  135.             &Primitive::Parameter(ref a) => {
  136.                 "Parameter".hash(state);
  137.                 a.hash(state)
  138.             }
  139.             &Primitive::Sigmoid(minus, ref a) => {
  140.                 "Sigmoid".hash(state);
  141.                 minus.hash(state);
  142.                 a.hash(state)
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148. impl<T: NodeData> Clone for Sum<T> {
  149.     fn clone(&self) -> Sum<T> {
  150.         Sum {
  151.             pre_hash: self.pre_hash.clone(),
  152.             minus: self.minus.clone(),
  153.             constant: self.constant.clone(),
  154.             terms: self.terms.clone(),
  155.         }
  156.     }
  157. }
  158.  
  159. impl<T: NodeData> Clone for Product<T> {
  160.     fn clone(&self) -> Product<T> {
  161.         Product {
  162.             pre_hash: self.pre_hash.clone(),
  163.             coefficient: self.coefficient.clone(),
  164.             powers: self.powers.clone(),
  165.         }
  166.     }
  167. }
  168.  
  169. impl<T: NodeData> Clone for Power<T> {
  170.     fn clone(&self) -> Power<T> {
  171.         Power {
  172.             exponent: self.exponent.clone(),
  173.             primitive: self.primitive.clone(),
  174.         }
  175.     }
  176. }
  177.  
  178. impl<T: NodeData> Clone for Primitive<T> {
  179.     fn clone(&self) -> Primitive<T> {
  180.         match self {
  181.             &Primitive::Input(ref a) => Primitive::Input(a.clone()),
  182.             &Primitive::SystemVariable(ref a) => Primitive::SystemVariable(a.clone()),
  183.             &Primitive::Parameter(ref a) => Primitive::Parameter(a.clone()),
  184.             &Primitive::Sigmoid(minus, ref a) => Primitive::Sigmoid(minus, a.clone()),
  185.         }
  186.     }
  187. }
  188.  
  189. impl<T: NodeData> Sum<T> {
  190.     pub fn new(minus: bool, constant: T::Constant, terms: Vec<Container<Product<T>>>) -> Sum<T> {
  191.         let mut s = DefaultHasher::new();
  192.         minus.hash(&mut s);
  193.         constant.hash(&mut s);
  194.         terms.hash(&mut s);
  195.         Sum {
  196.             pre_hash: s.finish(),
  197.             minus: minus,
  198.             constant: constant,
  199.             terms: terms,
  200.         }
  201.     }
  202. }
  203.  
  204. impl<T: NodeData> Product<T> {
  205.     pub fn new(coefficient: T::Constant, powers: Vec<Container<Power<T>>>) -> Product<T> {
  206.         let mut s = DefaultHasher::new();
  207.         coefficient.hash(&mut s);
  208.         powers.hash(&mut s);
  209.         Product {
  210.             pre_hash: s.finish(),
  211.             coefficient: coefficient,
  212.             powers: powers,
  213.         }
  214.     }
  215. }
  216.  
  217. impl<T: NodeData> Power<T> {
  218.     pub fn new(exponent: T::Exponent, primitive: Container<Primitive<T>>) -> Power<T> {
  219.         Power {
  220.             exponent: exponent,
  221.             primitive: primitive,
  222.         }
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement