Class: SY::Composition
- Inherits:
-
Hash
- Object
- Hash
- SY::Composition
- Defined in:
- lib/sy/composition.rb
Overview
Composition of quantities.
Constant Summary collapse
- SR =
Simplification rules for quantity combinations.
SIMPLIFICATION_RULES = []
- QUANTITY_TABLE =
Cache for quantity construction.
Hash.new { |
Class Method Summary collapse
Instance Method Summary collapse
-
#*(number) ⇒ Object
Multiplication by a number.
-
#+(other) ⇒ Object
Merges two compositions.
-
#+@ ⇒ Object
Returns a new instance with same hash.
-
#-(other) ⇒ Object
Subtracts two compositions.
-
#-@ ⇒ Object
Negates hash exponents.
-
#/(number) ⇒ Object
Division by a number.
-
#atomic? ⇒ Boolean
Atomic compositions are singular compositions, whose quantity dimension is a base dimension.
-
#dimension ⇒ Object
Dimension of a quantity composition is the sum of its dimensions.
-
#expand ⇒ Object
Try to simplify the composition by decomposing its quantities.
-
#infer_mapping ⇒ Object
Infers the mapping of the composition’s quantity.
-
#irreducible? ⇒ Boolean
Whether it is possible to expand this.
-
#new_quantity(args = {}) ⇒ Object
Directly (without attempts to simplify) creates a new quantity from self.
-
#simple? ⇒ Boolean
Simple composition is one that is either empty or singular.
-
#simplify ⇒ Object
Simplifies a quantity hash by applying simplification rules.
-
#singular? ⇒ Boolean
Singular compositions consist of only one quantity.
-
#to_quantity(args = {}) ⇒ Object
Returns the quantity appropriate to this composition.
Class Method Details
.empty ⇒ Object
61 62 63 |
# File 'lib/sy/composition.rb', line 61 def empty self[] end |
.singular(quantity) ⇒ Object
57 58 59 |
# File 'lib/sy/composition.rb', line 57 def singular quantity self[ SY.Quantity( quantity ) => 1 ] end |
Instance Method Details
#*(number) ⇒ Object
Multiplication by a number.
135 136 137 |
# File 'lib/sy/composition.rb', line 135 def * number self.class[ self.with_values do |v| v * number end ] end |
#+(other) ⇒ Object
Merges two compositions.
123 124 125 |
# File 'lib/sy/composition.rb', line 123 def + other self.class[ self.merge( other ) { |_, v1, v2| v1 + v2 } ] end |
#+@ ⇒ Object
Returns a new instance with same hash.
111 112 113 |
# File 'lib/sy/composition.rb', line 111 def +@ self.class[ self ] end |
#-(other) ⇒ Object
Subtracts two compositions.
129 130 131 |
# File 'lib/sy/composition.rb', line 129 def - other self + -other end |
#-@ ⇒ Object
Negates hash exponents.
117 118 119 |
# File 'lib/sy/composition.rb', line 117 def -@ self.class[ self.with_values do |v| -v end ] end |
#/(number) ⇒ Object
Division by a number.
141 142 143 144 145 146 147 |
# File 'lib/sy/composition.rb', line 141 def / number self.class[ self.with_values do |val| raise TErr, "Compositions with rational exponents " + "not implemented!" if val % number != 0 val / number end ] end |
#atomic? ⇒ Boolean
Atomic compositions are singular compositions, whose quantity dimension is a base dimension.
105 106 107 |
# File 'lib/sy/composition.rb', line 105 def atomic? singular? && first[0].dimension.base? end |
#dimension ⇒ Object
Dimension of a quantity composition is the sum of its dimensions.
175 176 177 |
# File 'lib/sy/composition.rb', line 175 def dimension map { |qnt, exp| qnt.dimension * exp }.reduce SY::Dimension.zero, :+ end |
#expand ⇒ Object
Try to simplify the composition by decomposing its quantities.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/sy/composition.rb', line 203 def return self if irreducible? puts "#expand: #{self} not irreducible" if SY::DEBUG self.class[ reduce( self.class.empty ) { |c |
#infer_mapping ⇒ Object
Infers the mapping of the composition’s quantity. (Mapping defines mapping of a quantity to the standard quantity of its dimension.)
182 183 184 185 186 187 188 |
# File 'lib/sy/composition.rb', line 182 def infer_mapping puts "#infer_mapping; hash is #{self}" if SY::DEBUG map do |qnt, exp| qnt.standardish? ? SY::Mapping.identity : qnt.mapping_to( qnt.standard ) ** exp end.reduce( SY::Mapping.identity, :* ) end |
#irreducible? ⇒ Boolean
Whether it is possible to expand this
197 198 199 |
# File 'lib/sy/composition.rb', line 197 def irreducible? all? { |qnt, exp| qnt.irreducible? } end |
#new_quantity(args = {}) ⇒ Object
Directly (without attempts to simplify) creates a new quantity from self. If self is empty or singular, SY::Amount, resp. the singular quantity in question is returned.
169 170 171 |
# File 'lib/sy/composition.rb', line 169 def new_quantity args={} SY::Quantity.new args.merge( composition: self ) end |
#simple? ⇒ Boolean
Simple composition is one that is either empty or singular.
192 193 194 |
# File 'lib/sy/composition.rb', line 192 def simple? empty? or singular? end |
#simplify ⇒ Object
Simplifies a quantity hash by applying simplification rules.
151 152 153 154 155 156 |
# File 'lib/sy/composition.rb', line 151 def simplify |
#singular? ⇒ Boolean
Singular compositions consist of only one quantity.
98 99 100 |
# File 'lib/sy/composition.rb', line 98 def singular? size == 1 && first[1] == 1 end |
#to_quantity(args = {}) ⇒ Object
Returns the quantity appropriate to this composition.
160 161 162 163 |
# File 'lib/sy/composition.rb', line 160 def to_quantity args={} # All the work is delegated to the quantity table: QUANTITY_TABLE[ args.merge( self ) ] end |