Math-Zap
view release on metacpan or search on metacpan
lib/Math/Zap/Matrix2.pm view on Meta::CPAN
Identity matrix
=cut
sub identity()
{bless
{11=>1, 21=>0,
12=>0, 22=>1,
};
}
=head3 new2v
Create a matrix from two vectors
=cut
sub new2v($$)
{vector2Check(@_) if debug;
my ($a, $b, $c) = @_;
my $m = round(bless(
{11=>$a->{x}, 12=>$b->{x},
21=>$a->{y}, 22=>$b->{y},
}));
singular($m, 1);
$m;
}
=head2 Methods
=head3 check
Check its a matrix
=cut
sub check(@)
{if (debug)
{for my $m(@_)
{confess "$m is not a matrix2" unless ref($m) eq __PACKAGE__;
}
}
return (@_)
}
=head3 is
Test its a matrix
=cut
sub is(@)
{for my $m(@_)
{return 0 unless ref($m) eq __PACKAGE__;
}
'matrix2';
}
=head3 accuracy
Get/Set accuracy
=cut
my $accuracy = 1e-10;
sub accuracy
{return $accuracy unless scalar(@_);
$accuracy = shift();
}
=head3 round
Round: round to nearest integer if within accuracy of that integer
=cut
sub round($)
{unless (debug)
{return $_[0];
}
else
{my ($a) = @_;
for my $k(keys(%$a))
{my $n = $a->{$k};
my $N = int($n);
$a->{$k} = $N if abs($n-$N) < $accuracy;
}
return $a;
}
}
=head3 singular
Singular matrix?
=cut
sub singular($$)
{my $m = shift; # Matrix
my $a = 1e-2; # Accuracy
my $A = shift; # Action 0: return indicator, 1: confess
my $n = abs
($m->{11} * $m->{22} -
$m->{12} * $m->{21})
< $a;
( run in 0.425 second using v1.01-cache-2.11-cpan-524268b4103 )