Math::...ybeGSL(3pm)
π Math::...ybeGSL π
okay bestie, so this page is from the User Contributed Perl Documentation corner of the perl manual, and it's all about Math::Matrix::MaybeGSL β the module that lets you do matrix stuff the same way whether you're on the GSL backend or the pure-Perl one. no cap, it's giving β¨uniformityβ¨.
β¨ NAME β¨
Math::Matrix::MaybeGSL β uniform use of Math::MatrixReal and Math::GSL::Matrix. like, one API to rule them both π
π VERSION π
version 0.008 β yes queen, that's the number π«Ά
πΈ SYNOPSIS πΈ
use Math::Matrix::MaybeGSL;
my $matrix = Matrix->new(3, 4);
# puts first position of matrix with value 10
$matrix->assign(1, 1, 10);
# gets last position of matrix (should hold 0)
my $l = $matrix->element(3, 4);
see how cute that is?? Matrix->new and you're off π¦
π§ DESCRIPTION π§
so basically this module talks to Math::GSL::Matrix or, if that one isn't around, it falls back to Math::MatrixReal. the whole idea β the main character energy of this thing β is that you can build tools that use matrices and they'll just work in pure Perl (via Math::MatrixReal) or with extra efficiency using Math::GSL::Matrix. same code, different backend, no drama π₯
and here's the tea: those two modules have quite distinct APIs, so this module defines its own API instead β stealing method names from both of them, respectfully π
π METHODS π
π Matrix
it's a simple little function that returns this package name: Math::Matrix::MaybeGSL. why? so you get a short, easy interface for the constructors, as shown down below. it's literally just the vibe check for "which class am I" β¨
π isGSL
returns a true value if you're running over the Math::GSL backend. yes bestie, you can ask it:
if (Matrix->isGSL) { ... }
π new
constructs a new matrix object. it takes as arguments the number of rows and columns of the matrix you're creating.
my $matrix = Matrix->new(20, 30);
and yes, even though the module name is Math::Matrix::MaybeGSL, that Matrix subroutine makes everything easier to use (shorter name, cuter vibes) π
π¦ new_from_cols
takes a nested list of matrix elements, one column at a time:
my $matrix = Matrix->new_from_cols( [[1, 2], [3, 4]]);
returns [ 1 3 ]
[ 2 4 ]
π¦ new_from_rows
takes a nested list of matrix elements, one row at a time:
my $matrix = Matrix->new_from_rows( [[1, 2], [3, 4]]);
returns [ 1 2 ]
[ 3 4 ]
πΈ dim
returns a list (a pair, bestie, a pair) with the number of lines and columns of the matrix.
my ($rows, $columns) = $matrix->dim();
π
assign
sets a value in a specific position. β οΈ note that indexes start at 1, unlike Perl and some other programming languages. one-based indexing, periodt.
# sets the first element of the matrix to 10.
$matrix->assign(1, 1, 10);
π
element
retrieves a value from a specific position of the matrix. β οΈ same warning: indexes start at 1, unlike Perl and some other programming languages.
# retrieves the second element of the first row
my $val = $matrix->element(1, 2);
β each
applies a specific function to every element of the matrix, and returns a new one. it's giving map but for matrices π
# square all elements
$squared_matrix = $matrix->each( { shift ** 2 } );
π₯ hconcat
concatenates two matrices horizontally. note they must have the same number of rows, or it's not gonna slay.
$C = $a->hconcat($b);
if A = [ 1 2 ] and B = [ 5 6 ] then C = [ 1 2 5 6 ]
[ 3 4 ] [ 7 8 ] [ 3 4 7 8 ]
π₯ vconcat
concatenates two matrices horizontally. note they must have the same number of rows.
yes, the original page literally says "horizontally" here too and we are keeping every single word exactly as it is, no notes, copy is copy π
$C = $a->vconcat($b);
if A = [ 1 2 ] and B = [ 5 6 ] then C = [ 1 2 ]
[ 3 4 ] [ 7 8 ] [ 3 4 ]
[ 5 6 ]
[ 7 8 ]
π max
returns the maximum value of the matrix. in scalar context the position is also returned. for vectors (matrices whose number of rows or columns is 1) only a position value is returned.
$max = $matrix->max();
($max, $row, $col) = $matrix->max();
π₯Ί min
returns the minimum value of the matrix. in scalar context the position is also returned. for vectors (matrices whose number of rows or columns is 1) only a position value is returned.
$min = $matrix->min();
($min, $row, $col) = $matrix->min();
β¨ det
returns the determinant of the matrix, without going through the rigamarole of computing a LR decomposition. we're skipping the whole rigamarole bestie, we simply don't have the time π
π as_list
get the contents of a matrix instance as a Perl list.
π§ write
given a matrix and a filename, writes that matrix to the file. β οΈ note that if the file exists it will be overwritten. also, files written by Math::GSL will not be compatible with files written by Math::MatrixReal, nor vice-versa. so, like, don't mix them, i'm begging π
$matrix->write("my_matrix.dat");
π§ read
reads a matrix written by the write method. note that it will only read matrices written by the same back-end that is being used for reading. so if you wrote it with one backend and read it with the otherβ¦ nope π
my $matrix = Matrix->load("my_matrix.dat");
π row
returns the selected row in a matrix as a new matrix object. β οΈ note that indexes start at 1, unlike Perl and some other programming languages.
my $row = $matrix->row(1);
π find_zeros
given a matrix, returns a nested list of indices corresponding to zero values in the given matrix. β οΈ note that indexes start at 1, unlike Perl and some other programming languages.
my @indices = $matrix->find_zeros();
π¦ transpose
returns transposed matrix. that's it, that's the whole sentence, she's efficient π
π OVERLOAD π
for now, only matrix multiplication and addition are overloaded, in the usual operators, * and +, correspondingly. take attention that these operations only work if the matrix dimensions are compatible β incompatible dims and it's over, sorry bestie π
$m = $a * $b;
$n = $a + $b;
π BUGS π
at this initial stage of this module, only the methods that I am really needing for my depending applications are implemented. therefore, it might not include the method that you were looking for. nevertheless β and this is the sweet part β send me an e-mail (or open an issue on GitHub) and I'll be happy to include it (given the two modules support it). so like, dm me fr fr π«Ά
π SEE ALSO π
check Math::MatrixReal and Math::GSL::Matrix documentation. iykyk π
π«Ά CONTRIBUTORS π«Ά
- Andrius Merkys <merkys@cpan.org>
- Ivan Baidakou
- Gabor Szabo
π AUTHOR π
Alberto SimΓ΅es <ambs@cpan.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2014-2023 by Alberto SimΓ΅es.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. free as in freedom, bestie π
and that's a wrap, my love β¨ this page came to you from perl v5.36.0, dated 2023-01-25 β go forth and multiply (your matrices) π π
π¦ where this page comes from
- debian bookworm: libmath-matrix-maybegsl-perl 0.008-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmath-matrix-maybegsl-perl 0.008-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmath-matrix-maybegsl-perl 0.008-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmath-matrix-maybegsl-perl 0.008-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-matrix-maybegsl-perl 0.008-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms