MatrixReal.pm (Math-MatrixReal-2.12) | : | MatrixReal.pm (Math-MatrixReal-2.13) | ||
---|---|---|---|---|
# Copyright (c) 1996, 1997 by Steffen Beyer. All rights reserved. | # Copyright (c) 1996, 1997 by Steffen Beyer. All rights reserved. | |||
# Copyright (c) 1999 by Rodolphe Ortalo. All rights reserved. | # Copyright (c) 1999 by Rodolphe Ortalo. All rights reserved. | |||
# Copyright (c) 2001-2015 by Jonathan Leto. All rights reserved. | # Copyright (c) 2001-2016 by Jonathan Leto. All rights reserved. | |||
# This package is free software; you can redistribute it and/or | # This package is free software; you can redistribute it and/or | |||
# modify it under the same terms as Perl itself. | # modify it under the same terms as Perl itself. | |||
package Math::MatrixReal; | package Math::MatrixReal; | |||
use strict; | use strict; | |||
use warnings; | use warnings; | |||
use Carp; | use Carp; | |||
use Data::Dumper; | use Data::Dumper; | |||
use Scalar::Util qw/reftype/; | use Scalar::Util qw/reftype/; | |||
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); | |||
require Exporter; | require Exporter; | |||
@ISA = qw(Exporter); | @ISA = qw(Exporter); | |||
@EXPORT = qw(); | @EXPORT = qw(); | |||
@EXPORT_OK = qw(min max); | @EXPORT_OK = qw(min max); | |||
%EXPORT_TAGS = (all => [@EXPORT_OK]); | %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||
$VERSION = '2.12'; | $VERSION = '2.13'; | |||
use overload | use overload | |||
'.' => '_concat', | '.' => '_concat', | |||
'neg' => '_negate', | 'neg' => '_negate', | |||
'~' => '_transpose', | '~' => '_transpose', | |||
'bool' => '_boolean', | 'bool' => '_boolean', | |||
'!' => '_not_boolean', | '!' => '_not_boolean', | |||
'abs' => '_norm', | 'abs' => '_norm', | |||
'+' => '_add', | '+' => '_add', | |||
'-' => '_subtract', | '-' => '_subtract', | |||
skipping to change at line 660 | skipping to change at line 660 | |||
} | } | |||
sub as_list | sub as_list | |||
{ | { | |||
croak "Usage: \$matrix->as_list();" if (@_ != 1); | croak "Usage: \$matrix->as_list();" if (@_ != 1); | |||
my($self) = @_; | my($self) = @_; | |||
my($rows,$cols) = ($self->[1], $self->[2]); | my($rows,$cols) = ($self->[1], $self->[2]); | |||
my @list; | my @list; | |||
for(my $i = 0; $i < $rows; $i++ ){ | for(my $i = 0; $i < $rows; $i++ ){ | |||
for(my $j = 0; $j < $rows; $j++){ | for(my $j = 0; $j < $cols; $j++){ | |||
push @list, $self->[0][$i][$j]; | push @list, $self->[0][$i][$j]; | |||
} | } | |||
} | } | |||
return @list; | return @list; | |||
} | } | |||
sub _undo_LR | sub _undo_LR | |||
{ | { | |||
croak "Usage: \$matrix->_undo_LR();" | croak "Usage: \$matrix->_undo_LR();" | |||
if (@_ != 1); | if (@_ != 1); | |||
skipping to change at line 3641 | skipping to change at line 3641 | |||
=item * @all_elements = $matrix-E<gt>as_list; | =item * @all_elements = $matrix-E<gt>as_list; | |||
Get the contents of a Math::MatrixReal object as a Perl list. | Get the contents of a Math::MatrixReal object as a Perl list. | |||
Example: | Example: | |||
my $matrix = Math::MatrixReal->new_from_rows([ [1, 2], [3, 4] ]); | my $matrix = Math::MatrixReal->new_from_rows([ [1, 2], [3, 4] ]); | |||
my @list = $matrix->as_list; # 1, 2, 3, 4 | my @list = $matrix->as_list; # 1, 2, 3, 4 | |||
This method is suitable for use with OpenGL. For example, there is need to | ||||
rotate model around X-axis to 90 degrees clock-wise. That could be achieved via: | ||||
use Math::Trig; | ||||
use OpenGL; | ||||
...; | ||||
my $axis = [1, 0, 0]; | ||||
my $angle = 90; | ||||
... | ||||
my ($x, $y, $z) = @$axis; | ||||
my $f = $angle; | ||||
my $cos_f = cos(deg2rad($f)); | ||||
my $sin_f = sin(deg2rad($f)); | ||||
my $rotation = Math::MatrixReal->new_from_rows([ | ||||
[$cos_f+(1-$cos_f)*$x**2, (1-$cos_f)*$x*$y-$sin_f*$z, (1-$cos_f)*$x*$z+$s | ||||
in_f*$y, 0 ], | ||||
[(1-$cos_f)*$y*$z+$sin_f*$z, $cos_f+(1-$cos_f)*$y**2 , (1-$cos_f)*$y*$z-$s | ||||
in_f*$x, 0 ], | ||||
[(1-$cos_f)*$z*$x-$sin_f*$y, (1-$cos_f)*$z*$y+$sin_f*$x, $cos_f+(1-$cos_f)*$ | ||||
z**2 ,0 ], | ||||
[0, 0, 0, | ||||
1 ], | ||||
]); | ||||
...; | ||||
my $model_initial = Math::MatrixReal->new_diag( [1, 1, 1, 1] ); # identity matr | ||||
ix | ||||
my $model = $model_initial * $rotation; | ||||
$model = ~$model; # OpenGL operates on transposed matrices | ||||
my $model_oga = OpenGL::Array->new_list(GL_FLOAT, $model->as_list); | ||||
$shader->SetMatrix(model => $model_oga); # instance of OpenGL::Shader | ||||
See L<OpenGL>, L<OpenGL::Shader>, L<OpenGL::Array>, | ||||
L<rotation matrix|https://en.wikipedia.org/wiki/Rotation_matrix>. | ||||
=item * $new_matrix = $matrix-E<gt>each( \&function ); | =item * $new_matrix = $matrix-E<gt>each( \&function ); | |||
Creates a new matrix by evaluating a code reference on each element of the | Creates a new matrix by evaluating a code reference on each element of the | |||
given matrix. The function is passed the element, the row index and the column | given matrix. The function is passed the element, the row index and the column | |||
index, in that order. The value the function returns ( or the value of the last | index, in that order. The value the function returns ( or the value of the last | |||
executed statement ) is the value given to the corresponding element in $new_mat rix. | executed statement ) is the value given to the corresponding element in $new_mat rix. | |||
Example: | Example: | |||
# add 1 to every element in the matrix | # add 1 to every element in the matrix | |||
skipping to change at line 3730 | skipping to change at line 3759 | |||
# $pos = [ 1 2 2 ] | # $pos = [ 1 2 2 ] | |||
($max, $pos) = $B->maximum(); # $max = 9 | ($max, $pos) = $B->maximum(); # $max = 9 | |||
# $pos = 3 | # $pos = 3 | |||
=back | =back | |||
=head2 Matrix Operations | =head2 Matrix Operations | |||
=over 4 | =over 4 | |||
=item * $det = $matrix-E<gt>det(); | =item * | |||
C<$det = $matrix-E<gt>det();> | ||||
Returns the determinant of the matrix, without going through | Returns the determinant of the matrix, without going through | |||
the rigamarole of computing a LR decomposition. This method should | the rigamarole of computing a LR decomposition. This method should | |||
be much faster than LR decomposition if the matrix is diagonal or | be much faster than LR decomposition if the matrix is diagonal or | |||
triangular. Otherwise, it is just a wrapper for | triangular. Otherwise, it is just a wrapper for | |||
C<$matrix-E<gt>decompose_LR-E<gt>det_LR>. If the determinant is zero, | C<$matrix-E<gt>decompose_LR-E<gt>det_LR>. If the determinant is zero, | |||
there is no inverse and vice-versa. Only quadratic matrices have | there is no inverse and vice-versa. Only quadratic matrices have | |||
determinants. | determinants. | |||
=item * | =item * | |||
skipping to change at line 3984 | skipping to change at line 4015 | |||
=item * | =item * | |||
C<$adjoint = $matrix-E<gt>adjoint();> | C<$adjoint = $matrix-E<gt>adjoint();> | |||
The adjoint is just the transpose of the cofactor matrix. This method is | The adjoint is just the transpose of the cofactor matrix. This method is | |||
just an alias for C< ~($matrix-E<gt>cofactor)>. | just an alias for C< ~($matrix-E<gt>cofactor)>. | |||
=back | =back | |||
=item * | ||||
C<$part_of_matrix = $matrix-E<gt>submatrix(x1,y1,x2,Y2);> | ||||
Submatrix permit to select only part of existing matrix in order to produce a ne | ||||
w one. | ||||
This method take four arguments to define a selection area: | ||||
=over 6 | ||||
=item - firstly: Coordinate of top left corner to select (x1,y1) | ||||
=item - secondly: Coordinate of bottom right corner to select (x2,y2) | ||||
=back | ||||
Example: | ||||
my $matrix = Math::MatrixReal->new_from_string(<<'MATRIX'); | ||||
[ 0 0 0 0 0 0 0 ] | ||||
[ 0 0 0 0 0 0 0 ] | ||||
[ 0 0 0 0 0 0 0 ] | ||||
[ 0 0 0 0 0 0 0 ] | ||||
[ 0 0 0 0 1 0 1 ] | ||||
[ 0 0 0 0 0 1 0 ] | ||||
[ 0 0 0 0 1 0 1 ] | ||||
MATRIX | ||||
my $submatrix = $matrix->submatrix(5,5,7,7); | ||||
$submatrix->display_precision(0); | ||||
print $submatrix; | ||||
Output: | ||||
[ 1 0 1 ] | ||||
[ 0 1 0 ] | ||||
[ 1 0 1 ] | ||||
=back | ||||
=head2 Arithmetic Operations | =head2 Arithmetic Operations | |||
=over 4 | =over 4 | |||
=item * | =item * | |||
C<$matrix1-E<gt>add($matrix2,$matrix3);> | C<$matrix1-E<gt>add($matrix2,$matrix3);> | |||
Calculates the sum of matrix "C<$matrix2>" and matrix "C<$matrix3>" | Calculates the sum of matrix "C<$matrix2>" and matrix "C<$matrix3>" | |||
and stores the result in matrix "C<$matrix1>" (which must already exist | and stores the result in matrix "C<$matrix1>" (which must already exist | |||
skipping to change at line 5500 | skipping to change at line 5570 | |||
=back | =back | |||
=head1 SEE ALSO | =head1 SEE ALSO | |||
Math::VectorReal, Math::PARI, Math::MatrixBool, | Math::VectorReal, Math::PARI, Math::MatrixBool, | |||
Math::Vec, DFA::Kleene, Math::Kleene, | Math::Vec, DFA::Kleene, Math::Kleene, | |||
Set::IntegerRange, Set::IntegerFast . | Set::IntegerRange, Set::IntegerFast . | |||
=head1 VERSION | =head1 VERSION | |||
This man page documents Math::MatrixReal version 2.10 | This man page documents Math::MatrixReal version 2.13 | |||
The latest code can be found at | The latest code can be found at | |||
https://github.com/leto/math--matrixreal . | https://github.com/leto/math--matrixreal . | |||
=head1 AUTHORS | =head1 AUTHORS | |||
Steffen Beyer <sb@engelschall.com>, Rodolphe Ortalo <ortalo@laas.fr>, | Steffen Beyer <sb@engelschall.com>, Rodolphe Ortalo <ortalo@laas.fr>, | |||
Jonathan "Duke" Leto <jonathan@leto.net>. | Jonathan "Duke" Leto <jonathan@leto.net>. | |||
Currently maintained by Jonathan "Duke" Leto, send all bugs/patches | Currently maintained by Jonathan "Duke" Leto, send all bugs/patches | |||
skipping to change at line 5522 | skipping to change at line 5592 | |||
=head1 CREDITS | =head1 CREDITS | |||
Many thanks to Prof. Pahlings for stoking the fire of my enthusiasm for | Many thanks to Prof. Pahlings for stoking the fire of my enthusiasm for | |||
Algebra and Linear Algebra at the university (RWTH Aachen, Germany), and | Algebra and Linear Algebra at the university (RWTH Aachen, Germany), and | |||
to Prof. Esser and his assistant, Mr. Jarausch, for their fascinating | to Prof. Esser and his assistant, Mr. Jarausch, for their fascinating | |||
lectures in Numerical Analysis! | lectures in Numerical Analysis! | |||
=head1 COPYRIGHT | =head1 COPYRIGHT | |||
Copyright (c) 1996-2015 by various authors including the original developer | Copyright (c) 1996-2016 by various authors including the original developer | |||
Steffen Beyer, Rodolphe Ortalo, the current maintainer Jonathan "Duke" Leto and | Steffen Beyer, Rodolphe Ortalo, the current maintainer Jonathan "Duke" Leto and | |||
all the wonderful people in the AUTHORS file. All rights reserved. | all the wonderful people in the AUTHORS file. All rights reserved. | |||
=head1 LICENSE AGREEMENT | =head1 LICENSE AGREEMENT | |||
This package is free software; you can redistribute it and/or | This package is free software; you can redistribute it and/or | |||
modify it under the same terms as Perl itself. Fuck yeah. | modify it under the same terms as Perl itself. Fuck yeah. | |||
End of changes. 8 change blocks. | ||||
6 lines changed or deleted | 82 lines changed or added |