File Coverage

blib/lib/Math/Approx/Symbolic.pm
Criterion Covered Total %
statement 34 34 100.0
branch 4 6 66.6
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 46 48 95.8


line stmt bran cond sub pod time code
1            
2             =head1 NAME
3            
4             Math::Approx::Symbolic - Symbolic representation of interpolated polynomials
5            
6             =head1 SYNOPSIS
7            
8             use Math::Approx::Symbolic;
9             # ... use as you would use Math::Approx ...
10            
11             my $symbolic = $approximation->symbolic();
12            
13             # ... $symbolic is now a Math::Symbolic object.
14            
15             =head1 DESCRIPTION
16            
17             This module is a thin wrapper around the Math::Approx module. It subclasses
18             Math::Approx and adds the "symbolic" subroutine that returns a
19             Math::Symbolic object representing the calculated approximation.
20            
21             =head2 EXPORT
22            
23             None. Ever.
24            
25             =cut
26            
27             package Math::Approx::Symbolic;
28            
29 1     1   785 use 5.006;
  1         4  
  1         33  
30 1     1   5 use strict;
  1         2  
  1         30  
31 1     1   20 use warnings;
  1         1  
  1         35  
32            
33 1     1   753 use Math::Approx;
  1         4947  
  1         33  
34 1     1   869 use Math::Symbolic;
  1         590617  
  1         91  
35            
36 1     1   16 use base 'Math::Approx';
  1         2  
  1         455  
37            
38             our $VERSION = '0.100';
39            
40             =head2 symbolic() method
41            
42             This is the only method added to the ones from Math::Approx.
43             It takes an optional argument indicating the variable name to use
44             for the symbolic representation of the approximation polynomial.
45            
46             It returns a Math::Symbolic object representing the approximation
47             polynomial.
48            
49             =cut
50            
51             sub symbolic {
52 1     1 1 4160 my $self = shift;
53 1         3 my $var = shift;
54 1 50       8 $var = 'x' unless defined $var;
55 1 50       17 $var = Math::Symbolic::Variable->new($var)
56             unless ref($var) =~ /^Math::Symbolic::Variable/;
57 1         24 my $degree = $self->{N};
58 1         3 my @coeff = @{ $self->{A} };
  1         5  
59            
60 1         4 my $constant = shift @coeff;
61            
62 1         3 my $cur_degree = $degree;
63 1         2 my @exps;
64 1         2 foreach ( reverse @coeff ) {
65 5 100       36 push @exps,
66             Math::Symbolic::Operator->new(
67             '*',
68             Math::Symbolic::Constant->new($_),
69             (
70             $cur_degree == 1
71             ? $var
72             : Math::Symbolic::Operator->new(
73             '^', $var, Math::Symbolic::Constant->new($cur_degree)
74             )
75             )
76             );
77 5         340 $cur_degree--;
78             }
79 1         3 my $symbolic = shift @exps;
80 1         93 $symbolic += $_ foreach @exps;
81 1         172 return $symbolic + Math::Symbolic::Constant->new($constant);
82             }
83            
84             1;
85             __END__