Class: Quanty::Fact

Inherits:
Hash show all
Defined in:
src/util/quanty/fact.rb,
data/quanty/lib/quanty/fact.rb

Constant Summary

Self =
self
Parser =
Parse.new

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Hash

#obj_path

Constructor Details

- (Fact) initialize(key = nil, base = false)

Basic units: Fact.new(“m”,true) => Quanty::Fact.&“m&“m”=>1 Derivative units: Fact.new(“km”) => 1000*Quanty::Fact.&“m&“m”=>1



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'src/util/quanty/fact.rb', line 20

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

- (Object) factor (readonly)

Returns the value of attribute factor



16
17
18
# File 'src/util/quanty/fact.rb', line 16

def factor
  @factor
end

Class Method Details

+ (Object) mkdump(filename)



173
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
# File 'src/util/quanty/fact.rb', line 173

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

- (Object) *(other)



94
95
96
# File 'src/util/quanty/fact.rb', line 94

def * (other)
  dup.mul!(other)
end

- (Object) **(other)



117
118
119
# File 'src/util/quanty/fact.rb', line 117

def ** (other)
  dup.pow!(other)
end

- (Object) /(other)



106
107
108
# File 'src/util/quanty/fact.rb', line 106

def / (other)
  dup.div!(other)
end

- (Object) ==(other) Also known as: __equal__



150
151
152
153
154
155
156
# File 'src/util/quanty/fact.rb', line 150

def ==(other)
  if other.kind_of?(Numeric)
  null? && @factor==other
  else
  __equal__(other) && @factor==other.factor
  end
end

- (Object) ===(other)

check only dimension



159
160
161
162
163
164
165
# File 'src/util/quanty/fact.rb', line 159

def ===(other)
  if other.kind_of?(Numeric)
  null?
  else
  __equal__(other)
  end
end

- (Object) []=(key, val)



37
38
39
40
41
42
43
# File 'src/util/quanty/fact.rb', line 37

def []=(key,val)
  if val == 0
  delete(key)
  else
  super(key,val)
  end
end

- (Object) decomp(a)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'src/util/quanty/fact.rb', line 64

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

- (Object) div!(other)



98
99
100
101
102
103
104
# File 'src/util/quanty/fact.rb', line 98

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

- (Object) dup



50
51
52
# File 'src/util/quanty/fact.rb', line 50

def dup
  Fact.new(self)
end

- (Object) fac!(other)



121
122
123
124
125
# File 'src/util/quanty/fact.rb', line 121

def fac!(other)
  raise unless other.kind_of?(Numeric)
  @factor *= other
  self
end

- (Object) find_prefix(a, n)



54
55
56
57
58
59
60
61
62
# File 'src/util/quanty/fact.rb', line 54

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

- (Object) inspect



127
128
129
# File 'src/util/quanty/fact.rb', line 127

def inspect
  @factor.to_s+"*"+super
end

- (Object) mul!(other)



86
87
88
89
90
91
92
# File 'src/util/quanty/fact.rb', line 86

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

- (Boolean) null?

Returns:

  • (Boolean)


143
144
145
146
# File 'src/util/quanty/fact.rb', line 143

def null?
  each_value{ |val| return false if val != 0 }
  true
end

- (Object) pow!(other)



110
111
112
113
114
115
# File 'src/util/quanty/fact.rb', line 110

def pow!(other)
  raise unless other.kind_of?(Numeric)
  each{ |key,val| self[key] = other*val }
  @factor **= other
  self
end

- (Object) replace(other)



45
46
47
48
# File 'src/util/quanty/fact.rb', line 45

def replace(other)
  @factor = other.factor
  super(other)
end

- (Object) to_f



167
168
169
170
# File 'src/util/quanty/fact.rb', line 167

def to_f
  raise inspect + ": not null unit" unless null?
  @factor
end

- (Object) to_s



131
132
133
134
135
136
137
138
139
140
141
# File 'src/util/quanty/fact.rb', line 131

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