| 1 | package Math::Function::Interpolator::Quadratic; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | our $VERSION = '1.03'; ## VERSION | |||||||||
| 8 | |||||||||||
| 9 | 4 | our @ISA = qw(Math::Function::Interpolator); | |||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 4 | ▶ | use Math::Cephes::Matrix qw(mat); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | Math::Function::Interpolator::Quadratic | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | use Math::Function::Interpolator::Quadratic; | ||||||||||
| 22 | |||||||||||
| 23 | my $interpolator = Math::Function::Interpolator::Quadratic->new( | ||||||||||
| 24 | points => {1=>2,2=>3,3=>4,4=>5,5=>6} | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | $interpolator->quadratic(2.5); | ||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | Math::Function::Interpolator::Quadratic helps you to do the interpolation calculation with quadratic method. | ||||||||||
| 32 | It solves the interpolated_y given point_x and a minimum of 3 data points. | ||||||||||
| 33 | |||||||||||
| 34 | =head1 FIELDS | ||||||||||
| 35 | |||||||||||
| 36 | =head2 points (REQUIRED) | ||||||||||
| 37 | |||||||||||
| 38 | HashRef of points for interpolations | ||||||||||
| 39 | |||||||||||
| 40 | =head1 METHODS | ||||||||||
| 41 | |||||||||||
| 42 | =head2 quadratic | ||||||||||
| 43 | |||||||||||
| 44 | quadratic | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | # Returns the interpolated_y value given point_x with 3 data points | ||||||||||
| 49 | 6 | ▶ | sub quadratic { | ||||||||
|
Subroutine
quadratic: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 50 | 6 | my ($self, $x) = @_; | |||||||||
| 51 | |||||||||||
| 52 | 6 | ▶ | confess "sought_point[$x] must be a number" unless looks_like_number($x); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 5 | my $ap = $self->points; | |||||||||
| 54 | 5 | ▶ | return $ap->{$x} if defined $ap->{$x}; # no need to interpolate | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | 5 | my @Xs = keys %$ap; | |||||||||
| 57 | 5 | ▶ | confess "cannot interpolate with fewer than 3 data points" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | if scalar @Xs < 3; | ||||||||||
| 59 | |||||||||||
| 60 | 4 | my @points = $self->closest_three_points($x, \@Xs); | |||||||||
| 61 | |||||||||||
| 62 | # Three cofficient | ||||||||||
| 63 | 4 | my $abc = mat([map { [$_**2, $_, 1] } @points]); | |||||||||
| 64 | |||||||||||
| 65 | 4 | my $y = [map { $ap->{$_} } @points]; | |||||||||
| 66 | |||||||||||
| 67 | 4 | my $solution; | |||||||||
| 68 | 4 | ▶ | eval { $solution = $abc->simq($y); 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | or confess 'Insoluble matrix: ' . $_; | ||||||||||
| 70 | 4 | my ($A, $B, $C) = @$solution; | |||||||||
| 71 | |||||||||||
| 72 | 4 | return ($A * ($x**2) + $B * $x + $C); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =head1 AUTHOR | ||||||||||
| 76 | |||||||||||
| 77 | Binary.com, C<< <perl at binary.com> >> | ||||||||||
| 78 | |||||||||||
| 79 | =head1 BUGS | ||||||||||
| 80 | |||||||||||
| 81 | Please report any bugs or feature requests to C<bug-math-function-interpolator at rt.cpan.org>, or through | ||||||||||
| 82 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-Function-Interpolator>. I will be notified, and then you'll | ||||||||||
| 83 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 84 | |||||||||||
| 85 | =head1 SUPPORT | ||||||||||
| 86 | |||||||||||
| 87 | You can find documentation for this module with the perldoc command. | ||||||||||
| 88 | |||||||||||
| 89 | perldoc Math::Function::Interpolator | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | You can also look for information at: | ||||||||||
| 93 | |||||||||||
| 94 | =over 4 | ||||||||||
| 95 | |||||||||||
| 96 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 97 | |||||||||||
| 98 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Math-Function-Interpolator> | ||||||||||
| 99 | |||||||||||
| 100 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 101 | |||||||||||
| 102 | L<http://annocpan.org/dist/Math-Function-Interpolator> | ||||||||||
| 103 | |||||||||||
| 104 | =item * CPAN Ratings | ||||||||||
| 105 | |||||||||||
| 106 | L<http://cpanratings.perl.org/d/Math-Function-Interpolator> | ||||||||||
| 107 | |||||||||||
| 108 | =item * Search CPAN | ||||||||||
| 109 | |||||||||||
| 110 | L<http://search.cpan.org/dist/Math-Function-Interpolator/> | ||||||||||
| 111 | |||||||||||
| 112 | =back | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | 4 | 1; # End of Math::Function::Interpolator::Quadratic | |||||||||