Debian Codegraph @

lib/PDL/LinearAlgebra/Special.pm

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