package Math::Matrix::Inverse;

use strict;
use base qw(Math::Matrix);
use vars qw($VERSION);
$VERSION='0.01';


sub inverse{
	my $self=shift;
	my $class = ref($self);
	my $tmp=$class->new([]);
	
	my $det = $self->determinant;
	return undef unless $det;

	for my $i (0 .. $#{$self}){
		for my $j (0 .. $#{$self->[0]}){
			$tmp->[$i][$j]= (-1)**($i+1+$j+1) * $self->submatrix($i,$j)->determinant;
		}
	}
	
	return $tmp->multiply_scalar(1/$det);
}

sub submatrix{
	my($self,$col,$row)=@_;
	my $class = ref($self);

	my @ret;
	for my $i (0 .. $#{$self}){
		for my $j (0 .. $#{$self->[0]}){
			push(@ret,$self->[$i][$j]) if $col ne $i && $row ne $j;
		}
		push(@ret,"end") if($#ret > -1 && $ret[-1] ne "\n");
	}

	my $submatrix = $class->new([]);
	my $k=0;
	my $m=0;
	foreach my $ret (@ret){
		if($ret eq "end"){
			$m=0;
			$k++;
			next;
		}
		$submatrix->[$k][$m]=$ret;
		$m++;
	}
	return $submatrix;
}


1;

__END__

=head1 NAME

Math::Matrix::Inverse  - Add Inverse Matrix method

=head1 REQUIREMENTS

Math::Mtrix

=head1 SYNOPSIS

my $mt = Math::Matrix::Inverse->new([1,2],[2,1]);
my $im = $mt->inverse;

=head1 DESCRIPTION

Add Inverse Matrix method.

=head1 SEE ALSO

L<Math::Matrix>

=head1 AUTHOR

kazeburo <kazeburo@nomadscafe.jp>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

