Class: NumericWithUnit::Unit
- Inherits:
-
Object
- Object
- NumericWithUnit::Unit
- Defined in:
- lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/unit.rb,
lib/numeric_with_unit/unit.rb
Defined Under Namespace
Classes: Config, NoUnitError
Constant Summary collapse
- @@list =
[]
- @@prefix =
{ 'Y' => 10**24, 'Z' => 10**21, 'E' => 10**18, 'P' => 10**15, 'T' => 10**12, 'G' => 10**9, 'M' => 10**6, 'k' => 10**3, 'h' => 10**2, 'da' => 10**1, 'd' => 10**-1, 'c' => 10**-2, 'm' => 10**-3, 'μ' => 10**-6, 'u' => 10**-6, 'n' => 10**-9, 'p' => 10**-12, 'f' => 10**-15, 'a' => 10**-18, 'z' => 10**-21, 'y' => 10**-24, 'Ki' => 2**10, 'Mi' => 2**20, 'Gi' => 2**30, 'Ti' => 2**40, 'Pi' => 2**50, 'Ei' => 2**60, 'Zi' => 2**70, 'Yi' => 2**80 }
Instance Attribute Summary collapse
-
#derivation ⇒ Object
readonly
Returns the value of attribute derivation.
-
#dimension ⇒ Object
readonly
Returns the value of attribute dimension.
-
#symbol ⇒ Object
readonly
Instance Methods.
Class Method Summary collapse
-
.<<(arg) ⇒ Object
add unit to base unit list.
-
.[](arg) ⇒ Object
apply to_s to arg and return parsed unit.
-
.[]=(key, arg) ⇒ Object
cast unit and add unit to base unit list at the same time.
-
.assign ⇒ Object
create new unit and add unit to base unit list at the same time.
-
.delete(unit_symbol) ⇒ Object
remove unit from base unit list.
-
.derive {|derivation| ... } ⇒ Object
create new unit from derivation _(for internal use)_ .
-
.list ⇒ Object
return base unit list.
-
.parse(unit_str) ⇒ Object
parsing unit_str (ex. “kg”, km/hr“, ”cm2“) to (derived) unit.
Instance Method Summary collapse
- #*(other_unit) ⇒ Object
- #**(num) ⇒ Object
- #/(other_unit) ⇒ Object
- #==(other) ⇒ Object
-
#cast(new_symbol, factor = 1) ⇒ Object
create new unit with new symbol and factor from self.
-
#dimension_equal?(other_unit) ⇒ Boolean
return true if self and other_unit have the same dimension.
- #dimensionless? ⇒ Boolean
- #from_si(value) ⇒ Object
-
#initialize(derivation = nil) {|config| ... } ⇒ Unit
constructor
A new instance of Unit.
- #inspect ⇒ Object
- #simplify ⇒ Object
- #to_s ⇒ Object
- #to_si(value) ⇒ Object
Constructor Details
#initialize(derivation = nil) {|config| ... } ⇒ Unit
Returns a new instance of Unit.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/numeric_with_unit/unit.rb', line 329 def initialize(derivation=nil) # TODO: Unit::Configとinitializeの役割が分離できていないので見なおせ config = Config.new yield(config) if block_given? config.compile @symbol = config.symbol @dimension = config.dimension @from_si = config.from_si @to_si = config.to_si unless derivation derivation = Hash.new(0) derivation[self] += 1 end @derivation = derivation end |
Instance Attribute Details
#derivation ⇒ Object (readonly)
Returns the value of attribute derivation.
327 328 329 |
# File 'lib/numeric_with_unit/unit.rb', line 327 def derivation @derivation end |
#dimension ⇒ Object (readonly)
Returns the value of attribute dimension.
327 328 329 |
# File 'lib/numeric_with_unit/unit.rb', line 327 def dimension @dimension end |
#symbol ⇒ Object (readonly)
Instance Methods
326 327 328 |
# File 'lib/numeric_with_unit/unit.rb', line 326 def symbol @symbol end |
Class Method Details
.<<(arg) ⇒ Object
add unit to base unit list.
191 192 193 194 195 196 197 |
# File 'lib/numeric_with_unit/unit.rb', line 191 def self.<<(arg) if arg.is_a?(self) @@list << arg else @@list << Unit[arg] end end |
.[](arg) ⇒ Object
apply to_s to arg and return parsed unit.
169 170 171 |
# File 'lib/numeric_with_unit/unit.rb', line 169 def self.[](arg) self.parse(arg.to_s) end |
.[]=(key, arg) ⇒ Object
cast unit and add unit to base unit list at the same time.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/numeric_with_unit/unit.rb', line 174 def self.[]=(key, arg) if arg.is_a?(Array) and arg.size == 2 a = [key, arg.first] u = arg.last else a = [key] u = arg end @@list << (u.is_a?(self) ? u : self[u]).cast(*a) end |
.assign ⇒ Object
create new unit and add unit to base unit list at the same time.
205 206 207 |
# File 'lib/numeric_with_unit/unit.rb', line 205 def self.assign @@list << self.new{|config| yield(config)} end |
.delete(unit_symbol) ⇒ Object
remove unit from base unit list.
200 201 202 |
# File 'lib/numeric_with_unit/unit.rb', line 200 def self.delete(unit_symbol) @@list.delete_if{|unit| unit.symbol == unit_symbol} end |
.derive {|derivation| ... } ⇒ Object
create new unit from derivation _(for internal use)_ .
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 |
# File 'lib/numeric_with_unit/unit.rb', line 109 def self.derive #(block) derivation = Hash.new(0) yield(derivation) return Unit.new if derivation.empty? derivation.delete_if{|k,v| k.symbol.nil?} # constructing symbol h = derivation.reject{|k,v| k.symbol.empty?}.sort_by{|u,v| u.symbol}.sort_by{|u,v| v} syms_pos = h.select{|u,v| v > 0}.map{|u,v| u.symbol + (v.abs>1 ? v.abs.to_s : '')} syms_neg = h.select{|u,v| v < 0}.map{|u,v| u.symbol + (v.abs>1 ? v.abs.to_s : '')} symbol = syms_pos.join('.') symbol += '/' + (syms_neg.size>1 ? "(#{syms_neg.join('.')})" : "#{syms_neg.first}") unless syms_neg.empty? # constructing dimension dimension = Hash.new(0) derivation.each do |u,v| u.dimension.each do |d,i| dimension[d] += i*v end end # constructing from_si proc from_si = derivation.map{|u,v| prc = if v > 0 ->(x){u.from_si(x)} else ->(x){x.quo(u.from_si(1)-u.from_si(0))} #FIXME: ℃とKの変換のような場合に、変換式の切片を消すため。変換式が線形じゃないケースは想定していない end [prc, v.abs] }.map{|prc,v| ->(x){ v.times{x = prc[x]}; x } }.reduce{|memo, prc| ->(x){memo[prc[x]]} } # constructing to_si proc to_si = derivation.map{|u,v| prc = if v > 0 ->(x){u.to_si(x)} else ->(x){x.quo(u.to_si(1)-u.to_si(0))} #FIXME: ℃とKの変換のような場合に、変換式の切片を消すため。変換式が線形じゃないケースは想定していない end [prc, v.abs] }.map{|prc,v| ->(x){ v.times{x = prc[x]}; x } }.reduce{|memo, prc| ->(x){memo[prc[x]]} } # deriving new unit self.new(derivation){|config| config.symbol = symbol config.dimension = dimension config.from_si = from_si config.to_si = to_si } end |
.list ⇒ Object
return base unit list.
186 187 188 |
# File 'lib/numeric_with_unit/unit.rb', line 186 def self.list @@list end |
.parse(unit_str) ⇒ Object
parsing unit_str (ex. “kg”, km/hr“, ”cm2“) to (derived) unit.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/numeric_with_unit/unit.rb', line 211 def self.parse(unit_str) rec = ->(arg){__send__(__method__, arg)} dervation_str = parse_3rd(parse_2nd(parse_1st(unit_str))) derive{|derivation| dervation_str.each do |unit_str, order| if i = @@list.rindex{|unit| unit.symbol == unit_str} derivation[@@list[i]] += order elsif m = unit_str.match(/^(?<prefix>#{@@prefix.keys.join('|')})(?<unit>#{list.join('|')})$/) and m[:unit].empty?.! u = rec[m[:unit]].cast(unit_str, @@prefix[m[:prefix]]) derivation[u] += order else raise NoUnitError, "[#{unit_str}] is not defined!" end end } end |
Instance Method Details
#*(other_unit) ⇒ Object
394 395 396 397 398 399 |
# File 'lib/numeric_with_unit/unit.rb', line 394 def *(other_unit) self.class.derive do |derivation| @derivation.each{|k, v| derivation[k] += v} other_unit.derivation.each{|k, v| derivation[k] += v} end end |
#**(num) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/numeric_with_unit/unit.rb', line 408 def **(num) if num.zero? self.class.new else self.class.derive do |derivation| # NOTE: # ここto_iでOKか?v*numが整数じゃなければraiseすべき?→すべき→NumericWithUnitでやるべき? # Unitでは整数じゃない次数の単位は許容すべきか否か→していい気がする @derivation.each{|k, v| derivation[k] = (v*num).to_i} end end end |
#/(other_unit) ⇒ Object
401 402 403 404 405 406 |
# File 'lib/numeric_with_unit/unit.rb', line 401 def /(other_unit) self.class.derive do |derivation| @derivation.each{|k, v| derivation[k] += v} other_unit.derivation.each{|k, v| derivation[k] -= v} end end |
#==(other) ⇒ Object
386 387 388 389 390 391 392 |
# File 'lib/numeric_with_unit/unit.rb', line 386 def ==(other) if other.is_a?(self.class) symbol == other.symbol and dimension == other.dimension else super end end |
#cast(new_symbol, factor = 1) ⇒ Object
create new unit with new symbol and factor from self. use for converting [in] = 25.4 .
349 350 351 352 353 354 355 356 |
# File 'lib/numeric_with_unit/unit.rb', line 349 def cast(new_symbol, factor = 1) self.class.new do |config| config.symbol = new_symbol config.dimension = @dimension config.from_si = ->(x){from_si(x.quo(factor))} config.to_si = ->(x){to_si(x * factor)} end end |
#dimension_equal?(other_unit) ⇒ Boolean
return true if self and other_unit have the same dimension.
380 381 382 383 384 |
# File 'lib/numeric_with_unit/unit.rb', line 380 def dimension_equal?(other_unit) (@dimension.keys | other_unit.dimension.keys).all?{|k| @dimension[k] == other_unit.dimension[k] } end |
#dimensionless? ⇒ Boolean
375 376 377 |
# File 'lib/numeric_with_unit/unit.rb', line 375 def dimensionless? @dimension.all?{|k,v| v.zero?} end |
#from_si(value) ⇒ Object
370 371 372 |
# File 'lib/numeric_with_unit/unit.rb', line 370 def from_si(value) @from_si.call(value) end |
#inspect ⇒ Object
362 363 364 |
# File 'lib/numeric_with_unit/unit.rb', line 362 def inspect "#<#{self.class}:[#{@symbol}] #{@dimension}>" end |
#simplify ⇒ Object
422 423 424 425 426 427 428 429 430 |
# File 'lib/numeric_with_unit/unit.rb', line 422 def simplify self.class.derive{|derivation| @dimension.each do|d,o| u = self.class.list.find{|u| u.dimension == {d => 1}} #TODO: find? ok? raise NoUnitError, "No unit with #{{d=>1}} dimension is assined." unless u derivation[u] = o end } end |
#to_s ⇒ Object
358 359 360 |
# File 'lib/numeric_with_unit/unit.rb', line 358 def to_s @symbol end |
#to_si(value) ⇒ Object
366 367 368 |
# File 'lib/numeric_with_unit/unit.rb', line 366 def to_si(value) @to_si.call(value) end |