AI-NeuralNet-Hopfield
view release on metacpan or search on metacpan
lib/AI/NeuralNet/Hopfield.pm view on Meta::CPAN
}
sub convert_array() {
my $rows = shift;
my $cols = shift;
my @pattern = @_;
my $result = Math::SparseMatrix->new(1, $cols);
for (my $i = 0; $i < ($#pattern + 1); $i++) {
if ($pattern[$i] =~ m/true/ig) {
$result->set(1, ($i +1 ), 1);
} else {
$result->set(1, ($i + 1), -1);
}
}
return $result;
}
sub transpose() {
my $matrix = shift;
my $rows = $matrix->{_rows};
my $cols = $matrix->{_cols};
my $inverse = Math::SparseMatrix->new($cols, $rows);
for (my $r = 1; $r <= $rows; $r++) {
for (my $c = 1; $c <= $cols; $c++) {
my $value = $matrix->get($r, $c);
$inverse->set($c, $r, $value);
}
}
return $inverse;
}
sub multiply() {
my $matrix_a = shift;
my $matrix_b = shift;
my $a_rows = $matrix_a->{_rows};
lib/AI/NeuralNet/Hopfield.pm view on Meta::CPAN
if ($matrix_a->{_cols} != $matrix_b->{_rows}) {
die "To use ordinary matrix multiplication the number of columns on the first matrix must mat the number of rows on the second";
}
for (my $result_row = 1; $result_row <= $a_rows; $result_row++) {
for(my $result_col = 1; $result_col <= $b_cols; $result_col++) {
my $value = 0;
for (my $i = 1; $i <= $a_cols; $i++) {
$value += ($matrix_a->get($result_row, $i)) * ($matrix_b->get($i, $result_col));
}
$result->set($result_row, $result_col, $value);
}
}
return $result;
}
sub identity() {
my $size = shift;
if ($size < 1) {
die "Identity matrix must be at least of size 1.";
}
my $result = Math::SparseMatrix->new ($size, $size);
for (my $i = 1; $i <= $size; $i++) {
$result->set($i, $i, 1);
}
return $result;
}
sub subtract() {
my $matrix_a = shift;
my $matrix_b = shift;
my $a_rows = $matrix_a->{_rows};
my $a_cols = $matrix_a->{_cols};
lib/AI/NeuralNet/Hopfield.pm view on Meta::CPAN
my $result = Math::SparseMatrix->new($a_rows, $a_cols);
for (my $result_row = 1; $result_row <= $a_rows; $result_row++) {
for (my $result_col = 1; $result_col <= $a_cols; $result_col++) {
my $value = ( $matrix_a->get($result_row, $result_col) ) - ( $matrix_b->get($result_row, $result_col));
if ($value == 0) {
$value += 2;
}
$result->set($result_row, $result_col, $value);
}
}
return $result;
}
sub add() {
#weight matrix.
my $matrix_a = shift;
#identity matrix.
my $matrix_b = shift;
lib/AI/NeuralNet/Hopfield.pm view on Meta::CPAN
if ($a_cols != $b_cols) {
die "To add the matrixes they must have the same number of rows and columns.";
}
my $result = Math::SparseMatrix->new($a_rows, $a_cols);
for (my $result_row = 1; $result_row <= $a_rows; $result_row++) {
for (my $result_col = 1; $result_col <= $a_cols; $result_col++) {
my $value = $matrix_b->get($result_row, $result_col);
$result->set($result_row, $result_col, $matrix_a->get($result_row, $result_col) + $value )
}
}
return $result;
}
sub dot_product() {
my $matrix_a = shift;
my $matrix_b = shift;
my $a_rows = $matrix_a->{_rows};
lib/AI/NeuralNet/Hopfield.pm view on Meta::CPAN
my $matrix_rows = $self->matrix_rows();
if ($col > $matrix_rows) {
die "Can't get column";
}
my $new_matrix = Math::SparseMatrix->new($matrix_rows, 1);
for (my $row = 1; $row <= $matrix_rows; $row++) {
my $value = $matrix->get($row, $col);
$new_matrix->set($row, 1, $value);
}
return $new_matrix;
}
sub print_matrix() {
my $matrix = shift;
my $rs = $matrix->{_rows};
my $cs = $matrix->{_cols};
for (my $i = 1; $i <= $rs; $i++) {
( run in 1.430 second using v1.01-cache-2.11-cpan-49f99fa48dc )