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.0002';
4 12     12   52030 use 5.010;
  12         40  
5              
6 12     12   63 use Scalar::Util qw(blessed looks_like_number);
  12         27  
  12         620  
7 12     12   4697 use Math::Complex qw();
  12         107324  
  12         372  
8              
9 12     12   387 use Class::Tiny::Antlers qw(-default around);
  12         3160  
  12         113  
10 12     12   2301 use namespace::clean;
  12         9010  
  12         89  
11              
12             extends 'Statistics::R::REXP::Vector';
13 12     12   3371 use overload;
  12         29  
  12         66  
14              
15              
16 12     12   531 use constant sexptype => 'CPLXSXP';
  12         27  
  12         4541  
17              
18              
19             sub BUILDARGS {
20 194     194 0 17127 my $class = shift;
21 194         636 my $attributes = $class->SUPER::BUILDARGS(@_);
22              
23 193 100       572 if (ref($attributes->{elements}) eq 'ARRAY') {
24             $attributes->{elements} = [
25 318 100 66     4067 map { (blessed($_) && $_->isa('Math::Complex')) ? $_ :
    100          
26             looks_like_number $_ ? Math::Complex::cplx($_) :
27             undef }
28 179         286 Statistics::R::REXP::Vector::_flatten(@{$attributes->{elements}})
  179         517  
29             ]
30             }
31             $attributes
32 193         1538 }
33              
34              
35             sub BUILD {
36 190     190 0 4733 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 318 100 33     2859 grep { defined($_) && !(blessed($_) && $_->isa('Math::Complex') ||
42             Scalar::Util::looks_like_number($_)) }
43 190 50 33     2649 @{$self->elements}
  190         3322  
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   10 sub _type { 'complex'; }
73              
74              
75             1; # End of Statistics::R::REXP::Complex
76              
77             __END__