PDLã®åºç¤ãå¦ã¼ã
Perl
›
ã¢ã¸ã¥ã¼ã«
›
PDL
Rè¨èªã§çµ±è¨è§£æãè¡ã£ã¦ãã人ããPerlã§ã®PDLã使ã£ãçµ±è¨è§£æä½æ¥ãè¡ããããã«ãPDLã®å ¥éãæ¸ãã¦ã¿ããã¨æãã¾ããPerlã®çµ±è¨è§£æã©ã¤ãã©ãªã§ããPDLã使ããããã«ãªãã°ãExcelã§è¡ããããªè¤æ°åã®ãã¼ã¿ã®çµ±è¨å¦çãè¡ããããã«ãªãã¾ãã
ã¾ããã£ã¨ãç°¡åãª1次å ã®ãã¼ã¿æ§é ãä½æãã¦ã¿ã¾ãããã次ã®ã³ã¼ããè¦ã¦ãã ãããpdlé¢æ°ãå©ç¨ããã¨ã1次å ã®ãã¼ã¿æ§é ãä½æãããã¨ãã§ãã¾ãã
use strict; use warnings; use PDL::Lite; # ãã¼ã¿ã®ä½æ my $nums = pdl [2, 4, 7]; # ãã¼ã¿ã®åºå print $nums;
ãã®ãµã³ãã«ãå®è¡ããã¨ãç»é¢ã«ã¯æ¬¡ã®ããã«åºåããã¾ãã
[2 4 7]
PDLå¤æ°ã®ä½æ
ã¾ãpdlé¢æ°ã使ç¨ããã¨PDLå¤æ°ãä½æãããã¨ãã§ãã¾ããã¾ãPDLã®æä½éã®æ©è½ãå©ç¨ããããã®PDL::Liteãèªã¿è¾¼ã¿ã¾ãã
use PDL::Lite;
PDL::Liteãèªã¿è¾¼ãã¨pdlé¢æ°ã使ç¨ã§ããããã«ãªãã¾ããPDLå¤æ°ãä½æããã«ã¯pdlé¢æ°ã使ç¨ãã¾ãã
my $nums = pdl [2, 4, 7];
ãã¼ã¿ã®åå¾ã¨å¤æ´
pdlé¢æ°ã§ä½æããããã¼ã¿ã¯ãPDLå¤æ°ã¨å¼ã°ãã¾ãã1次å ã®PDLå¤æ°ã®è¦ç´ ãåå¾ãã¦ã¿ã¾ãããããã¼ã¿ãåå¾ããã«ã¯atã¡ã½ããã使ç¨ãã¾ãã
my $first = $nums->at(0); my $second = $nums->at(1);
ä¸è¨ã®ä¾ã§ã¯ãPDLå¤æ°ã«å«ã¾ããè¦ç´ ã®å¤ãåå¾ãã¾ããããã©ãPDLå¤æ°ã¨ãã¦åå¾ãããã¨ãã§ãã¾ãã
ä¸é¨ã®ãã¼ã¿ãPDLå¤æ°ã¨ãã¦åå¾ããã«ã¯ãããã¤ãã®æ¹æ³ãããã¾ãããã¤ã¹ã¹ã©ã¤ã¹è¨æ³ã§è¨è¿°ããã®ãç°¡åã§ãããã¤ã¹ã¹ã©ã¤ã¹è¨æ³ãè¨è¿°ããããã®PDL::NiceSliceã¢ã¸ã¥ã¼ã«ãèªã¿ã¾ããããããã¯ã¹ã¯ãªããã®å é ã§è¡ã£ã¦ããã¾ãã
use PDL::NiceSlice;
ãè¦ç´ ãPDLå¤æ°ã¨ãã¦åå¾ããã«ã¯$å¤æ°å(è¦ç´ çªå·)ã¨ããè¨æ³ã使ç¨ãã¾ãã
# è¦ç´ ãPDLå¤æ°ã¨ãã¦åå¾ my $pdl_first = $nums(0); my $pdl_second = $nums(1);
ãããã¯PDLå¤æ°ã§ããããè¦ç´ ã®å¤èªä½ãåå¾ããããã°atã¡ã½ããã§åãåºããã¨ãã§ãã¾ãã
my $first = $pdl_first->at(0); my $second = $pdl_second->at(0);
è¦ç´ ã®å¤ã®å¤æ´ãè¡ãå ´åã¯ãããã¤ã¹ã¹ã©ã¤ã¹è¨æ³ã使ç¨ãã¾ãã.=æ¼ç®åã使ç¨ãã¾ãã.=ã¯æååã®çµåã§ã¯ãªãã¦ãä»£å ¥ã®ããã«ãªã¼ãã¼ã©ã¤ãããã¦ãã¾ãã
# è¦ç´ ã®å¤ãå¤æ´ $nums(0) .= 5;
PDLã§ã¯PDLå¤æ°ã®è¤æ°ã®é¨åãæ°ããPDLå¤æ°ã¨ãã¦åãåºããã¨ãã§ãã¾ããäºçªç®ã¨ä¸çªç®ã®è¦ç´ ãåãåºãã¦è¦ã¾ãããã次ã®ããã«$å¤æ°å(n:m)ã¨ããè¨æ³ãå©ç¨ããã¨ãPDLå¤æ°ã®ä¸é¨ãåãåºããã¨ãã§ãã¾ãã
my $pdl_parts = $nums(1:2);
åãåºããPDLå¤æ°ã®ãã¹ã¦ã®è¦ç´ ã«å¤ãä»£å ¥ãããã¨ãã§ãã¾ãã
$nums(1:2) .= 8;
ãã®ããã«åãåºããé¨åã®éåã«å¯¾ãã¦ãåãæ¼ç®ãè¡ããã¨ãã§ããã®ãPDLã®ç¹å¾´ã«ãªã£ã¦ãã¾ãã
1次å ã®ãã¼ã¿ã®é·ããåå¾ãã¦ã¿ã¾ãããããã¼ã¿ã®é·ããåå¾ããã«ã¯dimsã¡ã½ããã使ç¨ãã¾ãã
my $length = $nums->dims;
ãµã³ãã«
å®è¡ã§ãããµã³ãã«ã§ãã
use strict; use warnings; use PDL::Lite; use PDL::NiceSlice; # ãã¼ã¿ã®ä½æ my $nums = pdl [2, 4, 7]; # è¦ç´ ã®åºå print $nums->at(0) . "\n"; print $nums->at(1) . "\n"; # PDLå¤æ°ã®åãåºã my $pdl_first = $nums(0); my $pdl_second = $nums(1); # ä»£å ¥ $nums(0) .= 5; # è¤æ°è¦ç´ ã®PDLå¤æ°ã®åå¾ my $pdl_parts = $nums(1:2); # PDLå¤æ°ã¨ãã¦åãåºããè¤æ°ã®è¦ç´ ã¸ã®ä»£å ¥ $nums(1:2) .= 8; print $nums . "\n";
å®æ°ã®ååæ¼ç®
PDLå¤æ°ã®ãã¹ã¦ã®å¤ã«åãå¤ãå ç®ããã«ã¯+æ¼ç®åã使ç¨ãã¾ãã
use PDL::Lite; my $pdl = pdl [1, 1, 2, 2, 2, 3]; my $pdl_add = $pdl + 3;
å¼ãç®ãæãç®ãå²ãç®ãåãã¹ãä¹ãåãããã«è¨ç®ãããã¨ãã§ãã¾ãã
# å¼ãç® my $pdl_sub = $pdl - 3; # æãç® my $pdl_product = $pdl * 3; # å²ãç® my $pdl_div = $pdl / 3; # å my $pdl_quotient = $pdl % 3; # ã¹ãä¹ my $pdl_pow = $pdl + 3;
æãç®ã¨å²ãç®ã«ã«ãããã®ã¯ããã¯ãã«ã®å®æ°åã«è©²å½ãã¾ããPDLå¤æ°ã©ããã®æ¼ç®ã§ã¯ãªãã¦ãPDLå¤æ°ã¨æ®éã®å¤ã¨ã®æ¼ç®ãè¡ã£ã¦ãããã¨ãæèã«æ¢ãã¦ããã¾ãããã
ã¦ãã¼ã¯ãªå¤ãåãåºã
ã¦ãã¼ã¯ãªå¤ãåãåºãã«ã¯uniqã¡ã½ããã使ç¨ãã¾ãã
my $pdl_uniq = $pdl->uniq;
PDLå¤æ°ã©ããã®ååæ¼ç®
PDLå¤æ°ã©ãããå ç®ããã«ã¯ãPDLå¤æ°ã©ããã«å¯¾ãã¦+æ¼ç®åã使ç¨ãã¾ããåè¦ç´ ãå ç®ããã¾ãã
# PDLå¤æ°ã©ããã®å ç® my $pdl1 = pdl [1, 2, 3]; my $pdl2 = pdl [4, 5, 6]; my $pdl_add = $pdl1 + $pdl2;
å¼ãç®ãæãç®ãå²ãç®ãåãã¹ãä¹ãåãããã«è¨ç®ãããã¨ãã§ãã¾ããåè¦ç´ ãè¨ç®ããã¾ãã
# å¼ãç® my $pdl_sub = $pdl1 - $pdl2; # æãç® my $pdl_product = $pdl1 * $pdl2; # å²ãç® my $pdl_div = $pdl1 / $pdl2; # å my $pdl_quotient = $pdl1 % $pdl2; # ã¹ãä¹ my $pdl_pow = $pdl1 + $pdl2;
å æ¸ç®ã¯ããã¯ãã«ã®å æ¸ç®ã«è©²å½ãã¾ãã
å ç©
å ç©ãè¨ç®ããç¹å¥ãªæ¹æ³ã¯ããã¾ãããå ç©ãè¨ç®ããã«ã¯PDLå¤æ°ã©ããã®æãç®ãè¡ã£ã¦ããããã®åãæ±ãã¾ãã
# å ç© my $inner_product = ($pdl1 * $pdl2)->sum;
å ç©ã®çµæã¯ãPDLå¤æ°ã§ã¯ãªãã£ã¦ããã ã®å¤ã«ãªãã¾ãã
ãµã³ãã«
ãå®è¡ã§ãããµã³ãã«ã§ãã
use strict; use warnings; use PDL::Lite; # 足ãç® my $pdl = pdl [1, 1, 2, 2, 2, 3]; my $pdl_add = $pdl + 3; # å¼ãç® my $pdl_sub = $pdl - 3; # æãç® my $pdl_product = $pdl * 3; # å²ãç® my $pdl_div = $pdl / 3; # å my $pdl_quotient = $pdl % 3; # ã¦ãã¼ã¯ãªå¤ãåå¾ my $pdl_uniq = $pdl->uniq; # è¨ç®çµæ print "$pdl_add\n"; print "$pdl_sub\n"; print "$pdl_product\n"; print "$pdl_div\n"; print "$pdl_quotient\n"; print "$pdl_uniq\n";
use strict; use warnings; use PDL::Lite; # PDLå¤æ°ã©ããã®å ç® my $pdl1 = pdl [1, 2, 3]; my $pdl2 = pdl [4, 5, 6]; my $pdl_add = $pdl1 + $pdl2; # å¼ãç® my $pdl_sub = $pdl1 - $pdl2; # æãç® my $pdl_product = $pdl1 * $pdl2; # å²ãç® my $pdl_div = $pdl1 / $pdl2; # å my $pdl_quotient = $pdl1 % $pdl2; # ã¹ãä¹ my $pdl_pow = $pdl1 + $pdl2; # å ç© my $inner_product = ($pdl1 * $pdl2)->sum; # è¨ç®çµæ print "$pdl_add\n"; print "$pdl_sub\n"; print "$pdl_product\n"; print "$pdl_div\n"; print "$pdl_quotient\n"; print "$inner_product\n";