uncertain 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ == 0.1.0 / 2007-01-17
2
+
3
+ * Beta release
4
+ * Still needs lots of tests
5
+ * doesn't handle trig math yet
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/uncertain
6
+ lib/uncertain.rb
7
+ lib/math.rb
8
+ lib/numeric.rb
9
+ lib/object.rb
10
+ lib/string.rb
11
+ test/test_uncertain.rb
12
+ test/test_numeric.rb
13
+ test/test_object.rb
14
+ test/test_string.rb
15
+ test/test_uncertain.rb
@@ -0,0 +1,50 @@
1
+ by Kevin Olbrich, Ph.D. (kevin.olbrich@gmail.com)
2
+ http://www.sciwerks.com
3
+ http://rubyforge.org/projects/uncertain
4
+
5
+ == DESCRIPTION:
6
+
7
+ 'Uncertain' adds a Numeric class that encapsulates and handles numbers with uncertainty (e.g., 1.0 +/- 0.2).
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Properly performs math on uncertain numbers (except for trig functions)
12
+ * Plays well with 'ruby-units'... just create a unit like '1 +/- 0.1 mm'.unit
13
+
14
+ == SYNOPSIS:
15
+
16
+ Uncertain(1,0.1) #=> 1 +/- 0.1
17
+ 1.to_uncertain(0.1) #=> 1 +/- 0.1
18
+
19
+ == REQUIREMENTS:
20
+
21
+ * 'ruby-units' is optional
22
+
23
+ == INSTALL:
24
+
25
+ * sudo gem install uncertain
26
+
27
+ == LICENSE:
28
+
29
+ (The MIT License)
30
+
31
+ Copyright (c) 2007 Kevin Olbrich
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/uncertain.rb'
6
+
7
+ Hoe.new('uncertain', Uncertain::VERSION) do |p|
8
+ p.name = 'uncertain'
9
+ p.author = ['Kevin Olbrich']
10
+ p.email = 'kevin.olbrich@gmail.com'
11
+ p.summary = p.paragraphs_of('README.txt', 1).join("\n\n")
12
+ p.description = "'Uncertain' adds a Numeric class that encapsulates and handles numbers with uncertainty (e.g., 1.0 +/- 0.2)."
13
+ p.url = "http://rubyforge.org/projects/uncertain/"
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ end
16
+
17
+ # vim: syntax=Ruby
File without changes
@@ -0,0 +1,37 @@
1
+ module Math
2
+ alias uncertain_exp exp
3
+ def exp(n)
4
+ if Uncertain === n
5
+ x = uncertain_exp(n.value)
6
+ Uncertain.new(x,n.uncertainty*x)
7
+ else
8
+ uncertain_exp(n)
9
+ end
10
+ end
11
+
12
+ alias uncertain_log log
13
+ def log(n)
14
+ if Uncertain == n
15
+ Uncertain.new(uncertain_log(n.value),n.uncertainty/n.value)
16
+ else
17
+ uncertain_log(n)
18
+ end
19
+ end
20
+
21
+ alias uncertain_sqrt sqrt
22
+ def sqrt(n)
23
+ if Uncertain === n
24
+ n**(0.5)
25
+ else
26
+ uncertain_sqrt(n)
27
+ end
28
+ end
29
+
30
+ module_function :uncertain_sqrt
31
+ module_function :sqrt
32
+ module_function :uncertain_exp
33
+ module_function :exp
34
+ module_function :uncertain_log
35
+ module_function :log
36
+
37
+ end
@@ -0,0 +1,6 @@
1
+ class Numeric
2
+ def to_uncertain(uncertainty = 0)
3
+ Uncertain.new(self, uncertainty)
4
+ end
5
+ alias :uncertain :to_uncertain
6
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def Uncertain(v,u=0)
3
+ Uncertain.new(v,u)
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ class String
2
+ alias :uncertain_format :%
3
+
4
+ def %(*args)
5
+ if Uncertain === args[0]
6
+ args[0].to_s(self)
7
+ else
8
+ uncertain_format(*args)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,135 @@
1
+ require 'lib/math'
2
+ require 'lib/numeric'
3
+ require 'lib/object'
4
+ require 'lib/string'
5
+
6
+
7
+ # Uncertain
8
+ #
9
+ # Handles uncertain unit math using 'propogation of errors' rules
10
+ # see http://en.wikipedia.org/wiki/Propagation_of_uncertainty
11
+ #
12
+ class Uncertain < Numeric
13
+ include Comparable
14
+
15
+ @@html_output = false
16
+ VERSION = '0.1.0'
17
+
18
+ attr_accessor :value, :uncertainty
19
+
20
+ # set to true to use &plusmn; instead of '+/-'
21
+ def Uncertain.html_output=(style)
22
+ @@html_output = style
23
+ end
24
+
25
+ def Uncertain.html_output
26
+ @@html_output
27
+ end
28
+
29
+ def initialize(v,u=0)
30
+ @value = v.to_f
31
+ @uncertainty = u.to_f
32
+ self.freeze
33
+ end
34
+
35
+ # Add two uncertain numbers
36
+ # (A +/- dA) + (B +/- dB) = (A+B) +/- (dA**2 + db**2)**(1/2)
37
+ def +(other)
38
+ if Uncertain === other
39
+ Uncertain.new(@value+other.value, Math.sqrt(@uncertainty**2+other.uncertainty**2))
40
+ else
41
+ x,y = coerce(other)
42
+ x + y
43
+ end
44
+ end
45
+
46
+ # substract two uncertain numbers
47
+ # (A +/- dA) - (B +/- dB) = (A-B) +/- (dA**2 + db**2)**(1/2)
48
+ def -(other)
49
+ if Uncertain === other
50
+ Uncertain.new(@value-other.value, Math.sqrt(@uncertainty**2+other.uncertainty**2))
51
+ else
52
+ x,y = coerce(other)
53
+ x - y
54
+ end
55
+ end
56
+
57
+ def -@
58
+ Uncertain.new(-@value,@uncertainty)
59
+ end
60
+
61
+ def *(other)
62
+ return 0 if other.zero?
63
+ if Uncertain === other
64
+ x = @value * other.value
65
+ Uncertain.new(x, x*Math.sqrt((@uncertainty/@value)**2+(other.uncertainty/other.value)**2))
66
+ else
67
+ x,y = coerce(other)
68
+ x * y
69
+ end
70
+ end
71
+
72
+ def **(other)
73
+ raise ArgumentError, "exponent may not be Uncertain" if Uncertain === other
74
+ x = @value**other
75
+ Uncertain.new(x,x*other.abs*@uncertainty/@value)
76
+ end
77
+
78
+ def /(other)
79
+ if Uncertain === other
80
+ x = @value/other.value
81
+ Uncertain.new(x, x*Math.sqrt((@uncertainty/@value)**2+(other.uncertainty/other.value)**2))
82
+ else
83
+ x,y = coerce(other)
84
+ y / x
85
+ end
86
+ end
87
+
88
+ def inspect
89
+ "#{@value} +/- #{@uncertainty}"
90
+ end
91
+
92
+ def coerce(other)
93
+ if defined?(Unit) && Unit === other
94
+ [other, Unit.new(self)]
95
+ else
96
+ [Uncertain.new(other), self]
97
+ end
98
+ end
99
+
100
+ def to_s(format = '%g')
101
+ "#{format % @value} #{@@html_output ? '&plusmn;' : '+/-'} #{format % @uncertainty}"
102
+ end
103
+
104
+ def zero?
105
+ @value.zero? && @uncertainty.zero?
106
+ end
107
+
108
+ # Uncertain numbers only use the value part for comparisons
109
+ def <=>(other)
110
+ case other
111
+ when Uncertain:
112
+ self.value <=> other.value
113
+ else
114
+ x,y = coerce(other)
115
+ x <=> y
116
+ end
117
+ end
118
+
119
+ def ===(other)
120
+ case other
121
+ when Uncertain:
122
+ self.value == other.value && self.uncertainty == other.uncertainty
123
+ else
124
+ x,y = coerce(other)
125
+ x <=> y
126
+ end
127
+ end
128
+
129
+ end
130
+
131
+
132
+
133
+
134
+
135
+
@@ -0,0 +1,109 @@
1
+ # Code Generated by ZenTest v. 3.4.3
2
+ # classname: asrt / meth = ratio%
3
+ # Math: 0 / 3 = 0.00%
4
+
5
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
6
+
7
+ class TestMath < Test::Unit::TestCase
8
+ def test_class_acos
9
+ raise NotImplementedError, 'Need to write test_class_acos'
10
+ end
11
+
12
+ def test_class_acosh
13
+ raise NotImplementedError, 'Need to write test_class_acosh'
14
+ end
15
+
16
+ def test_class_asin
17
+ raise NotImplementedError, 'Need to write test_class_asin'
18
+ end
19
+
20
+ def test_class_asinh
21
+ raise NotImplementedError, 'Need to write test_class_asinh'
22
+ end
23
+
24
+ def test_class_atan
25
+ raise NotImplementedError, 'Need to write test_class_atan'
26
+ end
27
+
28
+ def test_class_atan2
29
+ raise NotImplementedError, 'Need to write test_class_atan2'
30
+ end
31
+
32
+ def test_class_atanh
33
+ raise NotImplementedError, 'Need to write test_class_atanh'
34
+ end
35
+
36
+ def test_class_cos
37
+ raise NotImplementedError, 'Need to write test_class_cos'
38
+ end
39
+
40
+ def test_class_cosh
41
+ raise NotImplementedError, 'Need to write test_class_cosh'
42
+ end
43
+
44
+ def test_class_erf
45
+ raise NotImplementedError, 'Need to write test_class_erf'
46
+ end
47
+
48
+ def test_class_erfc
49
+ raise NotImplementedError, 'Need to write test_class_erfc'
50
+ end
51
+
52
+ def test_class_exp
53
+ raise NotImplementedError, 'Need to write test_class_exp'
54
+ end
55
+
56
+ def test_class_frexp
57
+ raise NotImplementedError, 'Need to write test_class_frexp'
58
+ end
59
+
60
+ def test_class_hypot
61
+ raise NotImplementedError, 'Need to write test_class_hypot'
62
+ end
63
+
64
+ def test_class_ldexp
65
+ raise NotImplementedError, 'Need to write test_class_ldexp'
66
+ end
67
+
68
+ def test_class_log
69
+ raise NotImplementedError, 'Need to write test_class_log'
70
+ end
71
+
72
+ def test_class_log10
73
+ raise NotImplementedError, 'Need to write test_class_log10'
74
+ end
75
+
76
+ def test_class_sin
77
+ raise NotImplementedError, 'Need to write test_class_sin'
78
+ end
79
+
80
+ def test_class_sinh
81
+ raise NotImplementedError, 'Need to write test_class_sinh'
82
+ end
83
+
84
+ def test_class_sqrt
85
+ raise NotImplementedError, 'Need to write test_class_sqrt'
86
+ end
87
+
88
+ def test_class_tan
89
+ raise NotImplementedError, 'Need to write test_class_tan'
90
+ end
91
+
92
+ def test_class_tanh
93
+ raise NotImplementedError, 'Need to write test_class_tanh'
94
+ end
95
+
96
+ def test_class_uncertain_exp
97
+ raise NotImplementedError, 'Need to write test_class_uncertain_exp'
98
+ end
99
+
100
+ def test_class_uncertain_log
101
+ raise NotImplementedError, 'Need to write test_class_uncertain_log'
102
+ end
103
+
104
+ def test_class_uncertain_sqrt
105
+ raise NotImplementedError, 'Need to write test_class_uncertain_sqrt'
106
+ end
107
+ end
108
+
109
+ # Number of errors detected: 26
@@ -0,0 +1,14 @@
1
+ # Code Generated by ZenTest v. 3.4.3
2
+ # classname: asrt / meth = ratio%
3
+ # Numeric: 0 / 1 = 0.00%
4
+
5
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
6
+
7
+ class TestNumeric < Test::Unit::TestCase
8
+ def test_to_uncertain
9
+ assert_equal (1.to_uncertain(0.1)),Uncertain(1,0.1)
10
+ assert_equal (1.0.to_uncertain(0.1)),Uncertain(1.0,0.1)
11
+ end
12
+ end
13
+
14
+ # Number of errors detected: 22
@@ -0,0 +1,17 @@
1
+ # Code Generated by ZenTest v. 3.4.3
2
+ # classname: asrt / meth = ratio%
3
+ # Object: 0 / 1 = 0.00%
4
+ # Number of errors detected: 1
5
+
6
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
7
+ require 'uncertain'
8
+
9
+ class TestObject < Test::Unit::TestCase
10
+ def test_uncertain
11
+ a = Uncertain(1,0.1)
12
+ assert a.kind_of?(Uncertain)
13
+ assert a.kind_of?(Numeric)
14
+ assert_equal a.value, 1
15
+ assert_equal a.uncertainty, 0.1
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # Code Generated by ZenTest v. 3.4.3
2
+ # classname: asrt / meth = ratio%
3
+ # String: 0 / 1 = 0.00%
4
+
5
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
6
+
7
+
8
+ class TestString < Test::Unit::TestCase
9
+
10
+ def test_uncertain_format
11
+ assert_equal "#{'%0.1f' % Uncertain(1,0.1)}", "1.0 +/- 0.1"
12
+ end
13
+
14
+ end
15
+
16
+ # Number of errors detected: 67
@@ -0,0 +1,69 @@
1
+ # Code Generated by ZenTest v. 3.4.3
2
+ # classname: asrt / meth = ratio%
3
+ # Numeric: 0 / 1 = 0.00%
4
+ # Math: 0 / 3 = 0.00%
5
+ # Object: 0 / 1 = 0.00%
6
+ # Uncertain: 0 / 10 = 0.00%
7
+ # String: 0 / 1 = 0.00%
8
+
9
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
10
+
11
+
12
+ class TestUncertain < Test::Unit::TestCase
13
+
14
+ def test_html_output
15
+ assert Uncertain.html_output=true
16
+ assert_equal Uncertain(1,0.1).to_s, "1 &plusmn; 0.1"
17
+ end
18
+
19
+ def test_compare
20
+ assert Uncertain(1,0.1) > Uncertain(0,0.1)
21
+ assert Uncertain(0,0.1) < Uncertain(1,0.1)
22
+ end
23
+
24
+ def test_coerce
25
+ raise NotImplementedError, 'Need to write test_coerce'
26
+ end
27
+
28
+ def test_div
29
+ raise NotImplementedError, 'Need to write test_div'
30
+ end
31
+
32
+ def test_minus
33
+ raise NotImplementedError, 'Need to write test_minus'
34
+ end
35
+
36
+ def test_plus
37
+ raise NotImplementedError, 'Need to write test_plus'
38
+ end
39
+
40
+ def test_times
41
+ raise NotImplementedError, 'Need to write test_times'
42
+ end
43
+
44
+ def test_times2
45
+ raise NotImplementedError, 'Need to write test_times2'
46
+ end
47
+
48
+ def test_unary_minus
49
+ raise NotImplementedError, 'Need to write test_unary_minus'
50
+ end
51
+
52
+ def test_uncertainty
53
+ raise NotImplementedError, 'Need to write test_uncertainty'
54
+ end
55
+
56
+ def test_uncertainty_equals
57
+ raise NotImplementedError, 'Need to write test_uncertainty_equals'
58
+ end
59
+
60
+ def test_value
61
+ raise NotImplementedError, 'Need to write test_value'
62
+ end
63
+
64
+ def test_value_equals
65
+ raise NotImplementedError, 'Need to write test_value_equals'
66
+ end
67
+ end
68
+
69
+ # Number of errors detected: 128
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: uncertain
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-01-17 00:00:00 -05:00
8
+ summary: "== DESCRIPTION: 'Uncertain' adds a Numeric class that encapsulates and handles numbers with uncertainty (e.g., 1.0 +/- 0.2)."
9
+ require_paths:
10
+ - lib
11
+ email: kevin.olbrich@gmail.com
12
+ homepage: http://rubyforge.org/projects/uncertain/
13
+ rubyforge_project: uncertain
14
+ description: "'Uncertain' adds a Numeric class that encapsulates and handles numbers with uncertainty (e.g., 1.0 +/- 0.2)."
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Kevin Olbrich
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - bin/uncertain
37
+ - lib/uncertain.rb
38
+ - lib/math.rb
39
+ - lib/numeric.rb
40
+ - lib/object.rb
41
+ - lib/string.rb
42
+ - test/test_uncertain.rb
43
+ - test/test_numeric.rb
44
+ - test/test_object.rb
45
+ - test/test_string.rb
46
+ test_files:
47
+ - test/test_math.rb
48
+ - test/test_numeric.rb
49
+ - test/test_object.rb
50
+ - test/test_string.rb
51
+ - test/test_uncertain.rb
52
+ rdoc_options: []
53
+
54
+ extra_rdoc_files: []
55
+
56
+ executables:
57
+ - uncertain
58
+ extensions: []
59
+
60
+ requirements: []
61
+
62
+ dependencies:
63
+ - !ruby/object:Gem::Dependency
64
+ name: hoe
65
+ version_requirement:
66
+ version_requirements: !ruby/object:Gem::Version::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.1.7
71
+ version: