## Math/BLAS.pm --- basic linear algebra subroutines.
# Copyright (C) 2010 Ralph Schleicher. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
## Code:
package Math::BLAS;
use strict;
use warnings;
use Exporter qw(import);
use Math::BLAS::Enum;
use Math::BLAS::PP;
BEGIN
{
our $VERSION = '1.01';
our @EXPORT = ();
our @EXPORT_OK = ();
our %EXPORT_TAGS = ();
# Named constants.
$EXPORT_TAGS{enum}
= [ @Math::BLAS::Enum::EXPORT ];
push (@EXPORT_OK, @{ $EXPORT_TAGS{enum} });
push (@EXPORT, @{ $EXPORT_TAGS{enum} });
# Subroutines.
$EXPORT_TAGS{sub}
= [qw(blas_dot
blas_norm
blas_sum
blas_min_val
blas_amin_val
blas_max_val
blas_amax_val
blas_sumsq
blas_rscale
blas_axpby
blas_waxpby
blas_copy
blas_swap
blas_gemv
blas_gemm)];
push (@EXPORT_OK, @{ $EXPORT_TAGS{sub} });
push (@EXPORT, @{ $EXPORT_TAGS{sub} });
# Define aliases.
if (1)
{
no strict 'refs';
my ($s, @bare) = ();
foreach (@{ $EXPORT_TAGS{sub} })
{
($s = $_) =~ s/\Ablas_//;
*{$s} = \&{$_};
push (@bare, $s);
}
$EXPORT_TAGS{bare} = [@bare];
push (@EXPORT_OK, @bare);
}
$EXPORT_TAGS{all} = [ @EXPORT_OK ];
}
# Dot product.
sub blas_dot ($$$%)
{
my ($n, $x, $y, %opt) = @_;
dot_d (int ($n),
$opt{alpha} // 1,
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$y,
int ($opt{y_ind} // 0),
int ($opt{y_incr} // 1),
$opt{beta} // 0,
$opt{r} // 0,
int ($opt{r_ind} // 0));
}
# Vector norms.
sub blas_norm ($$%)
{
my ($n, $x, %opt) = @_;
norm_d ($opt{norm} // BLAS_ONE_NORM,
int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Sum.
sub blas_sum ($$%)
{
my ($n, $x, %opt) = @_;
sum_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Minimum value.
sub blas_min_val ($$%)
{
my ($n, $x, %opt) = @_;
min_val_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Minimum absolute value.
sub blas_amin_val ($$%)
{
my ($n, $x, %opt) = @_;
amin_val_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Maximum value.
sub blas_max_val ($$%)
{
my ($n, $x, %opt) = @_;
max_val_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Maximum absolute value.
sub blas_amax_val ($$%)
{
my ($n, $x, %opt) = @_;
amax_val_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Sum of squares.
sub blas_sumsq ($$%)
{
my ($n, $x, %opt) = @_;
sumsq_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$opt{sumsq},
$opt{scale});
}
# Reciprocal scale.
sub blas_rscale ($$%)
{
my ($n, $x, %opt) = @_;
rscale_d (int ($n),
$opt{alpha} // 1,
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1));
}
# Scaled vector accumulation.
sub blas_axpby ($$$%)
{
my ($n, $x, $y, %opt) = @_;
axpby_d (int ($n),
$opt{alpha} // 1,
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$opt{beta} // 1,
$y,
int ($opt{y_ind} // 0),
int ($opt{y_incr} // 1));
}
# Scaled vector addition.
sub blas_waxpby ($$$$%)
{
my ($n, $x, $y, $w, %opt) = @_;
waxpby_d (int ($n),
$opt{alpha} // 1,
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$opt{beta} // 1,
$y,
int ($opt{y_ind} // 0),
int ($opt{y_incr} // 1),
$w,
int ($opt{w_ind} // 0),
int ($opt{w_incr} // 1));
}
# Copy vector.
sub blas_copy ($$$%)
{
my ($n, $x, $y, %opt) = @_;
copy_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$y,
int ($opt{y_ind} // 0),
int ($opt{y_incr} // 1));
}
# Swap vectors.
sub blas_swap ($$$%)
{
my ($n, $x, $y, %opt) = @_;
swap_d (int ($n),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$y,
int ($opt{y_ind} // 0),
int ($opt{y_incr} // 1));
}
# General matrix/vector multiplication.
sub blas_gemv ($$$$$%)
{
my ($m, $n, $a, $x, $y, %opt) = @_;
my $a_op = $opt{a_op} // BLAS_NO_TRANS;
gemv_d ($a_op,
int ($m),
int ($n),
$opt{alpha} // 1,
$a,
int ($opt{a_ind} // 0),
int ($opt{a_incr} // ($a_op == BLAS_NO_TRANS ? $n : $m)),
$x,
int ($opt{x_ind} // 0),
int ($opt{x_incr} // 1),
$opt{beta} // 0,
$y,
int ($opt{y_ind} // 0),
int ($opt{y_incr} // 1));
}
# General matrix/matrix multiplication.
sub blas_gemm ($$$$$$%)
{
my ($m, $n, $k, $a, $b, $c, %opt) = @_;
my $a_op = $opt{a_op} // BLAS_NO_TRANS;
my $b_op = $opt{b_op} // BLAS_NO_TRANS;
gemm_d ($a_op,
$b_op,
int ($m),
int ($n),
int ($k),
$opt{alpha} // 1,
$a,
int ($opt{a_ind} // 0),
int ($opt{a_incr} // ($a_op == BLAS_NO_TRANS ? $k : $m)),
$b,
int ($opt{b_ind} // 0),
int ($opt{b_incr} // ($b_op == BLAS_NO_TRANS ? $n : $k)),
$opt{beta} // 0,
$c,
int ($opt{c_ind} // 0),
int ($opt{c_incr} // $n));
}
1;
__END__
=pod
=encoding utf8
=head1 NAME
Math::BLAS - basic linear algebra subroutines
=head1 SYNOPSIS
use Math::BLAS;
=head1 DESCRIPTION
=head2 General Conventions
=head3 Notation
The following notation is used in this document.
=over
=item *
I, I**, and I are matrices
=item *
I is a diagonal matrix
=item *
I** is a permutation matrix
=item *
op(I) denotes I or I'
=item *
I' is the transpose matrix of I
=item *
I__, I, I, I, I, and I are vectors
=item *
I, I__~~, and I are scalars
=item *
α and β are constants
=item *
← denotes assignment
=back
=head3 Subroutine Arguments
=over
=item Problem Size
The problem size is specified by the integral numbers I and I.
For vector operations, argument I is the number of vector elements.
For matrix operations, I is the number of matrix rows and I is the
number of matrix columns. For square matrix operations, I is the
number of matrix rows and matrix columns.
Size arguments which are not integral numbers are truncated to the next
integral number using Perl's built-in procedure C.
=item Scalar Operands
A scalar operand is a Perl scalar value. Scalar operands are specified
as properties with appropriate default values.
=item Vector Operands
A vector operand I is specified by three arguments. Required
argument I is a Perl array reference. The corresponding array index
property I specifies the Perl array index of the first vector
element. The default value for the array index property is zero.
Vectors are permitted to have spacing between elements. This spacing is
specified by the Perl array index increment property I. The
default value for the array index increment property is one.
=item Matrix Operands
A matrix operand I~~~~ is specified by three arguments. Required
argument I is a Perl array reference. The corresponding array index
property I specifies the Perl array index of the first matrix
element. The default value for the array index property is zero.
Matrices are permitted to have more rows and/or columns than specified
by the problem size. The actual number of columns is specified by the
Perl array index increment property I. The default value for
the array index increment property is derived from the problem size.
=back
=head2 Reduction Operations
=over
=item C (I, I, I, ...)
Dot product.
=over
=item
I ← α I'·I + β I
=back
The C function adds the scaled dot product of two vectors I and
I into a scaled scalar I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the left-hand side vector operand.
=item *
Third argument I is the right-hand side vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, and I. The
following table lists the non-standard property names together with
their meaning.
=over
=item I
The scale factor for the dot product.
Default value is one.
=item I
The scale factor for the scalar operand.
Default value is zero.
=item I
The scalar operand. Value is either a scalar value or an array
reference. Default value is zero.
=item I
The Perl array index of the array element for the array I. This
property is only evaluated if I is an array reference. Default value
is zero.
=back
=back
Arguments I and I are only evaluated if I is not equal to
zero and if I is greater than zero. Argument I is only evaluated
if I is not equal to zero.
Return value is the result of the form. If I is less than zero, the
function returns immediately with a return value of C.
=item C (I, I, ...)
Vector norms.
=over
=item one-norm
I ← ∑ |x|
=item two-norm
I² ← ∑ x²
=item infinity-norm
I ← max |x|
=back
The C function computes either the one-norm, two-norm (that is
Euclidean norm), or infinity-norm of a vector I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I and I. The following table lists the
non-standard property names together with their meaning.
=over
=item I
The type of vector norm. Value is either C,
C (or C), or C.
Default is to compute the one-norm.
=back
=back
Argument I is only evaluated if I is greater than zero.
Return value is the vector norm. If I is less than or equal to zero,
the function returns immediately with a return value of zero.
=item C (I, I, ...)
Sum of vector elements.
=over
=item
I ← ∑ x
=back
The C function computes the sum of the elements of a vector I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I and I.
=back
Argument I is only evaluated if I is greater than zero.
Return value is the result of the form. If I is less than or equal
to zero, the function returns immediately with a return value of zero.
=item C (I, I, ...)
=item C (I, I, ...)
=item C (I, I, ...)
=item C (I, I, ...)
Minimum/maximum value and location.
=over
=item minimum value
I, I ← min x
=item minimum absolute value
I, I ← min |x|
=item maximum value
I, I ← max x
=item maximum absolute value
I, I ← max |x|
=back
The C function finds the smallest element of a vector. The
C function finds the smallest element of a vector with respect
to the absolute value. The C function finds the largest
element of a vector. The C function finds the largest element
of a vector with respect to the absolute value.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I and I.
=back
Argument I is only evaluated if I is greater than zero.
Return value is a list with two elements. First element is the index
offset of the vector element. Second element is the value of the
corresponding vector element. If argument I is less than or equal to
zero, the function returns immediately with an index offset of C
and an element value of zero.
If you are only interested in one of the return values, either assign
the unwanted return value to C or directly subscribe the returned
list. Say, for example
(undef, $val) = blas_min_val ($n, $x);
or
$val = (blas_min_val ($n, $x))[1];
=item C (I, I, ...)
Sum of squares.
=over
=item
I~~~~·I² ← I~~~~·I² + ∑ x²
=back
The C function computes the scaled sum of squares I~~~~ and
the scale factor I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I and I. The following table lists the
non-standard property names together with their meaning.
=over
=item I
The start value for the scaled sum of squares.
Default value is zero.
=item I
The start value for the scale factor.
Default value is one.
=back
=back
Arguments I, I, and I are only evaluated if I is
greater than zero.
Return value is a list with two elements. First element is the scaled
sum of squares. Second element is the scale factor. If I is less
than or equal to zero, the function returns immediately with the
unchanged values of I and I.
=back
=head2 Vector Operations
=over
=item C (I, I, ...)
Reciprocal scale.
=over
=item
I ← I/α
=back
The C function divides the elements of the vector I by the
real scalar α. The scalar α is expected to be non-zero.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I and I. The following table lists the
non-standard property names together with their meaning.
=over
=item I
The reciprocal scale factor for the vector operand.
Default value is one.
=back
=back
Argument I is only evaluated if I is not equal to one.
The procedure returns immediately if I is less than or equal to zero.
=item C (I, I, I, ...)
Scaled vector accumulation.
=over
=item
I ← α I + β I
=back
The C function adds the scaled vector I into the scaled vector
I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the first vector operand.
=item *
Third argument I is the second vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, and I. The
following table lists the non-standard property names together with
their meaning.
=over
=item I
The scale factor for the first vector operand.
Default value is one.
=item I
The scale factor for the second vector operand.
Default value is one.
=back
=back
Argument I is only evaluated if I is not equal to zero.
The procedure returns immediately if I is less than or equal to zero.
=item C (I, I, I, I, ...)
Scaled vector addition.
=over
=item
I ← α I + β I
=back
The C function adds two scaled vectors I and I and stores
the result in the vector I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the first vector operand.
=item *
Third argument I is the second vector operand.
=item *
Fourth argument I is the result vector.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, I, I, and
I. The following table lists the non-standard property names
together with their meaning.
=over
=item I
The scale factor for the first vector operand.
Default value is one.
=item I
The scale factor for the second vector operand.
Default value is one.
=back
=back
Argument I is only evaluated if I is not equal to zero.
Argument I is only evaluated if I is not equal to zero.
The procedure returns immediately if I is less than or equal to zero.
=back
=head2 Data Movement with Vectors
=over
=item C (I, I, I, ...)
Copy vector elements.
=over
=item
I ← I
=back
The C function assigns the elements of the vector I to the
elements of the vector I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the source vector.
=item *
Third argument I is the destination vector.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, and I.
=back
The procedure returns immediately if I is less than or equal to zero.
=item C (I, I, I, ...)
Interchange vector elements.
=over
=item
I ↔ I
=back
The C function interchanges the elements of the vector I with
the elements of the vector I.
=over
=item *
First argument I is the number of vector elements.
=item *
Second argument I is the first vector operand.
=item *
Third argument I is the second vector operand.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, and I.
=back
The procedure returns immediately if I is less than or equal to zero.
=back
=head2 Matrix/Vector Operations
=over
=item C (I, I, I~~~~, I, I, ...)
General matrix/vector multiplication.
=over
=item
I ← α op(I)·I + β I
=back
The C function multiplies the matrix I with the
vector I and adds the scaled product into the scaled vector I.
If S) = I>, I is a S<(I, I)> matrix.
If S) = I'>, I is a S<(I, I)> matrix.
Operand I is a vector with I elements and I is a vector with
I elements.
=over
=item *
First argument I is the number of matrix rows.
=item *
Second argument I is the number of matrix columns.
=item *
Third argument I is the matrix operand.
=item *
Fourth argument I is the vector operand.
=item *
Fifth argument I is the result vector.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, I, I, and
I. The following table lists the non-standard property names
together with their meaning.
=over
=item I
The transpose flag for the matrix I. Value is either
C or C. Default is to not transpose the
matrix I.
=item I
The scale factor for the matrix/vector product.
Default value is one.
=item I
The scale factor for the result vector.
Default value is zero.
=back
=back
The procedure returns immediately if I or I is less than or equal
to zero.
=back
=head2 Matrix/Matrix Operations
=over
=item C (I, I, I, I, I~~**, I, ...)
General matrix/matrix multiplication.
=over
=item
I ← α op(I****)·op(I****) + β I
=back
The C function multiplies the matrix I**** with the matrix I**** and
adds the scaled product into the scaled S<(I, I)> matrix I.
If S) = I****>, I is a S<(I, I)> matrix.
If S) = I'>, I is a S<(I, I)> matrix.
If S) = I****>, I**** is a S<(I, I)> matrix.
If S) = I****'>, I**** is a S<(I, I)> matrix.
=over
=item *
First argument I is the number of matrix rows.
=item *
Second argument I is the number of matrix columns.
=item *
Third argument I is the number of vector elements for the
matrix/matrix product.
=item *
Fourth argument I**** is the left-hand side matrix operand.
=item *
Fifth argument I**** is the right-hand side matrix operand.
=item *
Sixth argument I is the result matrix.
=item *
The rest of the arguments form a property list. Applicable standard
properties are I, I, I, I, I, and
I. The following table lists the non-standard property names
together with their meaning.
=over
=item I
The transpose flag for the matrix I****. Value is either
C or C. Default is to not transpose the
matrix I.
=item I
The transpose flag for the matrix I****. Value is either
C or C. Default is to not transpose the
matrix I****.
=item I
The scale factor for the matrix/matrix product.
Default value is one.
=item I
The scale factor for the result matrix.
Default value is zero.
=back
=back
The procedure returns immediately if I or I is less than or equal
to zero.
=back
=head1 SEE ALSO
Math::BLAS::L,
Math::BLAS::L
=head2 External Links
L
=head1 AUTHOR
Ralph Schleicher
=cut
## Math/BLAS.pm ends here
**