Perl PDL 中的 R ifelse 等价物是什么



我是PDL的新手。R 的 ifelse(( 方法可以进行条件元素选择。例如

x <- c(1,2,3,4)
ifelse(x%%2, x, x*2)
# [1] 1 4 3 8

有人知道如何在 PDL 中做到这一点吗?我知道你可以像下面这样做,但是有没有更好的方法?

pdl(map { $_ % 2 ? $_ : $_*2 } @{$x->unpdl} )
#! /usr/bin/perl
use warnings;
use strict;
use PDL;
my $x     = 'PDL'->new([1, 2, 3, 4]);
my $where = ! ($x % 2);               # [0 1 0 1]
my $y     = $x * ($where + 1);
print $y;                             # [1 4 3 8]

或者,很快

my $y = $x * ( 2 - $x % 2 );

自己回答这个问题。它可以是这样的,

use PDL;                                                                      
sub ifelse {                                                                  
    my ( $test, $yes, $no ) = @_;                                             
    $test = pdl($test);                                                       
    my ( $ok, $nok ) = which_both($test);                                     
    my $rslt = zeros( $test->dim(0) );                                        
    unless ( $ok->isempty ) {                                                 
        $yes = pdl($yes);                                                     
        $rslt->slice($ok) .= $yes->index( $ok % $yes->dim(0) );               
    }                                                                         
    unless ( $nok->isempty ) {                                                
        $no = pdl($no);                                                       
        $rslt->slice($nok) .= $no->index( $nok % $no->dim(0) );               
    }                                                                         
    return $rslt;                                                             
}                                                                             
my $x = pdl( 1, 2, 3, 4 );                                                    
say ifelse( $x % 2, $x, $x * 2 );       # [1 4 3 8]                                             
say ifelse( $x % 2, 5, sequence( 3 ) ); # [5 1 5 0]                                      
say ifelse( 42, $x, $x * 2 );           # [1]

在PDL中,这类事情的一般解决方案可能涉及切片和类似。查看PDL(2.077(的最新发行说明,它有一个新where_both,我想起了这个问题(披露:我目前的维护者(。虽然您的具体问题仅涉及偶数值的更改,但我还将展示将赔率加 2 的情况:

my ($odd, $even) = where_both($x, $x % 2);
$odd += 2, $even *= 2; # the "," form is just a flourish, it could be 2 lines

它以适当的PDL风格高效,因为$x的扫描只发生一次(你不会惊讶地发现它也在引擎盖下使用which_both(,并且突变只查看相关的片段。与您的代码非常相似,但它被捕获到一个小的、可广泛重用的函数中。(如果你有兴趣的话,我写它是为了将 TriD EuclidAxes 的东西从使用 Perl for 循环转变为实际使用 ndarrays(

$x ? $y : $z好?不是我想的,但这是风格和品味的问题

sub ifelse {
    my ($x,$y,$z) = @_;
    $x ? $y : $z ;
    if($x){$y}else{$z} ;
    [$y,$z]->[!$x] ;
    [$z,$y]->[!!$x] ;
    ($x && $y) || $z ;        # valid only if $y is always true
    (!$x && $z) || $y ;       # valid only if $z is always true
}

最新更新