Class: Quanty::Fact

Inherits:
Hash
  • Object
show all
Defined in:
lib/quanty/fact.rb

Constant Summary collapse

Self =
self
Parser =
Parse.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, base = false) ⇒ Fact

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/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

#factorObject (readonly)

Returns the value of attribute factor.



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

def factor
  @factor
end

Class Method Details

.mkdump(filename) ⇒ Object



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 'lib/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

#*(other) ⇒ Object



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

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

#**(other) ⇒ Object



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

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

#/(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#===(other) ⇒ Object

check only dimension



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

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

#[]=(key, val) ⇒ Object



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

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

#__equal__Object



148
# File 'lib/quanty/fact.rb', line 148

alias __equal__ :==

#decomp(a) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/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

#div!(other) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/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

#dupObject



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

def dup
  Fact.new(self)
end

#fac!(other) ⇒ Object



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

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

#find_prefix(a, n) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/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

#inspectObject



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

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

#mul!(other) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/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

#null?Boolean

Returns:

  • (Boolean)


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

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

#pow!(other) ⇒ Object



110
111
112
113
114
115
# File 'lib/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

#replace(other) ⇒ Object



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

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

#to_fObject



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

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

#to_sObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/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