quanty 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/ChangeLog +14 -0
- data/History.txt +6 -0
- data/Manifest.txt +18 -0
- data/README.en +56 -0
- data/README.txt +26 -0
- data/Rakefile +28 -0
- data/extconf.rb +51 -0
- data/lib/quanty/fact.rb +228 -0
- data/lib/quanty/main.rb +159 -0
- data/lib/quanty/parse.rb +852 -0
- data/lib/quanty.rb +7 -0
- data/mkdump.rb +4 -0
- data/parse.y +106 -0
- data/quanty-en.rd +107 -0
- data/quanty-ja.rd +102 -0
- data/test.rb +14 -0
- data/units.dat +3549 -0
- metadata +107 -0
data/lib/quanty.rb
ADDED
data/mkdump.rb
ADDED
data/parse.y
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
#
|
2
|
+
# parse.y, quanty/parse.rb
|
3
|
+
#
|
4
|
+
# Copyright (c) 2001 Masahiro Tanaka <masa@ir.isas.ac.jp>
|
5
|
+
#
|
6
|
+
# This program is free software.
|
7
|
+
# You can distribute/modify this program under the terms of
|
8
|
+
# the GNU General Public License version 2 or later.
|
9
|
+
|
10
|
+
class Parse
|
11
|
+
|
12
|
+
prechigh
|
13
|
+
nonassoc UMINUS
|
14
|
+
right POW UPOW
|
15
|
+
left '*' '/' '|'
|
16
|
+
left '+' '-'
|
17
|
+
preclow
|
18
|
+
|
19
|
+
rule
|
20
|
+
|
21
|
+
target: val
|
22
|
+
| /* none */ { result = Quanty::Fact.new }
|
23
|
+
| num { result = Quanty::Fact.new(val[0]) }
|
24
|
+
;
|
25
|
+
|
26
|
+
num : NUMBER
|
27
|
+
| '-' NUMBER = UMINUS { result = -val[1] }
|
28
|
+
| num '+' num { result += val[2] }
|
29
|
+
| num '-' num { result -= val[2] }
|
30
|
+
| num '|' num { result /= val[2] }
|
31
|
+
| num '/' num { result /= val[2] }
|
32
|
+
| num '*' num { result *= val[2] }
|
33
|
+
| num POW num { result **= val[2] }
|
34
|
+
| '(' num ')' { result = val[1] }
|
35
|
+
;
|
36
|
+
|
37
|
+
val : seq
|
38
|
+
| num seq { result = val[1].fac!(val[0]) }
|
39
|
+
| '/' exp { result = val[1].pow!(-1) }
|
40
|
+
| num '/' exp { result = val[2].pow!(-1).fac!(val[0]) }
|
41
|
+
;
|
42
|
+
|
43
|
+
seq : exp
|
44
|
+
| seq exp { result.mul!(val[1]) }
|
45
|
+
| seq '*' exp { result.mul!(val[2]) }
|
46
|
+
| seq '/' exp { result.div!(val[2]) }
|
47
|
+
| seq '*' num { result.fac!(val[2]) }
|
48
|
+
| seq '/' num { result.fac!(val[2]**-1) }
|
49
|
+
;
|
50
|
+
|
51
|
+
exp : unit
|
52
|
+
| unit num = UPOW { result.pow!(val[1]) }
|
53
|
+
| unit POW num { result.pow!(val[2]) }
|
54
|
+
;
|
55
|
+
|
56
|
+
unit : WORD { result = Quanty::Fact.new(val[0]) }
|
57
|
+
| '(' val ')' { result = val[1] }
|
58
|
+
| '[' val ']' { result = val[1] }
|
59
|
+
;
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
---- header ----
|
64
|
+
|
65
|
+
# parse.y, quanty/parse.rb
|
66
|
+
#
|
67
|
+
# by Masahiro Tanaka <masa@ir.isas.ac.jp>
|
68
|
+
#
|
69
|
+
class Quanty
|
70
|
+
|
71
|
+
---- inner ----
|
72
|
+
|
73
|
+
def parse( str )
|
74
|
+
@q = []
|
75
|
+
|
76
|
+
while str.size > 0 do
|
77
|
+
#p str
|
78
|
+
case str
|
79
|
+
when /\A[\s\n]+/ou
|
80
|
+
when /\A\d+\.?\d*([eE][+-]?\d+)?/ou
|
81
|
+
@q.push [:NUMBER, $&.to_f]
|
82
|
+
when /\A([A-Z]\.){2}/u
|
83
|
+
when /\A[A-Za-z_]+ -/u
|
84
|
+
when /\A[A-Za-z_µ]+([A-Za-z_µ0-9-]+[A-Za-z_µ])?/ou
|
85
|
+
@q.push [:WORD, $&]
|
86
|
+
when /\A[$%'"]'?/ou
|
87
|
+
@q.push [:WORD, $&]
|
88
|
+
when /\A\^|\A\*\*/ou
|
89
|
+
@q.push [:POW, $&]
|
90
|
+
when /\A./ou
|
91
|
+
@q.push [$&,$&]
|
92
|
+
end
|
93
|
+
str = $'
|
94
|
+
end
|
95
|
+
@q.push [false, '$end']
|
96
|
+
|
97
|
+
do_parse
|
98
|
+
end
|
99
|
+
|
100
|
+
def next_token
|
101
|
+
@q.shift
|
102
|
+
end
|
103
|
+
|
104
|
+
---- footer ----
|
105
|
+
|
106
|
+
end # class Quanty
|
data/quanty-en.rd
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
=begin
|
2
|
+
= class Quanty
|
3
|
+
|
4
|
+
== SYNOPSIS
|
5
|
+
|
6
|
+
A class whose instances consist of "value (physical quantity)" and "unit".
|
7
|
+
Featuring:
|
8
|
+
* Unit conversion like ((%units%)) command.
|
9
|
+
* Operation between quantities having different units,
|
10
|
+
like ((*km*)) and ((*mile*)), with automatic unit conversion.
|
11
|
+
|
12
|
+
== EXAMPLE
|
13
|
+
|
14
|
+
require 'quanty'
|
15
|
+
Quanty(1.23,'km') + Quanty(4.56,'m') #=> Quanty(1.23456,'km')
|
16
|
+
Quanty(123,'mile') / Quanty(2,'hr') #=> Quanty(61,'mile / hr')
|
17
|
+
Quanty(61,'miles/hr').want('m/s') #=> Quanty(27.26944,'m/s')
|
18
|
+
Quanty(1.0,'are') == Quanty(10,'m')**2 #=> true
|
19
|
+
Math.cos(Quanty(60,'degree')) #=> 0.5
|
20
|
+
|
21
|
+
== Quanty class
|
22
|
+
|
23
|
+
=== Super Class:
|
24
|
+
Object (Numeric is better??)
|
25
|
+
|
26
|
+
=== Class method:
|
27
|
+
--- Quanty.new([value],[unit])
|
28
|
+
--- Quanty([value],[unit])
|
29
|
+
Create Quanty class instance having ((|value|)) and ((|unit|)) (is String).
|
30
|
+
If ((|value|)) is omitted, ((|value|)) = 1 is assumed.
|
31
|
+
If ((|unit|)) is omitted, ((|unit|)) = "" is assumed,
|
32
|
+
which is regarded as a quantity with "dimensionless" unit
|
33
|
+
(i.e. all dimensions of unit are zero).
|
34
|
+
Refer to ((<Notation of unit>)) below.
|
35
|
+
|
36
|
+
=== Methods:
|
37
|
+
--- self + other
|
38
|
+
--- self - other
|
39
|
+
Addition and subtraction of quantities.
|
40
|
+
Operation is made after
|
41
|
+
the unit of ((|other|)) is converted to the unit of ((|self|)).
|
42
|
+
Exception is raised if unit conversion is failed.
|
43
|
+
Return the Quanty class instance with the unit of ((|self|)).
|
44
|
+
|
45
|
+
--- self * other
|
46
|
+
Multiplication of quantities.
|
47
|
+
Resulting unit is made by concatenating ((|self|)) and ((|other|)).
|
48
|
+
--- self / other
|
49
|
+
Division of quantities.
|
50
|
+
Resulting unit is made by placing (({"/"}))
|
51
|
+
between ((|self|)) and ((|other|)), and concatenating them.
|
52
|
+
|
53
|
+
--- self ** number
|
54
|
+
Power of quantities.
|
55
|
+
Resulting unit is made by "(unit of self)^((|number|))"
|
56
|
+
|
57
|
+
--- self == other
|
58
|
+
--- self < other
|
59
|
+
--- self <= other
|
60
|
+
--- self > other
|
61
|
+
--- self >= other
|
62
|
+
Comparison of quantities.
|
63
|
+
|
64
|
+
--- coerce(number)
|
65
|
+
Convert ((|number|)) to Quanty class instance with dimensionless unit,
|
66
|
+
then return [((|number|)), ((|self|))].
|
67
|
+
|
68
|
+
--- to_f
|
69
|
+
If ((|self|)) is a quantity with a dimensionless unit, return its value.
|
70
|
+
If ((|self|)) is a quantity with an angular unit,
|
71
|
+
return the value converted into radian.
|
72
|
+
Otherwise, raise exception.
|
73
|
+
|
74
|
+
--- unit
|
75
|
+
Return the string of unit.
|
76
|
+
|
77
|
+
--- val
|
78
|
+
--- value
|
79
|
+
Return the value.
|
80
|
+
|
81
|
+
--- want(unit)
|
82
|
+
Convert ((|self|)) to a quantity with ((|unit|)) (is String),
|
83
|
+
and return Quanty class instance.
|
84
|
+
|
85
|
+
== Notation of unit
|
86
|
+
|
87
|
+
* Multiplication
|
88
|
+
'N m' , 'N*m' are same.
|
89
|
+
|
90
|
+
* Division
|
91
|
+
'/s' , 'm/s'
|
92
|
+
|
93
|
+
* Power
|
94
|
+
'm-2' , 'm^-2' , 'm**-2' are all same.
|
95
|
+
|
96
|
+
* Numerical factor
|
97
|
+
'12 inch' --- same as 'feet'.
|
98
|
+
|
99
|
+
* Combination
|
100
|
+
'm/s*m' --- same as 'm^2/s'.
|
101
|
+
'm/(s*m)' --- same as '/s'.
|
102
|
+
|
103
|
+
* See ((%parse.y%)) for more.
|
104
|
+
|
105
|
+
by ((<Masahiro Tanaka|URL:http://www.ir.isas.ac.jp/~masa/index-e.html>))
|
106
|
+
(2001-04-25)
|
107
|
+
=end
|
data/quanty-ja.rd
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
=begin
|
2
|
+
= class Quanty
|
3
|
+
|
4
|
+
== ����
|
5
|
+
|
6
|
+
���� (ʪ����)�פȡ�ñ�̡פ�ʻ�����ĥ��饹��
|
7
|
+
((%units%)) ���ޥ�ɤΤ褦��ñ���Ѵ���ǽ��¾��
|
8
|
+
((*km*)) �� ((*mile*)) �ʤɡ��ۤʤ�ñ�̤�����̤α黻���ǽ��
|
9
|
+
|
10
|
+
== ��
|
11
|
+
|
12
|
+
require 'quanty'
|
13
|
+
Quanty(1.23,'km') + Quanty(4.56,'m') #=> Quanty(1.23456,'km')
|
14
|
+
Quanty(123,'mile') / Quanty(2,'hr') #=> Quanty(61,'mile / hr')
|
15
|
+
Quanty(61,'miles/hr').want('m/s') #=> Quanty(27.26944,'m/s')
|
16
|
+
Quanty(1.0,'are') == Quanty(10,'m')**2 #=> true
|
17
|
+
Math.cos(Quanty(60,'degree')) #=> 0.5
|
18
|
+
|
19
|
+
== Quanty ���饹
|
20
|
+
|
21
|
+
=== �����ѡ����饹:
|
22
|
+
�Ȥꤢ���� Object��(Numeric�Τۤ����褤����)
|
23
|
+
|
24
|
+
=== ���饹��å�:
|
25
|
+
--- Quanty.new([value],[unit])
|
26
|
+
--- Quanty([value],[unit])
|
27
|
+
�̤��ͤ�((|value|))��ñ�̤�((|unit|)) (ʸ����)�Ȥ���
|
28
|
+
Unit ���饹�Υ������������롣
|
29
|
+
((|value|))����ά���줿���ϡ�1�����ꤵ�줿����Ʊ����
|
30
|
+
((|unit|))����ά���줿���ϡ�""�����ꤵ�줿����Ʊ���ǡ�ñ�̤ʤ��̤ˤʤ롣
|
31
|
+
ñ�̤ν����ϡ�����((<ñ��ɽ��ˡ>))�ȡ�
|
32
|
+
|
33
|
+
=== ��å�:
|
34
|
+
--- self + other
|
35
|
+
--- self - other
|
36
|
+
�̤βû���������
|
37
|
+
((|other|))��ñ�̤�((|self|))��ñ�̤��Ѵ����Ʊ黻���롣
|
38
|
+
ñ���Ѵ����Ǥ��ʤ�����㳰��ȯ�����롣
|
39
|
+
��̤�((|self|))��ñ�̤ˤ��� Quanty ���饹�Υ������֤���
|
40
|
+
|
41
|
+
--- self * other
|
42
|
+
�̤ξ軻��
|
43
|
+
��̤�ñ�̤ϡ�((|self|))��((|other|))��ñ�̤�Ϣ�뤷�ƺ�롣
|
44
|
+
--- self / other
|
45
|
+
�̤ν�����
|
46
|
+
��̤�ñ�̤ϡ�
|
47
|
+
((|self|))��((|other|))��ñ�̤�(({"/"}))��Ϥ����Ϣ�뤷�ƺ�롣
|
48
|
+
|
49
|
+
--- self ** number
|
50
|
+
�̤��Ѿ衣
|
51
|
+
��̤�ñ�̤ϡ�"(((|self|))��ñ��)^((|number|))" �Ȥ��ƺ�롣
|
52
|
+
|
53
|
+
--- self == other
|
54
|
+
--- self < other
|
55
|
+
--- self <= other
|
56
|
+
--- self > other
|
57
|
+
--- self >= other
|
58
|
+
�̤���ӡ�
|
59
|
+
|
60
|
+
--- coerce(number)
|
61
|
+
((|number|))��ñ�̤ʤ�Quanty���饹�Υ����ˤ���
|
62
|
+
[((|number|)),((|self|))]�Ȥ����֤���
|
63
|
+
|
64
|
+
--- to_f
|
65
|
+
((|self|))��ñ�̤ʤ��̤ξ��ϡ����Ȥ��ͤ��֤���
|
66
|
+
((|self|))�����٤ξ��ϡ�radian���Ѵ������ͤ��֤���
|
67
|
+
����ʳ���ñ�̤ξ��ϡ��㳰��ȯ�����롣
|
68
|
+
|
69
|
+
--- unit
|
70
|
+
ñ�̤�ʸ������֤���
|
71
|
+
|
72
|
+
--- val
|
73
|
+
--- value
|
74
|
+
�̤��ͤ��֤���
|
75
|
+
|
76
|
+
--- want(unit)
|
77
|
+
((|self|))�� ((|unit|)) (ʸ����) ��ñ�̤Ȥ����̤��Ѵ����롣
|
78
|
+
|
79
|
+
|
80
|
+
== ñ��ɽ��ˡ
|
81
|
+
|
82
|
+
* ��ˡ
|
83
|
+
'N m' �� 'N*m' ��Ʊ����
|
84
|
+
|
85
|
+
* ��ˡ
|
86
|
+
'/s' , 'm/s'
|
87
|
+
|
88
|
+
* �٤�
|
89
|
+
'm-2' , 'm^-2' , 'm**-2' �Ϥ��٤�Ʊ����
|
90
|
+
|
91
|
+
* ������
|
92
|
+
'12 inch' --- 'feet' ��Ʊ����
|
93
|
+
|
94
|
+
* ����
|
95
|
+
'm/s*m' --- 'm^2/s' ��Ʊ����
|
96
|
+
'm/(s*m)' --- '/s' ��Ʊ����
|
97
|
+
|
98
|
+
* �ܺ٤� ((%parse.y%)) �ȤΤ��ȡ�
|
99
|
+
|
100
|
+
by ((<Masahiro Tanaka|URL:http://www.ir.isas.ac.jp/~masa/>))
|
101
|
+
(2001-04-25)
|
102
|
+
=end
|
data/test.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'quanty'
|
2
|
+
require 'irb/xmp'
|
3
|
+
|
4
|
+
xmp 'Quanty(1,"N").fact'
|
5
|
+
xmp 'Quanty(1,"erg").fact'
|
6
|
+
xmp 'Quanty(1.0,"are") > Quanty(3.0,"m") ** 2'
|
7
|
+
xmp 'Quanty(1.0,"are") + Quanty(3.0,"km") ** 2'
|
8
|
+
xmp 'Quanty(3.0,"N") * Quanty(2.0,"10 km^2.5m")'
|
9
|
+
xmp 'Quanty(3.0,"N") / Quanty(2.0,"km2")'
|
10
|
+
xmp 'Quanty(3.0,"m") / Quanty(2.0,"km")'
|
11
|
+
xmp 'Quanty(3.0,"cm") + Quanty(2.0,"inch")'
|
12
|
+
xmp 'Quanty(1.23,"km") + Quanty(4.56,"m")'
|
13
|
+
xmp '4.56 + Quanty(1.23,"1000") '
|
14
|
+
xmp '3 * Quanty(4,"cm") '
|