How to Use

In Ubuntu, PerlTeX is available in the texlive-latex-extra package.

sudo apt-get install texlive-latex-extra

To compile the document:

perltex --latex=pdflatex matrix_test.tex

Code

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{parskip}
\usepackage{url}
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{perltex}
 
\title{Document Title}
\author{Ryan Fox}
\date{\today}
 
\perlnewcommand{\defmatrix}[2]
{
    sub trim
    {
        my $str = shift;
        $str =~ s/^\s+|\s+$//g;
        return $str;
    }
 
    use Math::Matrix;
    use Math::Trig;
 
    my ($name, $text) = @_;
    my @matrix;
 
    for(split /\\\\/, $text)
    {
        if(trim($_) ne "")
        {
            my @split;
            for(map {trim $_} split /&/)
            {
                my $tmp = $1 if $_ =~ s/\\//g;
                my $e = eval $_ or eval $tmp or $_;
                push @split, $e;
            }
            push @matrix, \@split;
        }
    }
    $global{$name} = new Math::Matrix(@matrix);
    return "";
}
 
\perlnewcommand{\printmatrix}[1]
{
    my $name = $_[0];
    my @matrix = @{$global{$name}};
    my $text = "\\begin{bmatrix}";
    for my $r (@matrix)
    {
        my @row = @{$r};
        for(my $i = 0; $i < @row-1; ++$i)
        {
            $text .= "$row[$i] & ";
        }
        $text .= "$row[$#row] \\\\";
    }
    $text .= "\\end{bmatrix}";
    return $text;
}
 
\perlnewcommand{\invmatrix}[2]
{
    my $name = $_[0];
    my $newname = $_[1];
    my $matrix = $global{$name};
    $global{$newname} = $matrix->invert;
    return "";
}
 
\perlnewcommand{\solvematrices}[3]
{
    my $result = $_[2];
    my $matrix1 = $global{$_[0]};
    my $matrix2 = $global{$_[1]};
    $global{$result} = $matrix1->concat($matrix2)->solve;
    return "";
}
 
\perlnewcommand{\multmatrices}[3]
{
    my $result = $_[2];
    my $matrix1 = $global{$_[0]};
    my $matrix2 = $global{$_[1]};
    if(ref $matrix2)
    {
        $global{$result} = $matrix1->multiply($matrix2);
    }
    else
    {
        $global{$result} = $matrix1->multiply_scalar($_[1]);
    }
    return "";
}
 
\perlnewcommand{\addmatrices}[3]
{
    my $result = $_[2];
    my $matrix1 = $global{$_[0]};
    my $matrix2 = $global{$_[1]};
    if(ref $matrix2)
    {
        $global{$result} = $matrix1->add($matrix2);
    }
    else
    {
        my ($m,$n) = $matrix1->size;
        my @vals;
        for(1..$m)
        {
            my @v;
            for(1..$n)
            {
                push @v, $_[1];
            }
            push @vals,\@v;
        }
        my $tmp = new Math::Matrix(@vals);
        $global{$result} = $matrix1->add($tmp);
    }
    return "";
}
 
\perlnewcommand{\dumper}[1]
{
    use Data::Dumper;
    return "\\begin{verbatim}".Dumper($global{$_[0]})."\\end{verbatim}";
}
 
\begin{document}
 
\defmatrix{test}
{
    sin(pi/2) & cos(0) & 0 \\
    2 & -1 & 0 \\
    1 & rand & 1 \\
}
 
\defmatrix{test2}
{
    (rand()>0.5 ? 1 : 0) & 0 & 1 \\
    0 & 1 & 0 \\
    1 & 0 & 0 \\
}
 
\invmatrix{test2}{test2}
\addmatrices{test2}{test}{sol}
 
\begin{eqnarray}
  \printmatrix{test} + \printmatrix{test2} = \printmatrix{sol}
\end{eqnarray}
 
\end{document}
 
perltex_matrix_manipulation_macros.txt · Last modified: 2010/07/12 16:05 by Ryan Fox
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki