line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Vector::Real::Random; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Math::Vector::Real; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
25213
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
9
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
102
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
1064
|
use Math::Random (); |
|
1
|
|
|
|
|
7867
|
|
|
1
|
|
|
|
|
32
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
7
|
use constant _PI => 3.14159265358979323846264338327950288419716939937510; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
556
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub random_in_box { |
16
|
0
|
0
|
|
0
|
0
|
|
if (ref $_[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
|
0
|
|
|
|
|
$size = 1 unless defined $size; |
23
|
0
|
|
|
|
|
|
return bless [Math::Random::random_uniform($dim, 0, $size)], $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub random_in_sphere { |
28
|
0
|
|
|
0
|
0
|
|
my ($class, $dim, $size) = @_; |
29
|
0
|
|
0
|
|
|
|
$size ||= 1; |
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
|
|
|
|
|
|
|
sub random_versor { |
37
|
0
|
|
|
0
|
0
|
|
my ($class, $dim, $scale) = @_; |
38
|
0
|
|
|
|
|
|
my @n; |
39
|
0
|
0
|
|
|
|
|
$scale = 1 unless defined $scale; |
40
|
0
|
0
|
|
|
|
|
if ($dim >= 3) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
@n = Math::Random::random_normal $dim, 0, 1; |
42
|
0
|
|
|
|
|
|
my $d = 0; |
43
|
0
|
|
|
|
|
|
$d += $_ * $_ for @n; |
44
|
0
|
0
|
|
|
|
|
unless ($d) { |
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
|
0
|
|
|
|
|
@n = (rand >= 0.5 ? $scale : -$scale); |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
bless \@n, $class; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub random_normal { |
63
|
0
|
|
|
0
|
0
|
|
my ($class, $dim, $sd) = @_; |
64
|
0
|
|
0
|
|
|
|
$sd ||= 1; |
65
|
0
|
|
|
|
|
|
bless [Math::Random::random_normal $dim, 0, $sd], $class; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
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 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, L. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 AUTHORS |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Salvador Fandino, Esalva@E, David Serrano. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Copyright (C) 2011, 2013 by Salvador FandiEo |
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 |