Class: Quanty

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

Overview

parse.y, quanty/parse.rb

  by Masahiro Tanaka <masa@ir.isas.ac.>

Direct Known Subclasses

ODDB::Dose

Defined Under Namespace

Classes: Fact, Parse

Constant Summary

Self =
self
RadianUnit =
Quanty::Fact.new('radian')

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Quanty) initialize(*a)

A new instance of Quanty



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

def initialize(*a)
  case a.size
  when 1
    if String === a[0]
@val,@unit,@fact = 1.0, a[0], nil
    else
@val,@unit,@fact = a[0], '', nil
    end
  when 2..3
      @val,@unit,@fact = a
  else
    raise ArgumentError, 'wrong # of arguments'
  end
  unless Fact === @fact
    @fact = Fact.new(@unit)
  end
end

Instance Attribute Details

- (Object) fact (readonly)

Returns the value of attribute fact



40
41
42
# File 'src/util/quanty/main.rb', line 40

def fact
  @fact
end

- (Object) unit (readonly)

Returns the value of attribute unit



39
40
41
# File 'src/util/quanty/main.rb', line 39

def unit
  @unit
end

- (Object) val (readonly) Also known as: value

Returns the value of attribute val



38
39
40
# File 'src/util/quanty/main.rb', line 38

def val
  @val
end

Instance Method Details

- (Object) *(other)



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'src/util/quanty/main.rb', line 98

def * (other)
  if other.kind_of?(Self)
    unit = other.unit
    unless @unit.empty?
if unit.empty?
  unit = @unit
else
  if /\A[A-Za-z_]/ou =~ unit
    unit = @unit+' '+unit
  else
    unit = @unit+' ('+unit+')' 
  end
end
    end
    Self.new( @val*other.val, unit, @fact*other.fact )
  else
    Self.new( @val*other, @unit, @fact )
  end
end

- (Object) **(n)



89
90
91
92
93
94
95
96
# File 'src/util/quanty/main.rb', line 89

def **(n)
  if /^[A-Za-z_]+&/ou =~ @unit
    unit = @unit+'^'+n.to_s
  else
    unit = '('+@unit+')^'+n.to_s+''
  end
  Self.new( @val**n, unit, @fact**n )
end

- (Object) +(other)



61
62
63
64
65
66
67
68
# File 'src/util/quanty/main.rb', line 61

def + (other)
  val = @val + adjust(other)
  if @unit==''
    val
  else
    Self.new( val, @unit, @fact )      
  end
end

- (Object) +@



79
# File 'src/util/quanty/main.rb', line 79

def +@ ; Self.new(  @val, @unit, @fact ) end

- (Object) -(other)



70
71
72
73
74
75
76
77
# File 'src/util/quanty/main.rb', line 70

def - (other)
  val = @val - adjust(other)
  if @unit==''
    val
  else
    Self.new( val, @unit, @fact )      
  end
end

- (Object) -@



80
# File 'src/util/quanty/main.rb', line 80

def -@ ; Self.new( -@val, @unit, @fact ) end

- (Object) /(other)



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'src/util/quanty/main.rb', line 118

def / (other)
  if other.kind_of?(Self)
    unit = other.unit
    if unit.empty?
unit = @unit
    else
if /\A[A-Za-z_-]+((\^|\*\*)?[0-9.]+)?$/ou =~ unit
  unit = '/ '+unit
else
  unit = '/ ('+unit+')' 
end
unit = @unit+' '+unit unless @unit.empty?
    end
    Self.new( @val/other.val, unit, @fact/other.fact )
  else
    Self.new( @val/other, @unit, @fact )
  end
end

- (Object) <(other)



86
# File 'src/util/quanty/main.rb', line 86

def  <  (other) @val  <  adjust(other) end

- (Object) <=(other)



85
# File 'src/util/quanty/main.rb', line 85

def  <= (other) @val  <= adjust(other) end

- (Object) <=>(other)



82
# File 'src/util/quanty/main.rb', line 82

def <=> (other) @val <=> adjust(other) end

- (Object) ==(other)



83
# File 'src/util/quanty/main.rb', line 83

def  == (other) @val  == adjust(other) end

- (Object) >(other)



87
# File 'src/util/quanty/main.rb', line 87

def  >  (other) @val  >  adjust(other) end

- (Object) >=(other)



84
# File 'src/util/quanty/main.rb', line 84

def  >= (other) @val  >= adjust(other) end

- (Object) adjust(other)



43
44
45
46
47
48
49
50
51
52
53
# File 'src/util/quanty/main.rb', line 43

def adjust(other)
  if other.kind_of?(Self)
    unless @fact === other.fact
raise "not same unit: %s != %s" % [@unit,other.unit]
    end
    other.val * ( other.fact.factor / @fact.factor )
  else
    raise @unit + ": not null unit" unless @fact.null?
    other / @fact.factor
  end
end

- (Object) coerce(other)



137
138
139
# File 'src/util/quanty/main.rb', line 137

def coerce(other)
  [ Self.new(other), self ]
end

- (Object) inspect



155
156
157
# File 'src/util/quanty/main.rb', line 155

def inspect
  "Quanty(" + @val.to_s + ",'" + @unit + "')"
end

- (Object) to_f



141
142
143
144
145
146
147
148
149
# File 'src/util/quanty/main.rb', line 141

def to_f
  if @fact.null?
    @val * @fact.factor
  elsif @fact === RadianUnit
    want('radian').value
  else
    raise 'cannot convert into non-dimensional Float'
  end
end

- (Object) to_s



151
152
153
# File 'src/util/quanty/main.rb', line 151

def to_s
  @val.to_s + "[" + @unit + "]"
end

- (Object) want(unit)



55
56
57
58
59
# File 'src/util/quanty/main.rb', line 55

def want(unit)
  obj = Self.new(unit)
  val = obj.adjust(self)
  Self.new( val, unit, obj.fact )
end