Class: Quanty::Fact
- Inherits:
-
Hash
- Object
- Hash
- Quanty::Fact
- Defined in:
- lib/oddb/util/quanty/fact.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#factor ⇒ Object
readonly
Returns the value of attribute factor.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #/(other) ⇒ Object
- #==(other) ⇒ Object
-
#===(other) ⇒ Object
check only dimension.
- #[]=(key, val) ⇒ Object
- #__equal__ ⇒ Object
- #decomp(a) ⇒ Object
- #div!(other) ⇒ Object
- #dup ⇒ Object
- #fac!(other) ⇒ Object
- #find_prefix(a, n) ⇒ Object
-
#initialize(key = nil, base = false) ⇒ Fact
constructor
Basic units: Fact.new(“m”,true) => “m”=>1 Derivative units: Fact.new(“km”) => 1000*“m”=>1.
- #inspect ⇒ Object
- #mul!(other) ⇒ Object
- #null? ⇒ Boolean
- #pow!(other) ⇒ Object
- #replace(other) ⇒ Object
- #to_f ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key = nil, base = false) ⇒ Fact
Basic units: Fact.new(“m”,true) => “m”=>1 Derivative units: Fact.new(“km”) => 1000*“m”=>1
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/oddb/util/quanty/fact.rb', line 21 def initialize(key=nil,base=false) self.default = 0.0 @factor = 1.0 case key when Numeric @factor = key when String if base store(key, 1.0) else decomp(key) end when Self replace(key) end end |
Instance Attribute Details
#factor ⇒ Object (readonly)
Returns the value of attribute factor.
17 18 19 |
# File 'lib/oddb/util/quanty/fact.rb', line 17 def factor @factor end |
Class Method Details
.mkdump(filename) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/oddb/util/quanty/fact.rb', line 174 def mkdump filename Prefix.clear List.clear #s = open("units.succ","w") #f = open("units.fail","w") open("units.dat","r").readlines.each do |str| if /^([µA-Za-z_0-9%$"'-]+)\s+([^#]+)/u =~ str name,repr = $1,$2.strip # conversion due to the different rule from GNU units: # A / B C => A / (B C) if /\//u =~ repr #/ pre,suf = $`,$'.strip if /\s/u =~ suf repr = pre + ' / (' + suf + ')' end end if repr=="!" List[name] = Fact.new(name,true).freeze elsif /-$/u =~ name Prefix[name[0..-2]] = Prefix[repr] || (List[repr] || repr).to_f else #p [name,repr] List[name] = Fact.new(repr).freeze end #s.print str #rescue #f.print str end end #Prefix.each{|key,val| p [key,val]} #List.each{|key,val| p [key,val]} Marshal.dump( [Prefix, List], open(filename,"w") ) end |
Instance Method Details
#*(other) ⇒ Object
95 96 97 |
# File 'lib/oddb/util/quanty/fact.rb', line 95 def * (other) dup.mul!(other) end |
#**(other) ⇒ Object
118 119 120 |
# File 'lib/oddb/util/quanty/fact.rb', line 118 def ** (other) dup.pow!(other) end |
#/(other) ⇒ Object
107 108 109 |
# File 'lib/oddb/util/quanty/fact.rb', line 107 def / (other) dup.div!(other) end |
#==(other) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/oddb/util/quanty/fact.rb', line 151 def ==(other) if other.kind_of?(Numeric) null? && @factor==other else __equal__(other) && @factor==other.factor end end |
#===(other) ⇒ Object
check only dimension
160 161 162 163 164 165 166 |
# File 'lib/oddb/util/quanty/fact.rb', line 160 def ===(other) if other.kind_of?(Numeric) null? else __equal__(other) end end |
#[]=(key, val) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/oddb/util/quanty/fact.rb', line 38 def []=(key,val) if val == 0 delete(key) else super(key,val) end end |
#__equal__ ⇒ Object
149 |
# File 'lib/oddb/util/quanty/fact.rb', line 149 alias __equal__ :== |
#decomp(a) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/oddb/util/quanty/fact.rb', line 65 def decomp(a) if /^([µA-Za-z_]+([A-Za-z_0-9-]+[A-Za-z_])?)$|^[$%'"]'?$/ou =~ a #if /^[A-Za-z_0-9$%-]+$/o =~ a unit = List[a] || find_prefix(a,0) || if a.size>3 && /chs$/ou !~ a && /(.*[a-rt-y])s$/ou =~ a b = $1 List[b] || find_prefix(b,2) || if a.size>4 && /(.*s|.*z|.*ch)es$/ou =~ a b = $1 List[b] || find_prefix(b,2) end end else unit = Parser.parse(a) end unless unit raise "`%s': unknown unit"%a end @factor *= factor if factor mul!(unit) end |
#div!(other) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/oddb/util/quanty/fact.rb', line 99 def div!(other) raise unless other.kind_of?(Fact) other.each{ |key,val| self[key] -= val } delete_if{ |key,val| val == 0 } @factor /= other.factor self end |
#fac!(other) ⇒ Object
122 123 124 125 126 |
# File 'lib/oddb/util/quanty/fact.rb', line 122 def fac!(other) raise unless other.kind_of?(Numeric) @factor *= other self end |
#find_prefix(a, n) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/oddb/util/quanty/fact.rb', line 55 def find_prefix(a,n) Prefix.each{ |key,factor| if /^#{key}-?/u =~ a && (unit = List[b=$']) && b.size>n #p [a,b,factor] return Fact.new(b).fac!(factor) end } nil end |
#inspect ⇒ Object
128 129 130 |
# File 'lib/oddb/util/quanty/fact.rb', line 128 def inspect @factor.to_s+"*"+super end |
#mul!(other) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/oddb/util/quanty/fact.rb', line 87 def mul!(other) raise unless other.kind_of?(Fact) other.each{ |key,val| self[key] += val } delete_if{ |key,val| val == 0 } @factor *= other.factor self end |
#null? ⇒ Boolean
144 145 146 147 |
# File 'lib/oddb/util/quanty/fact.rb', line 144 def null? each_value{ |val| return false if val != 0 } true end |
#pow!(other) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/oddb/util/quanty/fact.rb', line 111 def pow!(other) raise unless other.kind_of?(Numeric) each{ |key,val| self[key] = other*val } @factor **= other self end |
#replace(other) ⇒ Object
46 47 48 49 |
# File 'lib/oddb/util/quanty/fact.rb', line 46 def replace(other) @factor = other.factor super(other) end |
#to_f ⇒ Object
168 169 170 171 |
# File 'lib/oddb/util/quanty/fact.rb', line 168 def to_f raise inspect + ": not null unit" unless null? @factor end |
#to_s ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/oddb/util/quanty/fact.rb', line 132 def to_s a = [] each{|k,v| if v != 1 v = v.to_i if v%1 == 0 k += v.to_s end a.push k } @factor.to_s+" "+a.join(" ") end |