Math::...Matrix(3pm)

πŸ’– Math::Cephes::Matrix β€” bestie edition πŸ’–

okay so literally the whole vibe of this page is Math::Cephes::Matrix, a Perl interface to the cephes matrix routines, and it's giving "User Contributed Perl Documentation" energy, iykyk. come sit with me, we're doing linear algebra and we're gonna slay it 🫢


✨ NAME ✨

Math::Cephes::Matrix β€” no cap, it's the Perl interface to the cephes matrix routines. that's the whole name. that's her. πŸ’…


🌸 SYNOPSIS 🌸

use Math::Cephes::Matrix qw(mat);
# 'mat' is a shortcut for Math::Cephes::Matrix->new
my $M = mat([ [1, 2, -1], [2, -3, 1], [1, 0, 3]]);
my $C = mat([ [1, 2, 4], [2, 9, 2], [6, 2, 7]]);
my $D = $M->add($C);          # D = M + C
my $Dc = $D->coef;
for (my $i=0; $i<3; $i++) {
  print "row $i:\n";
  for (my $j=0; $j<3; $j++) {
      print "\tcolumn $j: $Dc->[$i]->[$j]\n";
  }
}

like… mat is right there being a shortcut for Math::Cephes::Matrix->new, and yes the comment says exactly that because we keep the facts bestie, always. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

So this module is a layer on top of the basic routines in the cephes math library for operations on square matrices. periodt. in everything that follows, a Math::Cephes::Matrix object gets created like this:

my $M = Math::Cephes::Matrix->new($arr_ref);

where $arr_ref is a reference to an array of arrays, exactly like this lil cutie:

$arr_ref = [ [1, 2, -1], [2, -3, 1], [1, 0, 3] ]

which represents… drumroll… πŸ’«

   / 1   2  -1  \
   | 2  -3   1  |
   \ 1   0   3  /

look at her. iconic. 🌈

And if you wanna copy a Math::Cephes::Matrix object? obsessed, easy:

my $M_copy = $M->new();

🧚 Methods 🧚

πŸ’œ coef: get coefficients of the matrix

SYNOPSIS:

my $c = $M->coef;

DESCRIPTION:

this returns a reference to an array of arrays containing the coefficients of the matrix. (yes the original says "an reference", we love her, we're not fixing grammar, we're keeping the facts.) 😌


πŸ“ clr: set all coefficients equal to a value.

SYNOPSIS:

$M->clr($n);

DESCRIPTION:

this sets all the coefficients of the matrix identically to $n. and if $n isn't given? a default of 0 is used. no thoughts, just zeros. πŸ’€


✨ add: add two matrices

SYNOPSIS:

$P = $M->add($N);

DESCRIPTION:

this sets $P equal to $M + $N. simple math, main character energy. πŸ‘‘


🌸 sub: subtract two matrices

SYNOPSIS:

$P = $M->sub($N);

DESCRIPTION:

this sets $P equal to $M - $N. she takes away. respectfully. πŸ’…


πŸ”₯ mul: multiply two matrices or a matrix and a vector

SYNOPSIS:

$P = $M->mul($N);

DESCRIPTION:

this sets $P equal to $M * $N. and bestie she is versatile β€” this method can handle matrix multiplication, when $N is a matrix, as well as matrix-vector multiplication, where $N is an array reference representing a column vector. range. πŸ’–


πŸ¦‹ div: divide two matrices

SYNOPSIS:

$P = $M->div($N);

DESCRIPTION:

this sets $P equal to $M * ($N)^(-1). not me reminding you it's multiplication by the inverse, not elemental division β€” that's just what it says, fr fr. πŸ™ˆ


πŸ’« inv: invert a matrix

SYNOPSIS:

$I = $M->inv();

DESCRIPTION:

this sets $I equal to ($M)^(-1). the inverse arc. she's thriving. ✨


πŸŽ€ transp: transpose a matrix

SYNOPSIS:

$T = $M->transp();

DESCRIPTION:

this sets $T equal to the transpose of $M. flip her, she eats. πŸ“


🌈 simq: solve simultaneous equations

SYNOPSIS:

my $M = Math::Cephes::Matrix->new([ [1, 2, -1], [2, -3, 1], [1, 0, 3]]);
my $B = [2, -1, 10];
my $X = $M->simq($B);
for (my $i=0; $i<3; $i++) {
   print "X[$i] is $X->[$i]\n";
 }

where $M is a Math::Cephes::Matrix object, $B is an input array reference, and $X is an output array reference. note who's input and who's output, bestie, that's important. πŸ“

DESCRIPTION:

a set of N simultaneous equations may be represented in matrix form as

M X = B

where M is an N x N square matrix and X and B are column vectors of length N. math said "let me make it a vibe" and here we are. 🌟


πŸ‘‘ eigens: eigenvalues and eigenvectors of a real symmetric matrix

SYNOPSIS:

my $S = Math::Cephes::Matrix->new([ [1, 2, 3], [2, 2, 3], [3, 3, 4]]);
my ($E, $EV1) = $S->eigens();
my $EV = $EV1->coef;
for (my $i=0; $i<3; $i++) {
  print "For i=$i, with eigenvalue $E->[$i]\n";
  my $v = [];
  for (my $j=0; $j<3; $j++) {
    $v->[$j] = $EV->[$i]->[$j];
  }
  print "The eigenvector is @$v\n";
}

where $M is a Math::Cephes::Matrix object representing a real symmetric matrix. $E is an array reference containing the eigenvalues of $M, and $EV is a Math::Cephes::Matrix object representing the eigenvalues, the ith row corresponding to the ith eigenvalue. (yes it says eigenvalues both times up there β€” we're keeping every word, that's the deal.) πŸ’œ

DESCRIPTION:

if M is an N x N real symmetric matrix, and X is an N component column vector, the eigenvalue problem

M X = lambda X

will in general have N solutions, with X the eigenvectors and lambda the eigenvalues. not one solution, N solutions. she's multidimensional like that. 😭✨


πŸ’€ BUGS πŸ’€

found a bug? please report any to Randy Kobes <randy@theoryx5.uwinnipeg.ca>. be nice, bestie, he's the one who made this. 🫢


the C code for the Cephes Math Library is Copyright 1984, 1987, 1989, 2002 by Stephen L. Moshier, and is available at http://www.netlib.org/cephes/. direct inquiries to 30 Frost Street, Cambridge, MA 02140. πŸ’Œ

the perl interface is copyright 2000, 2002 by Randy Kobes. this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. free! as in freedom! we love to see it. 🌸


ok that's the whole page, no cap, every flag and every fact is still in there. go do matrices. slay. πŸ’…

perl v5.42.3 Β· 2026-08-19 Β· and that's the sign-off bestie, generated on that date by that perl, mwah πŸ’‹πŸ¦‹

πŸ“¦ where this page comes from