| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Math::SymbolicX::Error; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '1.01'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
575811
|
use 5.006; |
|
|
3
|
|
|
|
|
13
|
|
|
|
3
|
|
|
|
|
118
|
|
|
6
|
3
|
|
|
3
|
|
14
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
91
|
|
|
7
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
92
|
|
|
8
|
3
|
|
|
3
|
|
876
|
use Number::WithError; |
|
|
3
|
|
|
|
|
11572
|
|
|
|
3
|
|
|
|
|
141
|
|
|
9
|
3
|
|
|
3
|
|
1488
|
use Math::Symbolic; |
|
|
3
|
|
|
|
|
144898
|
|
|
|
3
|
|
|
|
|
126
|
|
|
10
|
3
|
|
|
3
|
|
22
|
use Carp qw/confess cluck/; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
709
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Math::SymbolicX::ParserExtensionFactory ( |
|
14
|
|
|
|
|
|
|
'error' => sub { |
|
15
|
3
|
|
|
|
|
6068
|
my $argstring = shift; |
|
16
|
|
|
|
|
|
|
|
|
17
|
3
|
|
|
|
|
6
|
my $num; |
|
18
|
3
|
|
|
|
|
3
|
eval { $num = Number::WithError->new($argstring); }; |
|
|
3
|
|
|
|
|
27
|
|
|
19
|
3
|
50
|
33
|
|
|
410
|
confess "Could not generate Number::WithError object from '$argstring' " |
|
20
|
|
|
|
|
|
|
."in Math::Symbolic parse." |
|
21
|
|
|
|
|
|
|
if $@ or not defined $num; |
|
22
|
|
|
|
|
|
|
|
|
23
|
3
|
|
|
|
|
25
|
return Math::Symbolic::Constant->new($num); |
|
24
|
|
|
|
|
|
|
}, |
|
25
|
|
|
|
|
|
|
'error_big' => sub { |
|
26
|
3
|
|
|
|
|
9550
|
my $argstring = shift; |
|
27
|
|
|
|
|
|
|
|
|
28
|
3
|
|
|
|
|
6
|
my $num; |
|
29
|
3
|
|
|
|
|
5
|
eval { $num = Number::WithError->new_big($argstring); }; |
|
|
3
|
|
|
|
|
24
|
|
|
30
|
3
|
50
|
33
|
|
|
142436
|
confess "Could not generate Number::WithError object with Math::BigFloat representation from '$argstring' " |
|
31
|
|
|
|
|
|
|
."in Math::Symbolic parse." |
|
32
|
|
|
|
|
|
|
if $@ or not defined $num; |
|
33
|
|
|
|
|
|
|
|
|
34
|
3
|
|
|
|
|
30
|
return Math::Symbolic::Constant->new($num); |
|
35
|
|
|
|
|
|
|
}, |
|
36
|
3
|
|
|
3
|
|
2712
|
); |
|
|
3
|
|
|
|
|
2550
|
|
|
|
3
|
|
|
|
|
32
|
|
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
1; |
|
40
|
|
|
|
|
|
|
__END__ |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 NAME |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Math::SymbolicX::Error - Parser extension for dealing with numeric errors |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
use Math::Symbolic qw/parse_from_string/; |
|
49
|
|
|
|
|
|
|
use Math::SymbolicX::Error; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Inlined Number::WithError declarations: |
|
52
|
|
|
|
|
|
|
my $formula = parse_from_string('3 * error(3 +/- 0.2)^2 + error(1 +/- 0.1)'); |
|
53
|
|
|
|
|
|
|
print $formula->value(); |
|
54
|
|
|
|
|
|
|
# prints '2.80e+01 +/- 3.6e+00' |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# High precision support using Math::BigFloat |
|
57
|
|
|
|
|
|
|
my $high_precision = parse_from_string('3 * error_big(3e-12 +/- 0.2e-12'); |
|
58
|
|
|
|
|
|
|
print $high_precision->value(); |
|
59
|
|
|
|
|
|
|
# prints '9.00e-12 +/- 6.0e-13' |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
This module adds numeric error (or uncertainty) support to the Math::Symbolic |
|
64
|
|
|
|
|
|
|
parser. It does so by extending the parser grammar of the Math::Symbolic |
|
65
|
|
|
|
|
|
|
module (that is, the one stored in $Math::Symbolic::Parser) with certain |
|
66
|
|
|
|
|
|
|
special functions that create constants as L<Number::WithError> objects. |
|
67
|
|
|
|
|
|
|
(Math::Symbolic::Variable objects have been able to contain objects since |
|
68
|
|
|
|
|
|
|
the very beginning.) |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 MOTIVATION |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
All constants in strings that are parsed by Math::Symbolic::Parser are |
|
73
|
|
|
|
|
|
|
converted to Math::Symbolic::Constant objects holding the value |
|
74
|
|
|
|
|
|
|
associated to the constant in an ordinary Perl Scalar by default. |
|
75
|
|
|
|
|
|
|
Unfortunately, that means you are limited to real floating point numbers. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
On the other hand, it might be necessary to attach a certain error to |
|
78
|
|
|
|
|
|
|
a number and calculate using error propagation. Math::SymbolicX::Error |
|
79
|
|
|
|
|
|
|
helps making this process more opaque. Since the Math::Symbolic::Constant |
|
80
|
|
|
|
|
|
|
objects can hold any object, you can build your trees by hand using |
|
81
|
|
|
|
|
|
|
Math::Complex objects instead of Perl Scalars for the value of the constants. |
|
82
|
|
|
|
|
|
|
But since the Math::Symbolic::Parser is by far the most convenient interface |
|
83
|
|
|
|
|
|
|
to Math::Symbolic, there had to be a reasonably simple way of introducing |
|
84
|
|
|
|
|
|
|
Number::WithError support to the parser. So here goes. |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 USAGE |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
In order to numeric constants with errors in Math::Symbolic trees from |
|
89
|
|
|
|
|
|
|
the parser, you just load this extension module and wrap any of the |
|
90
|
|
|
|
|
|
|
functions listed hereafter around any constants that have an associated error. |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
The aforementioned functions are C<error()> and C<error_big()>. |
|
93
|
|
|
|
|
|
|
C<error()> turns its arguments into a Number::WithError object which is |
|
94
|
|
|
|
|
|
|
injected into the Math::Symbolic tree at the point in the parse tree at which |
|
95
|
|
|
|
|
|
|
the C<error()> function was found. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Similarily, C<error_big()> creates a Number::WithError object but with |
|
98
|
|
|
|
|
|
|
Math::BigFloat support. That is, arbitrary precision. |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 AUTHOR |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Steffen Mueller, E<lt>symbolic-module at steffen-mueller dot net<gt> |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Copyright (C) 2006-2008 by Steffen Mueller |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
109
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
|
110
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
New versions of this module can be found on |
|
115
|
|
|
|
|
|
|
http://steffen-mueller.net or CPAN. |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
You should definately be familiar with L<Number::WithError> before you start |
|
118
|
|
|
|
|
|
|
using this module because the objects that are returned |
|
119
|
|
|
|
|
|
|
from C<$formula->value()> calls are Number::WithError objects. |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Also have a look at L<Math::Symbolic>, |
|
122
|
|
|
|
|
|
|
and at L<Math::Symbolic::Parser> |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Refer to L<Math::SymbolicX::ParserExtensionFactory> for the implementation |
|
125
|
|
|
|
|
|
|
details. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Other parser extensions include big- and complex number support: |
|
128
|
|
|
|
|
|
|
L<Math::SymbolicX::BigNum>, L<Math::SymbolicX::Complex> |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |