Tuesday, July 21, 2015

Tuesday, July 7, 2015

My first application of the Perl Data Language

I've been installing and testing and playing with PDL for a few years now, but I've never had a need to use it for anything at work. Until now! Recently I needed to update the coordinate file for a set of devices to test. The layout editor showed that the centers of the devices were not rectangular shapes but polygons instead. That made it difficult for the software to give me a central coordinate of the object. What L-Edit does give me though are the vertices of the polygon that makes up that object, and that data was easy enough for me to copy and paste into TextPad. With a few regular expressions and search/replace commands I was able to format the data in a way that Perl could read. I then used PDL to quickly calculate the centroid of the coordinate set, and voila! I had my center coordinate for the device I needed to test. It was my first real use of PDL and I think it saved me a few lines of code. The cool thing was turning it into a PDL function, which could be called by the PDL REPL.

#!/usr/bin/env perl
use Modern::Perl;
use PDL;

sub FDC53_NE_9x9_X4Y0_Centroid() {
    my @V;
    push @V, [53028.301,22242.396];
    push @V, [53028.477,22241.163]; 
    push @V, [53028.532,22240.839];
    push @V, [53028.646,22240.268]; 
    push @V, [53028.958,22239.265];
    push @V, [53029.368,22238.308]; 
    push @V, [53029.672,22237.713];
    push @V, [53029.932,22237.280]; 
    push @V, [53030.687,22236.313];
    push @V, [53031.811,22235.189]; 
    push @V, [53032.783,22234.430];
    push @V, [53033.207,22234.176]; 
    push @V, [53033.814,22233.865];
    push @V, [53034.769,22233.456]; 
    push @V, [53035.763,22233.147];
    push @V, [53036.373,22233.025]; 
    push @V, [53036.628,22232.982];
    push @V, [53037.896,22232.801]; 
    push @V, [53038.608,22232.750];
    push @V, [53127.893,22232.750]; 
    push @V, [53128.603,22232.800];
    push @V, [53129.865,22232.981]; 
    push @V, [53130.134,22233.027];
    push @V, [53130.732,22233.147]; 
    push @V, [53131.735,22233.458];
    push @V, [53132.692,22233.868]; 
    push @V, [53133.287,22234.172];
    push @V, [53133.720,22234.432]; 
    push @V, [53134.687,22235.187];
    push @V, [53135.815,22236.315]; 
    push @V, [53136.566,22237.277];
    push @V, [53136.824,22237.707]; 
    push @V, [53137.134,22238.314];
    push @V, [53137.544,22239.270]; 
    push @V, [53137.852,22240.262];
    push @V, [53137.972,22240.861]; 
    push @V, [53138.020,22241.141];
    push @V, [53138.199,22242.396]; 
    push @V, [53138.250,22243.108];
    push @V, [53138.250,22332.893]; 
    push @V, [53138.200,22333.603];
    push @V, [53138.019,22334.865]; 
    push @V, [53137.973,22335.134];
    push @V, [53137.853,22335.732]; 
    push @V, [53137.542,22336.735];
    push @V, [53137.132,22337.692]; 
    push @V, [53136.828,22338.287];
    push @V, [53136.568,22338.720]; 
    push @V, [53135.813,22339.687];
    push @V, [53134.681,22340.819]; 
    push @V, [53133.727,22341.564];
    push @V, [53133.293,22341.824]; 
    push @V, [53132.686,22342.135];
    push @V, [53131.731,22342.544]; 
    push @V, [53130.737,22342.853];
    push @V, [53130.127,22342.975]; 
    push @V, [53129.872,22343.018];
    push @V, [53128.604,22343.199]; 
    push @V, [53127.892,22343.250];
    push @V, [53038.607,22343.250]; 
    push @V, [53037.897,22343.200];
    push @V, [53036.635,22343.019]; 
    push @V, [53036.366,22342.973];
    push @V, [53035.768,22342.853]; 
    push @V, [53034.765,22342.542];
    push @V, [53033.808,22342.132]; 
    push @V, [53033.213,22341.828];
    push @V, [53032.780,22341.568]; 
    push @V, [53031.813,22340.813];
    push @V, [53030.686,22339.686]; 
    push @V, [53029.933,22338.722];
    push @V, [53029.676,22338.293]; 
    push @V, [53029.365,22337.686];
    push @V, [53028.956,22336.731]; 
    push @V, [53028.647,22335.737];
    push @V, [53028.525,22335.127]; 
    push @V, [53028.482,22334.872];
    push @V, [53028.301,22333.604]; 
    push @V, [53028.250,22332.892];
    push @V, [53028.250,22243.108]; 
   
    my $data = pdl(@V);
    my $mySums = sumover $data->xchg(0,1);
    my @dims = $data->dims;
    return $mySums / $dims[1];
}
       
1;