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>


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