anirbanphys has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am parsing some data and structured in 9x9 or 3x3 tables.

Now I need to get a result at a common point for those table values and for my work I have to use Bi-Cubic Interpolation. But I am not able to get any perl module in CPAN. Is there any way to achieve it?

Example for a 3x3 Table and there corresponding values:

1 2 3

4 0.1 0.2 0.3

5 0.4 0.5 0.6

6 0.7 0.8 0.9

I need to get a value at (2.5,5.8) point using bicubic interpolation.

Replies are listed 'Best First'.
Re: Bicubic Interpolation Math Function
by BrowserUk (Patriarch) on May 12, 2016 at 17:37 UTC

    Do you have a feel for what result you expect for your example? Does 0.75515 sound right?

    This won't win any awards for speed, but it might serve as a starting point for something quicker (assuming it is correct):

    #! perl -slw use strict; use Data::Dump qw[ pp ]; sub cubicInterpolate { my $x = shift; return $_[1] + 0.5 * $x*( $_[2] - $_[0] + $x*( 2*$_[0] - 5*$_[1] + + 4*$_[2] - $_[3] + $x*( 3*( $_[1]-$_[2] ) + $_[3] - $_[0] ) ) ); } sub bicubicInterpolate { my( $x, $y, $aref ) = @_; return cubicInterpolate( $x, map cubicInterpolate( $y, @{ $aref->[ + $_] } ), 0 .. $#$aref ); } sub readTable { my $fh = shift; my @table; my @xs = split ' ', <DATA>; my( @ys, @vals ); while( <$fh> ) { my( $y, @vals ) = split; @{ $table[ $y ] }[ @xs ] = @vals; } return \@table; } sub get4x4 { my( $x, $y, $table ) = @_; my @m; $x = int( $x ); $y = int( $y ); for my $yi ( $y-1 .. $y+2 ) { my $yt = $yi < 0 ? 0 : $yi > $#{ $table } ? $#{ $table } : $yi +; push @m, []; for my $xi ( $x-1 .. $x+2 ) { my $xt = $xi < 0 ? 0 : $xi > $#{ $table->[ $yt ] } ? $#{ $ +table->[ $yt ] } : $xi; push @{ $m[ $#m ] }, $table->[ $yt ][ $xt ]; } } return \@m; } my $table = readTable( *DATA ); pp $table; my $m4x4 = get4x4( 2.5, 5.8, $table ); pp $m4x4; my $interpolated = bicubicInterpolate( 0.5, 0.8, $m4x4 ); print $interpolated; __DATA__ 1 2 3 4 0.1 0.2 0.3 5 0.4 0.5 0.6 6 0.7 0.8 0.9

    Output:

    C:\test>1162861.pl [ undef, undef, undef, undef, [undef, 0.1, 0.2, 0.3], [undef, 0.4, 0.5, 0.6], [undef, 0.7, 0.8, 0.9], ] [ [0.1, 0.2, 0.3, 0.3], [0.4, 0.5, 0.6, 0.6], [0.7, 0.8, 0.9, 0.9], [0.7, 0.8, 0.9, 0.9], ] 0.75515

    It's based upon this;


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Yes, it should work the way you had written the code. Very sorry for the delayed response. I was checking with the real/actual numbers and from the actual calculation the code result deviating 5%. Now I have to work more to get the more accurate result. But this is a very good reference to work with. I am really thankful for providing such guidance.

        Now I have to work more to get the more accurate result.

        Take note of the section entitled:"The first and the last interval".

        The code I posted uses the first method; perhaps if you switched it to the second method it would be closer to what you are looking for.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Bicubic Interpolation Math Function
by GotToBTru (Prior) on May 12, 2016 at 15:25 UTC

    PDL::Interpolate only handles linear interpolation, but perhaps this could be your chance to contribute by adding the bicubic variation!

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Bicubic Interpolation Math Function
by VinsWorldcom (Prior) on May 12, 2016 at 16:17 UTC

    Math::Function::Interpolator and Math::Spline both mention "cubic interpolation" in their respective POD. Wikipedia tells me there is a difference and that bicubic is a version of cubic interpolation. All a bit over my head ...