1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
package PDL::LinearAlgebra::Special;
use PDL::Core;
use PDL::NiceSlice;
use PDL::Slices;
use PDL::Basic qw (sequence xvals yvals);
use PDL::MatrixOps qw (identity);
use PDL::LinearAlgebra qw ( );
use PDL::LinearAlgebra::Real;
use PDL::LinearAlgebra::Complex;
use PDL::Exporter;
no warnings 'uninitialized';
@EXPORT_OK = qw( mhilb mvander mpart mhankel mtoeplitz mtri mpascal mcompanion);
%EXPORT_TAGS = (Func=>[@EXPORT_OK]);
our $VERSION = '0.14';
$VERSION = eval $VERSION;
our @ISA = ( 'PDL::Exporter');
use strict;
=encoding utf8
=head1 NAME
PDL::LinearAlgebra::Special - Special matrices for PDL
=head1 SYNOPSIS
use PDL::LinearAlgebra::Special;
$a = mhilb(5,5);
=head1 DESCRIPTION
This module provides some constructors of well known matrices.
=head1 FUNCTIONS
=head2 mhilb
=for ref
Construct Hilbert matrix from specifications list or template ndarray
=for usage
PDL(Hilbert) = mpart(PDL(template) | ARRAY(specification))
=for example
my $hilb = mhilb(float,5,5);
=cut
sub mhilb {
if(ref($_[0]) && ref($_[0]) eq 'PDL'){
my $pdl = shift;
$pdl->mhilb(@_);
}
else{
PDL->mhilb(@_);
}
}
sub PDL::mhilb {
my $class = shift;
my $pdl1 = scalar(@_)? $class->new_from_specification(@_) : $class->copy;
my $pdl2 = scalar(@_)? $class->new_from_specification(@_) : $class->copy;
1 / ($pdl1->inplace->axisvals + $pdl2->inplace->axisvals(1) + 1);
}
=head2 mtri
=for ref
Return zeroed matrix with upper or lower triangular part from another matrix.
Return trapezoid matrix if entry matrix is not square.
Supports threading.
Uses L<tricpy|PDL::LinearAlgebra::Real/tricpy> or L<tricpy|PDL::LinearAlgebra::Complex/ctricpy>.
=for usage
PDL = mtri(PDL, SCALAR)
SCALAR : UPPER = 0 | LOWER = 1, default = 0
=for example
my $a = random(10,10);
my $b = mtri($a, 0);
=cut
sub mtri{
my $m = shift;
$m->mtri(@_);
}
sub PDL::mtri {
&PDL::LinearAlgebra::_2d_array;
my ($m, $upper) = @_;
$m->tricpy($upper, my $b = $m->_similar_null);
$b;
}
=head2 mvander
Return (primal) Vandermonde matrix from vector.
=for ref
mvander(M,P) is a rectangular version of mvander(P) with M Columns.
=cut
sub mvander($;$) {
my $exp = @_ == 2 ? sequence(shift) : sequence($_[0]->dim(-1));
$_[0]->dummy(-2)**$exp;
}
=head2 mpart
=for ref
Return antisymmetric and symmetric part of a real or complex square matrix.
=for usage
( PDL(antisymmetric), PDL(symmetric) ) = mpart(PDL, SCALAR(conj))
conj : if true Return AntiHermitian, Hermitian part.
=for example
my $a = random(10,10);
my ( $antisymmetric, $symmetric ) = mpart($a);
=cut
*mpart = \&PDL::mpart;
sub PDL::mpart {
&PDL::LinearAlgebra::_square;
my ($m, $conj) = @_;
# antisymmetric and symmetric part
return (0.5* ($m - $m->t($conj))),(0.5* ($m + $m->t($conj)));
}
=head2 mhankel
=for ref
Return Hankel matrix also known as persymmetric matrix.
Handles complex data.
=for usage
mhankel(c,r), where c and r are vectors, returns matrix whose first column
is c and whose last row is r. The last element of c prevails.
mhankel(c) returns matrix with element below skew diagonal (anti-diagonal) equals
to zero. If c is a scalar number, make it from sequence beginning at one.
=for ref
The elements are:
H (i,j) = c (i+j), i+j+1 <= m;
H (i,j) = r (i+j-m+1), otherwise
where m is the size of the vector.
If c is a scalar number, its determinant can be computed by:
floor(n/2) n
Det(H(n)) = (-1) * n
=cut
*mhankel = \&PDL::mhankel;
sub PDL::mhankel {
my $di = $_[0]->dims_internal;
my ($m, $n) = @_;
$m = xvals($m) + 1 unless ref($m);
my @dims = $m->dims;
$n = PDL::zeroes($m) unless defined $n;
my $index = xvals($dims[$di]);
$index = $index->dummy(0) + $index;
if (@dims == 2){
$m = mstack($m,$n(,1:));
$n = $m->re->index($index)->r2C;
$n((1),).= $m((1),)->index($index);
return $n;
}
else{
$m = augment($m,$n(1:));
return $m->index($index)->sever;
}
}
=head2 mtoeplitz
=for ref
Return toeplitz matrix.
Handles complex data.
=for usage
mtoeplitz(c,r), where c and r are vectors, returns matrix whose first column
is c and whose last row is r. The last element of c prevails.
mtoeplitz(c) returns symmetric matrix.
=cut
*mtoeplitz = \&PDL::mtoeplitz;
sub PDL::mtoeplitz {
my $di = $_[0]->dims_internal;
my $slice_prefix = ',' x $di;
my ($m, $n) = @_;
$n = $m->copy unless defined $n;
my $mdim= $m->dim(-1);
my $ndim= $n->dim(-1);
my $res = $m->_similar($ndim,$mdim);
$ndim--;
my $min = $mdim <= $ndim ? $mdim : $ndim;
for(1..$min) {
$res->slice("$slice_prefix$_:,(@{[$_-1]})") .= $n->slice("${slice_prefix}1:@{[$ndim-$_+1]}");
}
$mdim--;
$min = $mdim < $ndim ? $mdim : $ndim;
for(0..$min){
$res->slice("${slice_prefix}($_),$_:") .= $m->slice("${slice_prefix}:@{[$mdim-$_]}");
}
return $res;
}
=head2 mpascal
Return Pascal matrix (from Pascal's triangle) of order N.
=for usage
mpascal(N,uplo).
uplo:
0 => upper triangular (Cholesky factor),
1 => lower triangular (Cholesky factor),
2 => symmetric.
=for ref
This matrix is obtained by writing Pascal's triangle (whose elements are binomial
coefficients from index and/or index sum) as a matrix and truncating appropriately.
The symmetric Pascal is positive definite, its inverse has integer entries.
Their determinants are all equal to one and:
S = L * U
where S, L, U are symmetric, lower and upper pascal matrix respectively.
=cut
*mpascal = \&PDL::mpascal;
sub PDL::mpascal {
my ($m, $n) = @_;
my $mat = eval {
require PDL::GSLSF::GAMMA;
if ($n > 1){
my $mat = xvals($m);
return (PDL::GSLSF::GAMMA::gsl_sf_choose($mat + $mat->dummy(0),$mat))[0];
}else{
my $mat = xvals($m, $m);
return (PDL::GSLSF::GAMMA::gsl_sf_choose($mat->tritosym,$mat->xchg(0,1)->tritosym))[0]->mtri($n);
}
};
return $mat if !$@;
warn("mpascal: can't compute binomial coefficients without".
" PDL::GSLSF::GAMMA\n");
return;
}
=head2 mcompanion
Return a matrix with characteristic polynomial equal to p if p is monic.
If p is not monic the characteristic polynomial of A is equal to p/c where c is the
coefficient of largest degree in p (here p is in descending order).
=for usage
mcompanion(PDL(p),SCALAR(charpol)).
charpol:
0 => first row is -P(1:n-1)/P(0),
1 => last column is -P(1:n-1)/P(0),
=cut
*mcompanion = \&PDL::mcompanion;
sub PDL::mcompanion{
my $di = $_[0]->dims_internal;
my $slice_prefix = ',' x $di;
my ($m, $char) = @_;
my( @dims, $dim, $ret);
$m = $m->{PDL} if (UNIVERSAL::isa($m, 'HASH') && exists $m->{PDL});
@dims = $m->dims;
$dim = $dims[-1] - 1;
my $id = identity($dim-1); $id = $id->r2C if $m->_is_complex;
if($char){
$ret = (-$m->slice("${slice_prefix}1:$dim")->dummy($di+1)/$m->slice("${slice_prefix}0"))->_call_method('mstack', $id->mstack(zeroes($m->dims_internal_values,$dim-1)->dummy($di)));
}
else{
$ret = $m->_similar($dim-1)->dummy($di+1)->_call_method('mstack', $id)->mstack(-$m->slice("${slice_prefix}$dim:1")->dummy($di)/$m->slice("${slice_prefix}(0)"));
}
$ret->sever;
}
=head1 AUTHOR
Copyright (C) Grégory Vanuxem 2005-2007.
This library is free software; you can redistribute it and/or modify
it under the terms of the artistic license as specified in the Artistic
file.
=cut
# Exit with OK status
1;
|