blib/lib/Math/Vector/Real/Random.pm

filter: Statementstmt 36.1% 17 / 47 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 26.8% 22 / 82 CC 14 SCAR 37.1
CC
14
Cov
25%
random_versor
90.0
random_in_box
12.0
random_in_sphere
12.0
CRAP 40.9
↑ Summary
1 package Math::Vector::Real::Random;
2  
3 1 our $VERSION = '0.03';
4  
5 package Math::Vector::Real;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Math::Random ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use constant _PI => 3.14159265358979323846264338327950288419716939937510;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub random_in_box {
Subroutine random_in_box: not called CC=3 SCAR=24.8
Pod undocumented
16 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
17 0 my $box = shift;
18 0 return bless [map Math::Random::random_uniform(1, 0, $_), @$box], ref $box;
19 }
20 else {
21 0 my ($class, $dim, $size) = @_;
22 0 ▶ $size = 1 unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 0 0
23 0 return bless [Math::Random::random_uniform($dim, 0, $size)], $class;
24 }
25 }
26  
27 0 ▶sub random_in_sphere {
Subroutine random_in_sphere: not called CC=3 SCAR=24.8
Pod undocumented
28 0 my ($class, $dim, $size) = @_;
29 0 ▶ $size ||= 1;
Condition0%0/2
$size ||= 1
l!l
00
MC/DC0%0/1
$size ||= 1
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
30 0 my $n = $class->random_versor($dim);
31 0 my $f = $size * (Math::Random::random_uniform(1, 0, 1) ** (1/$dim));
32 0 $_ *= $f for @$n;
33 0 $n;
34 }
35  
36 0 ▶sub random_versor {
Subroutine random_versor: not called CC=9 SCAR=45.0
Pod undocumented
37 0 my ($class, $dim, $scale) = @_;
38 0 my @n;
39 0 ▶ $scale = 1 unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 0 0 0
40 0 ▶ if ($dim >= 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dim >= 3) { } 0 0 0
elsif ($dim >= 2) { } 0 0 0
elsif ($dim >= 1) { } 0 0 0
41 0 @n = Math::Random::random_normal $dim, 0, 1;
42 0 my $d = 0;
43 0 $d += $_ * $_ for @n;
44 0 ▶ unless ($d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d) { } 0 0 0
45 0 $n[0] = $scale;
46 }
47 else {
48 0 $d = $scale/sqrt($d);
49 0 $_ *= $d for @n;
50 }
51 }
52 elsif ($dim >= 2) {
53 0 my $ang = Math::Random::random_uniform(1, -(_PI), _PI);
54 0 @n = ($scale * sin $ang, $scale * cos $ang);
55 }
56 elsif ($dim >= 1) {
57 0 ▶ @n = (rand >= 0.5 ? $scale : -$scale);
Branch: 1 branch
BranchTrueFalseTotal
rand >= 0.5 ? : 0 0 0
58 }
59 0 bless \@n, $class;
60 }
61  
62 0 ▶sub random_normal {
Subroutine random_normal: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my ($class, $dim, $sd) = @_;
64 0 ▶ $sd ||= 1;
Condition0%0/2
$sd ||= 1
l!l
00
MC/DC0%0/1
$sd ||= 1
$sd
Truth table0%0/2
A
A= $sd
Aresult
11
00
65 0 bless [Math::Random::random_normal $dim, 0, $sd], $class;
66 }
67  
68 1 1;
69  
70 =head1 NAME
71  
72 Math::Vector::Real::Random - Generate random real vectors
73  
74 =head1 SYNOPSIS
75  
76 use Math::Vector::Real qw(V);
77 use Math::Vector::Real::Random;
78  
79 my $v = Math::Vector::Real->random_normal(7);
80 my $v2 = $c->random_normal;
81  
82 =head1 DESCRIPTION
83  
84 This module extends the L<Math::Vector::Real> package adding some
85 methods for random generation of vectors.
86  
87 =head2 Methods
88  
89 The extra methods are:
90  
91 =over 4
92  
93 =item Math::Vector::Real->random_in_box($dim, $size)
94  
95 =item $v->random_in_box
96  
97 When called as a class method, returns a random vector of dimension
98 C<$dim> contained inside the hypercube of the given size.
99  
100 When called as an instance method, returns a random vector contained
101 in the box defined by the given vector instance.
102  
103 =item Math::Vector::Real->random_in_sphere($dim, $radio)
104  
105 Returns random vector inside the hyper-sphere of the given dimension
106 and radio.
107  
108 =item Math::Vector::Real->random_versor($dim)
109  
110 Returns a randon vector of norm 1.0.
111  
112 =item Math::Vector::Real->random_normal($dim, $sd)
113  
114 Returns a random vector in the space of the given dimension, where
115 each component follows a normal distribution with standard deviation
116 C<$sd> (defaults to 1.0).
117  
118 =back
119  
120 =head1 SEE ALSO
121  
122 L<Math::Vector::Real>, L<Math::Random>.
123  
124 =head1 AUTHORS
125  
126 Salvador Fandino, E<lt>salva@E<gt>, David Serrano.
127  
128 =head1 COPYRIGHT AND LICENSE
129  
130 Copyright (C) 2011, 2013 by Salvador FandiE<ntilde>o
131  
132 This library is free software; you can redistribute it and/or modify
133 it under the same terms as Perl itself, either Perl version 5.12.3 or,
134 at your option, any later version of Perl 5 you may have available.
135  
136  
137 =cut
↑ Summary