user765443
user765443

Reputation: 1892

PDL::Slatec::chcm example/usage

I have list of data points that contain x and y following way. I need to determine monotonicity in Perl(due to legacy as I do not have much info about perl). I have searched and found the following url which contain such information But has not provided any example. Any example will help me a lot.

http://pdl.perl.org/?docs=Slatec&title=PDL::Slatec#chcm

1.00000000000001e-08 4.9281e-02
2.00000000000001e-08 4.9280e-02
3.00000000000001e-08 4.2290e-01
4e-08 4.3381e-01
5.00000000000001e-08 4.3801e-01
6.00000000000001e-08 4.3965e-01
7.00000000000001e-08 4.4004e-01
8.00000000000001e-08 4.4020e-01
9e-08 4.4025e-01
1e-07 4.4026e-01
1.1e-07 5.1206e-02
1.2e-07 4.9925e-02
1.3e-07 4.9480e-02
1.4e-07 4.9326e-02
1.5e-07 4.9295e-02
1.6e-07 4.9284e-02
1.7e-07 4.9280e-02
1.8e-07 4.9279e-02
1.9e-07 4.2288e-01
2e-07 4.3380e-01
2.1e-07 4.3801e-01
2.2e-07 4.3965e-01
2.3e-07 4.4023e-01
2.4e-07 4.4036e-01
2.5e-07 4.4035e-01
2.6e-07 4.4032e-01
2.7e-07 5.1209e-02
2.8e-07 4.9924e-02
2.9e-07 4.9475e-02
3e-07 4.9334e-02
3.1e-07 4.9294e-02
3.2e-07 4.9283e-02
3.3e-07 4.9280e-02
3.4e-07 4.9280e-02
3.5e-07 4.2289e-01
3.6e-07 4.3381e-01
3.7e-07 4.3801e-01
3.8e-07 4.3965e-01
3.9e-07 4.4023e-01
4e-07 4.4036e-01
4.1e-07 4.4035e-01
4.2e-07 4.4032e-01
4.3e-07 5.1209e-02
4.4e-07 4.9925e-02
4.5e-07 4.9478e-02
4.6e-07 4.9332e-02
4.7e-07 4.9295e-02
4.8e-07 4.9282e-02
4.9e-07 4.9279e-02
5e-07 4.9278e-02
5.1e-07 4.2289e-01
5.2e-07 4.3381e-01
5.3e-07 4.3801e-01
5.4e-07 4.3965e-01
5.5e-07 4.4023e-01
5.6e-07 4.4036e-01
5.7e-07 4.4035e-01
5.8e-07 4.4032e-01
5.9e-07 5.1209e-02
6e-07 4.9924e-02
6.1e-07 4.9474e-02
6.2e-07 4.9334e-02
6.3e-07 4.9294e-02
6.4e-07 4.9283e-02
6.5e-07 4.9280e-02
6.6e-07 4.9280e-02
6.7e-07 4.2287e-01
6.8e-07 4.3380e-01
6.9e-07 4.3801e-01
7e-07 4.3961e-01
7.1e-07 4.4004e-01
7.2e-07 4.4021e-01
7.3e-07 4.4027e-01
7.4e-07 4.4028e-01
7.5e-07 5.1207e-02
7.6e-07 4.9925e-02
7.7e-07 4.9481e-02
7.8e-07 4.9323e-02
7.9e-07 4.9294e-02
8e-07 4.9284e-02
8.1e-07 4.9281e-02
8.2e-07 4.9280e-02
8.3e-07 4.2289e-01
8.4e-07 4.3381e-01
8.5e-07 4.3801e-01
8.6e-07 4.3966e-01
8.7e-07 4.4023e-01
8.8e-07 4.4036e-01
8.9e-07 4.4035e-01
9e-07 4.4032e-01
9.1e-07 5.1208e-02
9.2e-07 4.9926e-02
9.3e-07 4.9481e-02
9.4e-07 4.9323e-02
9.5e-07 4.9294e-02
9.6e-07 4.9284e-02
9.7e-07 4.9281e-02
9.8e-07 4.9280e-02
9.9e-07 4.2288e-01

Upvotes: 2

Views: 40

Answers (1)

Håkon Hægland
Håkon Hægland

Reputation: 40778

Here is an example of how you an use the chim and chcm to check a piecewise cubic Hermite function for monotonicity:

use feature qw(say);
use strict;
use warnings;
use PDL;
use PDL::Slatec;

{
    my $x   = float( 1, 2, 3, 5, 6, 7 );
    my $f   = float( 1, 2, 3, 4, 5 ,6 );
    check_monotonicity($x, $f);
}
{
    my $x   = float( 1, 2, 3, 5, 6, 7 );
    my $f   = float( 6, 5, 4, 3, 2 ,1 );
    check_monotonicity($x, $f);
}

{
    my $x   = float( 1, 2, 3, 5, 6, 7 );
    my $f   = float( 6, 5, 4, 4, 5 ,6 );
    check_monotonicity($x, $f);
}

sub check_monotonicity {
    my ( $x, $f ) = @_;

    my ( $deriv, $err ) = chim($x, $f);
    if ( $err < 0 ) {
        say "Could not calculate derivative";
    }
    else {
        say "Derivative: ", $deriv;
        my $check = 1;
        my ( $ismon, $err ) = chcm($x, $f, $deriv, $check);
        if ( $err == -1 ) {
            say "Error: too few points..";
        }
        elsif ( $err == -3 ) {
            say "Error: x is not strictly increasing";
        }
        else {
            say "Monotonicity: ", $ismon;
        }
    }
}

Output:

Derivative: [1 1 0.692308 0.692308 1 1]
Monotonicity: [1 1 1 1 1 1]
Derivative: [-1 -1 -0.692308 -0.692308 -1 -1]
Monotonicity: [-1 -1 -1 -1 -1 -1]
Derivative: [-1 -1 0 0 1 1]
Monotonicity: [-1 -1 0 1 1 2]

Upvotes: 2

Related Questions