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

I'm testing some module code I'm working on and need a way to visualise a surface. PDL seems a pretty natural fit for manipulating the data. However I would like to initialise a 2D structure with values that are a function of grid location. Ideally I'd be able to do something like:

use warnings; use strict; use PDL; my @funcs = ( '1/(0.5 + 0.02 * $i*$i) * cos($i)', '1/(0.5 + 0.02 * $i*$i) * cos($i)', ); my $surface = initPDL (\&shape, (10) x @funcs); ... sub shape { my @values = @_; my $sum = 0; for my $idx (0 .. $#values) { my $i = $values[$idx]; my $value = eval $funcs[$idx]; $sum += $value * $value; } return sqrt ($sum); }

The "tricky/cool" bit is the putative my $surface = initPDL (\&shape, (10) x @funcs); line. Does PDL provide a way do do something of that sort?


Perl reduces RSI - it saves typing

Replies are listed 'Best First'.
Re: I need a liddle piddle advice
by plobsing (Friar) on Oct 14, 2008 at 07:49 UTC
    I'm assuming you want a PDL with the value of  sqrt( 1/( 0.5 + 0.02*$x**2 )*cos($x) + 1/( 0.5 + 0.02*$y**2 )*cos($y) ) for x and y intervals of 0 to 10, stepping by 1.

    The closest thing I've seen is things similar to the example in the synopsis of PDL::Graphics::TriD::Contours.

    Here is a lightly modified version with your function:
    use PDL; use strict; use warnings; use constant X_NPOINTS => 11; use constant Y_NPOINTS => 11; my $proto = zeroes( X_NPOINTS, Y_NPOINTS ); my $x = $proto->xvals; my $y = $proto->yvals; my $z = sqrt( 1/(0.5 + 0.02*$x**2)*cos($x) + 1/(0.5 + 0.02*$y**2)*cos($y) );
    Update: Fixed broken link

      Excellent, that makes sense. Thank you. There is a small error in your implementation of my sample function. It should be:

      my $z = sqrt( (1/(0.5 + 0.02*$x**2)*cos($x))**2 + (1/(0.5 + 0.02*$y**2)*cos($y))**2 );

      But the illustration of the technique was the important thing!


      Perl reduces RSI - it saves typing