File Coverage

blib/lib/Statistics/R/REXP/Complex.pm
Criterion Covered Total %
statement 32 32 100.0
branch 9 10 90.0
condition 4 9 44.4
subroutine 10 10 100.0
pod 0 2 0.0
total 55 63 87.3


line stmt bran cond sub pod time code
1             package Statistics::R::REXP::Complex;
2             # ABSTRACT: an R numeric vector
3             $Statistics::R::REXP::Complex::VERSION = '1.0';
4 11     11   15358 use 5.010;
  11         24  
5              
6 11     11   35 use Scalar::Util qw(blessed looks_like_number);
  11         12  
  11         497  
7 11     11   6001 use Math::Complex qw();
  11         78178  
  11         297  
8              
9 11     11   816 use Class::Tiny::Antlers qw(-default around);
  11         3922  
  11         89  
10 11     11   1598 use namespace::clean;
  11         8967  
  11         72  
11              
12             extends 'Statistics::R::REXP::Vector';
13 11     11   1633 use overload;
  11         14  
  11         46  
14              
15              
16 11     11   346 use constant sexptype => 'CPLXSXP';
  11         13  
  11         3827  
17              
18              
19             sub BUILDARGS {
20 131     131 0 90864 my $class = shift;
21 131         384 my $attributes = $class->SUPER::BUILDARGS(@_);
22              
23 130 100       336 if (ref($attributes->{elements}) eq 'ARRAY') {
24             $attributes->{elements} = [
25 219 100 66     2740 map { (blessed($_) && $_->isa('Math::Complex')) ? $_ :
    100          
26             looks_like_number $_ ? Math::Complex::cplx($_) :
27             undef }
28 117         128 Statistics::R::REXP::Vector::_flatten(@{$attributes->{elements}})
  117         293  
29             ]
30             }
31             $attributes
32 130         866 }
33              
34              
35             sub BUILD {
36 127     127 0 2760 my ($self, $args) = @_;
37              
38             # Required attribute type
39             die "Elements of the 'elements' attribute must be scalar numbers or instances of Math::Complex" if
40             defined $self->elements &&
41 219 100 33     1915 grep { defined($_) && !(blessed($_) && $_->isa('Math::Complex') ||
42             Scalar::Util::looks_like_number($_)) }
43 127 50 33     1783 @{$self->elements}
  127         2085  
44             }
45              
46              
47             around _eq => sub {
48             my $orig = shift;
49              
50             return unless Statistics::R::REXP::_eq(@_);
51            
52             my ($self, $obj) = (shift, shift);
53              
54             my $a = $self->elements;
55             my $b = $obj->elements;
56             return undef unless scalar(@$a) == scalar(@$b);
57             for (my $i = 0; $i < scalar(@{$a}); $i++) {
58             my $x = $a->[$i];
59             my $y = $b->[$i];
60             if (defined($x) && defined($y)) {
61             return undef unless
62             $x == $y;
63             } else {
64             return undef if defined($x) or defined($y);
65             }
66             }
67            
68             1
69             };
70              
71              
72 4     4   9 sub _type { 'complex'; }
73              
74              
75             1; # End of Statistics::R::REXP::Complex
76              
77             __END__