### base matrix class

sub new {
    my ($class, $rows, $columns) = @_;

    $self = { base => (), rows => $rows, columns => $columns, };

    for (my $i = 0; $i < $rows; $i++) {
	push ($self->{base}, ());
	for (my $j = 0; $j < $columns; $j++) {

	    push ($self->{base}[$i], rand);
	    
	}
    }
    
    bless $self, $class;
};

sub indexOf {
    my ($self, $r, $c) = @_;

    return $self->{base}[$r][$c];
};

### by vector
sub multiply {
    my ($self, $vector) = @_;

    my $sum;
    my $resultv = vector->new;
    
    for (my $i = 0; $i < $self->{rows}; $i++) {
	$sum = 0;
	for (my $j = 0; $j < $self->{columns}; $j++) {
	    $sum += $self->{base}[$i][$j] * $vector->{base}[$j];
	}
	$resultv->add($sum);
    }

    return $resultv;
}

sub multiplyMatrix {
    my ($self, $matrix) = @_;

    my $sum;
    my $v;
    my $returnm = matrix->new($rows, $columns);
    
    for (my $k = 0; $k < $matrix->{rows}; $k++) {
	$v = vector->new;
	for (my $i = 0; $i < $self->{rows}; $i++) {
	    $sum = 0;
	    for (my $j = 0; $j < $self->{columns}; $j++) {
		$sum = $self->{base}[$i][$j] * $matrix->{base}[$j][$i];
	    }
	    $v->add($sum);
	}
	$returnm->{base}[$k] = $v;
    }

    return $returnm;
}
1;
