line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Algorithm::DimReduction; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
684
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
54
|
|
5
|
2
|
|
|
2
|
|
1231
|
use Algorithm::DimReduction::Result; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
50
|
|
6
|
2
|
|
|
2
|
|
2650
|
use File::Temp; |
|
2
|
|
|
|
|
78038
|
|
|
2
|
|
|
|
|
164
|
|
7
|
2
|
|
|
2
|
|
1976
|
use File::Copy; |
|
2
|
|
|
|
|
5955
|
|
|
2
|
|
|
|
|
219
|
|
8
|
2
|
|
|
2
|
|
2691
|
use Storable qw( nstore retrieve ); |
|
2
|
|
|
|
|
8331
|
|
|
2
|
|
|
|
|
176
|
|
9
|
2
|
|
|
2
|
|
17
|
use base qw( Class::Accessor::Fast ); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
2460
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.00001'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub analyze { |
14
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
15
|
0
|
|
|
|
|
|
my $matrix = shift; |
16
|
0
|
|
|
|
|
|
my $matrix_fh = $self->_output_temp_matrix($matrix); |
17
|
0
|
|
|
|
|
|
my ( $svd_file, $eigens ) = $self->_do_svd($matrix_fh); |
18
|
0
|
|
|
|
|
|
my $result = Algorithm::DimReduction::Result->new( |
19
|
|
|
|
|
|
|
svd_file => $svd_file, |
20
|
|
|
|
|
|
|
eigens => $eigens, |
21
|
|
|
|
|
|
|
); |
22
|
0
|
|
|
|
|
|
return $result; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub reduce { |
26
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
27
|
0
|
|
|
|
|
|
my $result = shift; |
28
|
0
|
|
|
|
|
|
my $reduce_to = shift; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $svd_file = $result->{svd_file}; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $octave_cmd = <<" END"; |
33
|
|
|
|
|
|
|
echo "\ |
34
|
|
|
|
|
|
|
load('$svd_file'); |
35
|
|
|
|
|
|
|
num = $reduce_to; |
36
|
|
|
|
|
|
|
s_sqrt = sqrt(s); |
37
|
|
|
|
|
|
|
max = size(u)(1,:); |
38
|
|
|
|
|
|
|
reduced_matrix = u([1:max],[1:num]) * s_sqrt([1:num],[1:num]); |
39
|
|
|
|
|
|
|
save $svd_file *; |
40
|
|
|
|
|
|
|
" | octave -q |
41
|
|
|
|
|
|
|
END |
42
|
0
|
|
|
|
|
|
system($octave_cmd); |
43
|
0
|
|
|
|
|
|
my $reduced_matrix = $self->_pickup_matrix($svd_file); |
44
|
0
|
|
|
|
|
|
return $reduced_matrix; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub save_analyzed { |
48
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
49
|
0
|
|
|
|
|
|
my $result = shift; |
50
|
0
|
|
|
|
|
|
my $save_dir = shift; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
0
|
|
|
|
$save_dir ||= $ENV{PWD} . '/RESULT'; |
53
|
0
|
|
|
|
|
|
$save_dir =~ s/\/$//; |
54
|
0
|
0
|
|
|
|
|
unless ( -e $save_dir ) { |
55
|
0
|
|
|
|
|
|
system("mkdir -p $save_dir"); |
56
|
|
|
|
|
|
|
} |
57
|
0
|
|
|
|
|
|
copy( $result->{svd_file}, $save_dir . '/svd.oct' ); |
58
|
0
|
|
|
|
|
|
$result->{svd_file} = $save_dir . '/svd.oct'; |
59
|
0
|
|
|
|
|
|
nstore( $result, $save_dir . '/result.bin' ); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub load_analyzed { |
63
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
64
|
0
|
|
|
|
|
|
my $save_dir_name = shift; |
65
|
0
|
|
|
|
|
|
my $result = retrieve( $save_dir_name . '/result.bin' ); |
66
|
0
|
|
|
|
|
|
return $result; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _output_temp_matrix { |
70
|
0
|
|
|
0
|
|
|
my $self = shift; |
71
|
0
|
|
|
|
|
|
my $matrix = shift; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my %args = ( |
74
|
|
|
|
|
|
|
TEMPLATE => 'matrix_XXXX', |
75
|
|
|
|
|
|
|
SUFFIX => '.mat', |
76
|
|
|
|
|
|
|
); |
77
|
0
|
|
|
|
|
|
my $matrix_fh = File::Temp->new(%args); |
78
|
0
|
|
|
|
|
|
for my $i ( 0 .. @$matrix - 1 ) { |
79
|
0
|
|
|
|
|
|
for my $j ( 0 .. @{ $matrix->[0] } - 1 ) { |
|
0
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
print $matrix_fh $matrix->[$i]->[$j], "\t"; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
|
|
|
|
|
print $matrix_fh "\n"; |
83
|
|
|
|
|
|
|
} |
84
|
0
|
|
|
|
|
|
return $matrix_fh; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _do_svd { |
88
|
0
|
|
|
0
|
|
|
my $self = shift; |
89
|
0
|
|
|
|
|
|
my $matrix_fh = shift; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $matrix_file = $matrix_fh->filename; |
92
|
0
|
|
|
|
|
|
my %args = ( |
93
|
|
|
|
|
|
|
TEMPLATE => 'svd_XXXX', |
94
|
|
|
|
|
|
|
SUFFIX => '.oct', |
95
|
|
|
|
|
|
|
); |
96
|
0
|
|
|
|
|
|
my $svd_fh = File::Temp->new(%args); |
97
|
0
|
|
|
|
|
|
my $svd_file = $svd_fh->filename; |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my $octarve_cmd = <<" END"; |
100
|
|
|
|
|
|
|
echo "\ |
101
|
|
|
|
|
|
|
matrix = load $matrix_file; |
102
|
|
|
|
|
|
|
[u, s, v] = svd(matrix); |
103
|
|
|
|
|
|
|
for i=1:size(diag(s))(1:1) |
104
|
|
|
|
|
|
|
info(i) = sum(diag(s)([1:i],:))/sum(diag(s)); |
105
|
|
|
|
|
|
|
printf('%g,', info(i)); |
106
|
|
|
|
|
|
|
end |
107
|
|
|
|
|
|
|
save $svd_file *; |
108
|
|
|
|
|
|
|
" | octave -q |
109
|
|
|
|
|
|
|
END |
110
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
my @desc_order_eigens = split( ',', `$octarve_cmd` ); |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
if ( $self->{save_svd_file} ) { |
114
|
0
|
|
|
|
|
|
copy( $svd_file, $self->{save_svd_file} ); |
115
|
|
|
|
|
|
|
} |
116
|
0
|
|
|
|
|
|
$self->{svd_fh} = $svd_fh; |
117
|
0
|
|
|
|
|
|
return ( $svd_file, \@desc_order_eigens ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _pickup_matrix { |
121
|
0
|
|
|
0
|
|
|
my $self = shift; |
122
|
0
|
|
|
|
|
|
my $svd_file = shift; |
123
|
0
|
|
|
|
|
|
my $reduced_matrix; |
124
|
0
|
|
|
|
|
|
open( OCT, $svd_file ); |
125
|
|
|
|
|
|
|
LABEL: |
126
|
0
|
|
|
|
|
|
while () { |
127
|
0
|
0
|
|
|
|
|
if ( $_ =~ /# name: reduced_matrix/ ) { |
128
|
0
|
|
|
|
|
|
my $type = ; |
129
|
0
|
|
|
|
|
|
my $rows = ; |
130
|
0
|
|
|
|
|
|
my $columns = ; |
131
|
0
|
|
|
|
|
|
while () { |
132
|
0
|
0
|
|
|
|
|
last LABEL if ( $_ =~ /#/ ); |
133
|
0
|
|
|
|
|
|
chomp $_; |
134
|
0
|
|
|
|
|
|
my @cols = split( ' ', $_ ); |
135
|
0
|
0
|
|
|
|
|
shift @cols if $cols[0] eq ''; |
136
|
0
|
|
|
|
|
|
push( @$reduced_matrix, \@cols ); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
close(OCT); |
141
|
0
|
|
|
|
|
|
return $reduced_matrix; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
__END__ |