Debian Codegraph @

lib/PDL/LinearAlgebra/Special.pm

raw symbols SCIP
1 package PDL::LinearAlgebra::Specialscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/;
2 use PDL::Corescip-perl cpan PDL-LinearAlgebra 0.437 PDL/Core/;
3 use PDL::NiceSlicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/NiceSlice/;
4 use PDL::Slicesscip-perl cpan PDL-LinearAlgebra 0.437 PDL/Slices/;
5 use PDL::Basicscip-perl cpan PDL-LinearAlgebra 0.437 PDL/Basic/ qw (sequence xvals yvals);
6 use PDL::MatrixOpsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/MatrixOps/ qw (identity);
7 use PDL::LinearAlgebrascip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/ qw ( );
8 use PDL::LinearAlgebra::Realscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Real/;
9 use PDL::LinearAlgebra::Complexscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Complex/;
10 use PDL::Exporterscip-perl cpan PDL-LinearAlgebra 0.437 PDL/Exporter/;
11 use strictscip-perl cpan PDL-LinearAlgebra 0.437 strict/;
12 use warningsscip-perl cpan PDL-LinearAlgebra 0.437 warnings/;
13 our @EXPORT_OKscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@EXPORT_OK`. = qw(mhilb mvander mpart mhankel mtoeplitz mtri mpascal mcompanion);
14 our %EXPORT_TAGSscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`%EXPORT_TAGS`. = (Func=>\@EXPORT_OKscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@EXPORT_OK`.);
15
16 our $VERSIONscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/$VERSION. = '0.14';
17 $VERSIONscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/$VERSION. = eval $VERSIONscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/$VERSION.;
18
19 our @ISAscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@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 mhilbscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mhilb(). {
56 if(refscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/ref().($_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.[0]) && refscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/ref().($_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.[0]) eq 'PDL'){
57 my $pdl = shiftscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/shift().;
58 $pdl->mhilbscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mhilb().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.);
59 }
60 else{
61 PDL->mhilbscip-perl cpan PDL-LinearAlgebra 0.437 PDL/mhilb().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.);
62 }
63 }
64 sub PDL::mhilbscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mhilb`(). {
65 my $class = shiftscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/shift().;
66 my $pdl1 = scalarscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/scalar().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.)? $class->new_from_specificationscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/new_from_specification().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.) : $class->copyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/copy().;
67 my $pdl2 = scalarscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/scalar().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.)? $class->new_from_specificationscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/new_from_specification().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.) : $class->copyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/copy().;
68 1 / ($pdl1->inplacescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/inplace().->axisvalsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/axisvals(). + $pdl2->inplacescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/inplace().->axisvalsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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 mtriscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mtri().{
93 my $m = shiftscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/shift().;
94 $m->mtriscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mtri().(@_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.);
95 }
96
97 sub PDL::mtriscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mtri`(). {
98 &PDL::LinearAlgebra::_2d_arrayscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/_2d_array().;
99 my ($m, $upper) = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.;
100 $m->tricpyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/tricpy().($upper // 0, my $b = $m->_similar_nullscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/_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 mvanderscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mvander().($;$) {
115 my $exp = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`. == 2 ? sequencescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/sequence().(shiftscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/shift().) : sequencescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/sequence().($_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.[0]->dimscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dim().(-1));
116 $_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.[0]->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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::mpartscip-perl cpan PDL-LinearAlgebra 0.437 PDL/mpart().;
138
139 sub PDL::mpartscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mpart`(). {
140 &PDL::LinearAlgebra::_squarescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/_square().;
141 my ($m, $conj) = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.;
142 # antisymmetric and symmetric part
143 return (0.5* ($m - $m->tscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/t().($conj))),(0.5* ($m + $m->tscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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::mhankelscip-perl cpan PDL-LinearAlgebra 0.437 PDL/mhankel().;
176
177 sub PDL::mhankelscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mhankel`(). {
178 my ($m, $n) = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.;
179 $m = xvalsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/xvals().($m) + 1 unless refscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/ref().($m);
180 my @dims = $m->dimsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dims().;
181 $n = PDL::zeroesscip-perl cpan PDL-LinearAlgebra 0.437 PDL/zeroes().($m) unless definedscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/defined(). $n;
182 my $index = xvalsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/xvals().($dims[0]);
183 $index = $index->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dummy().(0) + $index;
184 if (@dims == 2){
185 $m = mstack($m,$n(,1:));
186 $n = $m->rescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/re().->indexscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/index().($index)->r2Cscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/r2C().;
187 $n((1),).= $m((1),)->indexscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/index().($index);
188 return $n;
189 }
190 else{
191 $m = augment($m,$n(1:));
192 return $m->indexscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/index().($index)->severscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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::mtoeplitzscip-perl cpan PDL-LinearAlgebra 0.437 PDL/mtoeplitz().;
212 sub PDL::mtoeplitzscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mtoeplitz`(). {
213 my ($m, $n) = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.;
214 $n = $m->copyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/copy(). unless definedscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/defined(). $n;
215 my $mdim= $m->dimscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dim().(-1);
216 my $ndim= $n->dimscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dim().(-1);
217 my $res = $m->_similarscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/_similar().($ndim,$mdim);
218 $ndim--;
219 my $min = $mdim <= $ndim ? $mdim : $ndim;
220 for(1..$min) {
221 $res->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("$_:,(@{[$_-1]})") .= $n->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("1:@{[$ndim-$_+1]}");
222 }
223 $mdim--;
224 $min = $mdim < $ndim ? $mdim : $ndim;
225 for(0..$min){
226 $res->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("($_),$_:") .= $m->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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::mpascalscip-perl cpan PDL-LinearAlgebra 0.437 PDL/mpascal().;
258 sub PDL::mpascalscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mpascal`(). {
259 my ($m, $n) = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.;
260 my $mat = eval {
261 requirescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/require(). PDL::GSL::SF;
262 if ($n > 1){
263 my $mat = xvalsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/xvals().($m);
264 return (PDL::GSL::SF::gsl_sf_choosescip-perl cpan PDL-LinearAlgebra 0.437 PDL/GSL/SF/gsl_sf_choose().($mat + $mat->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dummy().(0),$mat))[0];
265 }else{
266 my $mat = xvalsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/xvals().($m, $m);
267 return (PDL::GSL::SF::gsl_sf_choosescip-perl cpan PDL-LinearAlgebra 0.437 PDL/GSL/SF/gsl_sf_choose().($mat->tritosymscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/tritosym().,$mat->xchgscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/xchg().(0,1)->tritosymscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/tritosym().))[0]->mtriscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mtri().($n);
268 }
269 };
270 return $mat if !$@scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`$@`.;
271 warnscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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::mcompanionscip-perl cpan PDL-LinearAlgebra 0.437 PDL/mcompanion().;
291 sub PDL::mcompanionscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`PDL::mcompanion`().{
292 my ($m, $char) = @_scip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/`@_`.;
293 my( @dims, $dim, $ret);
294 $m = $m->{PDL} if (UNIVERSAL::isascip-perl cpan PDL-LinearAlgebra 0.437 UNIVERSAL/isa().($m, 'HASH') && existsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/exists(). $m->{PDL});
295 @dims = $m->dimsscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dims().;
296 $dim = $dims[-1] - 1;
297 my $id = identityscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/identity().($dim-1); $id = $id->r2Cscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/r2C(). if $m->_is_complexscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/_is_complex().;
298 if($char){
299 $ret = (-$m->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("1:$dim")->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dummy().(1)/$m->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("0"))->_call_methodscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/_call_method().('mstack', $id->mstackscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mstack().(zeroesscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/zeroes().($dim-1)->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dummy().(0)));
300 }
301 else{
302 $ret = $m->_similarscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/_similar().($dim-1)->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dummy().(1)->_call_methodscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/_call_method().('mstackscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/mstack().', $id)->mstack(-$m->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("$dim:1")->dummyscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/dummy().(0)/$m->slicescip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/slice().("(0)"));
303 }
304 $ret->severscip-perl cpan PDL-LinearAlgebra 0.437 PDL/LinearAlgebra/Special/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;