| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package PDL::Core; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Core routines for PDL module |
|
4
|
|
|
|
|
|
|
|
|
5
|
71
|
|
|
71
|
|
616
|
use strict; |
|
|
71
|
|
|
|
|
175
|
|
|
|
71
|
|
|
|
|
2738
|
|
|
6
|
71
|
|
|
71
|
|
342
|
use warnings; |
|
|
71
|
|
|
|
|
147
|
|
|
|
71
|
|
|
|
|
3370
|
|
|
7
|
71
|
|
|
71
|
|
33233
|
use PDL::Exporter; |
|
|
71
|
|
|
|
|
218
|
|
|
|
71
|
|
|
|
|
459
|
|
|
8
|
71
|
|
|
71
|
|
534
|
use DynaLoader; |
|
|
71
|
|
|
|
|
214
|
|
|
|
71
|
|
|
|
|
5850
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw( PDL::Exporter DynaLoader ); |
|
10
|
|
|
|
|
|
|
our $VERSION = '2.096'; # as of 2.097 it's back in lib/PDL.pm - EUMM XSMULTI extracts version from this as the XS_VERSION for the object, so no override it |
|
11
|
|
|
|
|
|
|
bootstrap PDL::Core $VERSION; |
|
12
|
71
|
|
|
71
|
|
37239
|
use PDL::Types ':All'; |
|
|
71
|
|
|
|
|
301
|
|
|
|
71
|
|
|
|
|
31060
|
|
|
13
|
71
|
|
|
71
|
|
555
|
use Config; |
|
|
71
|
|
|
|
|
138
|
|
|
|
71
|
|
|
|
|
3945
|
|
|
14
|
71
|
|
|
71
|
|
469
|
use List::Util qw(max); |
|
|
71
|
|
|
|
|
121
|
|
|
|
71
|
|
|
|
|
6869
|
|
|
15
|
71
|
|
|
71
|
|
494
|
use Scalar::Util 'blessed'; |
|
|
71
|
|
|
|
|
136
|
|
|
|
71
|
|
|
|
|
30665
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# If quad (q/Q) is available for pack(). |
|
18
|
|
|
|
|
|
|
our $CAN_PACK_QUAD = !! eval { my $packed = pack "Q", 0; 1 }; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# If "D" is available for pack(). |
|
21
|
|
|
|
|
|
|
our $CAN_PACK_D = !! eval { my $packed = pack "D", 0; 1 }; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our @EXPORT = qw( piddle pdl null barf ); # Only stuff always exported! |
|
24
|
|
|
|
|
|
|
my @convertfuncs = map $_->convertfunc, PDL::Types::types(); |
|
25
|
|
|
|
|
|
|
my @exports_internal = qw(howbig broadcastids topdl); |
|
26
|
|
|
|
|
|
|
my @exports_normal = (@EXPORT, |
|
27
|
|
|
|
|
|
|
@convertfuncs, |
|
28
|
|
|
|
|
|
|
qw(nelem dims shape null |
|
29
|
|
|
|
|
|
|
empty dup dupN inflateN |
|
30
|
|
|
|
|
|
|
badflag |
|
31
|
|
|
|
|
|
|
convert inplace zeroes zeros ones nan inf i list listindices unpdl |
|
32
|
|
|
|
|
|
|
set at broadcast_define over reshape dog cat barf type |
|
33
|
|
|
|
|
|
|
thread_define dummy mslice approx flat sclr squeeze |
|
34
|
|
|
|
|
|
|
get_autopthread_targ set_autopthread_targ get_autopthread_actual |
|
35
|
|
|
|
|
|
|
get_autopthread_dim get_autopthread_size set_autopthread_size) ); |
|
36
|
|
|
|
|
|
|
our @EXPORT_OK = (@exports_internal, @exports_normal); |
|
37
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
|
38
|
|
|
|
|
|
|
Func => [@exports_normal], |
|
39
|
|
|
|
|
|
|
Internal => [@exports_internal] ); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our ($level, @dims, $sep, $sep2, $match); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Important variables (place in PDL namespace) |
|
44
|
|
|
|
|
|
|
# (twice to eat "used only once" warning) |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$PDL::debug = # Debugging info |
|
47
|
|
|
|
|
|
|
$PDL::debug = 0; |
|
48
|
|
|
|
|
|
|
$PDL::verbose = # Functions provide chatty information |
|
49
|
|
|
|
|
|
|
$PDL::verbose = 0; |
|
50
|
|
|
|
|
|
|
$PDL::use_commas = 0; # Whether to insert commas when printing arrays |
|
51
|
|
|
|
|
|
|
$PDL::floatformat = "%7g"; # Default print format for long numbers |
|
52
|
|
|
|
|
|
|
$PDL::doubleformat = "%10.8g"; |
|
53
|
|
|
|
|
|
|
$PDL::indxformat = "%12d"; # Default print format for PDL_Indx values |
|
54
|
|
|
|
|
|
|
$PDL::undefval = 0; # Value to use instead of undef when creating PDLs |
|
55
|
|
|
|
|
|
|
$PDL::toolongtoprint = 10000; # maximum pdl size to stringify for printing |
|
56
|
|
|
|
|
|
|
$PDL::infoformat = "%C: %T %D"; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
################ Exportable functions of the Core ###################### |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
*at_c = *at_bad_c; # back-compat alias |
|
61
|
|
|
|
|
|
|
*thread_define = *broadcast_define; |
|
62
|
|
|
|
|
|
|
|
|
63
|
71
|
|
|
71
|
|
46606
|
our @pdl_ones :shared; # optimisation to provide a "one" of the right type to avoid converttype |
|
|
71
|
|
|
|
|
111362
|
|
|
|
71
|
|
|
|
|
468
|
|
|
64
|
|
|
|
|
|
|
for my $t (PDL::Types::types()) { |
|
65
|
|
|
|
|
|
|
my $conv = $t->convertfunc; |
|
66
|
71
|
|
|
71
|
|
7482
|
no strict 'refs'; |
|
|
71
|
|
|
|
|
146
|
|
|
|
71
|
|
|
|
|
13359
|
|
|
67
|
|
|
|
|
|
|
*$conv = *{"PDL::$conv"} = sub { |
|
68
|
12093
|
100
|
|
12093
|
|
2452697
|
return $t unless @_; |
|
69
|
1080
|
100
|
|
|
|
5156
|
alltopdl('PDL', (@_>1 ? [@_] : shift), $t); |
|
70
|
|
|
|
|
|
|
}; |
|
71
|
|
|
|
|
|
|
$pdl_ones[$t->enum] = $Config{useithreads} ? 1 : pdl($t, 1); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
BEGIN { |
|
75
|
71
|
|
|
71
|
|
294
|
for (qw( |
|
76
|
|
|
|
|
|
|
inflateN badflag dup dupN howbig unpdl nelem inplace dims |
|
77
|
|
|
|
|
|
|
list broadcastids listindices null set at sclr shape |
|
78
|
|
|
|
|
|
|
broadcast_define convert over dog cat mslice |
|
79
|
|
|
|
|
|
|
type approx dummy isempty string |
|
80
|
|
|
|
|
|
|
)) { |
|
81
|
71
|
|
|
71
|
|
493
|
no strict 'refs'; *{$_} = \&{"PDL::$_"}; |
|
|
71
|
|
|
|
|
110
|
|
|
|
71
|
|
|
|
|
4765
|
|
|
|
1988
|
|
|
|
|
2448
|
|
|
|
1988
|
|
|
|
|
13767
|
|
|
|
1988
|
|
|
|
|
7337
|
|
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 NAME |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
PDL::Core - fundamental PDL functionality and vectorization/broadcasting |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Methods and functions for type conversions, PDL creation, |
|
92
|
|
|
|
|
|
|
type conversion, broadcasting etc. |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
use PDL::Core; # Normal routines |
|
97
|
|
|
|
|
|
|
use PDL::Core ':Internal'; # Hairy routines |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 VECTORIZATION/BROADCASTING: METHOD AND NOMENCLATURE |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
PDL provides vectorized operations via a built-in engine. |
|
102
|
|
|
|
|
|
|
Vectorization in PDL is called "broadcasting" (formerly, up to 2.074, "threading"). |
|
103
|
|
|
|
|
|
|
The broadcasting engine implements simple rules for each operation. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Each PDL object has a "shape" that is a generalized N-dimensional |
|
106
|
|
|
|
|
|
|
rectangle defined by a "dim list" of sizes in an arbitrary |
|
107
|
|
|
|
|
|
|
set of dimensions. A PDL with shape 2x3 has 6 elements and is |
|
108
|
|
|
|
|
|
|
said to be two-dimensional, or may be referred to as a 2x3-PDL. |
|
109
|
|
|
|
|
|
|
The dimensions are indexed numerically starting at 0, so a |
|
110
|
|
|
|
|
|
|
2x3-PDL has a dimension 0 (or "dim 0") with size 2 and a 1 dimension |
|
111
|
|
|
|
|
|
|
(or "dim 1") with size 3. |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
PDL generalizes *all* mathematical operations with the notion of |
|
114
|
|
|
|
|
|
|
"active dims": each operator has zero or more active dims that are |
|
115
|
|
|
|
|
|
|
used in carrying out the operation. Simple scalar operations like |
|
116
|
|
|
|
|
|
|
scalar multiplication ('*') have 0 active dims. More complicated |
|
117
|
|
|
|
|
|
|
operators can have more active dims. For example, matrix |
|
118
|
|
|
|
|
|
|
multiplication ('x') has 2 active dims. Additional dims are |
|
119
|
|
|
|
|
|
|
automatically vectorized across -- e.g. multiplying a 2x5-PDL with a |
|
120
|
|
|
|
|
|
|
2x5-PDL requires 10 simple multiplication operations, and yields a |
|
121
|
|
|
|
|
|
|
2x5-PDL result. |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 Broadcasting rules |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
In any PDL expression, the active dims appropriate for each operator |
|
126
|
|
|
|
|
|
|
are used starting at the 0 dim and working forward through the dim |
|
127
|
|
|
|
|
|
|
list of each object. All additional dims after the active dims are |
|
128
|
|
|
|
|
|
|
"broadcast dims". The broadcast dims do not have to agree exactly: they are |
|
129
|
|
|
|
|
|
|
coerced to agree according to simple rules: |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=over 3 |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item * Null PDLs match any dim list (see below). |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * Dims with sizes other than 1 must all agree in size. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item * Dims of size 1 are silently repeated as necessary except for C<[phys]> PDLs. |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item * Missing dims are expanded appropriately. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=back |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
A size-1 dim for C<[phys]> PDLs causes an exception if the dim is used |
|
144
|
|
|
|
|
|
|
in another parameter and has a size greater than 1. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
The "size 1" rule implements "generalized scalar" operation, by |
|
147
|
|
|
|
|
|
|
analogy to scalar multiplication. The "missing dims" rule |
|
148
|
|
|
|
|
|
|
acknowledges the ambiguity between a missing dim and a dim of size 1. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 Null PDLs |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
PDLs on the left-hand side of assignment can have the special value |
|
153
|
|
|
|
|
|
|
"Null". A null PDL has no dim list and no set size; its shape is |
|
154
|
|
|
|
|
|
|
determined by the computed shape of the expression being assigned to |
|
155
|
|
|
|
|
|
|
it. Null PDLs contain no values and can only be assigned to. When |
|
156
|
|
|
|
|
|
|
assigned to (e.g. via the C<.=> operator), they cease to be null PDLs. |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
To create a null PDL, use Cnull()>. |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 Empty PDLs |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
PDLs can represent the empty set using "structured Empty" variables. |
|
163
|
|
|
|
|
|
|
An empty PDL is not a null PDL. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Any dim of a PDL can be set explicitly to size 0. If so, the PDL |
|
166
|
|
|
|
|
|
|
contains zero values (because the total number of values is the |
|
167
|
|
|
|
|
|
|
product of all the sizes in the PDL's shape or dimlist). |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Scalar PDLs are zero-dimensional and have no entries in the dim list, |
|
170
|
|
|
|
|
|
|
so they cannot be empty. 1-D and higher PDLs can be empty. Empty |
|
171
|
|
|
|
|
|
|
PDLs are useful for set operations, and are most commonly encountered |
|
172
|
|
|
|
|
|
|
in the output from selection operators such as L |
|
173
|
|
|
|
|
|
|
and L. Not all empty PDLs have the same |
|
174
|
|
|
|
|
|
|
broadcasting properties -- e.g. a 2x0-PDL represents a collection of |
|
175
|
|
|
|
|
|
|
2-vectors that happens to contain no elements, while a simple 0-PDL |
|
176
|
|
|
|
|
|
|
represents a collection of scalar values (that also happens to contain |
|
177
|
|
|
|
|
|
|
no elements). |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Note that 0 dims are not adjustable via the broadcasting rules -- a dim |
|
180
|
|
|
|
|
|
|
with size 0 can only match a corresponding dim of size 0 or 1. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 Broadcast rules and assignments |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Versions of PDL through 2.4.10 have some irregularity with broadcasting and |
|
185
|
|
|
|
|
|
|
assignments. Currently the broadcasting engine performs a full expansion of |
|
186
|
|
|
|
|
|
|
both sides of the computed assignment operator C<.=> (which assigns values |
|
187
|
|
|
|
|
|
|
to a pre-existing PDL). This leads to counter-intuitive behavior in |
|
188
|
|
|
|
|
|
|
some cases: |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=over 3 |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item * Empty PDLs and generalized scalars |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Generalized scalars (PDLs with a dim of size 1) can match any size in the |
|
195
|
|
|
|
|
|
|
corresponding dim, including 0. Thus, |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
$x = ones(2,0); |
|
198
|
|
|
|
|
|
|
$y = sequence(2,1); |
|
199
|
|
|
|
|
|
|
$c = $x * $y; |
|
200
|
|
|
|
|
|
|
print $c; |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
prints C. |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
This behavior is counterintuitive but desirable, and will be preserved |
|
205
|
|
|
|
|
|
|
in future versions of PDL. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=back |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 VARIABLES |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
These are important variables of B scope and are placed |
|
212
|
|
|
|
|
|
|
in the PDL namespace. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head3 C<$PDL::debug> |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=over 4 |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
When true, PDL debugging information is printed. |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=back |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head3 C<$PDL::verbose> |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=over 4 |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
When true, PDL functions provide chatty information. |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=back |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head3 C<$PDL::use_commas> |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=over 4 |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Whether to insert commas when printing pdls |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=back |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head3 C<$PDL::floatformat>, C<$PDL::doubleformat>, C<$PDL::indxformat> |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=over 4 |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
The default print format for floats, doubles, and indx values, |
|
243
|
|
|
|
|
|
|
respectively. The default default values are: |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
$PDL::floatformat = "%7g"; |
|
246
|
|
|
|
|
|
|
$PDL::doubleformat = "%10.8g"; |
|
247
|
|
|
|
|
|
|
$PDL::indxformat = "%12d"; |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=back |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head3 C<$PDL::infoformat> |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=over 4 |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
The default format for C. The default value is: |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
$PDL::infoformat = "%C: %T %D"; |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=back |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head3 C<$PDL::undefval> |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=over 4 |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
The value to use instead of C when creating pdls. If is |
|
266
|
|
|
|
|
|
|
C, 0 will be used. |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=back |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head3 C<$PDL::toolongtoprint> |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=over 4 |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
The maximal size pdls to print (defaults to 10000 elements) |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=back |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 barf |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=for ref |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Standard error reporting routine for PDL. |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
C is the routine PDL modules should call to report errors. This |
|
288
|
|
|
|
|
|
|
is because C will report the error as coming from the correct |
|
289
|
|
|
|
|
|
|
line in the module user's script rather than in the PDL module. |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
For now, barf just calls Carp::confess() |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Remember C is your friend. *Use* it! |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=for example |
|
296
|
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
At the perl level: |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
barf("User has too low an IQ!"); |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
In C or XS code: |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
barf("You have made %d errors", count); |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Note: this is one of the few functions ALWAYS exported |
|
306
|
|
|
|
|
|
|
by PDL::Core |
|
307
|
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=cut |
|
309
|
|
|
|
|
|
|
|
|
310
|
71
|
|
|
71
|
|
505
|
use Carp; |
|
|
71
|
|
|
|
|
175
|
|
|
|
71
|
|
|
|
|
47917
|
|
|
311
|
165
|
|
|
165
|
1
|
50161
|
sub barf { goto &Carp::confess } |
|
312
|
12
|
|
|
12
|
0
|
313
|
sub cluck { goto &Carp::cluck } |
|
313
|
|
|
|
|
|
|
*PDL::barf = \&barf; |
|
314
|
|
|
|
|
|
|
*PDL::cluck = \&cluck; |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
########## Set Auto-PThread Based On Environment Vars ############ |
|
317
|
|
|
|
|
|
|
$ENV{PDL_AUTOPTHREAD_TARG} //= online_cpus(); |
|
318
|
|
|
|
|
|
|
PDL::set_autopthread_targ( $ENV{PDL_AUTOPTHREAD_TARG} ) if $ENV{PDL_AUTOPTHREAD_TARG} > 1; |
|
319
|
|
|
|
|
|
|
PDL::set_autopthread_size( $ENV{PDL_AUTOPTHREAD_SIZE} ) if( defined ( $ENV{PDL_AUTOPTHREAD_SIZE} ) ); |
|
320
|
|
|
|
|
|
|
################################################################## |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head2 pdl |
|
323
|
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=for ref |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
PDL constructor - creates new ndarray from perl scalars/arrays, ndarrays, and strings |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=for usage |
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
$double_pdl = pdl(SCALAR|ARRAY REFERENCE|ARRAY|STRING); # default type |
|
331
|
|
|
|
|
|
|
$type_pdl = pdl(PDL::Type,SCALAR|ARRAY REFERENCE|ARRAY|STRING); |
|
332
|
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=for example |
|
334
|
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
$x = pdl [1..10]; # 1D array of doubles |
|
336
|
|
|
|
|
|
|
$x = pdl ([1..10]); # 1D array |
|
337
|
|
|
|
|
|
|
$x = pdl (1,2,3,4); # Ditto |
|
338
|
|
|
|
|
|
|
$y = pdl [[1,2,3],[4,5,6]]; # 2D 3x2 array |
|
339
|
|
|
|
|
|
|
$y = pdl "[[1,2,3],[4,5,6]]"; # Ditto (slower) |
|
340
|
|
|
|
|
|
|
$y = pdl "[1 2 3; 4 5 6]"; # Ditto |
|
341
|
|
|
|
|
|
|
$y = pdl q[1 2 3; 4 5 6]; # Ditto, using the q quote operator |
|
342
|
|
|
|
|
|
|
$y = pdl "1 2 3; 4 5 6"; # Ditto, less obvious, but still works |
|
343
|
|
|
|
|
|
|
$y = pdl 42 # 0-dimensional scalar |
|
344
|
|
|
|
|
|
|
$c = pdl $x; # Make a new copy |
|
345
|
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
$u = pdl ushort(), 42 # 0-dimensional ushort scalar |
|
347
|
|
|
|
|
|
|
$y = pdl(byte(),[[1,2,3],[4,5,6]]); # 2D byte ndarray |
|
348
|
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
$n = pdl indx(), [1..5]; # 1D array of indx values |
|
350
|
|
|
|
|
|
|
$n = pdl indx, [1..5]; # ... can leave off parens |
|
351
|
|
|
|
|
|
|
$n = indx( [1..5] ); # ... still the same! |
|
352
|
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
$n = pdl cdouble, 2, 3; # native complex numbers, zero imaginary |
|
354
|
|
|
|
|
|
|
use Math::Complex qw(cplx); |
|
355
|
|
|
|
|
|
|
$n = pdl cdouble, 2, cplx(2, 1)); # explicit type |
|
356
|
|
|
|
|
|
|
$n = pdl 2, cplx(2, 1); # default cdouble if Math::Complex obj |
|
357
|
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
$x = pdl([[1,2,3],[4,5,6]]); # 2D |
|
359
|
|
|
|
|
|
|
$x = pdl([1,2,3],[4,5,6]); # 2D |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
Note the last two are equivalent - a list is automatically |
|
362
|
|
|
|
|
|
|
converted to a list reference for syntactic convenience. i.e. you |
|
363
|
|
|
|
|
|
|
can omit the outer C<[]> |
|
364
|
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
You can mix and match arrays, array refs, and PDLs in your argument |
|
366
|
|
|
|
|
|
|
list, and C will sort them out. You get back a PDL whose last |
|
367
|
|
|
|
|
|
|
(slowest running) dim runs across the top level of the list you hand |
|
368
|
|
|
|
|
|
|
in, and whose first (fastest running) dim runs across the deepest |
|
369
|
|
|
|
|
|
|
level that you supply. |
|
370
|
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
At the moment, you cannot mix and match those arguments with string |
|
372
|
|
|
|
|
|
|
arguments, though we can't imagine a situation in which you would |
|
373
|
|
|
|
|
|
|
really want to do that. |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
The string version of pdl also allows you to use the strings C, C, |
|
376
|
|
|
|
|
|
|
and C, and it will insert the values that you mean (and set the bad flag |
|
377
|
|
|
|
|
|
|
if you use C). You can mix and match case, though you shouldn't. Here are |
|
378
|
|
|
|
|
|
|
some examples: |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
$bad = pdl q[1 2 3 bad 5 6]; # Set fourth element to the bad value |
|
381
|
|
|
|
|
|
|
$bad = pdl q[1 2 3 BAD 5 6]; # ditto |
|
382
|
|
|
|
|
|
|
$bad = pdl q[1 2 inf bad 5]; # now third element is IEEE infinite value |
|
383
|
|
|
|
|
|
|
$bad = pdl q[nan 2 inf -inf]; # first value is IEEE nan value |
|
384
|
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
The default constructor uses IEEE double-precision floating point numbers. You |
|
386
|
|
|
|
|
|
|
can use other types, but you will get a warning if you try to use C with |
|
387
|
|
|
|
|
|
|
integer types (it will be replaced with the C value) and you will get a |
|
388
|
|
|
|
|
|
|
fatal error if you try to use C. |
|
389
|
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
Throwing a PDL into the mix has the same effect as throwing in a list ref: |
|
391
|
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
pdl(pdl(1,2),[3,4]) |
|
393
|
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
is the same as |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
pdl([1,2],[3,4]). |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
All of the dimensions in the list are "padded-out" with undefval to |
|
399
|
|
|
|
|
|
|
meet the widest dim in the list, so (e.g.) |
|
400
|
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
$x = pdl([[1,2,3],[2]]) |
|
402
|
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
gives you the same answer as |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
$x = pdl([[1,2,3],[2,undef,undef]]); |
|
406
|
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
If your PDL module has bad values compiled into it (see L), |
|
408
|
|
|
|
|
|
|
you can pass BAD values into the constructor within pre-existing PDLs. |
|
409
|
|
|
|
|
|
|
The BAD values are automatically kept BAD and propagated correctly. |
|
410
|
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
C is a functional synonym for the 'new' constructor, |
|
412
|
|
|
|
|
|
|
e.g.: |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
$x = PDL->new([1..10]); |
|
415
|
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
In order to control how undefs are handled in converting from perl lists to |
|
417
|
|
|
|
|
|
|
PDLs, one can set the variable C<$PDL::undefval>. |
|
418
|
|
|
|
|
|
|
For example: |
|
419
|
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
$foo = [[1,2,undef],[undef,3,4]]; |
|
421
|
|
|
|
|
|
|
$PDL::undefval = -999; |
|
422
|
|
|
|
|
|
|
$f = pdl $foo; |
|
423
|
|
|
|
|
|
|
print $f |
|
424
|
|
|
|
|
|
|
[ |
|
425
|
|
|
|
|
|
|
[ 1 2 -999] |
|
426
|
|
|
|
|
|
|
[-999 3 4] |
|
427
|
|
|
|
|
|
|
] |
|
428
|
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
C<$PDL::undefval> defaults to zero. |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
As a final note, if you include an Empty PDL in the list of objects to |
|
432
|
|
|
|
|
|
|
construct into a PDL, it is kept as a placeholder pane -- so if you feed |
|
433
|
|
|
|
|
|
|
in (say) 7 objects, you get a size of 7 in the 0th dim of the output PDL. |
|
434
|
|
|
|
|
|
|
The placeholder panes are completely padded out. But if you feed in only |
|
435
|
|
|
|
|
|
|
a single Empty PDL, you get back the Empty PDL (no padding). |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
=cut |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
=head2 empty |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
=for ref |
|
442
|
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
Returns an empty ndarray, with a single zero-length dimension. |
|
444
|
|
|
|
|
|
|
Only available as a function, not a method. |
|
445
|
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
=for usage |
|
447
|
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
$x = empty; # defaults to lowest type so it can always be promoted up |
|
449
|
|
|
|
|
|
|
$x = empty(float); |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=cut |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
sub empty { |
|
454
|
292
|
|
|
292
|
1
|
3766
|
my ($type) = @_; |
|
455
|
292
|
|
100
|
|
|
1135
|
$type //= 0; |
|
456
|
292
|
|
|
|
|
1201
|
PDL->new_from_specification(PDL::Type->new($type), 0); |
|
457
|
|
|
|
|
|
|
} |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=head2 null |
|
460
|
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
=for ref |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
Returns a 'null' ndarray. |
|
464
|
|
|
|
|
|
|
It is an error to pass one of these as an input to a function. |
|
465
|
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
=for usage |
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
$x = null; |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
C has a special meaning to L. It is used to |
|
471
|
|
|
|
|
|
|
flag a special kind of empty ndarray, which can grow to |
|
472
|
|
|
|
|
|
|
appropriate dimensions to store a result (as opposed to |
|
473
|
|
|
|
|
|
|
storing a result in an existing ndarray). |
|
474
|
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
=for example |
|
476
|
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
pdl> sumover sequence(10,10), $ans=null;p $ans |
|
478
|
|
|
|
|
|
|
[45 145 245 345 445 545 645 745 845 945] |
|
479
|
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
=cut |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
sub PDL::null{ |
|
483
|
2350
|
100
|
|
2350
|
0
|
23863
|
my $class = scalar(@_) ? shift : undef; # if this sub called with no |
|
484
|
|
|
|
|
|
|
# class ( i.e. like 'null()', instead |
|
485
|
|
|
|
|
|
|
# of '$obj->null' or 'CLASS->null', setup |
|
486
|
|
|
|
|
|
|
|
|
487
|
2350
|
100
|
|
|
|
4006
|
if( defined($class) ){ |
|
488
|
2179
|
|
66
|
|
|
5647
|
$class = ref($class) || $class; # get the class name |
|
489
|
|
|
|
|
|
|
} |
|
490
|
|
|
|
|
|
|
else{ |
|
491
|
171
|
|
|
|
|
357
|
$class = 'PDL'; # set class to the current package name if null called |
|
492
|
|
|
|
|
|
|
# with no arguments |
|
493
|
|
|
|
|
|
|
} |
|
494
|
|
|
|
|
|
|
|
|
495
|
2350
|
|
|
|
|
44731
|
return $class->initialize(); |
|
496
|
|
|
|
|
|
|
} |
|
497
|
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
=head2 nullcreate |
|
499
|
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=for ref |
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
Returns a 'null' ndarray. |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
=for usage |
|
505
|
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
$x = PDL->nullcreate($arg) |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
This is an routine used by many of the broadcasting primitives |
|
509
|
|
|
|
|
|
|
(i.e. L, |
|
510
|
|
|
|
|
|
|
L, etc.) to generate a null ndarray for the |
|
511
|
|
|
|
|
|
|
function's output that will behave properly for derived (or |
|
512
|
|
|
|
|
|
|
subclassed) PDL objects. |
|
513
|
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
For the above usage: |
|
515
|
|
|
|
|
|
|
If C<$arg> is a PDL, or a derived PDL, then C<$arg-Enull> is returned. |
|
516
|
|
|
|
|
|
|
If C<$arg> is a scalar (i.e. a zero-dimensional PDL) then Cnull> |
|
517
|
|
|
|
|
|
|
is returned. |
|
518
|
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
=for example |
|
520
|
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
PDL::Derived->nullcreate(10) |
|
522
|
|
|
|
|
|
|
returns PDL::Derived->null. |
|
523
|
|
|
|
|
|
|
PDL->nullcreate($pdlderived) |
|
524
|
|
|
|
|
|
|
returns $pdlderived->null. |
|
525
|
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
=cut |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
sub PDL::nullcreate{ |
|
529
|
1305
|
|
|
1305
|
0
|
2334
|
my ($type,$arg) = @_; |
|
530
|
1305
|
100
|
|
|
|
3641
|
return ref($arg) ? $arg->null : $type->null ; |
|
531
|
|
|
|
|
|
|
} |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
=head2 nelem |
|
534
|
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=for ref |
|
536
|
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
Return the number of elements in an ndarray |
|
538
|
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=for usage |
|
540
|
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
$n = nelem($ndarray); $n = $ndarray->nelem; |
|
542
|
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
=for example |
|
544
|
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
$mean = sum($data)/nelem($data); |
|
546
|
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
=head2 dims |
|
548
|
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
=for ref |
|
550
|
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
Return ndarray dimensions as a perl list |
|
552
|
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
=for usage |
|
554
|
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
@dims = $ndarray->dims; @dims = dims($ndarray); |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
=for example |
|
558
|
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
pdl> p @tmp = dims zeroes 10,3,22 |
|
560
|
|
|
|
|
|
|
10 3 22 |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
See also L which returns an ndarray instead. |
|
563
|
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
=head2 dimincs |
|
565
|
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
=for ref |
|
567
|
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
Return ndarray C as a perl list, reflecting any |
|
569
|
|
|
|
|
|
|
vaffine transformations of which this is a child. |
|
570
|
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
=for usage |
|
572
|
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
@dimincs = $ndarray->dimincs |
|
574
|
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
=for example |
|
576
|
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
pdl> p zeroes(10,3,22)->dimincs |
|
578
|
|
|
|
|
|
|
1 10 30 |
|
579
|
|
|
|
|
|
|
pdl> p zeroes(10,3,22)->t->dimincs |
|
580
|
|
|
|
|
|
|
10 1 30 |
|
581
|
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=head2 shape |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
=for ref |
|
585
|
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
Return ndarray dimensions as an ndarray |
|
587
|
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
=for usage |
|
589
|
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
$shape = $ndarray->shape; $shape = shape($ndarray); |
|
591
|
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
=for example |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
pdl> p $shape = shape zeroes 10,3,22 |
|
595
|
|
|
|
|
|
|
[10 3 22] |
|
596
|
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
See also L which returns a perl list. |
|
598
|
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=head2 ndims |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
=for ref |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
Returns the number of dimensions in an ndarray. Alias |
|
604
|
|
|
|
|
|
|
for L. |
|
605
|
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
=head2 getndims |
|
607
|
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
=for ref |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
Returns the number of dimensions in an ndarray |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
=for usage |
|
613
|
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
$ndims = $ndarray->getndims; |
|
615
|
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
=for example |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
pdl> p zeroes(10,3,22)->getndims |
|
619
|
|
|
|
|
|
|
3 |
|
620
|
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
=head2 dim |
|
622
|
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
=for ref |
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
Returns the size of the given dimension of an ndarray. Alias |
|
626
|
|
|
|
|
|
|
for L. |
|
627
|
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
=head2 getdim |
|
629
|
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
=for ref |
|
631
|
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
Returns the size of the given dimension. |
|
633
|
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
=for usage |
|
635
|
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
$dim0 = $ndarray->getdim(0); |
|
637
|
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
=for example |
|
639
|
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
pdl> p zeroes(10,3,22)->getdim(1) |
|
641
|
|
|
|
|
|
|
3 |
|
642
|
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
Negative indices count from the end of the dims array. |
|
644
|
|
|
|
|
|
|
Indices beyond the end will return a size of 1. This |
|
645
|
|
|
|
|
|
|
reflects the idea that any pdl is equivalent to an |
|
646
|
|
|
|
|
|
|
infinitely dimensional array in which only a finite number of |
|
647
|
|
|
|
|
|
|
dimensions have a size different from one. For example, in that sense a |
|
648
|
|
|
|
|
|
|
3D ndarray of shape [3,5,2] is equivalent to a [3,5,2,1,1,1,1,1,....] |
|
649
|
|
|
|
|
|
|
ndarray. Accordingly, |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
print $x->getdim(10000); |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
will print 1 for most practically encountered ndarrays. |
|
654
|
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
=head2 topdl |
|
656
|
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
=for ref |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
alternate ndarray constructor - ensures arg is an ndarray |
|
660
|
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=for usage |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
$x = topdl(SCALAR|ARRAY REFERENCE|ARRAY); |
|
664
|
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
The difference between L and C is that the |
|
666
|
|
|
|
|
|
|
latter will just 'fall through' if the argument is |
|
667
|
|
|
|
|
|
|
already an ndarray. It will return a reference and I |
|
668
|
|
|
|
|
|
|
a new copy. |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
This is particularly useful if you are writing a function |
|
671
|
|
|
|
|
|
|
which is doing some fiddling with internals and assumes |
|
672
|
|
|
|
|
|
|
an ndarray argument (e.g. for method calls). Using C |
|
673
|
|
|
|
|
|
|
will ensure nothing breaks if passed with '2'. |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
Note that C is not exported by default (see example |
|
676
|
|
|
|
|
|
|
below for usage). |
|
677
|
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
=for example |
|
679
|
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
use PDL::Core ':Internal'; # use the internal routines of |
|
681
|
|
|
|
|
|
|
# the Core module |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
$x = topdl 43; # $x is ndarray with value '43' |
|
684
|
|
|
|
|
|
|
$y = topdl $ndarray; # fall through |
|
685
|
|
|
|
|
|
|
$x = topdl (1,2,3,4); # Convert 1D array |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
=head2 tocomplex |
|
688
|
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
=for ref |
|
690
|
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
Return a complex-typed ndarray, either as a no-op or a conversion. |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
$cplx = $pdl->tocomplex; |
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
Not exported. Added in 2.099. |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
=cut |
|
698
|
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
sub PDL::tocomplex { |
|
700
|
4
|
|
|
4
|
0
|
16
|
my $pdl = PDL->topdl(@_); |
|
701
|
4
|
100
|
|
|
|
14
|
$pdl->type->real ? $pdl->r2C : $pdl; |
|
702
|
|
|
|
|
|
|
} |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
=head2 setdims |
|
705
|
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
=for ref |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
Sets the ndarray's dimension list. A very low-level routine which |
|
709
|
|
|
|
|
|
|
does not do any checks, so use with caution. |
|
710
|
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=for usage |
|
712
|
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
$ndarray->setdims(\@dimlist); |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
=head2 set_datatype |
|
716
|
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
=for ref |
|
718
|
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
Sets the ndarray's data type to the given value (the integer identifier |
|
720
|
|
|
|
|
|
|
for the type, see L). See L. Internal |
|
721
|
|
|
|
|
|
|
function. Errors if ndarray has child transforms. Severs if has a parent. |
|
722
|
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
=head2 get_datatype |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
=for ref |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
Internal: Return the numeric value identifying the ndarray datatype |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
=for usage |
|
730
|
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
$x = $ndarray->get_datatype; |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
Mainly used for internal routines. |
|
734
|
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
NOTE: get_datatype returns 'just a number' not any special |
|
736
|
|
|
|
|
|
|
type object, unlike L. |
|
737
|
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
=head2 howbig |
|
739
|
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
=for ref |
|
741
|
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
Returns the sizeof an ndarray datatype in bytes. |
|
743
|
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
Note that C is not exported by default (see example |
|
745
|
|
|
|
|
|
|
below for usage). |
|
746
|
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
=for usage |
|
748
|
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
use PDL::Core ':Internal'; # use the internal routines of |
|
750
|
|
|
|
|
|
|
# the Core module |
|
751
|
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
$size = howbig($ndarray->get_datatype); |
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
Mainly used for internal routines. |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
NOTE: NOT a method! This is because get_datatype returns |
|
757
|
|
|
|
|
|
|
'just a number' not any special object. |
|
758
|
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
=for example |
|
760
|
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
pdl> p howbig(ushort([1..10])->get_datatype) |
|
762
|
|
|
|
|
|
|
2 |
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
=head2 update_data_from |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
=for ref |
|
767
|
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
Update an ndarray's internal data for from packed data. |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
$data = read_from_datasource(); |
|
771
|
|
|
|
|
|
|
$pdl->update_data_from($data); |
|
772
|
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
Throws an exception if the argument is the wrong size for that ndarray. |
|
774
|
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
=head2 get_dataref |
|
776
|
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=for ref |
|
778
|
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
Return the internal data for an ndarray, as a perl SCALAR ref. |
|
780
|
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
If you are using this in order to manipulate the SV, then call |
|
782
|
|
|
|
|
|
|
L, then as of 2.099 you are recommended instead to just |
|
783
|
|
|
|
|
|
|
use L to streamline the process. |
|
784
|
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
Most ndarrays hold their internal data in a packed perl string, to take |
|
786
|
|
|
|
|
|
|
advantage of perl's memory management. This gives you direct access |
|
787
|
|
|
|
|
|
|
to the string, which is handy when you need to manipulate the binary |
|
788
|
|
|
|
|
|
|
data directly (e.g. for file I/O). If you modify the string, you'll |
|
789
|
|
|
|
|
|
|
need to call L afterward, to make sure that the |
|
790
|
|
|
|
|
|
|
ndarray points to the new location of the underlying perl variable. |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
Calling C automatically physicalizes your ndarray (see |
|
793
|
|
|
|
|
|
|
L). You definitely |
|
794
|
|
|
|
|
|
|
don't want to do anything to the SV to truncate or deallocate the |
|
795
|
|
|
|
|
|
|
string, unless you correspondingly call L to make the |
|
796
|
|
|
|
|
|
|
PDL match its new data dimension. |
|
797
|
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
You definitely don't want to use get_dataref unless you know what you |
|
799
|
|
|
|
|
|
|
are doing (or are trying to find out): you can end up scrozzling |
|
800
|
|
|
|
|
|
|
memory if you shrink or eliminate the string representation of the |
|
801
|
|
|
|
|
|
|
variable. Here be dragons. |
|
802
|
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
=head2 upd_data |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
=for ref |
|
806
|
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
Update the data pointer in an ndarray to match its perl SV. |
|
808
|
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
This is useful if you've been monkeying with the packed string |
|
810
|
|
|
|
|
|
|
representation of the PDL, which you probably shouldn't be doing |
|
811
|
|
|
|
|
|
|
anyway. (see L.) |
|
812
|
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
As of 2.099 you are recommended instead to just use |
|
814
|
|
|
|
|
|
|
L to streamline the process. |
|
815
|
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
=cut |
|
817
|
|
|
|
|
|
|
|
|
818
|
817
|
|
|
817
|
1
|
3312
|
sub topdl {PDL->topdl(@_)} |
|
819
|
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
####################### Subclass as hashref ####################### |
|
821
|
|
|
|
|
|
|
{ package # hide from PAUSE |
|
822
|
|
|
|
|
|
|
PDL::Hash; |
|
823
|
|
|
|
|
|
|
our @ISA = qw/PDL/; |
|
824
|
|
|
|
|
|
|
sub initialize { |
|
825
|
50
|
|
|
50
|
|
5262
|
my ($class) = @_; |
|
826
|
50
|
|
66
|
|
|
102
|
bless { PDL => PDL->null }, ref $class || $class; |
|
827
|
|
|
|
|
|
|
} |
|
828
|
|
|
|
|
|
|
} |
|
829
|
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
####################### Overloaded operators ####################### |
|
831
|
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
{ package # hide from MetaCPAN |
|
833
|
|
|
|
|
|
|
PDL; |
|
834
|
71
|
|
|
71
|
|
592
|
use Carp; |
|
|
71
|
|
|
|
|
222
|
|
|
|
71
|
|
|
|
|
17542
|
|
|
835
|
|
|
|
|
|
|
use overload |
|
836
|
|
|
|
|
|
|
'""' => \&PDL::Core::string, |
|
837
|
239
|
|
|
239
|
|
105295
|
"=" => sub {$_[0]}, # Don't deep copy, just copy reference |
|
838
|
|
|
|
|
|
|
bool => sub { |
|
839
|
7903
|
100
|
|
7903
|
|
52571
|
return 0 if $_[0]->isnull; |
|
840
|
7857
|
100
|
|
|
|
24236
|
confess("multielement ndarray in conditional expression (see PDL::FAQ questions 6-10 and 6-11)") |
|
841
|
|
|
|
|
|
|
unless $_[0]->nelem == 1; |
|
842
|
7853
|
100
|
66
|
|
|
24910
|
confess("bad value ndarray in conditional expression") |
|
843
|
|
|
|
|
|
|
if $_[0]->badflag and $_[0].'' eq 'BAD'; |
|
844
|
7852
|
|
|
|
|
20321
|
$_[0]->flat->at(0); |
|
845
|
|
|
|
|
|
|
}, |
|
846
|
71
|
|
|
71
|
|
574
|
; |
|
|
71
|
|
|
|
|
176
|
|
|
|
71
|
|
|
|
|
955
|
|
|
847
|
|
|
|
|
|
|
} |
|
848
|
|
|
|
|
|
|
|
|
849
|
|
|
|
|
|
|
##################### Data type/conversion stuff ######################## |
|
850
|
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
sub PDL::shape { # Return dimensions as a pdl |
|
852
|
55
|
|
|
55
|
0
|
1437
|
indx([PDL->topdl(shift)->dims]); |
|
853
|
|
|
|
|
|
|
} |
|
854
|
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
sub PDL::howbig { |
|
856
|
8181
|
|
|
8181
|
0
|
13411
|
my $t = shift; |
|
857
|
8181
|
100
|
|
|
|
19390
|
if("PDL::Type" eq ref $t) {$t = $t->[0]} |
|
|
7689
|
|
|
|
|
15623
|
|
|
858
|
8181
|
|
|
|
|
26608
|
PDL::howbig_c($t); |
|
859
|
|
|
|
|
|
|
} |
|
860
|
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
=head2 broadcastids |
|
862
|
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
=for ref |
|
864
|
|
|
|
|
|
|
|
|
865
|
|
|
|
|
|
|
Returns the ndarray broadcast IDs as a perl list |
|
866
|
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
Note that C is not exported by default (see example |
|
868
|
|
|
|
|
|
|
below for usage). |
|
869
|
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
=for usage |
|
871
|
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
use PDL::Core ':Internal'; # use the internal routines of |
|
873
|
|
|
|
|
|
|
# the Core module |
|
874
|
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
@ids = broadcastids $ndarray; |
|
876
|
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=cut |
|
878
|
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
################# Creation/copying functions ####################### |
|
880
|
|
|
|
|
|
|
|
|
881
|
0
|
|
|
0
|
0
|
0
|
sub piddle {PDL->pdl(@_)} |
|
882
|
8657
|
|
|
8657
|
1
|
5179671
|
sub pdl {PDL->pdl(@_)} |
|
883
|
8892
|
|
|
8892
|
0
|
230577
|
sub PDL::pdl { shift->new(@_) } |
|
884
|
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
=head2 readonly |
|
886
|
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
=for ref |
|
888
|
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
Make an ndarray read-only, returning the ndarray argument. |
|
890
|
|
|
|
|
|
|
This means any future transformation (a.k.a. PDL |
|
891
|
|
|
|
|
|
|
operation) applied to this ndarray I will cause an exception: |
|
892
|
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
$x = sequence(3)->readonly; # also works: $x = sequence(3); $x->readonly; |
|
894
|
|
|
|
|
|
|
$y = $x + 1; # fine |
|
895
|
|
|
|
|
|
|
$x .= 5; # error |
|
896
|
|
|
|
|
|
|
$x += 5; # also error |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
Not exported. |
|
899
|
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=for usage |
|
901
|
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
$x->readonly; |
|
903
|
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
=head2 is_readonly |
|
905
|
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
=for ref |
|
907
|
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
Test whether an ndarray is read-only. |
|
909
|
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
$bool = $x->is_readonly; |
|
911
|
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
Not exported. |
|
913
|
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
=for usage |
|
915
|
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
$x->is_readonly; |
|
917
|
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
=head2 flowing |
|
919
|
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
=for ref |
|
921
|
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
Turn on dataflow, forward only, only for the next operation (cf |
|
923
|
|
|
|
|
|
|
L), returning the ndarray argument. |
|
924
|
|
|
|
|
|
|
This means the next transformation (a.k.a. PDL |
|
925
|
|
|
|
|
|
|
operation) applied to this ndarray will have forward dataflow: |
|
926
|
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
$x = sequence 3; |
|
928
|
|
|
|
|
|
|
$y = $x->flowing + 1; # could instead do $x->flowing; $y = $x + 1 |
|
929
|
|
|
|
|
|
|
$x += 3; # this does not flow, but that would be an infinite loop anyway |
|
930
|
|
|
|
|
|
|
print "$y\n"; # [4 5 6] |
|
931
|
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
This replaced C as of 2.090. See L for more. |
|
933
|
|
|
|
|
|
|
|
|
934
|
|
|
|
|
|
|
=for usage |
|
935
|
|
|
|
|
|
|
|
|
936
|
|
|
|
|
|
|
$x->flowing; flowing($x); |
|
937
|
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
=head2 fflows |
|
939
|
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
=for ref |
|
941
|
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
Returns whether the ndarray's C flag is set. |
|
943
|
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
=head2 new |
|
945
|
|
|
|
|
|
|
|
|
946
|
|
|
|
|
|
|
=for ref |
|
947
|
|
|
|
|
|
|
|
|
948
|
|
|
|
|
|
|
new ndarray constructor method |
|
949
|
|
|
|
|
|
|
|
|
950
|
|
|
|
|
|
|
=for usage |
|
951
|
|
|
|
|
|
|
|
|
952
|
|
|
|
|
|
|
$x = PDL->new(SCALAR|ARRAY|ARRAY REF|STRING); |
|
953
|
|
|
|
|
|
|
|
|
954
|
|
|
|
|
|
|
=for example |
|
955
|
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
$x = PDL->new(42); # new from a Perl scalar |
|
957
|
|
|
|
|
|
|
$y = PDL->new(@list_of_vals); # new from Perl list |
|
958
|
|
|
|
|
|
|
$z = PDL->new(\@list_of_vals); # new from Perl list reference |
|
959
|
|
|
|
|
|
|
$w = PDL->new("[1 2 3]"); # new from Perl string, using |
|
960
|
|
|
|
|
|
|
# Matlab constructor syntax |
|
961
|
|
|
|
|
|
|
|
|
962
|
|
|
|
|
|
|
Constructs ndarray from perl numbers and lists |
|
963
|
|
|
|
|
|
|
and strings with Matlab/Octave style constructor |
|
964
|
|
|
|
|
|
|
syntax. |
|
965
|
|
|
|
|
|
|
|
|
966
|
|
|
|
|
|
|
The string input is fairly versatile though not |
|
967
|
|
|
|
|
|
|
performance optimized. The goal is to make it |
|
968
|
|
|
|
|
|
|
easy to copy and paste code from PDL output and |
|
969
|
|
|
|
|
|
|
to offer a familiar Matlab syntax for ndarray |
|
970
|
|
|
|
|
|
|
construction. As of May, 2010, it is a new |
|
971
|
|
|
|
|
|
|
feature, so feel free to report bugs or suggest |
|
972
|
|
|
|
|
|
|
new features. See documentation for L for |
|
973
|
|
|
|
|
|
|
more examples of usage. |
|
974
|
|
|
|
|
|
|
|
|
975
|
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
=cut |
|
977
|
|
|
|
|
|
|
|
|
978
|
71
|
|
|
71
|
|
24970
|
use Scalar::Util; # for looks_like_number test |
|
|
71
|
|
|
|
|
134
|
|
|
|
71
|
|
|
|
|
3442
|
|
|
979
|
71
|
|
|
71
|
|
397
|
use Carp 'carp'; # for carping (warnings in caller's context) |
|
|
71
|
|
|
|
|
124
|
|
|
|
71
|
|
|
|
|
173700
|
|
|
980
|
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
# This is the code that handles string arguments. It has now got quite large, |
|
982
|
|
|
|
|
|
|
# so here's the basic explanation. I want to allow expressions like 2, 1e3, +4, |
|
983
|
|
|
|
|
|
|
# bad, nan, inf, and more. Checking this can get tricky. This croaks when it |
|
984
|
|
|
|
|
|
|
# finds: |
|
985
|
|
|
|
|
|
|
# 1) strings of e or E that are longer than 1 character long (like eeee) |
|
986
|
|
|
|
|
|
|
# 2) non-supported characters or strings |
|
987
|
|
|
|
|
|
|
# 3) expressions that are syntactically erroneous, like '1 2 3 ]', which has an |
|
988
|
|
|
|
|
|
|
# extra bracket |
|
989
|
|
|
|
|
|
|
# 4) use of inf when the data type does not support inf (i.e. the integers) |
|
990
|
|
|
|
|
|
|
|
|
991
|
|
|
|
|
|
|
my @types = PDL::Types::types; |
|
992
|
|
|
|
|
|
|
my $STR_nan = PDL::_nan(); |
|
993
|
|
|
|
|
|
|
my $STR_i = PDL::_ci(); |
|
994
|
|
|
|
|
|
|
my $STR_inf = PDL::_inf(); |
|
995
|
|
|
|
|
|
|
my $STR_pi = 4 * atan2(1, 1); |
|
996
|
|
|
|
|
|
|
my $STR_e = exp(1); |
|
997
|
|
|
|
|
|
|
sub PDL::Core::new_pdl_from_string { |
|
998
|
491
|
|
|
491
|
0
|
1466
|
my ($new, $original_value, $this, $type) = @_; |
|
999
|
491
|
|
|
|
|
1015
|
my $value = $original_value; |
|
1000
|
|
|
|
|
|
|
|
|
1001
|
|
|
|
|
|
|
# Check for input that would generate empty ndarrays as output: |
|
1002
|
491
|
100
|
100
|
|
|
2179
|
return zeroes($types[$type], 0) if $value eq '' or $value eq '[]'; |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
# I check for invalid characters later, but arbitrary strings of e will |
|
1005
|
|
|
|
|
|
|
# pass that check, so I'll check for that here, first. |
|
1006
|
489
|
100
|
|
|
|
8722
|
croak("PDL::Core::new_pdl_from_string: found 'e' as part of a larger word in $original_value") |
|
1007
|
|
|
|
|
|
|
if $value =~ /e\p{IsAlpha}|\p{IsAlpha}e/; |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
|
|
|
|
|
|
# Only a few characters are allowed in the expression, but we want to allow |
|
1010
|
|
|
|
|
|
|
# expressions like 'inf' and 'bad'. As such, convert those values to internal |
|
1011
|
|
|
|
|
|
|
# representations that will pass the invalid-character check. We'll replace |
|
1012
|
|
|
|
|
|
|
# them with Perl-evalute-able strings in a little bit. Here, I represent |
|
1013
|
|
|
|
|
|
|
# bad => EE |
|
1014
|
|
|
|
|
|
|
# nan => ee |
|
1015
|
|
|
|
|
|
|
# inf => Ee |
|
1016
|
|
|
|
|
|
|
# pi => eE |
|
1017
|
|
|
|
|
|
|
# i => EeE |
|
1018
|
|
|
|
|
|
|
# --( Bad )-- |
|
1019
|
479
|
100
|
|
|
|
4336
|
croak("PDL::Core::new_pdl_from_string: found 'bad' as part of a larger word in $original_value") |
|
1020
|
|
|
|
|
|
|
if $value =~ /bad\B|\Bbad/; |
|
1021
|
475
|
|
|
|
|
4813
|
my ($has_bad) = ($value =~ s/\bbad\b/EE/gi); |
|
1022
|
|
|
|
|
|
|
# --( nan )-- |
|
1023
|
475
|
|
|
|
|
853
|
my $has_nan = 0; |
|
1024
|
475
|
50
|
|
|
|
3185
|
croak("PDL::Core::new_pdl_from_string: found 'nan' as part of a larger word in $original_value") |
|
1025
|
|
|
|
|
|
|
if $value =~ /\Bnan|nan\B/; |
|
1026
|
475
|
100
|
|
|
|
2233
|
$has_nan++ if ($value =~ s/\bnan\b/ee/gi); |
|
1027
|
|
|
|
|
|
|
# Strawberry Perl compatibility: |
|
1028
|
475
|
50
|
|
|
|
1266
|
croak("PDL::Core::new_pdl_from_string: found '1.#IND' as part of a larger word in $original_value") |
|
1029
|
|
|
|
|
|
|
if $value =~ /IND\B/i; |
|
1030
|
475
|
50
|
|
|
|
1507
|
$has_nan++ if ($value =~ s/1\.\#IND/ee/gi); |
|
1031
|
|
|
|
|
|
|
# --( inf )-- |
|
1032
|
475
|
|
|
|
|
793
|
my $has_inf = 0; |
|
1033
|
|
|
|
|
|
|
# Strawberry Perl compatibility: |
|
1034
|
475
|
100
|
|
|
|
1695
|
croak("PDL::Core::new_pdl_from_string: found '1.#INF' as part of a larger word in $original_value") |
|
1035
|
|
|
|
|
|
|
if $value =~ /INF\B/i; |
|
1036
|
473
|
100
|
|
|
|
1286
|
$has_inf++ if ($value =~ s/1\.\#INF/Ee/gi); |
|
1037
|
|
|
|
|
|
|
# Other platforms: |
|
1038
|
473
|
100
|
|
|
|
3737
|
croak("PDL::Core::new_pdl_from_string: found 'inf' as part of a larger word in $original_value") |
|
1039
|
|
|
|
|
|
|
if $value =~ /inf\B|\Binf/; |
|
1040
|
471
|
100
|
|
|
|
2015
|
$has_inf++ if ($value =~ s/\binf\b/Ee/gi); |
|
1041
|
|
|
|
|
|
|
# --( pi )-- |
|
1042
|
471
|
100
|
|
|
|
5390
|
croak("PDL::Core::new_pdl_from_string: found 'pi' as part of a larger word in $original_value") |
|
1043
|
|
|
|
|
|
|
if $value =~ /pi\B|\Bpi/; |
|
1044
|
467
|
|
|
|
|
1714
|
$value =~ s/\bpi\b/eE/gi; |
|
1045
|
|
|
|
|
|
|
# --( i )-- |
|
1046
|
467
|
|
|
|
|
692
|
my $has_i = 0; |
|
1047
|
467
|
50
|
|
|
|
4017
|
croak("PDL::Core::new_pdl_from_string: found 'i' as part of a larger word ($1) in $original_value") |
|
1048
|
|
|
|
|
|
|
if $value =~ /(i\B|[^\-+\d\s.\[]i)/; |
|
1049
|
467
|
100
|
|
|
|
4915
|
$has_i++ if ($value =~ s/([\-+\d]*)i\b/${1}EeE/gi); |
|
1050
|
467
|
100
|
|
|
|
1491
|
$type = $types[$type]->complexversion->enum if $has_i; |
|
1051
|
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
# Some data types do not support nan and inf, so check for and warn or croak, |
|
1053
|
|
|
|
|
|
|
# as appropriate: |
|
1054
|
467
|
50
|
66
|
|
|
1519
|
if ($has_nan and not $types[$type]->usenan) { |
|
1055
|
0
|
|
|
|
|
0
|
carp("PDL::Core::new_pdl_from_string: no nan for type $types[$type]; converting to bad value"); |
|
1056
|
0
|
|
|
|
|
0
|
$value =~ s/ee/EE/g; |
|
1057
|
0
|
|
|
|
|
0
|
$has_bad += $has_nan; |
|
1058
|
0
|
|
|
|
|
0
|
$has_nan = 0; |
|
1059
|
|
|
|
|
|
|
} |
|
1060
|
467
|
50
|
66
|
|
|
1237
|
croak("PDL::Core::new_pdl_from_string: type $types[$type] does not support inf") |
|
1061
|
|
|
|
|
|
|
if ($has_inf and not $types[$type]->usenan); |
|
1062
|
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
# Make the white-space uniform and see if any not-allowed characters are |
|
1064
|
|
|
|
|
|
|
# present: |
|
1065
|
467
|
|
|
|
|
4030
|
$value =~ s/\s+/ /g; |
|
1066
|
467
|
100
|
|
|
|
1866
|
if (my ($disallowed) = ($value =~ /([^\[\]\+\-0-9;,.eE ]+)/)) { |
|
1067
|
4
|
|
|
|
|
961
|
croak("PDL::Core::new_pdl_from_string: found disallowed character(s) '$disallowed' in '$original_value', value now: '$value'"); |
|
1068
|
|
|
|
|
|
|
} |
|
1069
|
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
# Wrap the string in brackets [], so that the following works: |
|
1071
|
|
|
|
|
|
|
# $x = PDL->new(q[1 2 3]); |
|
1072
|
|
|
|
|
|
|
# We'll have to check for dimensions of size one after we've parsed |
|
1073
|
|
|
|
|
|
|
# the string and built a PDL from the resulting array. |
|
1074
|
463
|
|
|
|
|
1018
|
$value = '[' . $value . ']'; |
|
1075
|
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
# Make sure that each closing bracket followed by an opening bracket |
|
1077
|
|
|
|
|
|
|
# has a comma in between them: |
|
1078
|
463
|
|
|
|
|
1119
|
$value =~ s/\]\s*\[/],[/g; |
|
1079
|
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
# Semicolons indicate 'start a new row' and require special handling: |
|
1081
|
463
|
100
|
|
|
|
1337
|
if ($value =~ /;/) { |
|
1082
|
143
|
|
|
|
|
1639
|
$value =~ s/(\[[^\]]+;[^\]]+\])/[$1]/g; |
|
1083
|
143
|
|
|
|
|
721
|
$value =~ s/;/],[/g; |
|
1084
|
|
|
|
|
|
|
} |
|
1085
|
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
# Remove ending decimal points and insert zeroes in front of starting |
|
1087
|
|
|
|
|
|
|
# decimal points. This makes the white-space-to-comma replacement |
|
1088
|
|
|
|
|
|
|
# in the next few lines much simpler. |
|
1089
|
463
|
|
|
|
|
1186
|
$value =~ s/(\d\.)(z|[^\d])/${1}0$2/g; |
|
1090
|
463
|
|
|
|
|
1180
|
$value =~ s/(\A|[^\d])\./${1}0./g; |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
# Remove whitespace between signs and the numbers that follow them: |
|
1093
|
463
|
|
|
|
|
1315
|
$value =~ s/([+\-])\s+/$1/g; |
|
1094
|
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
# Replace whitespace separators with commas: |
|
1096
|
463
|
|
|
|
|
7675
|
$value =~ s/([.\de])\s+(?=[+\-e\d])/$1,/gi; |
|
1097
|
|
|
|
|
|
|
|
|
1098
|
|
|
|
|
|
|
# Remove all other white space: |
|
1099
|
463
|
|
|
|
|
1594
|
$value =~ s/\s+//g; |
|
1100
|
|
|
|
|
|
|
|
|
1101
|
|
|
|
|
|
|
# Croak on operations with bad values. It might be nice to simply replace |
|
1102
|
|
|
|
|
|
|
# these with bad values, but that is more difficult than I like, so I'm just |
|
1103
|
|
|
|
|
|
|
# going to disallow that here: |
|
1104
|
463
|
50
|
|
|
|
3518
|
croak("PDL::Core::new_pdl_from_string: Operations with bad values are not supported") |
|
1105
|
|
|
|
|
|
|
if($value =~ /EE[+\-]|[+\-]EE/); |
|
1106
|
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
# Check for things that will evaluate as functions and croak if found |
|
1108
|
463
|
100
|
|
|
|
5969
|
if (my ($disallowed) = ($value =~ /((\D+|\A)[eE]\d+)/)) { |
|
1109
|
2
|
|
|
|
|
448
|
croak("PDL::Core::new_pdl_from_string: syntax error, looks like an improper exponentiation: $disallowed\n" |
|
1110
|
|
|
|
|
|
|
. "You originally gave me $original_value\n"); |
|
1111
|
|
|
|
|
|
|
} |
|
1112
|
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
# Replace the place-holder strings with strings that will evaluate to their |
|
1114
|
|
|
|
|
|
|
# correct numerical values |
|
1115
|
461
|
100
|
|
|
|
1757
|
my $bad = $has_bad ? $types[$type]->badvalue : undef; |
|
1116
|
461
|
|
|
|
|
1349
|
$value =~ s/\bEE\b/bad/g; |
|
1117
|
461
|
|
|
|
|
971
|
$value =~ s/\bee\b/nan/g; |
|
1118
|
461
|
100
|
|
|
|
2659
|
$value =~ s/([-+]*)(\d*)EeE\b/$1 . (length($2) ? $2 : '1') . 'i'/ge |
|
|
142
|
100
|
|
|
|
809
|
|
|
1119
|
|
|
|
|
|
|
if $has_i; |
|
1120
|
461
|
|
|
|
|
872
|
$value =~ s/\bEe\b/inf/g; |
|
1121
|
461
|
|
|
|
|
900
|
$value =~ s/\beE\b/pi/g; |
|
1122
|
|
|
|
|
|
|
|
|
1123
|
461
|
|
|
|
|
881
|
my $val = eval { |
|
1124
|
|
|
|
|
|
|
# Install the warnings handler: |
|
1125
|
461
|
|
|
|
|
1418
|
my $old_warn_handler = $SIG{__WARN__}; |
|
1126
|
|
|
|
|
|
|
local $SIG{__WARN__} = sub { |
|
1127
|
0
|
0
|
|
0
|
|
0
|
if ($_[0] =~ /(Argument ".*" isn't numeric)/) { |
|
|
|
0
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
# Send the error through die. This *always* gets caught, so keep |
|
1129
|
|
|
|
|
|
|
# it simple. |
|
1130
|
0
|
|
|
|
|
0
|
die "Incorrectly formatted input: $1\n"; |
|
1131
|
|
|
|
|
|
|
} |
|
1132
|
|
|
|
|
|
|
elsif ($old_warn_handler) { |
|
1133
|
0
|
|
|
|
|
0
|
$old_warn_handler->(@_); |
|
1134
|
|
|
|
|
|
|
} |
|
1135
|
|
|
|
|
|
|
else { |
|
1136
|
0
|
|
|
|
|
0
|
warn @_; |
|
1137
|
|
|
|
|
|
|
} |
|
1138
|
461
|
|
|
|
|
4356
|
}; |
|
1139
|
|
|
|
|
|
|
|
|
1140
|
|
|
|
|
|
|
# Let's see if we can parse it as an array-of-arrays: |
|
1141
|
461
|
|
|
|
|
1198
|
local $_ = $value; |
|
1142
|
461
|
|
|
|
|
1253
|
PDL::Core::parse_basic_string($bad, $has_i); |
|
1143
|
|
|
|
|
|
|
}; |
|
1144
|
|
|
|
|
|
|
|
|
1145
|
461
|
100
|
|
|
|
1698
|
if (ref $val ne 'ARRAY') { |
|
1146
|
4
|
|
|
|
|
20
|
my @message = ("PDL::Core::new_pdl_from_string: string input='$original_value', string output='$value'" ); |
|
1147
|
4
|
|
33
|
|
|
19
|
push @message, $@ || |
|
1148
|
|
|
|
|
|
|
"Internal error: unexpected output type ->$val<- is not ARRAY ref"; |
|
1149
|
4
|
|
|
|
|
850
|
croak join("\n ", @message); |
|
1150
|
|
|
|
|
|
|
} |
|
1151
|
457
|
|
|
|
|
9861
|
my $to_return = PDL::Core::pdl_avref($val,$this,$type); |
|
1152
|
457
|
100
|
|
|
|
2758
|
if( $to_return->dim(-1) == 1 ) { |
|
1153
|
193
|
100
|
|
|
|
944
|
if( $to_return->dims > 1 ) { |
|
|
|
50
|
|
|
|
|
|
|
1154
|
|
|
|
|
|
|
# remove potentially spurious last dimension |
|
1155
|
152
|
|
|
|
|
1876
|
$to_return = $to_return->mv(-1,1)->clump(2)->sever; |
|
1156
|
|
|
|
|
|
|
} elsif( $to_return->dims == 1 ) { |
|
1157
|
|
|
|
|
|
|
# fix scalar values |
|
1158
|
41
|
|
|
|
|
203
|
$to_return->setdims([]); |
|
1159
|
|
|
|
|
|
|
} |
|
1160
|
|
|
|
|
|
|
} |
|
1161
|
|
|
|
|
|
|
# Mark bad if appropriate |
|
1162
|
457
|
|
|
|
|
2326
|
$to_return->badflag($has_bad > 0); |
|
1163
|
457
|
|
|
|
|
7758
|
return $to_return; |
|
1164
|
|
|
|
|
|
|
} |
|
1165
|
|
|
|
|
|
|
|
|
1166
|
|
|
|
|
|
|
my $NUM_RE = qr/(\d+(?:\.\d+)?(?:e[-+]?\d+)?)/i; |
|
1167
|
|
|
|
|
|
|
sub PDL::Core::parse_basic_string { |
|
1168
|
|
|
|
|
|
|
# Assumes $_ holds the string of interest, and modifies that value |
|
1169
|
|
|
|
|
|
|
# in-place. |
|
1170
|
71
|
|
|
71
|
|
1152
|
use warnings; |
|
|
71
|
|
|
|
|
165
|
|
|
|
71
|
|
|
|
|
267088
|
|
|
1171
|
|
|
|
|
|
|
# Takes a string with proper bracketing, etc, and returns an array-of-arrays |
|
1172
|
|
|
|
|
|
|
# filled with numbers, suitable for use with pdl_avref. It uses recursive |
|
1173
|
|
|
|
|
|
|
# descent to handle the nested nature of the data. The string should have |
|
1174
|
|
|
|
|
|
|
# no whitespace and should be something that would evaluate into a Perl |
|
1175
|
|
|
|
|
|
|
# array-of-arrays (except that strings like 'inf', etc, are allowed). |
|
1176
|
1209
|
|
|
1209
|
0
|
2311
|
my ($bad, $has_i) = @_; |
|
1177
|
|
|
|
|
|
|
# First character should be a bracket: |
|
1178
|
1209
|
50
|
|
|
|
3916
|
die "Internal error: input string -->$_<-- did not start with an opening bracket\n" |
|
1179
|
|
|
|
|
|
|
unless s/^\[//; |
|
1180
|
1209
|
|
|
|
|
1942
|
my @to_return; |
|
1181
|
|
|
|
|
|
|
# Loop until we run into our closing bracket: |
|
1182
|
1209
|
|
|
|
|
1705
|
my $sign = 1; |
|
1183
|
1209
|
|
|
|
|
1609
|
my $expects_number = 0; |
|
1184
|
1209
|
|
|
|
|
7695
|
SYMBOL: until (s/^\]//) { |
|
1185
|
|
|
|
|
|
|
# If we have a bracket, then go recursive: |
|
1186
|
4475
|
100
|
66
|
|
|
68169
|
if (/^\[/) { |
|
|
|
100
|
66
|
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1187
|
748
|
50
|
|
|
|
1766
|
die "Expected a number but found a bracket at ... ", substr ($_, 0, 10), "...\n" |
|
1188
|
|
|
|
|
|
|
if $expects_number; |
|
1189
|
748
|
|
|
|
|
1829
|
push @to_return, PDL::Core::parse_basic_string(@_); |
|
1190
|
748
|
|
|
|
|
1451
|
next SYMBOL; |
|
1191
|
|
|
|
|
|
|
} |
|
1192
|
|
|
|
|
|
|
elsif (s/^\+//) { |
|
1193
|
12
|
100
|
|
|
|
55
|
die "Expected number but found a plus sign at ... ", substr ($_, 0, 10), "...\n" |
|
1194
|
|
|
|
|
|
|
if $expects_number; |
|
1195
|
11
|
|
|
|
|
18
|
$expects_number = 1; |
|
1196
|
11
|
|
|
|
|
24
|
redo SYMBOL; |
|
1197
|
|
|
|
|
|
|
} |
|
1198
|
|
|
|
|
|
|
elsif (s/^\-//) { |
|
1199
|
326
|
100
|
|
|
|
757
|
die "Expected number but found a minus sign at ... ", substr ($_, 0, 10), "...\n" |
|
1200
|
|
|
|
|
|
|
if $expects_number; |
|
1201
|
325
|
|
|
|
|
445
|
$sign = -1; |
|
1202
|
325
|
|
|
|
|
433
|
$expects_number = 1; |
|
1203
|
325
|
|
|
|
|
543
|
redo SYMBOL; |
|
1204
|
|
|
|
|
|
|
} |
|
1205
|
|
|
|
|
|
|
elsif (s/^bad//i) { |
|
1206
|
169
|
|
|
|
|
296
|
push @to_return, $bad; |
|
1207
|
|
|
|
|
|
|
} |
|
1208
|
|
|
|
|
|
|
elsif (s/^inf//i or s/1\.\#INF//i) { |
|
1209
|
9
|
|
|
|
|
33
|
push @to_return, $sign * $STR_inf; |
|
1210
|
|
|
|
|
|
|
} |
|
1211
|
|
|
|
|
|
|
elsif (s/^nan//i or s/^1\.\#IND//i) { |
|
1212
|
21
|
|
|
|
|
224
|
push @to_return, $sign * $STR_nan; |
|
1213
|
|
|
|
|
|
|
} |
|
1214
|
|
|
|
|
|
|
elsif (s/^pi//i) { |
|
1215
|
3
|
|
|
|
|
11
|
push @to_return, $sign * $STR_pi; |
|
1216
|
|
|
|
|
|
|
} |
|
1217
|
|
|
|
|
|
|
elsif (s/^e//i) { |
|
1218
|
13
|
|
|
|
|
43
|
push @to_return, $sign * $STR_e; |
|
1219
|
|
|
|
|
|
|
} |
|
1220
|
|
|
|
|
|
|
elsif ($has_i and s/^${NUM_RE}i//i) { |
|
1221
|
83
|
|
|
|
|
486
|
my $val = $sign * $1 * $STR_i; |
|
1222
|
83
|
|
|
|
|
278
|
push @to_return, $val; |
|
1223
|
|
|
|
|
|
|
} |
|
1224
|
|
|
|
|
|
|
elsif ($has_i and s/^$NUM_RE([-+])${NUM_RE}i//i) { |
|
1225
|
59
|
|
|
|
|
280
|
my $val = $sign * $1; |
|
1226
|
59
|
100
|
|
|
|
560
|
my $imag = $3 * ($2 eq '-' ? -1 : 1) * $STR_i; |
|
1227
|
59
|
|
|
|
|
303
|
push @to_return, $val + $imag; |
|
1228
|
|
|
|
|
|
|
} |
|
1229
|
|
|
|
|
|
|
elsif (s/^$NUM_RE([^e])/$2/i) { |
|
1230
|
|
|
|
|
|
|
# Note that improper numbers are handled by the warning signal |
|
1231
|
|
|
|
|
|
|
# handler |
|
1232
|
3030
|
|
|
|
|
8543
|
push @to_return, $sign * ($1 + 0x0); |
|
1233
|
|
|
|
|
|
|
} |
|
1234
|
|
|
|
|
|
|
else { |
|
1235
|
2
|
|
|
|
|
40
|
die "Incorrectly formatted input at:\n ", substr($_, 0, 10), "...\n"; |
|
1236
|
|
|
|
|
|
|
} |
|
1237
|
|
|
|
|
|
|
} |
|
1238
|
|
|
|
|
|
|
# Strip off any commas |
|
1239
|
|
|
|
|
|
|
continue { |
|
1240
|
4135
|
|
|
|
|
5550
|
$sign = 1; |
|
1241
|
4135
|
|
|
|
|
5074
|
$expects_number = 0; |
|
1242
|
4135
|
|
|
|
|
13551
|
s/^,//; |
|
1243
|
|
|
|
|
|
|
} |
|
1244
|
1205
|
|
|
|
|
6695
|
return \@to_return; |
|
1245
|
|
|
|
|
|
|
} |
|
1246
|
|
|
|
|
|
|
|
|
1247
|
|
|
|
|
|
|
my $MAX_TYPE = $types[-1]->enum; # use lexical @types from above |
|
1248
|
|
|
|
|
|
|
sub _establish_type { |
|
1249
|
4179
|
|
|
4179
|
|
7507
|
my ($item, $sofar) = @_; |
|
1250
|
4179
|
50
|
|
|
|
8205
|
barf("Error: $sofar > max type value($MAX_TYPE)") if $sofar > $MAX_TYPE; |
|
1251
|
4179
|
50
|
|
|
|
8892
|
return $sofar if $sofar == $MAX_TYPE; |
|
1252
|
4179
|
100
|
|
|
|
10799
|
return $PDL_CD if UNIVERSAL::isa($item, 'Math::Complex'); |
|
1253
|
4172
|
100
|
|
|
|
9533
|
return max($item->type->enum, $sofar) if UNIVERSAL::isa($item, 'PDL'); |
|
1254
|
3902
|
100
|
|
|
|
9566
|
return $PDL_D if ref($item) ne 'ARRAY'; |
|
1255
|
|
|
|
|
|
|
# only need to check first item for an array of complex vals |
|
1256
|
2145
|
50
|
|
|
|
4692
|
return $MAX_TYPE if _establish_type($item->[0], $sofar) == $MAX_TYPE; |
|
1257
|
|
|
|
|
|
|
# only need to recurse for items that are refs |
|
1258
|
|
|
|
|
|
|
# as $sofar will be $PDL_D at a minimum |
|
1259
|
2145
|
|
|
|
|
9766
|
max ($sofar, map _establish_type($_, $sofar), grep ref, @$item); |
|
1260
|
|
|
|
|
|
|
} |
|
1261
|
|
|
|
|
|
|
|
|
1262
|
|
|
|
|
|
|
sub PDL::new { |
|
1263
|
15587
|
100
|
66
|
15587
|
0
|
806419
|
return $_[0]->copy if ref($_[0]) and UNIVERSAL::isa($_[0], 'PDL'); |
|
1264
|
15584
|
|
|
|
|
25524
|
my $this = shift; |
|
1265
|
15584
|
100
|
|
|
|
56134
|
my $type = ref($_[0]) eq 'PDL::Type' ? shift->enum : undef; |
|
1266
|
15584
|
100
|
|
|
|
40648
|
my $value = (@_ > 1 ? [@_] : shift); |
|
1267
|
15584
|
100
|
|
|
|
32095
|
unless(defined $value) { |
|
1268
|
116
|
50
|
|
|
|
317
|
if($PDL::debug) { |
|
1269
|
0
|
|
|
|
|
0
|
print STDERR "Warning: PDL::new converted undef to \$PDL::undefval ($PDL::undefval)\n"; |
|
1270
|
|
|
|
|
|
|
} |
|
1271
|
116
|
|
100
|
|
|
395
|
$value = ($PDL::undefval//0)+0 |
|
1272
|
|
|
|
|
|
|
} |
|
1273
|
15584
|
100
|
66
|
|
|
57711
|
$type //= ref($value) ? _establish_type($value, $PDL_D) : $PDL_D; |
|
1274
|
|
|
|
|
|
|
|
|
1275
|
15584
|
100
|
|
|
|
146342
|
return pdl_avref($value,$this,$type) if ref($value) eq "ARRAY"; |
|
1276
|
8400
|
|
|
|
|
47817
|
my $new = $this->initialize; |
|
1277
|
8400
|
|
|
|
|
33763
|
$new->set_datatype($type); |
|
1278
|
|
|
|
|
|
|
|
|
1279
|
8400
|
100
|
66
|
|
|
23082
|
if (ref(\$value) eq "SCALAR") { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
# The string processing is extremely slow. Benchmarks indicated that it |
|
1281
|
|
|
|
|
|
|
# takes 10x longer to process a scalar number compared with normal Perl |
|
1282
|
|
|
|
|
|
|
# conversion of a string to a number. So, only use the string processing |
|
1283
|
|
|
|
|
|
|
# if the input looks like a real string, i.e. it doesn't look like a plain |
|
1284
|
|
|
|
|
|
|
# number. Note that for our purposes, looks_like_number incorrectly |
|
1285
|
|
|
|
|
|
|
# handles the strings 'inf' and 'nan' on Windows machines. We want to send |
|
1286
|
|
|
|
|
|
|
# those to the string processing, so this checks for them in a way that |
|
1287
|
|
|
|
|
|
|
# short-circuits the looks_like_number check. |
|
1288
|
8255
|
100
|
100
|
|
|
55023
|
if (PDL::Core::is_scalar_SvPOK($value) |
|
|
|
50
|
100
|
|
|
|
|
|
|
|
50
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
1289
|
|
|
|
|
|
|
and ($value =~ /inf/i or $value =~ /nan/i |
|
1290
|
|
|
|
|
|
|
or !Scalar::Util::looks_like_number($value))) { |
|
1291
|
|
|
|
|
|
|
# new was passed a string argument that doesn't look like a number |
|
1292
|
|
|
|
|
|
|
# so we can process as a Matlab-style data entry format. |
|
1293
|
491
|
|
|
|
|
1761
|
return PDL::Core::new_pdl_from_string($new,$value,$this,$type); |
|
1294
|
|
|
|
|
|
|
} elsif (! $CAN_PACK_QUAD && $pack[$new->get_datatype] =~ /^q\*$/i ) { |
|
1295
|
|
|
|
|
|
|
# special case when running on a perl without 64bit int support |
|
1296
|
|
|
|
|
|
|
# we have to avoid pack("q", ...) in this case |
|
1297
|
|
|
|
|
|
|
# because it dies with error: "Invalid type 'q' in pack" |
|
1298
|
0
|
|
|
|
|
0
|
$new->setdims([]); |
|
1299
|
0
|
|
|
|
|
0
|
set_c($new, [0], $value); |
|
1300
|
|
|
|
|
|
|
} elsif (! $CAN_PACK_D && $pack[$new->get_datatype] =~ /^(\QD*\E|\Q(DD)*\E)$/ ) { |
|
1301
|
|
|
|
|
|
|
# if "D" is not available for pack(), |
|
1302
|
|
|
|
|
|
|
# it dies with error: "Invalid type 'D' in pack". |
|
1303
|
0
|
|
|
|
|
0
|
$new->setdims([]); |
|
1304
|
0
|
|
|
|
|
0
|
set_c($new, [0], $value); |
|
1305
|
|
|
|
|
|
|
} else { |
|
1306
|
7764
|
|
|
|
|
34646
|
$new->setdims([]); |
|
1307
|
7764
|
100
|
|
|
|
18779
|
if ($value) { |
|
1308
|
7376
|
|
|
|
|
55560
|
$new->update_data_from( pack $pack[$new->get_datatype], $value ); |
|
1309
|
|
|
|
|
|
|
} else { # do nothing if 0 - allocdata already memsets to 0 |
|
1310
|
388
|
|
|
|
|
1636
|
$new->make_physical; |
|
1311
|
|
|
|
|
|
|
} |
|
1312
|
|
|
|
|
|
|
} |
|
1313
|
|
|
|
|
|
|
} |
|
1314
|
|
|
|
|
|
|
elsif (blessed($value) and UNIVERSAL::isa($value, 'Math::Complex')) { |
|
1315
|
1
|
|
|
|
|
6
|
$new->setdims([]); |
|
1316
|
1
|
|
|
|
|
10
|
set_c($new, [], $value); |
|
1317
|
|
|
|
|
|
|
} |
|
1318
|
|
|
|
|
|
|
elsif (blessed($value)) { # Object |
|
1319
|
144
|
|
|
|
|
488
|
$new = $value->copy; |
|
1320
|
|
|
|
|
|
|
} |
|
1321
|
|
|
|
|
|
|
else { |
|
1322
|
0
|
|
|
|
|
0
|
barf("Can not interpret argument $value of type ".ref($value) ); |
|
1323
|
|
|
|
|
|
|
} |
|
1324
|
7909
|
|
|
|
|
33004
|
$new; |
|
1325
|
|
|
|
|
|
|
} |
|
1326
|
|
|
|
|
|
|
|
|
1327
|
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
=head2 copy |
|
1329
|
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
=for ref |
|
1331
|
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
Make a physical copy of an ndarray |
|
1333
|
|
|
|
|
|
|
|
|
1334
|
|
|
|
|
|
|
=for usage |
|
1335
|
|
|
|
|
|
|
|
|
1336
|
|
|
|
|
|
|
$new = $old->copy; |
|
1337
|
|
|
|
|
|
|
|
|
1338
|
|
|
|
|
|
|
Since C<$new = $old> just makes a new reference, the |
|
1339
|
|
|
|
|
|
|
C method is provided to allow real independent |
|
1340
|
|
|
|
|
|
|
copies to be made. |
|
1341
|
|
|
|
|
|
|
|
|
1342
|
|
|
|
|
|
|
=cut |
|
1343
|
|
|
|
|
|
|
|
|
1344
|
|
|
|
|
|
|
sub PDL::copy { |
|
1345
|
884
|
|
|
884
|
0
|
20035
|
my $value = shift; |
|
1346
|
884
|
50
|
|
|
|
2206
|
barf("Argument is an ".ref($value)." not an object") unless blessed($value); |
|
1347
|
884
|
100
|
|
|
|
3299
|
return $value->nullcreate if $value->isnull; |
|
1348
|
|
|
|
|
|
|
# broadcastI(-1,[]) is just an identity vafftrans with broadcastid copying ;) |
|
1349
|
883
|
|
|
|
|
152374
|
$value->broadcastI(-1,[])->sever; |
|
1350
|
|
|
|
|
|
|
} |
|
1351
|
|
|
|
|
|
|
|
|
1352
|
|
|
|
|
|
|
=head2 hdr_copy |
|
1353
|
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
=for ref |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
Return an explicit copy of the header of a PDL. |
|
1357
|
|
|
|
|
|
|
|
|
1358
|
|
|
|
|
|
|
hdr_copy is just a wrapper for the internal routine _hdr_copy, which |
|
1359
|
|
|
|
|
|
|
takes the hash ref itself. That is the routine which is used to make |
|
1360
|
|
|
|
|
|
|
copies of the header during normal operations if the hdrcpy() flag of |
|
1361
|
|
|
|
|
|
|
a PDL is set. |
|
1362
|
|
|
|
|
|
|
|
|
1363
|
|
|
|
|
|
|
General-purpose deep copies are expensive in perl, so some simple |
|
1364
|
|
|
|
|
|
|
optimization happens: |
|
1365
|
|
|
|
|
|
|
|
|
1366
|
|
|
|
|
|
|
If the header is a tied array or a blessed hash ref with an associated |
|
1367
|
|
|
|
|
|
|
method called C, then that ->copy method is called. Otherwise, all |
|
1368
|
|
|
|
|
|
|
elements of the hash are explicitly copied. References are recursively |
|
1369
|
|
|
|
|
|
|
deep copied. |
|
1370
|
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
This routine seems to leak memory. |
|
1372
|
|
|
|
|
|
|
|
|
1373
|
|
|
|
|
|
|
=cut |
|
1374
|
|
|
|
|
|
|
|
|
1375
|
|
|
|
|
|
|
sub PDL::hdr_copy { |
|
1376
|
9
|
|
|
9
|
0
|
24
|
my $pdl = shift; |
|
1377
|
9
|
|
|
|
|
25
|
my $hdr = $pdl->gethdr; |
|
1378
|
9
|
|
|
|
|
24
|
return PDL::_hdr_copy($hdr); |
|
1379
|
|
|
|
|
|
|
} |
|
1380
|
|
|
|
|
|
|
|
|
1381
|
|
|
|
|
|
|
# Same as hdr_copy but takes a hash ref instead of a PDL. |
|
1382
|
|
|
|
|
|
|
sub PDL::_hdr_copy { |
|
1383
|
4803
|
|
|
4803
|
|
9758
|
my $hdr = shift; |
|
1384
|
4803
|
|
|
|
|
6964
|
my $tobj; |
|
1385
|
|
|
|
|
|
|
|
|
1386
|
4803
|
50
|
|
|
|
11100
|
print "called _hdr_copy\n" if($PDL::debug); |
|
1387
|
|
|
|
|
|
|
|
|
1388
|
4803
|
50
|
|
|
|
24499
|
unless( (ref $hdr)=~m/HASH/ ) { |
|
1389
|
0
|
0
|
|
|
|
0
|
print"returning undef\n" if($PDL::debug); |
|
1390
|
0
|
|
|
|
|
0
|
return undef ; |
|
1391
|
|
|
|
|
|
|
} |
|
1392
|
|
|
|
|
|
|
|
|
1393
|
4803
|
100
|
|
|
|
21446
|
if($tobj = tied %$hdr) { # |
|
|
|
50
|
|
|
|
|
|
|
1394
|
4657
|
50
|
|
|
|
2733987
|
print "tied..."if($PDL::debug); |
|
1395
|
4657
|
50
|
|
|
|
19944
|
if(UNIVERSAL::can($tobj,"copy")) { |
|
1396
|
0
|
|
|
|
|
0
|
my %rhdr; |
|
1397
|
0
|
|
|
|
|
0
|
tie(%rhdr, ref $tobj, $tobj->copy); |
|
1398
|
0
|
0
|
|
|
|
0
|
print "returning\n" if($PDL::debug); |
|
1399
|
0
|
|
|
|
|
0
|
return \%rhdr; |
|
1400
|
|
|
|
|
|
|
} |
|
1401
|
|
|
|
|
|
|
|
|
1402
|
|
|
|
|
|
|
# Astro::FITS::Header is special for now -- no copy method yet |
|
1403
|
|
|
|
|
|
|
# but it is recognized. Once it gets a copy method this will become |
|
1404
|
|
|
|
|
|
|
# vestigial: |
|
1405
|
|
|
|
|
|
|
|
|
1406
|
4657
|
50
|
|
|
|
17736
|
if(UNIVERSAL::isa($tobj,"Astro::FITS::Header")) { |
|
1407
|
4657
|
50
|
|
|
|
9169
|
print "Astro::FITS::Header..." if($PDL::debug); |
|
1408
|
4657
|
|
|
|
|
11063
|
my @cards = $tobj->cards; |
|
1409
|
4657
|
|
|
|
|
1286365
|
my %rhdr; |
|
1410
|
4657
|
|
|
|
|
18644
|
tie(%rhdr,"Astro::FITS::Header", new Astro::FITS::Header(Cards=>\@cards)); |
|
1411
|
4657
|
50
|
|
|
|
19573132
|
print "returning\n" if($PDL::debug); |
|
1412
|
4657
|
|
|
|
|
238157
|
return \%rhdr; |
|
1413
|
|
|
|
|
|
|
} |
|
1414
|
|
|
|
|
|
|
} |
|
1415
|
|
|
|
|
|
|
elsif(UNIVERSAL::can($hdr,"copy")) { |
|
1416
|
0
|
0
|
|
|
|
0
|
print "found a copy method\n" if($PDL::debug); |
|
1417
|
0
|
|
|
|
|
0
|
return $hdr->copy; |
|
1418
|
|
|
|
|
|
|
} |
|
1419
|
|
|
|
|
|
|
|
|
1420
|
|
|
|
|
|
|
# We got here if it's an unrecognized tie or if it's a vanilla hash. |
|
1421
|
146
|
50
|
|
|
|
280
|
print "Making a hash copy..." if($PDL::debug); |
|
1422
|
|
|
|
|
|
|
|
|
1423
|
146
|
|
|
|
|
307
|
return PDL::_deep_hdr_copy($hdr); |
|
1424
|
|
|
|
|
|
|
|
|
1425
|
|
|
|
|
|
|
} |
|
1426
|
|
|
|
|
|
|
|
|
1427
|
|
|
|
|
|
|
# |
|
1428
|
|
|
|
|
|
|
# Sleazy deep-copier that gets most cases |
|
1429
|
|
|
|
|
|
|
# --CED 14-April-2003 |
|
1430
|
|
|
|
|
|
|
# |
|
1431
|
|
|
|
|
|
|
|
|
1432
|
|
|
|
|
|
|
sub PDL::_deep_hdr_copy { |
|
1433
|
146
|
|
|
146
|
|
223
|
my $val = shift; |
|
1434
|
|
|
|
|
|
|
|
|
1435
|
146
|
50
|
|
|
|
398
|
if(ref $val eq 'HASH') { |
|
1436
|
146
|
|
|
|
|
231
|
my %a; |
|
1437
|
146
|
50
|
|
|
|
2572
|
@a{keys %$val} = map ref($_) ? PDL::_deep_hdr_copy($_) : $_, values %$val; |
|
1438
|
146
|
|
|
|
|
2130
|
return \%a; |
|
1439
|
|
|
|
|
|
|
} |
|
1440
|
|
|
|
|
|
|
|
|
1441
|
0
|
0
|
|
|
|
0
|
return [map ref($_) ? PDL::_deep_hdr_copy($_) : $_, @$val] if ref $val eq 'ARRAY'; |
|
|
|
0
|
|
|
|
|
|
|
1442
|
|
|
|
|
|
|
|
|
1443
|
0
|
0
|
|
|
|
0
|
if(ref $val eq 'SCALAR') { |
|
1444
|
0
|
|
|
|
|
0
|
my $x = $$val; |
|
1445
|
0
|
|
|
|
|
0
|
return \$x; |
|
1446
|
|
|
|
|
|
|
} |
|
1447
|
|
|
|
|
|
|
|
|
1448
|
0
|
0
|
|
|
|
0
|
if(ref $val eq 'REF') { |
|
1449
|
0
|
|
|
|
|
0
|
my $x = PDL::_deep_hdr_copy($$val); |
|
1450
|
0
|
|
|
|
|
0
|
return \$x; |
|
1451
|
|
|
|
|
|
|
} |
|
1452
|
|
|
|
|
|
|
|
|
1453
|
|
|
|
|
|
|
# Special case for PDLs avoids potential nasty header recursion... |
|
1454
|
0
|
0
|
|
|
|
0
|
if(UNIVERSAL::isa($val,'PDL')) { |
|
1455
|
0
|
|
|
|
|
0
|
my $h; |
|
1456
|
0
|
0
|
|
|
|
0
|
$val->hdrcpy(0) if($h = $val->hdrcpy); # assignment |
|
1457
|
0
|
|
|
|
|
0
|
my $out = $val->copy; |
|
1458
|
0
|
0
|
|
|
|
0
|
$val->hdrcpy($h) if($h); |
|
1459
|
0
|
|
|
|
|
0
|
return $out; |
|
1460
|
|
|
|
|
|
|
} |
|
1461
|
|
|
|
|
|
|
|
|
1462
|
0
|
0
|
|
|
|
0
|
if(UNIVERSAL::can($val,'copy')) { |
|
1463
|
0
|
|
|
|
|
0
|
return $val->copy; |
|
1464
|
|
|
|
|
|
|
} |
|
1465
|
|
|
|
|
|
|
|
|
1466
|
0
|
|
|
|
|
0
|
$val; |
|
1467
|
|
|
|
|
|
|
} |
|
1468
|
|
|
|
|
|
|
|
|
1469
|
|
|
|
|
|
|
|
|
1470
|
|
|
|
|
|
|
=head2 unwind |
|
1471
|
|
|
|
|
|
|
|
|
1472
|
|
|
|
|
|
|
=for ref |
|
1473
|
|
|
|
|
|
|
|
|
1474
|
|
|
|
|
|
|
Return an ndarray which is the same as the argument except |
|
1475
|
|
|
|
|
|
|
that all broadcastids have been removed. |
|
1476
|
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
=for usage |
|
1478
|
|
|
|
|
|
|
|
|
1479
|
|
|
|
|
|
|
$y = $x->unwind; |
|
1480
|
|
|
|
|
|
|
|
|
1481
|
|
|
|
|
|
|
=cut |
|
1482
|
|
|
|
|
|
|
|
|
1483
|
|
|
|
|
|
|
sub PDL::unwind { |
|
1484
|
0
|
|
|
0
|
0
|
0
|
my $value = shift; |
|
1485
|
0
|
|
|
|
|
0
|
my $foo = $value->null(); |
|
1486
|
0
|
|
|
|
|
0
|
$foo .= $value->unbroadcast(); |
|
1487
|
0
|
|
|
|
|
0
|
return $foo; |
|
1488
|
|
|
|
|
|
|
} |
|
1489
|
|
|
|
|
|
|
|
|
1490
|
|
|
|
|
|
|
=head2 make_physical |
|
1491
|
|
|
|
|
|
|
|
|
1492
|
|
|
|
|
|
|
=for ref |
|
1493
|
|
|
|
|
|
|
|
|
1494
|
|
|
|
|
|
|
Make sure the data portion of an ndarray can be accessed from XS code. |
|
1495
|
|
|
|
|
|
|
|
|
1496
|
|
|
|
|
|
|
=for example |
|
1497
|
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
$x->make_physical; |
|
1499
|
|
|
|
|
|
|
$x->call_my_xs_method; |
|
1500
|
|
|
|
|
|
|
|
|
1501
|
|
|
|
|
|
|
Ensures that an ndarray gets its own allocated copy of data. This obviously |
|
1502
|
|
|
|
|
|
|
implies that there are certain ndarrays which do not have their own data. |
|
1503
|
|
|
|
|
|
|
These are so called I ndarrays that make use of the I |
|
1504
|
|
|
|
|
|
|
optimisation (see L). |
|
1505
|
|
|
|
|
|
|
They do not have their own copy of |
|
1506
|
|
|
|
|
|
|
data but instead store only access information to some (or all) of another |
|
1507
|
|
|
|
|
|
|
ndarray's data. |
|
1508
|
|
|
|
|
|
|
|
|
1509
|
|
|
|
|
|
|
Note: this function should not be used unless absolutely necessary |
|
1510
|
|
|
|
|
|
|
since otherwise memory requirements might be severely increased. Instead |
|
1511
|
|
|
|
|
|
|
of writing your own XS code with the need to call C you |
|
1512
|
|
|
|
|
|
|
might want to consider using the PDL preprocessor |
|
1513
|
|
|
|
|
|
|
(see L) |
|
1514
|
|
|
|
|
|
|
which can be used to transparently access virtual ndarrays without the |
|
1515
|
|
|
|
|
|
|
need to physicalise them (though there are exceptions). |
|
1516
|
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
=head2 make_physvaffine |
|
1518
|
|
|
|
|
|
|
|
|
1519
|
|
|
|
|
|
|
=for ref |
|
1520
|
|
|
|
|
|
|
|
|
1521
|
|
|
|
|
|
|
A more "careful" function than C. For ndarrays |
|
1522
|
|
|
|
|
|
|
without a vaffine transformation as parent, it will just call |
|
1523
|
|
|
|
|
|
|
C. Otherwise, it will update the vaffine transformation |
|
1524
|
|
|
|
|
|
|
bookkeeping. |
|
1525
|
|
|
|
|
|
|
|
|
1526
|
|
|
|
|
|
|
=head2 make_physdims |
|
1527
|
|
|
|
|
|
|
|
|
1528
|
|
|
|
|
|
|
=for ref |
|
1529
|
|
|
|
|
|
|
|
|
1530
|
|
|
|
|
|
|
Ensures the ndarray's dimensions are up to date including changes in |
|
1531
|
|
|
|
|
|
|
parent's dimensions, and calling C. |
|
1532
|
|
|
|
|
|
|
|
|
1533
|
|
|
|
|
|
|
=head2 trans_parent |
|
1534
|
|
|
|
|
|
|
|
|
1535
|
|
|
|
|
|
|
=for ref |
|
1536
|
|
|
|
|
|
|
|
|
1537
|
|
|
|
|
|
|
Returns a PDL::Trans object representing the transformation (PDL |
|
1538
|
|
|
|
|
|
|
operation) that is the "parent" of this ndarray, or C if none. |
|
1539
|
|
|
|
|
|
|
|
|
1540
|
|
|
|
|
|
|
Such objects have these methods: |
|
1541
|
|
|
|
|
|
|
|
|
1542
|
|
|
|
|
|
|
=over |
|
1543
|
|
|
|
|
|
|
|
|
1544
|
|
|
|
|
|
|
=item parents |
|
1545
|
|
|
|
|
|
|
|
|
1546
|
|
|
|
|
|
|
Returns a list of ndarrays that are inputs to this trans. |
|
1547
|
|
|
|
|
|
|
|
|
1548
|
|
|
|
|
|
|
=item children |
|
1549
|
|
|
|
|
|
|
|
|
1550
|
|
|
|
|
|
|
Returns a list of ndarrays that are outputs to this trans (specified as |
|
1551
|
|
|
|
|
|
|
C<[o]>, C<[oca]>, C<[io]>, or C<[t]> in C). |
|
1552
|
|
|
|
|
|
|
|
|
1553
|
|
|
|
|
|
|
=item address |
|
1554
|
|
|
|
|
|
|
|
|
1555
|
|
|
|
|
|
|
The memory address of the struct. |
|
1556
|
|
|
|
|
|
|
|
|
1557
|
|
|
|
|
|
|
=item flags |
|
1558
|
|
|
|
|
|
|
|
|
1559
|
|
|
|
|
|
|
List of strings of flags set for this trans. |
|
1560
|
|
|
|
|
|
|
|
|
1561
|
|
|
|
|
|
|
=item affine |
|
1562
|
|
|
|
|
|
|
|
|
1563
|
|
|
|
|
|
|
Whether the trans is affine. |
|
1564
|
|
|
|
|
|
|
|
|
1565
|
|
|
|
|
|
|
=item offs |
|
1566
|
|
|
|
|
|
|
|
|
1567
|
|
|
|
|
|
|
Affine-only: the offset into the parent's data. |
|
1568
|
|
|
|
|
|
|
|
|
1569
|
|
|
|
|
|
|
=item incs |
|
1570
|
|
|
|
|
|
|
|
|
1571
|
|
|
|
|
|
|
Affine-only: the dimincs for each of the child's dims. |
|
1572
|
|
|
|
|
|
|
|
|
1573
|
|
|
|
|
|
|
=item ind_sizes |
|
1574
|
|
|
|
|
|
|
|
|
1575
|
|
|
|
|
|
|
The size of each named dim. |
|
1576
|
|
|
|
|
|
|
|
|
1577
|
|
|
|
|
|
|
=item inc_sizes |
|
1578
|
|
|
|
|
|
|
|
|
1579
|
|
|
|
|
|
|
The size of the inc for each use of a named dim. |
|
1580
|
|
|
|
|
|
|
|
|
1581
|
|
|
|
|
|
|
=item vtable |
|
1582
|
|
|
|
|
|
|
|
|
1583
|
|
|
|
|
|
|
This trans's vtable. |
|
1584
|
|
|
|
|
|
|
|
|
1585
|
|
|
|
|
|
|
=item C<< $vtable->name >> |
|
1586
|
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
The function name from this vtable. |
|
1588
|
|
|
|
|
|
|
|
|
1589
|
|
|
|
|
|
|
=item C<< $vtable->flags >> |
|
1590
|
|
|
|
|
|
|
|
|
1591
|
|
|
|
|
|
|
List of strings of flags set for this vtable. |
|
1592
|
|
|
|
|
|
|
|
|
1593
|
|
|
|
|
|
|
=item C<< $vtable->par_names >> |
|
1594
|
|
|
|
|
|
|
|
|
1595
|
|
|
|
|
|
|
List of 2 array-refs of strings of names of input pars, then output pars, |
|
1596
|
|
|
|
|
|
|
for this vtable. |
|
1597
|
|
|
|
|
|
|
|
|
1598
|
|
|
|
|
|
|
=back |
|
1599
|
|
|
|
|
|
|
|
|
1600
|
|
|
|
|
|
|
=head2 trans_children |
|
1601
|
|
|
|
|
|
|
|
|
1602
|
|
|
|
|
|
|
=for ref |
|
1603
|
|
|
|
|
|
|
|
|
1604
|
|
|
|
|
|
|
Returns a list of PDL::Trans objects (see L) representing |
|
1605
|
|
|
|
|
|
|
each transformation that has this ndarray as an input. |
|
1606
|
|
|
|
|
|
|
|
|
1607
|
|
|
|
|
|
|
=head2 address |
|
1608
|
|
|
|
|
|
|
|
|
1609
|
|
|
|
|
|
|
=for ref |
|
1610
|
|
|
|
|
|
|
|
|
1611
|
|
|
|
|
|
|
Returns the memory address of the ndarray's C. |
|
1612
|
|
|
|
|
|
|
|
|
1613
|
|
|
|
|
|
|
=head2 address_data |
|
1614
|
|
|
|
|
|
|
|
|
1615
|
|
|
|
|
|
|
=for ref |
|
1616
|
|
|
|
|
|
|
|
|
1617
|
|
|
|
|
|
|
Returns the value of the ndarray C's C member. |
|
1618
|
|
|
|
|
|
|
|
|
1619
|
|
|
|
|
|
|
=head2 set_donttouchdata |
|
1620
|
|
|
|
|
|
|
|
|
1621
|
|
|
|
|
|
|
=for ref |
|
1622
|
|
|
|
|
|
|
|
|
1623
|
|
|
|
|
|
|
Sets the C flag and the C to the given |
|
1624
|
|
|
|
|
|
|
value. Useful in memory-mapping functionality. |
|
1625
|
|
|
|
|
|
|
The C can be omitted, in which case only the flag is set. |
|
1626
|
|
|
|
|
|
|
|
|
1627
|
|
|
|
|
|
|
=head2 nbytes |
|
1628
|
|
|
|
|
|
|
|
|
1629
|
|
|
|
|
|
|
=for ref |
|
1630
|
|
|
|
|
|
|
|
|
1631
|
|
|
|
|
|
|
Returns the ndarray's C. |
|
1632
|
|
|
|
|
|
|
|
|
1633
|
|
|
|
|
|
|
=head2 seed |
|
1634
|
|
|
|
|
|
|
|
|
1635
|
|
|
|
|
|
|
=for ref |
|
1636
|
|
|
|
|
|
|
|
|
1637
|
|
|
|
|
|
|
Returns the random seed being used by PDL's RNG. |
|
1638
|
|
|
|
|
|
|
|
|
1639
|
|
|
|
|
|
|
=head2 set_debugging |
|
1640
|
|
|
|
|
|
|
|
|
1641
|
|
|
|
|
|
|
=for ref |
|
1642
|
|
|
|
|
|
|
|
|
1643
|
|
|
|
|
|
|
Sets whether PDL operations print lots of debugging info to standard |
|
1644
|
|
|
|
|
|
|
output. Returns the old value. |
|
1645
|
|
|
|
|
|
|
|
|
1646
|
|
|
|
|
|
|
=for example |
|
1647
|
|
|
|
|
|
|
|
|
1648
|
|
|
|
|
|
|
PDL::Core::set_debugging(1); |
|
1649
|
|
|
|
|
|
|
# ... these operations will have debugging info printed to stdout |
|
1650
|
|
|
|
|
|
|
PDL::Core::set_debugging(0); # turn it off again |
|
1651
|
|
|
|
|
|
|
|
|
1652
|
|
|
|
|
|
|
=head2 dummy |
|
1653
|
|
|
|
|
|
|
|
|
1654
|
|
|
|
|
|
|
=for ref |
|
1655
|
|
|
|
|
|
|
|
|
1656
|
|
|
|
|
|
|
Insert a 'dummy dimension' of given length (defaults to 1) |
|
1657
|
|
|
|
|
|
|
|
|
1658
|
|
|
|
|
|
|
No relation to the 'Dungeon Dimensions' in Discworld! |
|
1659
|
|
|
|
|
|
|
|
|
1660
|
|
|
|
|
|
|
Negative positions specify relative to last dimension, |
|
1661
|
|
|
|
|
|
|
i.e. C appends one dimension at end, |
|
1662
|
|
|
|
|
|
|
C inserts a dummy dimension in front of the |
|
1663
|
|
|
|
|
|
|
last dim, etc. |
|
1664
|
|
|
|
|
|
|
|
|
1665
|
|
|
|
|
|
|
If you specify a dimension position larger than the existing |
|
1666
|
|
|
|
|
|
|
dimension list of your PDL, the PDL gets automagically padded with extra |
|
1667
|
|
|
|
|
|
|
dummy dimensions so that you get the dim you asked for, in the slot you |
|
1668
|
|
|
|
|
|
|
asked for. This could cause you trouble if, for example, |
|
1669
|
|
|
|
|
|
|
you ask for $x->dummy(5000,1) because $x will get 5,000 dimensions, |
|
1670
|
|
|
|
|
|
|
each of rank 1. |
|
1671
|
|
|
|
|
|
|
|
|
1672
|
|
|
|
|
|
|
Because padding at the beginning of the dimension list moves existing |
|
1673
|
|
|
|
|
|
|
dimensions from slot to slot, it's considered unsafe, so automagic |
|
1674
|
|
|
|
|
|
|
padding doesn't work for large negative indices -- only for large |
|
1675
|
|
|
|
|
|
|
positive indices. |
|
1676
|
|
|
|
|
|
|
|
|
1677
|
|
|
|
|
|
|
=for usage |
|
1678
|
|
|
|
|
|
|
|
|
1679
|
|
|
|
|
|
|
$y = $x->dummy($position[,$dimsize]); |
|
1680
|
|
|
|
|
|
|
|
|
1681
|
|
|
|
|
|
|
=for example |
|
1682
|
|
|
|
|
|
|
|
|
1683
|
|
|
|
|
|
|
pdl> p sequence(3)->dummy(0,3) |
|
1684
|
|
|
|
|
|
|
[ |
|
1685
|
|
|
|
|
|
|
[0 0 0] |
|
1686
|
|
|
|
|
|
|
[1 1 1] |
|
1687
|
|
|
|
|
|
|
[2 2 2] |
|
1688
|
|
|
|
|
|
|
] |
|
1689
|
|
|
|
|
|
|
|
|
1690
|
|
|
|
|
|
|
pdl> p sequence(3)->dummy(3,2) |
|
1691
|
|
|
|
|
|
|
[ |
|
1692
|
|
|
|
|
|
|
[ |
|
1693
|
|
|
|
|
|
|
[0 1 2] |
|
1694
|
|
|
|
|
|
|
] |
|
1695
|
|
|
|
|
|
|
[ |
|
1696
|
|
|
|
|
|
|
[0 1 2] |
|
1697
|
|
|
|
|
|
|
] |
|
1698
|
|
|
|
|
|
|
] |
|
1699
|
|
|
|
|
|
|
|
|
1700
|
|
|
|
|
|
|
pdl> p sequence(3)->dummy(-3,2) |
|
1701
|
|
|
|
|
|
|
Runtime error: PDL: For safety, < -(dims+1) forbidden in dummy. min=-2, pos=-3 |
|
1702
|
|
|
|
|
|
|
|
|
1703
|
|
|
|
|
|
|
=cut |
|
1704
|
|
|
|
|
|
|
|
|
1705
|
|
|
|
|
|
|
sub PDL::dummy($$;$) :lvalue { |
|
1706
|
422
|
|
|
422
|
0
|
4690
|
my ($pdl,$dim,$size) = @_; |
|
1707
|
422
|
50
|
|
|
|
1081
|
barf("Missing position argument to dummy()") unless defined $dim; # required argument |
|
1708
|
422
|
100
|
|
|
|
1035
|
$dim = $pdl->getndims+1+$dim if $dim < 0; |
|
1709
|
422
|
100
|
|
|
|
1145
|
$size = defined($size) ? (1 * $size) : 1; # make $size a number (sf feature # 3479009) |
|
1710
|
422
|
50
|
|
|
|
943
|
barf("For safety, < -(dims+1) forbidden in dummy. min=" |
|
1711
|
|
|
|
|
|
|
. -($pdl->getndims+1).", pos=". ($dim-1-$pdl->getndims) ) if($dim<0); |
|
1712
|
|
|
|
|
|
|
# Avoid negative repeat count warning that came with 5.21 and later. |
|
1713
|
422
|
|
|
|
|
1755
|
my $dim_diff = $dim - $pdl->getndims; |
|
1714
|
422
|
100
|
|
|
|
1380
|
my($s) = ',' x ( $dim_diff > 0 ? $pdl->getndims : $dim ); |
|
1715
|
422
|
100
|
|
|
|
1032
|
$s .= '*1,' x ( $dim_diff > 0 ? $dim_diff : 0 ); |
|
1716
|
422
|
|
|
|
|
919
|
$s .= "*$size"; |
|
1717
|
422
|
|
|
|
|
1445
|
$pdl->slice($s); |
|
1718
|
|
|
|
|
|
|
} |
|
1719
|
|
|
|
|
|
|
|
|
1720
|
|
|
|
|
|
|
=head2 dup |
|
1721
|
|
|
|
|
|
|
|
|
1722
|
|
|
|
|
|
|
=for ref |
|
1723
|
|
|
|
|
|
|
|
|
1724
|
|
|
|
|
|
|
Duplicates an ndarray along a dimension |
|
1725
|
|
|
|
|
|
|
|
|
1726
|
|
|
|
|
|
|
=for example |
|
1727
|
|
|
|
|
|
|
|
|
1728
|
|
|
|
|
|
|
$x = sequence(3); |
|
1729
|
|
|
|
|
|
|
$y = $x->dup(0, 2); # doubles along first dimension |
|
1730
|
|
|
|
|
|
|
# $y now [0 1 2 0 1 2] |
|
1731
|
|
|
|
|
|
|
|
|
1732
|
|
|
|
|
|
|
=cut |
|
1733
|
|
|
|
|
|
|
|
|
1734
|
|
|
|
|
|
|
sub PDL::dup { |
|
1735
|
1
|
|
|
1
|
0
|
5
|
my ($this, $dim, $times) = @_; |
|
1736
|
1
|
50
|
|
|
|
6
|
return $this->copy if $times == 1; |
|
1737
|
1
|
|
|
|
|
5
|
$this->dummy($dim+1, $times)->clump($dim, $dim+1); |
|
1738
|
|
|
|
|
|
|
} |
|
1739
|
|
|
|
|
|
|
|
|
1740
|
|
|
|
|
|
|
=head2 dupN |
|
1741
|
|
|
|
|
|
|
|
|
1742
|
|
|
|
|
|
|
=for ref |
|
1743
|
|
|
|
|
|
|
|
|
1744
|
|
|
|
|
|
|
Duplicates an ndarray along several dimensions |
|
1745
|
|
|
|
|
|
|
|
|
1746
|
|
|
|
|
|
|
=for example |
|
1747
|
|
|
|
|
|
|
|
|
1748
|
|
|
|
|
|
|
$x = sequence(3,2); |
|
1749
|
|
|
|
|
|
|
$y = $x->dupN(2, 3); # doubles along first dimension, triples along second |
|
1750
|
|
|
|
|
|
|
# [ |
|
1751
|
|
|
|
|
|
|
# [0 1 2 0 1 2] |
|
1752
|
|
|
|
|
|
|
# [3 4 5 3 4 5] |
|
1753
|
|
|
|
|
|
|
# [0 1 2 0 1 2] |
|
1754
|
|
|
|
|
|
|
# [3 4 5 3 4 5] |
|
1755
|
|
|
|
|
|
|
# [0 1 2 0 1 2] |
|
1756
|
|
|
|
|
|
|
# [3 4 5 3 4 5] |
|
1757
|
|
|
|
|
|
|
# ] |
|
1758
|
|
|
|
|
|
|
|
|
1759
|
|
|
|
|
|
|
=cut |
|
1760
|
|
|
|
|
|
|
|
|
1761
|
|
|
|
|
|
|
sub PDL::dupN { |
|
1762
|
1
|
|
|
1
|
0
|
5
|
my ($this, @times) = @_; |
|
1763
|
1
|
50
|
|
|
|
6
|
return $this->copy if !grep $_ != 1, @times; |
|
1764
|
1
|
|
|
|
|
8
|
my $sl = join ',', map ":,*$_", @times; # insert right-size dummy after each real |
|
1765
|
1
|
|
|
|
|
6
|
$this = $this->slice($sl); |
|
1766
|
1
|
|
|
|
|
7
|
$this = $this->clump($_, $_+1) for 0..$#times; |
|
1767
|
1
|
|
|
|
|
6
|
$this; |
|
1768
|
|
|
|
|
|
|
} |
|
1769
|
|
|
|
|
|
|
|
|
1770
|
|
|
|
|
|
|
=head2 inflateN |
|
1771
|
|
|
|
|
|
|
|
|
1772
|
|
|
|
|
|
|
=for ref |
|
1773
|
|
|
|
|
|
|
|
|
1774
|
|
|
|
|
|
|
Inflates an ndarray along several dimensions, useful for e.g. Kronecker products |
|
1775
|
|
|
|
|
|
|
|
|
1776
|
|
|
|
|
|
|
cf L |
|
1777
|
|
|
|
|
|
|
|
|
1778
|
|
|
|
|
|
|
=for example |
|
1779
|
|
|
|
|
|
|
|
|
1780
|
|
|
|
|
|
|
$x = sequence(3,2); |
|
1781
|
|
|
|
|
|
|
$y = $x->inflateN(2, 2); # doubles along first two dimensions |
|
1782
|
|
|
|
|
|
|
# [ |
|
1783
|
|
|
|
|
|
|
# [0 0 1 1 2 2] |
|
1784
|
|
|
|
|
|
|
# [0 0 1 1 2 2] |
|
1785
|
|
|
|
|
|
|
# [3 3 4 4 5 5] |
|
1786
|
|
|
|
|
|
|
# [3 3 4 4 5 5] |
|
1787
|
|
|
|
|
|
|
# ] |
|
1788
|
|
|
|
|
|
|
|
|
1789
|
|
|
|
|
|
|
=cut |
|
1790
|
|
|
|
|
|
|
|
|
1791
|
|
|
|
|
|
|
sub PDL::inflateN { |
|
1792
|
1
|
|
|
1
|
0
|
5
|
my ($this, @times) = @_; |
|
1793
|
1
|
50
|
|
|
|
6
|
return $this->copy if !grep $_ != 1, @times; |
|
1794
|
1
|
|
|
|
|
7
|
my $sl = join ',', map "*$_,:", @times; |
|
1795
|
1
|
|
|
|
|
196
|
$this = $this->slice($sl); |
|
1796
|
1
|
|
|
|
|
7
|
$this = $this->clump($_, $_+1) for 0..$#times; |
|
1797
|
1
|
|
|
|
|
8
|
$this; |
|
1798
|
|
|
|
|
|
|
} |
|
1799
|
|
|
|
|
|
|
|
|
1800
|
|
|
|
|
|
|
=head2 clump |
|
1801
|
|
|
|
|
|
|
|
|
1802
|
|
|
|
|
|
|
=for ref |
|
1803
|
|
|
|
|
|
|
|
|
1804
|
|
|
|
|
|
|
"clumps" several dimensions into one large dimension |
|
1805
|
|
|
|
|
|
|
|
|
1806
|
|
|
|
|
|
|
If called with one argument C<$n> clumps the first C<$n> |
|
1807
|
|
|
|
|
|
|
dimensions into one. For example, if C<$x> has dimensions |
|
1808
|
|
|
|
|
|
|
C<(5,3,4)> then after |
|
1809
|
|
|
|
|
|
|
|
|
1810
|
|
|
|
|
|
|
=for example |
|
1811
|
|
|
|
|
|
|
|
|
1812
|
|
|
|
|
|
|
$y = $x->clump(2); # Clump 2 first dimensions |
|
1813
|
|
|
|
|
|
|
|
|
1814
|
|
|
|
|
|
|
the variable C<$y> will have dimensions C<(15,4)> |
|
1815
|
|
|
|
|
|
|
and the element C<$y-Eat(7,3)> refers to the element |
|
1816
|
|
|
|
|
|
|
C<$x-Eat(1,2,3)>. |
|
1817
|
|
|
|
|
|
|
|
|
1818
|
|
|
|
|
|
|
Use C to flatten an ndarray. The method L |
|
1819
|
|
|
|
|
|
|
is provided as a convenient alias. |
|
1820
|
|
|
|
|
|
|
|
|
1821
|
|
|
|
|
|
|
Clumping with a negative dimension in general leaves that many |
|
1822
|
|
|
|
|
|
|
dimensions behind -- e.g. clump(-2) clumps all of the first few |
|
1823
|
|
|
|
|
|
|
dimensions into a single one, leaving a 2-D ndarray. |
|
1824
|
|
|
|
|
|
|
|
|
1825
|
|
|
|
|
|
|
If C is called with an index list with more than one element |
|
1826
|
|
|
|
|
|
|
it is treated as a list of dimensions that should be clumped together |
|
1827
|
|
|
|
|
|
|
into one. The resulting |
|
1828
|
|
|
|
|
|
|
clumped dim is placed at the position of the lowest index in the list. |
|
1829
|
|
|
|
|
|
|
This convention ensures that C does the expected thing in |
|
1830
|
|
|
|
|
|
|
the usual cases. The following examples demonstrate usage: |
|
1831
|
|
|
|
|
|
|
|
|
1832
|
|
|
|
|
|
|
zeroes(2,3,5,7)->clump(-1)->info # PDL: Double D [210] |
|
1833
|
|
|
|
|
|
|
zeroes(2,3,5,7)->clump(-2)->info # PDL: Double D [30,7] |
|
1834
|
|
|
|
|
|
|
zeroes(2,3,5,7)->clump(2)->info # PDL: Double D [6,5,7] |
|
1835
|
|
|
|
|
|
|
zeroes(2,3,5,7)->clump(3)->info # PDL: Double D [30,7] |
|
1836
|
|
|
|
|
|
|
zeroes(2,3,5,7)->clump(1,2)->info # PDL: Double D [2,15,7] |
|
1837
|
|
|
|
|
|
|
zeroes(2,3,5,7)->clump(1,3)->info # PDL: Double D [2,21,5] |
|
1838
|
|
|
|
|
|
|
|
|
1839
|
|
|
|
|
|
|
Data flows back and forth as usual with slicing routines. |
|
1840
|
|
|
|
|
|
|
|
|
1841
|
|
|
|
|
|
|
=cut |
|
1842
|
|
|
|
|
|
|
|
|
1843
|
|
|
|
|
|
|
sub PDL::clump :lvalue { |
|
1844
|
11912
|
100
|
|
11912
|
0
|
391610
|
goto &PDL::_clump_int if @_ < 3; |
|
1845
|
23
|
|
|
|
|
71
|
my ($this,@dims) = @_; |
|
1846
|
23
|
|
|
|
|
139
|
my $ndims = $this->getndims; |
|
1847
|
23
|
|
|
|
|
47
|
my $targd = $ndims-1; |
|
1848
|
23
|
|
|
|
|
68
|
my @dimmark = (0..$ndims-1); |
|
1849
|
23
|
50
|
|
|
|
72
|
barf "too many dimensions" if @dims > $ndims; |
|
1850
|
23
|
|
|
|
|
55
|
for my $dim (@dims) { |
|
1851
|
46
|
50
|
|
|
|
122
|
barf "dimension index $dim larger than greatest dimension" |
|
1852
|
|
|
|
|
|
|
if $dim > $ndims-1 ; |
|
1853
|
46
|
100
|
|
|
|
103
|
$targd = $dim if $targd > $dim; |
|
1854
|
46
|
50
|
|
|
|
157
|
barf "duplicate dimension $dim" if $dimmark[$dim]++ > $dim; |
|
1855
|
|
|
|
|
|
|
} |
|
1856
|
23
|
|
|
|
|
92
|
my $clumped = $this->broadcast(@dims)->unbroadcast(0)->clump(scalar @dims); |
|
1857
|
23
|
100
|
|
|
|
224
|
$clumped = $clumped->mv(0,$targd) if $targd > 0; |
|
1858
|
23
|
|
|
|
|
119
|
return $clumped; |
|
1859
|
|
|
|
|
|
|
} |
|
1860
|
|
|
|
|
|
|
|
|
1861
|
|
|
|
|
|
|
=head2 broadcast_define |
|
1862
|
|
|
|
|
|
|
|
|
1863
|
|
|
|
|
|
|
=for ref |
|
1864
|
|
|
|
|
|
|
|
|
1865
|
|
|
|
|
|
|
define functions that support broadcasting at the perl level |
|
1866
|
|
|
|
|
|
|
|
|
1867
|
|
|
|
|
|
|
=for example |
|
1868
|
|
|
|
|
|
|
|
|
1869
|
|
|
|
|
|
|
broadcast_define 'tline(a(n);b(n))', over { |
|
1870
|
|
|
|
|
|
|
line $_[0], $_[1]; # make line compliant with broadcasting |
|
1871
|
|
|
|
|
|
|
}; |
|
1872
|
|
|
|
|
|
|
|
|
1873
|
|
|
|
|
|
|
|
|
1874
|
|
|
|
|
|
|
C provides some support for broadcasting (see |
|
1875
|
|
|
|
|
|
|
L) at the perl level. It allows you to do things for |
|
1876
|
|
|
|
|
|
|
which you normally would have resorted to PDL::PP (see L); |
|
1877
|
|
|
|
|
|
|
however, it is most useful to wrap existing perl functions so that the |
|
1878
|
|
|
|
|
|
|
new routine supports PDL broadcasting. |
|
1879
|
|
|
|
|
|
|
|
|
1880
|
|
|
|
|
|
|
C is used to define new I |
|
1881
|
|
|
|
|
|
|
functions. Its first argument is a symbolic repesentation of the new |
|
1882
|
|
|
|
|
|
|
function to be defined. The string is composed of the name of the new |
|
1883
|
|
|
|
|
|
|
function followed by its signature (see L and L) |
|
1884
|
|
|
|
|
|
|
in parentheses. The second argument is a subroutine that will be |
|
1885
|
|
|
|
|
|
|
called with the slices of the actual runtime arguments as specified by |
|
1886
|
|
|
|
|
|
|
its signature. Correct dimension sizes and minimal number of |
|
1887
|
|
|
|
|
|
|
dimensions for all arguments will be checked (assuming the rules of |
|
1888
|
|
|
|
|
|
|
PDL broadcasting, see L). |
|
1889
|
|
|
|
|
|
|
|
|
1890
|
|
|
|
|
|
|
The actual work is done by the C class which parses the signature |
|
1891
|
|
|
|
|
|
|
string, does runtime dimension checks and the routine C that |
|
1892
|
|
|
|
|
|
|
generates the loop over all appropriate slices of pdl arguments and creates |
|
1893
|
|
|
|
|
|
|
pdls as needed. |
|
1894
|
|
|
|
|
|
|
|
|
1895
|
|
|
|
|
|
|
Similar to C and its C option it is possible to |
|
1896
|
|
|
|
|
|
|
define the new function so that it accepts normal perl args as well as |
|
1897
|
|
|
|
|
|
|
ndarrays. You do this by using the C parameter in the |
|
1898
|
|
|
|
|
|
|
signature. The number of C specified will be passed |
|
1899
|
|
|
|
|
|
|
unaltered into the subroutine given as the second argument of |
|
1900
|
|
|
|
|
|
|
C. Let's illustrate this with an example: |
|
1901
|
|
|
|
|
|
|
|
|
1902
|
|
|
|
|
|
|
PDL::broadcast_define 'triangles(inda();indb();indc()), NOtherPars => 2', |
|
1903
|
|
|
|
|
|
|
PDL::over { |
|
1904
|
|
|
|
|
|
|
${$_[3]} .= $_[4].join(',',map {$_->at} @_[0..2]).",-1,\n"; |
|
1905
|
|
|
|
|
|
|
}; |
|
1906
|
|
|
|
|
|
|
|
|
1907
|
|
|
|
|
|
|
This defines a function C that takes 3 ndarrays as input |
|
1908
|
|
|
|
|
|
|
plus 2 arguments which are passed into the routine unaltered. This routine |
|
1909
|
|
|
|
|
|
|
is used to collect lists of indices into a perl scalar that is passed by |
|
1910
|
|
|
|
|
|
|
reference. Each line is preceded by a prefix passed as C<$_[4]>. Here is |
|
1911
|
|
|
|
|
|
|
typical usage: |
|
1912
|
|
|
|
|
|
|
|
|
1913
|
|
|
|
|
|
|
$txt = ''; |
|
1914
|
|
|
|
|
|
|
triangles(pdl(1,2,3),pdl(1),pdl(0),\$txt," "x10); |
|
1915
|
|
|
|
|
|
|
print $txt; |
|
1916
|
|
|
|
|
|
|
|
|
1917
|
|
|
|
|
|
|
resulting in the following output |
|
1918
|
|
|
|
|
|
|
|
|
1919
|
|
|
|
|
|
|
1,1,0,-1, |
|
1920
|
|
|
|
|
|
|
2,1,0,-1, |
|
1921
|
|
|
|
|
|
|
3,1,0,-1, |
|
1922
|
|
|
|
|
|
|
|
|
1923
|
|
|
|
|
|
|
which is used in |
|
1924
|
|
|
|
|
|
|
L |
|
1925
|
|
|
|
|
|
|
to generate VRML output. |
|
1926
|
|
|
|
|
|
|
|
|
1927
|
|
|
|
|
|
|
Currently, this is probably not much more than a POP (proof of principle) |
|
1928
|
|
|
|
|
|
|
but is hoped to be useful enough for some real life work. |
|
1929
|
|
|
|
|
|
|
|
|
1930
|
|
|
|
|
|
|
Check L for the format of the signature. Currently, the |
|
1931
|
|
|
|
|
|
|
C<[t]> qualifier and all type qualifiers are ignored. |
|
1932
|
|
|
|
|
|
|
|
|
1933
|
|
|
|
|
|
|
=cut |
|
1934
|
|
|
|
|
|
|
|
|
1935
|
6
|
|
|
6
|
0
|
1608
|
sub PDL::over (&) { $_[0] } |
|
1936
|
|
|
|
|
|
|
sub PDL::broadcast_define ($$) { |
|
1937
|
6
|
|
|
6
|
0
|
739
|
require PDL::PP::Signature; |
|
1938
|
6
|
|
|
|
|
27
|
my ($str,$sub) = @_; |
|
1939
|
6
|
|
|
|
|
12
|
my $others = 0; |
|
1940
|
6
|
100
|
|
|
|
82
|
if ($str =~ s/[,]*\s*NOtherPars\s*=>\s*([0-9]+)\s*[,]*//) {$others = $1} |
|
|
4
|
|
|
|
|
18
|
|
|
1941
|
6
|
50
|
|
|
|
85
|
barf "invalid string $str" unless $str =~ /\s*([^(]+)\((.+)\)\s*$/x; |
|
1942
|
6
|
|
|
|
|
24
|
my ($name,$sigstr) = ($1,$2); |
|
1943
|
6
|
50
|
|
|
|
14
|
print "defining '$name' with signature '$sigstr' and $others extra args\n" |
|
1944
|
|
|
|
|
|
|
if $PDL::debug; |
|
1945
|
6
|
|
|
|
|
38
|
my $sig = PDL::PP::Signature->new($sigstr); |
|
1946
|
6
|
|
|
|
|
11
|
my $args = @{$sig->names}; # number of ndarray arguments |
|
|
6
|
|
|
|
|
17
|
|
|
1947
|
6
|
50
|
|
|
|
13
|
barf "no ndarray args" if $args == 0; |
|
1948
|
6
|
|
|
|
|
7
|
$args--; |
|
1949
|
|
|
|
|
|
|
# TODO: $sig->dimcheck(@_) + proper creating generation |
|
1950
|
6
|
|
|
|
|
17
|
my $package = caller; |
|
1951
|
6
|
50
|
|
|
|
14
|
print "defining... $name\n" if $PDL::debug; |
|
1952
|
71
|
|
|
71
|
|
682
|
no strict 'refs'; |
|
|
71
|
|
|
|
|
204
|
|
|
|
71
|
|
|
|
|
53340
|
|
|
1953
|
6
|
|
|
|
|
56
|
*{"$package\::$name"} = sub { |
|
1954
|
12
|
|
|
12
|
|
400
|
@_[0..$args] = map PDL::Core::topdl($_), @_[0..$args]; |
|
1955
|
12
|
|
|
|
|
85
|
$sig->checkdims(@_); |
|
1956
|
10
|
|
|
|
|
43
|
PDL::broadcastover($sub,$sig->realdims,$sig->creating,$others,@_); |
|
1957
|
6
|
|
|
|
|
50
|
}; |
|
1958
|
|
|
|
|
|
|
} |
|
1959
|
|
|
|
|
|
|
|
|
1960
|
|
|
|
|
|
|
=head2 broadcast |
|
1961
|
|
|
|
|
|
|
|
|
1962
|
|
|
|
|
|
|
=for ref |
|
1963
|
|
|
|
|
|
|
|
|
1964
|
|
|
|
|
|
|
Use explicit broadcasting over specified dimensions (see also L) |
|
1965
|
|
|
|
|
|
|
|
|
1966
|
|
|
|
|
|
|
=for usage |
|
1967
|
|
|
|
|
|
|
|
|
1968
|
|
|
|
|
|
|
$y = $x->broadcast($dim,[$dim1,...]) |
|
1969
|
|
|
|
|
|
|
|
|
1970
|
|
|
|
|
|
|
=for example |
|
1971
|
|
|
|
|
|
|
|
|
1972
|
|
|
|
|
|
|
$x = zeroes 3,4,5; |
|
1973
|
|
|
|
|
|
|
$y = $x->broadcast(2,0); |
|
1974
|
|
|
|
|
|
|
print $y->info; # PDL: Double D [4] T1 [5,3] |
|
1975
|
|
|
|
|
|
|
$pb = zeroes(3,3); |
|
1976
|
|
|
|
|
|
|
print $pb->broadcast(0,1)->info; # PDL: Double D [] T1 [3,3] |
|
1977
|
|
|
|
|
|
|
print $pb->broadcast(0)->info; # 'PDL: Double D [3] T1 [3] |
|
1978
|
|
|
|
|
|
|
print zeroes(4,7,2,8)->broadcast(2)->info; # PDL: Double D [4,7,8] T1 [2] |
|
1979
|
|
|
|
|
|
|
print zeroes(4,7,2,8)->broadcast(2,1)->info; # PDL: Double D [4,8] T1 [2,7] |
|
1980
|
|
|
|
|
|
|
print zeroes(4,7,2,8,5,6)->broadcast(2,4)->info; # PDL: Double D [4,7,8,6] T1 [2,5] |
|
1981
|
|
|
|
|
|
|
print zeroes(4,7,2,8,5,6)->broadcast1(2)->broadcast2(3)->info; # PDL: Double D [4,7,8,6] T1 [2] T2 [5] |
|
1982
|
|
|
|
|
|
|
|
|
1983
|
|
|
|
|
|
|
Same as L, i.e. uses broadcast id 1. |
|
1984
|
|
|
|
|
|
|
To use broadcast id 2, use L, or L |
|
1985
|
|
|
|
|
|
|
directly. |
|
1986
|
|
|
|
|
|
|
|
|
1987
|
|
|
|
|
|
|
=cut |
|
1988
|
|
|
|
|
|
|
|
|
1989
|
|
|
|
|
|
|
sub PDL::broadcast :lvalue { |
|
1990
|
54
|
|
|
54
|
0
|
588
|
my $var = shift; |
|
1991
|
54
|
|
|
|
|
1504
|
$var->broadcastI(1,\@_); |
|
1992
|
|
|
|
|
|
|
} |
|
1993
|
|
|
|
|
|
|
|
|
1994
|
|
|
|
|
|
|
=head2 broadcast1 |
|
1995
|
|
|
|
|
|
|
|
|
1996
|
|
|
|
|
|
|
=for ref |
|
1997
|
|
|
|
|
|
|
|
|
1998
|
|
|
|
|
|
|
Explicit broadcasting over specified dims using broadcast id 1. |
|
1999
|
|
|
|
|
|
|
|
|
2000
|
|
|
|
|
|
|
=for usage |
|
2001
|
|
|
|
|
|
|
|
|
2002
|
|
|
|
|
|
|
$xx = $x->broadcast1(3,1) |
|
2003
|
|
|
|
|
|
|
|
|
2004
|
|
|
|
|
|
|
Convenience function interfacing to |
|
2005
|
|
|
|
|
|
|
L. |
|
2006
|
|
|
|
|
|
|
|
|
2007
|
|
|
|
|
|
|
=cut |
|
2008
|
|
|
|
|
|
|
|
|
2009
|
|
|
|
|
|
|
sub PDL::broadcast1 { |
|
2010
|
1
|
|
|
1
|
0
|
3
|
my $var = shift; |
|
2011
|
1
|
|
|
|
|
16
|
$var->broadcastI(1,\@_); |
|
2012
|
|
|
|
|
|
|
} |
|
2013
|
|
|
|
|
|
|
|
|
2014
|
|
|
|
|
|
|
=head2 broadcast2 |
|
2015
|
|
|
|
|
|
|
|
|
2016
|
|
|
|
|
|
|
=for ref |
|
2017
|
|
|
|
|
|
|
|
|
2018
|
|
|
|
|
|
|
Explicit broadcasting over specified dims using broadcast id 2. |
|
2019
|
|
|
|
|
|
|
|
|
2020
|
|
|
|
|
|
|
=for usage |
|
2021
|
|
|
|
|
|
|
|
|
2022
|
|
|
|
|
|
|
$xx = $x->broadcast2(3,1) |
|
2023
|
|
|
|
|
|
|
|
|
2024
|
|
|
|
|
|
|
Convenience function interfacing to |
|
2025
|
|
|
|
|
|
|
L. |
|
2026
|
|
|
|
|
|
|
|
|
2027
|
|
|
|
|
|
|
=cut |
|
2028
|
|
|
|
|
|
|
|
|
2029
|
|
|
|
|
|
|
sub PDL::broadcast2 { |
|
2030
|
1
|
|
|
1
|
0
|
2
|
my $var = shift; |
|
2031
|
1
|
|
|
|
|
10
|
$var->broadcastI(2,\@_); |
|
2032
|
|
|
|
|
|
|
} |
|
2033
|
|
|
|
|
|
|
|
|
2034
|
|
|
|
|
|
|
=head2 broadcast3 |
|
2035
|
|
|
|
|
|
|
|
|
2036
|
|
|
|
|
|
|
=for ref |
|
2037
|
|
|
|
|
|
|
|
|
2038
|
|
|
|
|
|
|
Explicit broadcasting over specified dims using broadcast id 3. |
|
2039
|
|
|
|
|
|
|
|
|
2040
|
|
|
|
|
|
|
=for usage |
|
2041
|
|
|
|
|
|
|
|
|
2042
|
|
|
|
|
|
|
$xx = $x->broadcast3(3,1) |
|
2043
|
|
|
|
|
|
|
|
|
2044
|
|
|
|
|
|
|
Convenience function interfacing to |
|
2045
|
|
|
|
|
|
|
L. |
|
2046
|
|
|
|
|
|
|
|
|
2047
|
|
|
|
|
|
|
=cut |
|
2048
|
|
|
|
|
|
|
|
|
2049
|
|
|
|
|
|
|
sub PDL::broadcast3 { |
|
2050
|
0
|
|
|
0
|
0
|
0
|
my $var = shift; |
|
2051
|
0
|
|
|
|
|
0
|
$var->broadcastI(3,\@_); |
|
2052
|
|
|
|
|
|
|
} |
|
2053
|
|
|
|
|
|
|
|
|
2054
|
|
|
|
|
|
|
my %info = ( |
|
2055
|
|
|
|
|
|
|
D => { |
|
2056
|
|
|
|
|
|
|
Name => 'Dimension', |
|
2057
|
|
|
|
|
|
|
Sub => \&PDL::Core::dimstr, |
|
2058
|
|
|
|
|
|
|
}, |
|
2059
|
|
|
|
|
|
|
T => { |
|
2060
|
|
|
|
|
|
|
Name => 'Type', |
|
2061
|
|
|
|
|
|
|
Sub => sub { return $_[0]->type->shortctype; }, |
|
2062
|
|
|
|
|
|
|
}, |
|
2063
|
|
|
|
|
|
|
S => { |
|
2064
|
|
|
|
|
|
|
Name => 'State', |
|
2065
|
|
|
|
|
|
|
Sub => sub { my $state = ''; |
|
2066
|
|
|
|
|
|
|
$state .= 'P' if $_[0]->allocated; |
|
2067
|
|
|
|
|
|
|
$state .= 'V' if $_[0]->vaffine && |
|
2068
|
|
|
|
|
|
|
!$_[0]->allocated; # apparently can be both? |
|
2069
|
|
|
|
|
|
|
$state .= '-' if $state eq ''; # lazy eval |
|
2070
|
|
|
|
|
|
|
$state .= 'C' if $_[0]->anychgd; |
|
2071
|
|
|
|
|
|
|
$state .= 'B' if $_[0]->badflag; |
|
2072
|
|
|
|
|
|
|
$state; |
|
2073
|
|
|
|
|
|
|
}, |
|
2074
|
|
|
|
|
|
|
}, |
|
2075
|
|
|
|
|
|
|
M => { |
|
2076
|
|
|
|
|
|
|
Name => 'Mem', |
|
2077
|
|
|
|
|
|
|
Sub => sub { my ($size,$unit) = ($_[0]->allocated ? |
|
2078
|
|
|
|
|
|
|
$_[0]->nelem* |
|
2079
|
|
|
|
|
|
|
PDL::howbig($_[0]->get_datatype)/1024 : 0, 'KB'); |
|
2080
|
|
|
|
|
|
|
if ($size > 0.01*1024) { $size /= 1024; |
|
2081
|
|
|
|
|
|
|
$unit = 'MB' }; |
|
2082
|
|
|
|
|
|
|
return sprintf "%6.2f%s",$size,$unit; |
|
2083
|
|
|
|
|
|
|
}, |
|
2084
|
|
|
|
|
|
|
}, |
|
2085
|
|
|
|
|
|
|
C => { |
|
2086
|
|
|
|
|
|
|
Name => 'Class', |
|
2087
|
|
|
|
|
|
|
Sub => sub { ref $_[0] } |
|
2088
|
|
|
|
|
|
|
}, |
|
2089
|
|
|
|
|
|
|
A => { |
|
2090
|
|
|
|
|
|
|
Name => 'Address', |
|
2091
|
71
|
|
|
71
|
|
692
|
Sub => sub { use Config; |
|
|
71
|
|
|
|
|
146
|
|
|
|
71
|
|
|
|
|
641880
|
|
|
2092
|
|
|
|
|
|
|
my $ivdformat = $Config{ivdformat}; |
|
2093
|
|
|
|
|
|
|
$ivdformat =~ s/"//g; |
|
2094
|
|
|
|
|
|
|
sprintf "%$ivdformat", $_[0]->address } |
|
2095
|
|
|
|
|
|
|
}, |
|
2096
|
|
|
|
|
|
|
); |
|
2097
|
|
|
|
|
|
|
|
|
2098
|
|
|
|
|
|
|
# print the dimension information about a pdl in some appropriate form |
|
2099
|
|
|
|
|
|
|
sub dimstr { |
|
2100
|
42
|
|
|
42
|
0
|
90
|
my $this = shift; |
|
2101
|
|
|
|
|
|
|
|
|
2102
|
42
|
|
|
|
|
269
|
my @dims = $this->dims; |
|
2103
|
42
|
|
|
|
|
202
|
my @ids = $this->broadcastids; |
|
2104
|
42
|
|
|
|
|
107
|
my ($nids,$i) = ($#ids,0); |
|
2105
|
42
|
|
|
|
|
210
|
my $dstr = 'D ['. join(',',@dims[0..($ids[0]-1)]) .']'; |
|
2106
|
42
|
100
|
|
|
|
170
|
if ($nids > 0) { |
|
2107
|
6
|
|
|
|
|
21
|
for $i (1..$nids) { |
|
2108
|
7
|
|
|
|
|
39
|
$dstr .= " T$i [". join(',',@dims[$ids[$i-1]..$ids[$i]-1]) .']'; |
|
2109
|
|
|
|
|
|
|
} |
|
2110
|
|
|
|
|
|
|
} |
|
2111
|
42
|
|
|
|
|
136
|
return $dstr; |
|
2112
|
|
|
|
|
|
|
} |
|
2113
|
|
|
|
|
|
|
|
|
2114
|
|
|
|
|
|
|
=head2 sever |
|
2115
|
|
|
|
|
|
|
|
|
2116
|
|
|
|
|
|
|
=for ref |
|
2117
|
|
|
|
|
|
|
|
|
2118
|
|
|
|
|
|
|
sever any links of this ndarray to parent ndarrays |
|
2119
|
|
|
|
|
|
|
|
|
2120
|
|
|
|
|
|
|
In PDL it is possible for an ndarray to be just another |
|
2121
|
|
|
|
|
|
|
view into another ndarray's data. In that case we call |
|
2122
|
|
|
|
|
|
|
this ndarray a I and the original ndarray owning |
|
2123
|
|
|
|
|
|
|
the data its parent. In other languages these alternate views |
|
2124
|
|
|
|
|
|
|
sometimes run by names such as I or I. |
|
2125
|
|
|
|
|
|
|
|
|
2126
|
|
|
|
|
|
|
Typical functions that return such ndarrays are C, C, |
|
2127
|
|
|
|
|
|
|
C, etc. Sometimes, however, you would like to separate the |
|
2128
|
|
|
|
|
|
|
I from its parent's data and just give it a life of |
|
2129
|
|
|
|
|
|
|
its own (so that manipulation of its data doesn't change the parent). |
|
2130
|
|
|
|
|
|
|
This is simply achieved by using C. For example, |
|
2131
|
|
|
|
|
|
|
|
|
2132
|
|
|
|
|
|
|
=for example |
|
2133
|
|
|
|
|
|
|
|
|
2134
|
|
|
|
|
|
|
$x = $pdl->index(pdl(0,3,7))->sever; |
|
2135
|
|
|
|
|
|
|
$x++; # important: $pdl is not modified! |
|
2136
|
|
|
|
|
|
|
|
|
2137
|
|
|
|
|
|
|
In many (but not all) circumstances it acts therefore similar to |
|
2138
|
|
|
|
|
|
|
L. |
|
2139
|
|
|
|
|
|
|
However, in general performance is better with C and secondly, |
|
2140
|
|
|
|
|
|
|
C doesn't lead to futile copying when used on ndarrays that |
|
2141
|
|
|
|
|
|
|
already have their own data. On the other hand, if you really want to make |
|
2142
|
|
|
|
|
|
|
sure to work on a copy of an ndarray use L. |
|
2143
|
|
|
|
|
|
|
|
|
2144
|
|
|
|
|
|
|
$x = zeroes(20); |
|
2145
|
|
|
|
|
|
|
$x->sever; # NOOP since $x is already its own boss! |
|
2146
|
|
|
|
|
|
|
|
|
2147
|
|
|
|
|
|
|
Again note: C I the same as L! |
|
2148
|
|
|
|
|
|
|
For example, |
|
2149
|
|
|
|
|
|
|
|
|
2150
|
|
|
|
|
|
|
$x = zeroes(1); # $x does not have a parent, i.e. it is not a slice etc |
|
2151
|
|
|
|
|
|
|
$y = $x->sever; # $y is now pointing to the same ndarray as $x |
|
2152
|
|
|
|
|
|
|
$y++; |
|
2153
|
|
|
|
|
|
|
print $x; |
|
2154
|
|
|
|
|
|
|
[1] |
|
2155
|
|
|
|
|
|
|
|
|
2156
|
|
|
|
|
|
|
but |
|
2157
|
|
|
|
|
|
|
|
|
2158
|
|
|
|
|
|
|
$x = zeroes(1); |
|
2159
|
|
|
|
|
|
|
$y = $x->copy; # $y is now pointing to a new ndarray |
|
2160
|
|
|
|
|
|
|
$y++; |
|
2161
|
|
|
|
|
|
|
print $x; |
|
2162
|
|
|
|
|
|
|
[0] |
|
2163
|
|
|
|
|
|
|
|
|
2164
|
|
|
|
|
|
|
|
|
2165
|
|
|
|
|
|
|
=head2 info |
|
2166
|
|
|
|
|
|
|
|
|
2167
|
|
|
|
|
|
|
=for ref |
|
2168
|
|
|
|
|
|
|
|
|
2169
|
|
|
|
|
|
|
Return formatted information about an ndarray. |
|
2170
|
|
|
|
|
|
|
|
|
2171
|
|
|
|
|
|
|
=for usage |
|
2172
|
|
|
|
|
|
|
|
|
2173
|
|
|
|
|
|
|
$x->info($format_string); |
|
2174
|
|
|
|
|
|
|
|
|
2175
|
|
|
|
|
|
|
=for example |
|
2176
|
|
|
|
|
|
|
|
|
2177
|
|
|
|
|
|
|
print $x->info("Type: %T Dim: %-15D State: %S"); |
|
2178
|
|
|
|
|
|
|
|
|
2179
|
|
|
|
|
|
|
Returns a string with info about an ndarray. Takes an optional |
|
2180
|
|
|
|
|
|
|
argument to specify the format of information a la sprintf. |
|
2181
|
|
|
|
|
|
|
Format specifiers are in the form C<%EwidthEEletterE> |
|
2182
|
|
|
|
|
|
|
where the width is optional and the letter is one of |
|
2183
|
|
|
|
|
|
|
|
|
2184
|
|
|
|
|
|
|
=over 7 |
|
2185
|
|
|
|
|
|
|
|
|
2186
|
|
|
|
|
|
|
=item T |
|
2187
|
|
|
|
|
|
|
|
|
2188
|
|
|
|
|
|
|
Type |
|
2189
|
|
|
|
|
|
|
|
|
2190
|
|
|
|
|
|
|
=item D |
|
2191
|
|
|
|
|
|
|
|
|
2192
|
|
|
|
|
|
|
Formatted Dimensions |
|
2193
|
|
|
|
|
|
|
|
|
2194
|
|
|
|
|
|
|
=item F |
|
2195
|
|
|
|
|
|
|
|
|
2196
|
|
|
|
|
|
|
Dataflow status |
|
2197
|
|
|
|
|
|
|
|
|
2198
|
|
|
|
|
|
|
=item S |
|
2199
|
|
|
|
|
|
|
|
|
2200
|
|
|
|
|
|
|
Some internal flags (P=physical,V=Vaffine,C=changed,B=may contain bad data) |
|
2201
|
|
|
|
|
|
|
|
|
2202
|
|
|
|
|
|
|
=item C |
|
2203
|
|
|
|
|
|
|
|
|
2204
|
|
|
|
|
|
|
Class of this ndarray, i.e. C[ ] |
|
2205
|
|
|
|
|
|
|
|
|
2206
|
|
|
|
|
|
|
=item A |
|
2207
|
|
|
|
|
|
|
|
|
2208
|
|
|
|
|
|
|
Address of the ndarray struct as a unique identifier |
|
2209
|
|
|
|
|
|
|
|
|
2210
|
|
|
|
|
|
|
=item M |
|
2211
|
|
|
|
|
|
|
|
|
2212
|
|
|
|
|
|
|
Calculated memory consumption of this ndarray's data area |
|
2213
|
|
|
|
|
|
|
|
|
2214
|
|
|
|
|
|
|
=back |
|
2215
|
|
|
|
|
|
|
|
|
2216
|
|
|
|
|
|
|
The default format is C<"%C: %T %D">. |
|
2217
|
|
|
|
|
|
|
This can be modified by assigning to C<$PDL::infoformat>. |
|
2218
|
|
|
|
|
|
|
=cut |
|
2219
|
|
|
|
|
|
|
|
|
2220
|
|
|
|
|
|
|
sub PDL::info { |
|
2221
|
89
|
|
|
89
|
0
|
10152617
|
my ($this,$str) = @_; |
|
2222
|
89
|
100
|
|
|
|
301
|
$str = $PDL::infoformat unless defined $str; |
|
2223
|
89
|
100
|
|
|
|
427
|
return ref($this)."->null" if $this->isnull; |
|
2224
|
88
|
|
|
|
|
1001
|
my @hash = split /(%[-,0-9]*[.]?[0-9]*\w)/, $str; |
|
2225
|
88
|
|
|
|
|
220
|
my @args = (); |
|
2226
|
88
|
|
|
|
|
226
|
my $nstr = ''; |
|
2227
|
88
|
|
|
|
|
232
|
for my $form (@hash) { |
|
2228
|
375
|
100
|
|
|
|
2003
|
if ($form =~ s/^%([-,0-9]*[.]?[0-9]*)(\w)$/%$1s/) { |
|
2229
|
172
|
50
|
|
|
|
732
|
barf "unknown format specifier $2" unless defined $info{$2}; |
|
2230
|
172
|
|
|
|
|
332
|
push @args, &{$info{$2}->{Sub}}($this); |
|
|
172
|
|
|
|
|
619
|
|
|
2231
|
|
|
|
|
|
|
} |
|
2232
|
375
|
|
|
|
|
819
|
$nstr .= $form; |
|
2233
|
|
|
|
|
|
|
} |
|
2234
|
88
|
|
|
|
|
1197
|
return sprintf $nstr, @args; |
|
2235
|
|
|
|
|
|
|
} |
|
2236
|
|
|
|
|
|
|
|
|
2237
|
|
|
|
|
|
|
=head2 pdump |
|
2238
|
|
|
|
|
|
|
|
|
2239
|
|
|
|
|
|
|
=for ref |
|
2240
|
|
|
|
|
|
|
|
|
2241
|
|
|
|
|
|
|
Returns a close analogue of the output of C<< $pdl->dump >> as a |
|
2242
|
|
|
|
|
|
|
string. Like that C function, it will not cause any physicalisation of |
|
2243
|
|
|
|
|
|
|
the ndarray. |
|
2244
|
|
|
|
|
|
|
|
|
2245
|
|
|
|
|
|
|
Not exported, and not inserted into the C namespace. |
|
2246
|
|
|
|
|
|
|
|
|
2247
|
|
|
|
|
|
|
=for example |
|
2248
|
|
|
|
|
|
|
|
|
2249
|
|
|
|
|
|
|
print PDL::Core::pdump($pdl); |
|
2250
|
|
|
|
|
|
|
|
|
2251
|
|
|
|
|
|
|
=cut |
|
2252
|
|
|
|
|
|
|
|
|
2253
|
|
|
|
|
|
|
sub pdump { |
|
2254
|
2
|
|
|
2
|
1
|
5169
|
my ($pdl) = @_; |
|
2255
|
2
|
|
|
|
|
14
|
my @dims = $pdl->dims_nophys; |
|
2256
|
2
|
|
|
|
|
9
|
my $svaddr = $pdl->address_datasv; |
|
2257
|
2
|
100
|
|
|
|
5
|
my @lines = ( |
|
2258
|
2
|
|
|
|
|
20
|
"State: ${\join '|', $pdl->flags}", |
|
2259
|
|
|
|
|
|
|
"Dims: (@dims)", |
|
2260
|
2
|
|
|
|
|
77
|
"BroadcastIds: (@{[$pdl->broadcastids_nophys]})", |
|
2261
|
|
|
|
|
|
|
!$svaddr ? () : sprintf("datasv: 0x%x, refcnt: %d", $svaddr, $pdl->datasv_refcount), |
|
2262
|
|
|
|
|
|
|
sprintf("data: 0x%x, nbytes: %d, nvals: %d", $pdl->address_data, $pdl->nbytes, $pdl->nelem_nophys), |
|
2263
|
|
|
|
|
|
|
); |
|
2264
|
2
|
50
|
|
|
|
13
|
push @lines, sprintf "Vaffine: 0x%x (parent)", $pdl->vaffine_from if $pdl->has_vafftrans; |
|
2265
|
2
|
0
|
|
|
|
25
|
push @lines, !$pdl->badflag ? () : ( |
|
|
|
50
|
|
|
|
|
|
|
2266
|
|
|
|
|
|
|
"Badvalue (".($pdl->has_badvalue ? 'bespoke' : 'orig')."): " . $pdl->badvalue |
|
2267
|
|
|
|
|
|
|
); |
|
2268
|
2
|
100
|
|
|
|
10
|
push @lines, !$pdl->allocated ? '(not allocated)' : |
|
2269
|
1
|
|
|
|
|
108
|
"First values: (@{[$pdl->firstvals_nophys]})", |
|
2270
|
|
|
|
|
|
|
; |
|
2271
|
2
|
100
|
|
|
|
602
|
if (my $trans = $pdl->trans_parent) { |
|
2272
|
1
|
|
|
|
|
4
|
push @lines, grep length, split "\n", pdump_trans($trans); |
|
2273
|
|
|
|
|
|
|
} |
|
2274
|
2
|
50
|
|
|
|
15
|
if (my @trans_children = $pdl->trans_children) { |
|
2275
|
0
|
|
|
|
|
0
|
push @lines, "CHILDREN:"; |
|
2276
|
0
|
|
|
|
|
0
|
push @lines, map " $_", grep length, split "\n", pdump_trans($_) for @trans_children; |
|
2277
|
|
|
|
|
|
|
} |
|
2278
|
2
|
|
|
|
|
3
|
join '', "PDUMPING 0x${\sprintf '%x', $pdl->address}, datatype: ${\$pdl->get_datatype}\n", map " $_\n", @lines; |
|
|
2
|
|
|
|
|
13
|
|
|
|
2
|
|
|
|
|
40
|
|
|
2279
|
|
|
|
|
|
|
} |
|
2280
|
|
|
|
|
|
|
|
|
2281
|
|
|
|
|
|
|
=head2 pdump_trans |
|
2282
|
|
|
|
|
|
|
|
|
2283
|
|
|
|
|
|
|
=for ref |
|
2284
|
|
|
|
|
|
|
|
|
2285
|
|
|
|
|
|
|
Returns a string representation of a C object, a close |
|
2286
|
|
|
|
|
|
|
analogue of part of the output of C<< $pdl->dump >>. |
|
2287
|
|
|
|
|
|
|
|
|
2288
|
|
|
|
|
|
|
Not exported, and not inserted into the C namespace. |
|
2289
|
|
|
|
|
|
|
|
|
2290
|
|
|
|
|
|
|
=for example |
|
2291
|
|
|
|
|
|
|
|
|
2292
|
|
|
|
|
|
|
print PDL::Core::pdump_trans($pdl_trans); |
|
2293
|
|
|
|
|
|
|
|
|
2294
|
|
|
|
|
|
|
=cut |
|
2295
|
|
|
|
|
|
|
|
|
2296
|
|
|
|
|
|
|
sub pdump_trans { |
|
2297
|
2
|
|
|
2
|
1
|
6
|
my ($trans) = @_; |
|
2298
|
2
|
|
|
|
|
13
|
my $vtable = $trans->vtable; |
|
2299
|
2
|
|
|
|
|
3
|
my @lines = ( |
|
2300
|
2
|
|
|
|
|
17
|
"State: ${\join '|', $trans->flags}", |
|
2301
|
2
|
|
|
|
|
11
|
"bvalflag: ${\$trans->bvalflag}", |
|
2302
|
2
|
|
|
|
|
12
|
"vtable flags: ${\join '|', $vtable->flags}", |
|
2303
|
|
|
|
|
|
|
); |
|
2304
|
2
|
|
|
|
|
11
|
my @ins = $trans->parents; |
|
2305
|
2
|
|
|
|
|
8
|
my @outs = $trans->children; |
|
2306
|
2
|
50
|
|
|
|
53
|
push @lines, |
|
|
|
50
|
|
|
|
|
|
|
2307
|
|
|
|
|
|
|
"AFFINE, " . ($outs[0]->dimschgd |
|
2308
|
|
|
|
|
|
|
? "BUT DIMSCHANGED" |
|
2309
|
0
|
|
|
|
|
0
|
: "o:".$trans->offs." i:(@{[$trans->incs]}) d:(@{[$outs[0]->dims_nophys]})") |
|
|
0
|
|
|
|
|
0
|
|
|
2310
|
|
|
|
|
|
|
if $trans->affine; |
|
2311
|
2
|
|
|
|
|
7
|
push @lines, |
|
2312
|
2
|
|
|
|
|
11
|
"ind_sizes: (@{[$trans->ind_sizes]})", |
|
2313
|
2
|
|
|
|
|
9
|
"inc_sizes: (@{[$trans->inc_sizes]})", |
|
2314
|
2
|
|
|
|
|
12
|
"trans_children_indices: (@{[$trans->trans_children_indices]})", |
|
2315
|
2
|
|
|
|
|
19
|
"INPUTS: (@{[map sprintf('0x%x', $_->address), @ins]}) OUTPUTS: (@{[map sprintf('0x%x', $_->address), @outs]})", |
|
|
2
|
|
|
|
|
14
|
|
|
2316
|
|
|
|
|
|
|
; |
|
2317
|
2
|
|
|
|
|
6
|
join '', "PDUMPTRANS 0x${\sprintf '%x', $trans->address} (${\$vtable->name})\n", map " $_\n", @lines; |
|
|
2
|
|
|
|
|
11
|
|
|
|
2
|
|
|
|
|
77
|
|
|
2318
|
|
|
|
|
|
|
} |
|
2319
|
|
|
|
|
|
|
|
|
2320
|
|
|
|
|
|
|
=head2 pdumphash |
|
2321
|
|
|
|
|
|
|
|
|
2322
|
|
|
|
|
|
|
=for ref |
|
2323
|
|
|
|
|
|
|
|
|
2324
|
|
|
|
|
|
|
Returns a hash-ref representing the information about a given object |
|
2325
|
|
|
|
|
|
|
(C or ndarray) and all the objects of either type it is |
|
2326
|
|
|
|
|
|
|
connected to. Includes similar information to that shown by L |
|
2327
|
|
|
|
|
|
|
and L. |
|
2328
|
|
|
|
|
|
|
|
|
2329
|
|
|
|
|
|
|
Not exported, and not inserted into the C namespace. |
|
2330
|
|
|
|
|
|
|
|
|
2331
|
|
|
|
|
|
|
=for example |
|
2332
|
|
|
|
|
|
|
|
|
2333
|
|
|
|
|
|
|
$hashref = PDL::Core::pdumphash($pdl_trans); # or |
|
2334
|
|
|
|
|
|
|
$hashref = PDL::Core::pdumphash($pdl); |
|
2335
|
|
|
|
|
|
|
|
|
2336
|
|
|
|
|
|
|
=cut |
|
2337
|
|
|
|
|
|
|
|
|
2338
|
|
|
|
|
|
|
# only look at each obj once, mutates the hash |
|
2339
|
|
|
|
|
|
|
sub pdumphash { |
|
2340
|
12
|
|
|
12
|
1
|
620
|
my ($obj, $sofar) = @_; |
|
2341
|
12
|
50
|
|
|
|
35
|
confess "expected object but got '$obj'" if !ref $obj; |
|
2342
|
12
|
|
100
|
|
|
36
|
$sofar ||= {}; |
|
2343
|
12
|
|
|
|
|
51
|
my $addr = sprintf '0x%x', $obj->address; # both ndarray and trans |
|
2344
|
12
|
50
|
|
|
|
31
|
return $sofar if $sofar->{$addr}; |
|
2345
|
12
|
100
|
|
|
|
65
|
if ($obj->isa('PDL::Trans')) { |
|
2346
|
4
|
|
|
|
|
15
|
my $vtable = $obj->vtable; |
|
2347
|
4
|
|
|
|
|
39
|
my @ins = $obj->parents; |
|
2348
|
4
|
|
|
|
|
20
|
my @outs = $obj->children; |
|
2349
|
4
|
50
|
66
|
|
|
121
|
$sofar->{$addr} = { |
|
2350
|
|
|
|
|
|
|
kind => 'trans', |
|
2351
|
|
|
|
|
|
|
name => $vtable->name, |
|
2352
|
|
|
|
|
|
|
flags => [$obj->flags], |
|
2353
|
|
|
|
|
|
|
vtable_flags => [$vtable->flags], |
|
2354
|
|
|
|
|
|
|
par_names => [$vtable->par_names], |
|
2355
|
|
|
|
|
|
|
!($obj->affine && !$outs[0]->dimschgd) ? () : ( |
|
2356
|
0
|
|
|
|
|
0
|
affine => "o:".$obj->offs." i:(@{[$obj->incs]}) d:(@{[$outs[0]->dims_nophys]})" |
|
|
0
|
|
|
|
|
0
|
|
|
2357
|
|
|
|
|
|
|
), |
|
2358
|
|
|
|
|
|
|
ins => [map sprintf('0x%x', $_->address), @ins], |
|
2359
|
|
|
|
|
|
|
outs => [map sprintf('0x%x', $_->address), @outs], |
|
2360
|
|
|
|
|
|
|
}; |
|
2361
|
4
|
100
|
|
|
|
13
|
for my $r (@ins, @outs) { pdumphash($r, $sofar) if !$sofar->{sprintf '0x%x', $r->address}; } |
|
|
9
|
|
|
|
|
69
|
|
|
2362
|
|
|
|
|
|
|
} else { |
|
2363
|
8
|
|
|
|
|
36
|
my @ins = grep defined, $obj->trans_parent; |
|
2364
|
8
|
|
|
|
|
30
|
my @outs = $obj->trans_children; |
|
2365
|
8
|
50
|
|
|
|
182
|
$sofar->{$addr} = { |
|
|
|
100
|
|
|
|
|
|
|
2366
|
|
|
|
|
|
|
kind => 'ndarray', |
|
2367
|
|
|
|
|
|
|
datatype => $obj->get_datatype, |
|
2368
|
|
|
|
|
|
|
flags => [$obj->flags], |
|
2369
|
|
|
|
|
|
|
!$obj->has_vafftrans ? () : ( |
|
2370
|
|
|
|
|
|
|
vaffine_from => sprintf("0x%x", $obj->vaffine_from), |
|
2371
|
|
|
|
|
|
|
), |
|
2372
|
|
|
|
|
|
|
!$obj->allocated ? () : ( |
|
2373
|
|
|
|
|
|
|
data => sprintf("0x%x", $obj->address_data), |
|
2374
|
|
|
|
|
|
|
nbytes => $obj->nbytes, |
|
2375
|
|
|
|
|
|
|
nelem_nophys => $obj->nelem_nophys, |
|
2376
|
|
|
|
|
|
|
firstvals => [$obj->firstvals_nophys], |
|
2377
|
|
|
|
|
|
|
), |
|
2378
|
|
|
|
|
|
|
ins => [map sprintf('0x%x', $_->address), @ins], |
|
2379
|
|
|
|
|
|
|
outs => [map sprintf('0x%x', $_->address), @outs], |
|
2380
|
|
|
|
|
|
|
}; |
|
2381
|
8
|
100
|
|
|
|
27
|
for my $r (@ins, @outs) { pdumphash($r, $sofar) if !$sofar->{sprintf '0x%x', $r->address}; } |
|
|
9
|
|
|
|
|
53
|
|
|
2382
|
|
|
|
|
|
|
} |
|
2383
|
12
|
|
|
|
|
42
|
$sofar; |
|
2384
|
|
|
|
|
|
|
} |
|
2385
|
|
|
|
|
|
|
|
|
2386
|
|
|
|
|
|
|
=head2 pdumpgraph |
|
2387
|
|
|
|
|
|
|
|
|
2388
|
|
|
|
|
|
|
=for ref |
|
2389
|
|
|
|
|
|
|
|
|
2390
|
|
|
|
|
|
|
Given a hash-ref returned by L, returns a L object |
|
2391
|
|
|
|
|
|
|
representing the same information. |
|
2392
|
|
|
|
|
|
|
|
|
2393
|
|
|
|
|
|
|
Not exported, and not inserted into the C namespace. |
|
2394
|
|
|
|
|
|
|
|
|
2395
|
|
|
|
|
|
|
=for example |
|
2396
|
|
|
|
|
|
|
|
|
2397
|
|
|
|
|
|
|
$g = PDL::Core::pdumphash($hashref); |
|
2398
|
|
|
|
|
|
|
|
|
2399
|
|
|
|
|
|
|
=cut |
|
2400
|
|
|
|
|
|
|
|
|
2401
|
|
|
|
|
|
|
sub pdumpgraph { |
|
2402
|
0
|
|
|
0
|
1
|
0
|
my ($hash) = @_; |
|
2403
|
0
|
|
|
|
|
0
|
require Graph; |
|
2404
|
0
|
|
|
|
|
0
|
my $g = Graph->new(multiedged=>1); |
|
2405
|
0
|
|
|
|
|
0
|
for my $addr (keys %$hash) { |
|
2406
|
0
|
|
|
|
|
0
|
$g->set_vertex_attributes($addr, my $props = $hash->{$addr}); |
|
2407
|
0
|
0
|
|
|
|
0
|
if ($props->{kind} eq 'trans') { |
|
2408
|
0
|
|
|
|
|
0
|
my ($ins, $outs) = @$props{qw(ins outs)}; |
|
2409
|
0
|
|
|
|
|
0
|
$g->add_edge_by_id($ins->[$_], $addr, $_) for 0..$#$ins; |
|
2410
|
0
|
|
|
|
|
0
|
$g->add_edge_by_id($addr, $outs->[$_], $_) for 0..$#$outs; |
|
2411
|
|
|
|
|
|
|
} |
|
2412
|
0
|
0
|
|
|
|
0
|
if (my $from = $props->{vaffine_from}) { |
|
2413
|
0
|
|
|
|
|
0
|
$g->add_edge_by_id($addr, $from, 'vaffine_from'); |
|
2414
|
|
|
|
|
|
|
} |
|
2415
|
|
|
|
|
|
|
} |
|
2416
|
0
|
|
|
|
|
0
|
$g; |
|
2417
|
|
|
|
|
|
|
} |
|
2418
|
|
|
|
|
|
|
|
|
2419
|
|
|
|
|
|
|
=head2 pdumpgraphvizify |
|
2420
|
|
|
|
|
|
|
|
|
2421
|
|
|
|
|
|
|
=for ref |
|
2422
|
|
|
|
|
|
|
|
|
2423
|
|
|
|
|
|
|
Given a L object returned by L, modifies it suitable |
|
2424
|
|
|
|
|
|
|
for input to L, then returns it. See example for |
|
2425
|
|
|
|
|
|
|
how to use. |
|
2426
|
|
|
|
|
|
|
|
|
2427
|
|
|
|
|
|
|
Not exported, and not inserted into the C namespace. |
|
2428
|
|
|
|
|
|
|
|
|
2429
|
|
|
|
|
|
|
=for example |
|
2430
|
|
|
|
|
|
|
|
|
2431
|
|
|
|
|
|
|
$g = PDL::Core::pdumpgraphvizify($g); |
|
2432
|
|
|
|
|
|
|
|
|
2433
|
|
|
|
|
|
|
# full example: |
|
2434
|
|
|
|
|
|
|
$count = 1; $format = 'png'; sub output { |
|
2435
|
|
|
|
|
|
|
$g = PDL::Core::pdumpgraph(PDL::Core::pdumphash($_[0])); |
|
2436
|
|
|
|
|
|
|
require GraphViz2; |
|
2437
|
|
|
|
|
|
|
$gv = GraphViz2->from_graph(PDL::Core::pdumpgraphvizify($g)); |
|
2438
|
|
|
|
|
|
|
$gv->run(format => $format, output_file => 'output'.$count++.".$format"); |
|
2439
|
|
|
|
|
|
|
} |
|
2440
|
|
|
|
|
|
|
# keep changing ndarray, then calling this to show each state: |
|
2441
|
|
|
|
|
|
|
output($pdl); |
|
2442
|
|
|
|
|
|
|
|
|
2443
|
|
|
|
|
|
|
# run the above script, then show the ndarray evolve over time, in a |
|
2444
|
|
|
|
|
|
|
# left-to-right montage using ImageMagick tools: |
|
2445
|
|
|
|
|
|
|
perl myscript.pl |
|
2446
|
|
|
|
|
|
|
montage output* -tile "$(echo output*|wc -w)"x1 -geometry '1x1<' final.png |
|
2447
|
|
|
|
|
|
|
display final.png |
|
2448
|
|
|
|
|
|
|
|
|
2449
|
|
|
|
|
|
|
=cut |
|
2450
|
|
|
|
|
|
|
|
|
2451
|
|
|
|
|
|
|
sub pdumpgraphvizify { |
|
2452
|
0
|
|
|
0
|
1
|
0
|
my ($g) = @_; |
|
2453
|
0
|
|
|
|
|
0
|
for my $v ($g->vertices) { |
|
2454
|
0
|
|
|
|
|
0
|
my $attrs = $g->get_vertex_attributes($v); |
|
2455
|
0
|
|
|
|
|
0
|
my $kind = $attrs->{kind}; |
|
2456
|
0
|
0
|
|
|
|
0
|
if (my $from = $attrs->{vaffine_from}) { |
|
2457
|
0
|
|
|
|
|
0
|
$g->set_edge_attribute_by_id( |
|
2458
|
|
|
|
|
|
|
$v, $from, 'vaffine_from', |
|
2459
|
|
|
|
|
|
|
graphviz => { style => 'dashed', constraint => 'false' }, |
|
2460
|
|
|
|
|
|
|
); |
|
2461
|
|
|
|
|
|
|
} |
|
2462
|
0
|
|
|
|
|
0
|
my @blocks = join '', map "$_\\l", @{$attrs->{flags}}; |
|
|
0
|
|
|
|
|
0
|
|
|
2463
|
0
|
0
|
|
|
|
0
|
if ($kind eq 'trans') { |
|
2464
|
0
|
|
|
|
|
0
|
my ($in_names, $out_names) = @{$attrs->{par_names}}[0,1]; |
|
|
0
|
|
|
|
|
0
|
|
|
2465
|
0
|
|
|
|
|
0
|
my ($ins, $outs) = @$attrs{qw(ins outs)}; |
|
2466
|
0
|
|
|
|
|
0
|
unshift @blocks, [map +{text=>$in_names->[$_],port=>"i$_"}, 0..$#$ins], $attrs->{name}; |
|
2467
|
|
|
|
|
|
|
$g->set_edge_attribute_by_id( |
|
2468
|
|
|
|
|
|
|
$ins->[$_], $v, $_, |
|
2469
|
|
|
|
|
|
|
graphviz => { headport => ["i$_","n"] }, |
|
2470
|
0
|
|
|
|
|
0
|
) for 0..$#$ins; |
|
2471
|
0
|
|
|
|
|
0
|
my @vflags = @{$attrs->{vtable_flags}}; |
|
|
0
|
|
|
|
|
0
|
|
|
2472
|
0
|
0
|
|
|
|
0
|
push @blocks, join '', map "$_\\l", @vflags ? @vflags : '(no vtable flags)'; |
|
2473
|
0
|
|
|
|
|
0
|
my $affine = $attrs->{affine}; |
|
2474
|
0
|
0
|
|
|
|
0
|
push @blocks, $affine if $affine; |
|
2475
|
0
|
|
|
|
|
0
|
push @blocks, [map +{text=>$out_names->[$_],port=>"o$_"}, 0..$#$outs]; |
|
2476
|
|
|
|
|
|
|
$g->set_edge_attribute_by_id( |
|
2477
|
|
|
|
|
|
|
$v, $outs->[$_], $_, |
|
2478
|
|
|
|
|
|
|
graphviz => { tailport => ["o$_","s"] }, |
|
2479
|
0
|
|
|
|
|
0
|
) for 0..$#$outs; |
|
2480
|
|
|
|
|
|
|
} else { |
|
2481
|
0
|
|
|
|
|
0
|
my $firstvals = $attrs->{firstvals}; |
|
2482
|
0
|
0
|
|
|
|
0
|
$firstvals = ", (".($firstvals ? "@$firstvals" : 'not allocated').")"; |
|
2483
|
0
|
|
|
|
|
0
|
push @blocks, "datatype: $attrs->{datatype}$firstvals"; |
|
2484
|
|
|
|
|
|
|
} |
|
2485
|
0
|
0
|
|
|
|
0
|
$g->set_vertex_attribute($v, graphviz => { |
|
2486
|
|
|
|
|
|
|
shape => 'record', |
|
2487
|
|
|
|
|
|
|
color => $kind eq 'ndarray' ? 'blue' : 'red', |
|
2488
|
|
|
|
|
|
|
label => [\@blocks], |
|
2489
|
|
|
|
|
|
|
}); |
|
2490
|
|
|
|
|
|
|
} |
|
2491
|
0
|
|
|
|
|
0
|
$g->set_graph_attribute(graphviz => { |
|
2492
|
|
|
|
|
|
|
global => {directed => 1, combine_node_and_port => 0}, |
|
2493
|
|
|
|
|
|
|
graph => {concentrate => 'true', rankdir => 'TB'}, |
|
2494
|
|
|
|
|
|
|
}); |
|
2495
|
0
|
|
|
|
|
0
|
$g; |
|
2496
|
|
|
|
|
|
|
} |
|
2497
|
|
|
|
|
|
|
|
|
2498
|
|
|
|
|
|
|
=head2 approx |
|
2499
|
|
|
|
|
|
|
|
|
2500
|
|
|
|
|
|
|
=for ref |
|
2501
|
|
|
|
|
|
|
|
|
2502
|
|
|
|
|
|
|
test for approximately equal values (relaxed C<==>) |
|
2503
|
|
|
|
|
|
|
|
|
2504
|
|
|
|
|
|
|
=for example |
|
2505
|
|
|
|
|
|
|
|
|
2506
|
|
|
|
|
|
|
# ok if all corresponding values in |
|
2507
|
|
|
|
|
|
|
# ndarrays are within 1e-8 of each other |
|
2508
|
|
|
|
|
|
|
print "ok\n" if all approx $x, $y, 1e-8; |
|
2509
|
|
|
|
|
|
|
|
|
2510
|
|
|
|
|
|
|
C is a relaxed form of the C<==> operator and |
|
2511
|
|
|
|
|
|
|
often more appropriate for floating point types (C |
|
2512
|
|
|
|
|
|
|
C and C). |
|
2513
|
|
|
|
|
|
|
|
|
2514
|
|
|
|
|
|
|
Usage: |
|
2515
|
|
|
|
|
|
|
|
|
2516
|
|
|
|
|
|
|
=for usage |
|
2517
|
|
|
|
|
|
|
|
|
2518
|
|
|
|
|
|
|
$res = approx $x, $y [, $eps] |
|
2519
|
|
|
|
|
|
|
|
|
2520
|
|
|
|
|
|
|
The optional parameter C<$eps> is remembered across invocations |
|
2521
|
|
|
|
|
|
|
and initially set to 1e-6, e.g. |
|
2522
|
|
|
|
|
|
|
|
|
2523
|
|
|
|
|
|
|
approx $x, $y; # last $eps used (1e-6 initially) |
|
2524
|
|
|
|
|
|
|
approx $x, $y, 1e-10; # 1e-10 |
|
2525
|
|
|
|
|
|
|
approx $x, $y; # also 1e-10 |
|
2526
|
|
|
|
|
|
|
|
|
2527
|
|
|
|
|
|
|
=cut |
|
2528
|
|
|
|
|
|
|
|
|
2529
|
|
|
|
|
|
|
my $approx = 1e-6; # a reasonable init value |
|
2530
|
|
|
|
|
|
|
sub PDL::approx { |
|
2531
|
15
|
|
|
15
|
0
|
937
|
my ($x,$y,$eps) = @_; |
|
2532
|
15
|
100
|
|
|
|
68
|
$eps = $approx unless defined $eps; # the default eps |
|
2533
|
15
|
|
|
|
|
29
|
$approx = $eps; # remember last eps |
|
2534
|
15
|
|
|
|
|
342
|
PDL->topdl($x)->approx_artol($y, $eps); |
|
2535
|
|
|
|
|
|
|
} |
|
2536
|
|
|
|
|
|
|
|
|
2537
|
|
|
|
|
|
|
=head2 mslice |
|
2538
|
|
|
|
|
|
|
|
|
2539
|
|
|
|
|
|
|
=for ref |
|
2540
|
|
|
|
|
|
|
|
|
2541
|
|
|
|
|
|
|
Alias to L. |
|
2542
|
|
|
|
|
|
|
|
|
2543
|
|
|
|
|
|
|
=cut |
|
2544
|
|
|
|
|
|
|
|
|
2545
|
|
|
|
|
|
|
*PDL::mslice = \&PDL::slice; |
|
2546
|
|
|
|
|
|
|
|
|
2547
|
|
|
|
|
|
|
=head2 nslice_if_pdl |
|
2548
|
|
|
|
|
|
|
|
|
2549
|
|
|
|
|
|
|
=for ref |
|
2550
|
|
|
|
|
|
|
|
|
2551
|
|
|
|
|
|
|
If C<$self> is a PDL, then calls C with all but the last |
|
2552
|
|
|
|
|
|
|
argument, otherwise C<< $self->($_[-1]) >> is called where C<$_[-1]> is the |
|
2553
|
|
|
|
|
|
|
original argument string found during PDL::NiceSlice filtering. |
|
2554
|
|
|
|
|
|
|
|
|
2555
|
|
|
|
|
|
|
DEVELOPER'S NOTE: this routine is found in Core.pm but would be |
|
2556
|
|
|
|
|
|
|
better placed in Slices/slices.pd. It is likely to be moved there |
|
2557
|
|
|
|
|
|
|
and/or changed to "slice_if_pdl" for PDL 3.0. |
|
2558
|
|
|
|
|
|
|
|
|
2559
|
|
|
|
|
|
|
=for usage |
|
2560
|
|
|
|
|
|
|
|
|
2561
|
|
|
|
|
|
|
$w = $x->nslice_if_pdl(...,'(args)'); |
|
2562
|
|
|
|
|
|
|
|
|
2563
|
|
|
|
|
|
|
=cut |
|
2564
|
|
|
|
|
|
|
|
|
2565
|
|
|
|
|
|
|
sub PDL::nslice_if_pdl :lvalue { |
|
2566
|
1
|
|
|
1
|
0
|
511
|
my ($pdl) = shift; |
|
2567
|
1
|
|
|
|
|
4
|
my ($orig_args) = pop; |
|
2568
|
|
|
|
|
|
|
|
|
2569
|
|
|
|
|
|
|
# warn "PDL::nslice_if_pdl called with (@_) args, originally ($orig_args)\n"; |
|
2570
|
|
|
|
|
|
|
|
|
2571
|
1
|
50
|
|
|
|
5
|
if (ref($pdl) eq 'CODE') { |
|
2572
|
|
|
|
|
|
|
# barf('PDL::nslice_if_pdl tried to process a sub ref, please use &$subref() syntax') |
|
2573
|
0
|
|
|
|
|
0
|
@_ = eval $orig_args; |
|
2574
|
0
|
|
|
|
|
0
|
goto &$pdl; |
|
2575
|
|
|
|
|
|
|
} |
|
2576
|
|
|
|
|
|
|
|
|
2577
|
1
|
|
|
|
|
3
|
unshift @_, $pdl; |
|
2578
|
1
|
|
|
|
|
5
|
goto &PDL::slice; |
|
2579
|
|
|
|
|
|
|
} |
|
2580
|
|
|
|
|
|
|
|
|
2581
|
|
|
|
|
|
|
# Convert everything to PDL if not blessed |
|
2582
|
|
|
|
|
|
|
sub alltopdl { |
|
2583
|
1140
|
100
|
|
1140
|
0
|
4296
|
if (ref $_[2] eq 'PDL::Type') { |
|
2584
|
1080
|
100
|
|
|
|
4415
|
return convert($_[1], $_[2]) if blessed($_[1]); |
|
2585
|
717
|
50
|
|
|
|
4222
|
return $_[0]->new($_[2], $_[1]) if $_[0] eq 'PDL'; |
|
2586
|
|
|
|
|
|
|
} |
|
2587
|
60
|
50
|
|
|
|
203
|
return $_[1] if blessed($_[1]); # Fall through |
|
2588
|
60
|
|
|
|
|
397
|
return $_[0]->new($_[1]); |
|
2589
|
|
|
|
|
|
|
} |
|
2590
|
|
|
|
|
|
|
|
|
2591
|
|
|
|
|
|
|
=head2 donttouch |
|
2592
|
|
|
|
|
|
|
|
|
2593
|
|
|
|
|
|
|
=for ref |
|
2594
|
|
|
|
|
|
|
|
|
2595
|
|
|
|
|
|
|
Returns whether the ndarray's C flag is set. |
|
2596
|
|
|
|
|
|
|
|
|
2597
|
|
|
|
|
|
|
=head2 allocated |
|
2598
|
|
|
|
|
|
|
|
|
2599
|
|
|
|
|
|
|
=for ref |
|
2600
|
|
|
|
|
|
|
|
|
2601
|
|
|
|
|
|
|
Returns whether the ndarray's C flag is set. |
|
2602
|
|
|
|
|
|
|
|
|
2603
|
|
|
|
|
|
|
=head2 vaffine |
|
2604
|
|
|
|
|
|
|
|
|
2605
|
|
|
|
|
|
|
=for ref |
|
2606
|
|
|
|
|
|
|
|
|
2607
|
|
|
|
|
|
|
Returns whether the ndarray's C flag is set. |
|
2608
|
|
|
|
|
|
|
|
|
2609
|
|
|
|
|
|
|
=head2 anychgd |
|
2610
|
|
|
|
|
|
|
|
|
2611
|
|
|
|
|
|
|
=for ref |
|
2612
|
|
|
|
|
|
|
|
|
2613
|
|
|
|
|
|
|
Returns whether the ndarray's C flag is set. |
|
2614
|
|
|
|
|
|
|
|
|
2615
|
|
|
|
|
|
|
=head2 dimschgd |
|
2616
|
|
|
|
|
|
|
|
|
2617
|
|
|
|
|
|
|
=for ref |
|
2618
|
|
|
|
|
|
|
|
|
2619
|
|
|
|
|
|
|
Returns whether the ndarray's C flag is set. |
|
2620
|
|
|
|
|
|
|
|
|
2621
|
|
|
|
|
|
|
=head2 inplace |
|
2622
|
|
|
|
|
|
|
|
|
2623
|
|
|
|
|
|
|
=for ref |
|
2624
|
|
|
|
|
|
|
|
|
2625
|
|
|
|
|
|
|
Flag an ndarray so that the next operation is done 'in place', returning |
|
2626
|
|
|
|
|
|
|
the ndarray. |
|
2627
|
|
|
|
|
|
|
|
|
2628
|
|
|
|
|
|
|
=for usage |
|
2629
|
|
|
|
|
|
|
|
|
2630
|
|
|
|
|
|
|
somefunc($x->inplace); somefunc(inplace $x); |
|
2631
|
|
|
|
|
|
|
|
|
2632
|
|
|
|
|
|
|
In most cases one likes to use the syntax C<$y = f($x)>, however |
|
2633
|
|
|
|
|
|
|
in many case the operation C can be done correctly |
|
2634
|
|
|
|
|
|
|
'in place', i.e. without making a new copy of the data for |
|
2635
|
|
|
|
|
|
|
output. To make it easy to use this, we write C in such |
|
2636
|
|
|
|
|
|
|
a way that it operates in-place, and use C to hint |
|
2637
|
|
|
|
|
|
|
that a new copy should be disabled. This also makes for |
|
2638
|
|
|
|
|
|
|
clear syntax. |
|
2639
|
|
|
|
|
|
|
|
|
2640
|
|
|
|
|
|
|
Obviously this will not work for all functions, and if in |
|
2641
|
|
|
|
|
|
|
doubt see the function's documentation. However one |
|
2642
|
|
|
|
|
|
|
can assume this is |
|
2643
|
|
|
|
|
|
|
true for all elemental functions (i.e. those which just |
|
2644
|
|
|
|
|
|
|
operate array element by array element like C). |
|
2645
|
|
|
|
|
|
|
|
|
2646
|
|
|
|
|
|
|
=for example |
|
2647
|
|
|
|
|
|
|
|
|
2648
|
|
|
|
|
|
|
pdl> $x = xvals zeroes 10; |
|
2649
|
|
|
|
|
|
|
pdl> log10(inplace $x) |
|
2650
|
|
|
|
|
|
|
pdl> p $x |
|
2651
|
|
|
|
|
|
|
[-inf 0 0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804 0.90308999 0.95424251] |
|
2652
|
|
|
|
|
|
|
|
|
2653
|
|
|
|
|
|
|
=head2 is_inplace |
|
2654
|
|
|
|
|
|
|
|
|
2655
|
|
|
|
|
|
|
=for ref |
|
2656
|
|
|
|
|
|
|
|
|
2657
|
|
|
|
|
|
|
Sets whether an ndarray will operate "in-place" for the next operation |
|
2658
|
|
|
|
|
|
|
if a (Boolean) value is given. Returns the old value. |
|
2659
|
|
|
|
|
|
|
|
|
2660
|
|
|
|
|
|
|
=for usage |
|
2661
|
|
|
|
|
|
|
|
|
2662
|
|
|
|
|
|
|
$out = ($in->is_inplace) ? $in : zeroes($in); |
|
2663
|
|
|
|
|
|
|
$in->set_inplace(0) |
|
2664
|
|
|
|
|
|
|
|
|
2665
|
|
|
|
|
|
|
Provides access to the L hint flag, within the perl milieu. |
|
2666
|
|
|
|
|
|
|
That way functions you write can be inplace aware... If given an |
|
2667
|
|
|
|
|
|
|
argument the inplace flag will be set or unset depending on the value |
|
2668
|
|
|
|
|
|
|
at the same time. Can be used for shortcut tests that delete the |
|
2669
|
|
|
|
|
|
|
inplace flag while testing: |
|
2670
|
|
|
|
|
|
|
|
|
2671
|
|
|
|
|
|
|
$out = ($in->is_inplace(0)) ? $in : zeroes($in); # test & unset! |
|
2672
|
|
|
|
|
|
|
|
|
2673
|
|
|
|
|
|
|
=head2 set_inplace |
|
2674
|
|
|
|
|
|
|
|
|
2675
|
|
|
|
|
|
|
=for ref |
|
2676
|
|
|
|
|
|
|
|
|
2677
|
|
|
|
|
|
|
Set the in-place flag on an ndarray |
|
2678
|
|
|
|
|
|
|
|
|
2679
|
|
|
|
|
|
|
=for usage |
|
2680
|
|
|
|
|
|
|
|
|
2681
|
|
|
|
|
|
|
$out = ($in->is_inplace) ? $in : zeroes($in); |
|
2682
|
|
|
|
|
|
|
$in->set_inplace(0); |
|
2683
|
|
|
|
|
|
|
|
|
2684
|
|
|
|
|
|
|
Provides access to the L hint flag, within the perl milieu. |
|
2685
|
|
|
|
|
|
|
Useful mainly for turning it OFF, as L turns it ON more |
|
2686
|
|
|
|
|
|
|
conveniently. |
|
2687
|
|
|
|
|
|
|
|
|
2688
|
|
|
|
|
|
|
=head2 new_or_inplace |
|
2689
|
|
|
|
|
|
|
|
|
2690
|
|
|
|
|
|
|
=for usage |
|
2691
|
|
|
|
|
|
|
|
|
2692
|
|
|
|
|
|
|
$w = new_or_inplace(shift()); |
|
2693
|
|
|
|
|
|
|
$w = new_or_inplace(shift(),$preferred_type); |
|
2694
|
|
|
|
|
|
|
|
|
2695
|
|
|
|
|
|
|
=for ref |
|
2696
|
|
|
|
|
|
|
|
|
2697
|
|
|
|
|
|
|
Return back either the argument pdl or a copy of it depending on whether |
|
2698
|
|
|
|
|
|
|
it be flagged in-place or no. Handy for building inplace-aware functions. |
|
2699
|
|
|
|
|
|
|
|
|
2700
|
|
|
|
|
|
|
If you specify a preferred type (must be one of the usual PDL type strings, |
|
2701
|
|
|
|
|
|
|
a list ref containing several of them, or a comma-separated string |
|
2702
|
|
|
|
|
|
|
containing several of them), |
|
2703
|
|
|
|
|
|
|
then the copy is coerced into the first preferred type listed if it is not |
|
2704
|
|
|
|
|
|
|
already one of the preferred types. |
|
2705
|
|
|
|
|
|
|
|
|
2706
|
|
|
|
|
|
|
Note that if the inplace flag is set, no coercion happens even if you specify |
|
2707
|
|
|
|
|
|
|
a preferred type. |
|
2708
|
|
|
|
|
|
|
|
|
2709
|
|
|
|
|
|
|
=cut |
|
2710
|
|
|
|
|
|
|
|
|
2711
|
|
|
|
|
|
|
sub new_or_inplace { |
|
2712
|
379
|
|
|
379
|
1
|
683
|
my $pdl = shift; |
|
2713
|
379
|
100
|
100
|
|
|
2215
|
if(blessed($pdl) && $pdl->is_inplace) { |
|
2714
|
29
|
|
|
|
|
122
|
$pdl->set_inplace(0); |
|
2715
|
29
|
|
|
|
|
98
|
return $pdl; |
|
2716
|
|
|
|
|
|
|
} |
|
2717
|
350
|
|
|
|
|
610
|
my $preferred = shift; |
|
2718
|
350
|
100
|
|
|
|
1701
|
return blessed($pdl) ? $pdl->copy : null() if !defined $preferred; |
|
|
|
100
|
|
|
|
|
|
|
2719
|
1
|
50
|
|
|
|
9
|
$preferred = [split ",",$preferred] if ref $preferred ne 'ARRAY'; |
|
2720
|
1
|
|
|
|
|
5
|
my $s = "".$pdl->type; |
|
2721
|
1
|
50
|
|
|
|
7
|
return $pdl->copy if grep $_ eq $s, @$preferred; # the PDL is one of the preferred types. |
|
2722
|
|
|
|
|
|
|
# No match - promote it to the first in the list. |
|
2723
|
1
|
|
|
|
|
5
|
return $pdl->convert(PDL::Type->new($preferred->[0])); |
|
2724
|
|
|
|
|
|
|
} |
|
2725
|
|
|
|
|
|
|
*PDL::new_or_inplace = \&new_or_inplace; |
|
2726
|
|
|
|
|
|
|
|
|
2727
|
|
|
|
|
|
|
# Allow specifications like zeroes(10,10) or zeroes($x) |
|
2728
|
|
|
|
|
|
|
# or zeroes(inplace $x) or zeroes(float,4,3) |
|
2729
|
|
|
|
|
|
|
|
|
2730
|
|
|
|
|
|
|
=head2 new_from_specification |
|
2731
|
|
|
|
|
|
|
|
|
2732
|
|
|
|
|
|
|
=for ref |
|
2733
|
|
|
|
|
|
|
|
|
2734
|
|
|
|
|
|
|
Internal method: create ndarray by specification |
|
2735
|
|
|
|
|
|
|
|
|
2736
|
|
|
|
|
|
|
This is the argument processing method called by L |
|
2737
|
|
|
|
|
|
|
and some other functions |
|
2738
|
|
|
|
|
|
|
which constructs ndarrays from argument lists of the form: |
|
2739
|
|
|
|
|
|
|
|
|
2740
|
|
|
|
|
|
|
[type], $nx, $ny, $nz,... |
|
2741
|
|
|
|
|
|
|
|
|
2742
|
|
|
|
|
|
|
For C<$nx>, C<$ny>, etc. 0 and 1D ndarrays are allowed. |
|
2743
|
|
|
|
|
|
|
Giving those has the same effect as if saying C<$arg-Elist>, |
|
2744
|
|
|
|
|
|
|
e.g. |
|
2745
|
|
|
|
|
|
|
|
|
2746
|
|
|
|
|
|
|
1, pdl(5,2), 4 |
|
2747
|
|
|
|
|
|
|
|
|
2748
|
|
|
|
|
|
|
is equivalent to |
|
2749
|
|
|
|
|
|
|
|
|
2750
|
|
|
|
|
|
|
1, 5, 2, 4 |
|
2751
|
|
|
|
|
|
|
|
|
2752
|
|
|
|
|
|
|
Note, however, that in all functions using C |
|
2753
|
|
|
|
|
|
|
calling C will probably not do what you want. So to play safe |
|
2754
|
|
|
|
|
|
|
use (e.g. with zeroes) |
|
2755
|
|
|
|
|
|
|
|
|
2756
|
|
|
|
|
|
|
$pdl = zeroes $dimpdl->list; |
|
2757
|
|
|
|
|
|
|
|
|
2758
|
|
|
|
|
|
|
Calling |
|
2759
|
|
|
|
|
|
|
|
|
2760
|
|
|
|
|
|
|
$pdl = zeroes $dimpdl; |
|
2761
|
|
|
|
|
|
|
|
|
2762
|
|
|
|
|
|
|
will rather be equivalent to |
|
2763
|
|
|
|
|
|
|
|
|
2764
|
|
|
|
|
|
|
$pdl = zeroes $dimpdl->dims; |
|
2765
|
|
|
|
|
|
|
|
|
2766
|
|
|
|
|
|
|
However, |
|
2767
|
|
|
|
|
|
|
|
|
2768
|
|
|
|
|
|
|
$pdl = zeroes ushort, $dimpdl; |
|
2769
|
|
|
|
|
|
|
|
|
2770
|
|
|
|
|
|
|
will again do what you intended since it is interpreted |
|
2771
|
|
|
|
|
|
|
as if you had said |
|
2772
|
|
|
|
|
|
|
|
|
2773
|
|
|
|
|
|
|
$pdl = zeroes ushort, $dimpdl->list; |
|
2774
|
|
|
|
|
|
|
|
|
2775
|
|
|
|
|
|
|
This is unfortunate and confusing but no good solution seems |
|
2776
|
|
|
|
|
|
|
obvious that would not break existing scripts. |
|
2777
|
|
|
|
|
|
|
|
|
2778
|
|
|
|
|
|
|
=head2 isnull |
|
2779
|
|
|
|
|
|
|
|
|
2780
|
|
|
|
|
|
|
=for ref |
|
2781
|
|
|
|
|
|
|
|
|
2782
|
|
|
|
|
|
|
Test whether an ndarray is null |
|
2783
|
|
|
|
|
|
|
|
|
2784
|
|
|
|
|
|
|
=for usage |
|
2785
|
|
|
|
|
|
|
|
|
2786
|
|
|
|
|
|
|
croak("Input ndarray mustn't be null!") |
|
2787
|
|
|
|
|
|
|
if $input_ndarray->isnull; |
|
2788
|
|
|
|
|
|
|
|
|
2789
|
|
|
|
|
|
|
This function returns 1 if the ndarray is null, zero if it is not. The purpose |
|
2790
|
|
|
|
|
|
|
of null ndarrays is to "tell" any PDL::PP methods to allocate new memory for |
|
2791
|
|
|
|
|
|
|
an output ndarray, but only when that PDL::PP method is called in full-arg |
|
2792
|
|
|
|
|
|
|
form. Of course, there's no reason you couldn't commandeer the special value |
|
2793
|
|
|
|
|
|
|
for your own purposes, for which this test function would prove most helpful. |
|
2794
|
|
|
|
|
|
|
But in general, you shouldn't need to test for an ndarray's nullness. |
|
2795
|
|
|
|
|
|
|
|
|
2796
|
|
|
|
|
|
|
See L for more information. |
|
2797
|
|
|
|
|
|
|
|
|
2798
|
|
|
|
|
|
|
=head2 isempty |
|
2799
|
|
|
|
|
|
|
|
|
2800
|
|
|
|
|
|
|
=for ref |
|
2801
|
|
|
|
|
|
|
|
|
2802
|
|
|
|
|
|
|
Test whether an ndarray is empty |
|
2803
|
|
|
|
|
|
|
|
|
2804
|
|
|
|
|
|
|
=for usage |
|
2805
|
|
|
|
|
|
|
|
|
2806
|
|
|
|
|
|
|
print "The ndarray has zero dimension\n" if $pdl->isempty; |
|
2807
|
|
|
|
|
|
|
|
|
2808
|
|
|
|
|
|
|
This function returns 1 if the ndarray has zero elements. This is |
|
2809
|
|
|
|
|
|
|
useful in particular when using the indexing function which. In the |
|
2810
|
|
|
|
|
|
|
case of no match to a specified criterion, the returned ndarray has |
|
2811
|
|
|
|
|
|
|
zero dimension. |
|
2812
|
|
|
|
|
|
|
|
|
2813
|
|
|
|
|
|
|
pdl> $w=sequence(10) |
|
2814
|
|
|
|
|
|
|
pdl> $i=which($w < -1) |
|
2815
|
|
|
|
|
|
|
pdl> print "I found no matches!\n" if ($i->isempty); |
|
2816
|
|
|
|
|
|
|
I found no matches! |
|
2817
|
|
|
|
|
|
|
|
|
2818
|
|
|
|
|
|
|
Note that having zero elements is rather different from the concept |
|
2819
|
|
|
|
|
|
|
of being a null ndarray, see the L and |
|
2820
|
|
|
|
|
|
|
L |
|
2821
|
|
|
|
|
|
|
manpages for discussions of this. |
|
2822
|
|
|
|
|
|
|
|
|
2823
|
|
|
|
|
|
|
=cut |
|
2824
|
|
|
|
|
|
|
|
|
2825
|
|
|
|
|
|
|
sub PDL::isempty { |
|
2826
|
3897
|
|
|
3897
|
0
|
22542
|
my $pdl=shift; |
|
2827
|
3897
|
|
|
|
|
47265
|
return ($pdl->nelem == 0); |
|
2828
|
|
|
|
|
|
|
} |
|
2829
|
|
|
|
|
|
|
|
|
2830
|
|
|
|
|
|
|
=head2 zeroes |
|
2831
|
|
|
|
|
|
|
|
|
2832
|
|
|
|
|
|
|
=for ref |
|
2833
|
|
|
|
|
|
|
|
|
2834
|
|
|
|
|
|
|
construct a zero filled ndarray from dimension list or template ndarray. |
|
2835
|
|
|
|
|
|
|
If called with no arguments, returns a zero-dimension ndarray (a scalar). |
|
2836
|
|
|
|
|
|
|
|
|
2837
|
|
|
|
|
|
|
Various forms of usage, |
|
2838
|
|
|
|
|
|
|
|
|
2839
|
|
|
|
|
|
|
(i) by specification or (ii) by template ndarray: |
|
2840
|
|
|
|
|
|
|
|
|
2841
|
|
|
|
|
|
|
=for usage |
|
2842
|
|
|
|
|
|
|
|
|
2843
|
|
|
|
|
|
|
# usage type (i): |
|
2844
|
|
|
|
|
|
|
$w = zeroes([type], $nx, $ny, $nz,...); |
|
2845
|
|
|
|
|
|
|
$w = PDL->zeroes([type], $nx, $ny, $nz,...); |
|
2846
|
|
|
|
|
|
|
$w = $pdl->zeroes([type], $nx, $ny, $nz,...); # all info about $pdl ignored |
|
2847
|
|
|
|
|
|
|
# usage type (ii): |
|
2848
|
|
|
|
|
|
|
$w = zeroes $y; |
|
2849
|
|
|
|
|
|
|
$w = $y->zeroes |
|
2850
|
|
|
|
|
|
|
zeroes inplace $w; # Equivalent to $w .= 0; |
|
2851
|
|
|
|
|
|
|
$w->inplace->zeroes; # "" |
|
2852
|
|
|
|
|
|
|
|
|
2853
|
|
|
|
|
|
|
=for example |
|
2854
|
|
|
|
|
|
|
|
|
2855
|
|
|
|
|
|
|
pdl> $z = zeroes 4,3 |
|
2856
|
|
|
|
|
|
|
pdl> p $z |
|
2857
|
|
|
|
|
|
|
[ |
|
2858
|
|
|
|
|
|
|
[0 0 0 0] |
|
2859
|
|
|
|
|
|
|
[0 0 0 0] |
|
2860
|
|
|
|
|
|
|
[0 0 0 0] |
|
2861
|
|
|
|
|
|
|
] |
|
2862
|
|
|
|
|
|
|
pdl> $z = zeroes ushort, 3,2 # Create ushort array |
|
2863
|
|
|
|
|
|
|
[ushort() etc. with no arg returns a PDL::Types token] |
|
2864
|
|
|
|
|
|
|
|
|
2865
|
|
|
|
|
|
|
See also L |
|
2866
|
|
|
|
|
|
|
for details on using ndarrays in the dimensions list. |
|
2867
|
|
|
|
|
|
|
|
|
2868
|
|
|
|
|
|
|
=cut |
|
2869
|
|
|
|
|
|
|
|
|
2870
|
488
|
100
|
100
|
488
|
1
|
1697640
|
sub zeroes { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? PDL::zeroes($_[0]) : PDL->zeroes(@_) } |
|
2871
|
|
|
|
|
|
|
|
|
2872
|
|
|
|
|
|
|
# Create convenience aliases for zeroes |
|
2873
|
|
|
|
|
|
|
|
|
2874
|
|
|
|
|
|
|
=head2 zeros |
|
2875
|
|
|
|
|
|
|
|
|
2876
|
|
|
|
|
|
|
=for ref |
|
2877
|
|
|
|
|
|
|
|
|
2878
|
|
|
|
|
|
|
construct a zero filled ndarray (see zeroes for usage) |
|
2879
|
|
|
|
|
|
|
|
|
2880
|
|
|
|
|
|
|
=cut |
|
2881
|
|
|
|
|
|
|
|
|
2882
|
|
|
|
|
|
|
*zeros = \&zeroes; |
|
2883
|
|
|
|
|
|
|
*PDL::zeros = \&PDL::zeroes; |
|
2884
|
|
|
|
|
|
|
|
|
2885
|
|
|
|
|
|
|
=head2 ones |
|
2886
|
|
|
|
|
|
|
|
|
2887
|
|
|
|
|
|
|
=for ref |
|
2888
|
|
|
|
|
|
|
|
|
2889
|
|
|
|
|
|
|
construct a one filled ndarray. |
|
2890
|
|
|
|
|
|
|
If called with no arguments, returns a zero-dimension ndarray (a scalar). |
|
2891
|
|
|
|
|
|
|
|
|
2892
|
|
|
|
|
|
|
=for usage |
|
2893
|
|
|
|
|
|
|
|
|
2894
|
|
|
|
|
|
|
$w = ones([type], $nx, $ny, $nz,...); |
|
2895
|
|
|
|
|
|
|
etc. (see 'zeroes') |
|
2896
|
|
|
|
|
|
|
|
|
2897
|
|
|
|
|
|
|
=for example |
|
2898
|
|
|
|
|
|
|
|
|
2899
|
|
|
|
|
|
|
see zeroes() and add one |
|
2900
|
|
|
|
|
|
|
|
|
2901
|
|
|
|
|
|
|
See also L |
|
2902
|
|
|
|
|
|
|
for details on using ndarrays in the dimensions list. |
|
2903
|
|
|
|
|
|
|
|
|
2904
|
|
|
|
|
|
|
=cut |
|
2905
|
|
|
|
|
|
|
|
|
2906
|
|
|
|
|
|
|
sub _construct { |
|
2907
|
901
|
50
|
|
901
|
|
2666
|
barf "No args given" if !@_; |
|
2908
|
901
|
100
|
|
|
|
6712
|
unshift @_, 'PDL' if !UNIVERSAL::can($_[0], 'new_from_specification'); |
|
2909
|
901
|
100
|
|
|
|
12841
|
@_>1 ? $_[0]->new_from_specification(@_[1..$#_]) : $_[0]->new_or_inplace; |
|
2910
|
|
|
|
|
|
|
} |
|
2911
|
205
|
100
|
100
|
205
|
1
|
592850
|
sub ones { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? PDL::ones($_[0]) : PDL->ones(@_) } |
|
2912
|
|
|
|
|
|
|
sub PDL::ones { |
|
2913
|
297
|
|
|
297
|
0
|
2483
|
my $pdl = &_construct; |
|
2914
|
297
|
|
33
|
|
|
2999
|
$pdl .= ($pdl_ones[$pdl->get_datatype]//barf "Couldn't find 'one' for type ", $pdl->get_datatype); |
|
2915
|
297
|
|
|
|
|
1460
|
return $pdl; |
|
2916
|
|
|
|
|
|
|
} |
|
2917
|
|
|
|
|
|
|
|
|
2918
|
|
|
|
|
|
|
=head2 nan |
|
2919
|
|
|
|
|
|
|
|
|
2920
|
|
|
|
|
|
|
=for ref |
|
2921
|
|
|
|
|
|
|
|
|
2922
|
|
|
|
|
|
|
construct a C filled ndarray. |
|
2923
|
|
|
|
|
|
|
If called with no arguments, returns a zero-dimension ndarray (a scalar). |
|
2924
|
|
|
|
|
|
|
|
|
2925
|
|
|
|
|
|
|
=for usage |
|
2926
|
|
|
|
|
|
|
|
|
2927
|
|
|
|
|
|
|
$w = nan([type], $nx, $ny, $nz,...); |
|
2928
|
|
|
|
|
|
|
etc. (see 'zeroes') |
|
2929
|
|
|
|
|
|
|
|
|
2930
|
|
|
|
|
|
|
=for example |
|
2931
|
|
|
|
|
|
|
|
|
2932
|
|
|
|
|
|
|
see zeroes() and add NaN |
|
2933
|
|
|
|
|
|
|
|
|
2934
|
|
|
|
|
|
|
See also L |
|
2935
|
|
|
|
|
|
|
for details on using ndarrays in the dimensions list. |
|
2936
|
|
|
|
|
|
|
|
|
2937
|
|
|
|
|
|
|
=cut |
|
2938
|
|
|
|
|
|
|
|
|
2939
|
12
|
100
|
100
|
12
|
1
|
2633
|
sub nan { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? PDL::nan($_[0]) : PDL->nan(@_) } |
|
2940
|
|
|
|
|
|
|
sub PDL::nan { |
|
2941
|
16
|
|
|
16
|
0
|
3893
|
my $pdl = &_construct; |
|
2942
|
16
|
|
|
|
|
207
|
$pdl .= PDL::_nan(); |
|
2943
|
16
|
|
|
|
|
140
|
return $pdl; |
|
2944
|
|
|
|
|
|
|
} |
|
2945
|
|
|
|
|
|
|
|
|
2946
|
|
|
|
|
|
|
=head2 inf |
|
2947
|
|
|
|
|
|
|
|
|
2948
|
|
|
|
|
|
|
=for ref |
|
2949
|
|
|
|
|
|
|
|
|
2950
|
|
|
|
|
|
|
construct an C filled ndarray. |
|
2951
|
|
|
|
|
|
|
If called with no arguments, returns a zero-dimension ndarray (a scalar). |
|
2952
|
|
|
|
|
|
|
|
|
2953
|
|
|
|
|
|
|
=for usage |
|
2954
|
|
|
|
|
|
|
|
|
2955
|
|
|
|
|
|
|
$w = inf([type], $nx, $ny, $nz,...); |
|
2956
|
|
|
|
|
|
|
etc. (see 'zeroes') |
|
2957
|
|
|
|
|
|
|
|
|
2958
|
|
|
|
|
|
|
=for example |
|
2959
|
|
|
|
|
|
|
|
|
2960
|
|
|
|
|
|
|
see zeroes() and add Inf |
|
2961
|
|
|
|
|
|
|
|
|
2962
|
|
|
|
|
|
|
See also L |
|
2963
|
|
|
|
|
|
|
for details on using ndarrays in the dimensions list. |
|
2964
|
|
|
|
|
|
|
|
|
2965
|
|
|
|
|
|
|
=cut |
|
2966
|
|
|
|
|
|
|
|
|
2967
|
17
|
100
|
100
|
17
|
1
|
3290
|
sub inf { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? PDL::inf($_[0]) : PDL->inf(@_) } |
|
2968
|
|
|
|
|
|
|
sub PDL::inf { |
|
2969
|
21
|
|
|
21
|
0
|
2322
|
my $pdl = &_construct; |
|
2970
|
21
|
|
|
|
|
179
|
$pdl .= PDL::_inf(); |
|
2971
|
21
|
|
|
|
|
188
|
return $pdl; |
|
2972
|
|
|
|
|
|
|
} |
|
2973
|
|
|
|
|
|
|
|
|
2974
|
|
|
|
|
|
|
=head2 i |
|
2975
|
|
|
|
|
|
|
|
|
2976
|
|
|
|
|
|
|
=for ref |
|
2977
|
|
|
|
|
|
|
|
|
2978
|
|
|
|
|
|
|
construct an ndarray filled with a native complex value equal to the |
|
2979
|
|
|
|
|
|
|
imaginary number "i", the square root of -1. |
|
2980
|
|
|
|
|
|
|
If called with no arguments, returns a zero-dimension ndarray (a scalar). |
|
2981
|
|
|
|
|
|
|
|
|
2982
|
|
|
|
|
|
|
=for usage |
|
2983
|
|
|
|
|
|
|
|
|
2984
|
|
|
|
|
|
|
$w = i([type], $nx, $ny, $nz,...); |
|
2985
|
|
|
|
|
|
|
etc. (see 'zeroes') |
|
2986
|
|
|
|
|
|
|
|
|
2987
|
|
|
|
|
|
|
=for example |
|
2988
|
|
|
|
|
|
|
|
|
2989
|
|
|
|
|
|
|
see zeroes() and add "i" |
|
2990
|
|
|
|
|
|
|
|
|
2991
|
|
|
|
|
|
|
See also L |
|
2992
|
|
|
|
|
|
|
for details on using ndarrays in the dimensions list. |
|
2993
|
|
|
|
|
|
|
|
|
2994
|
|
|
|
|
|
|
=cut |
|
2995
|
|
|
|
|
|
|
|
|
2996
|
56
|
100
|
100
|
56
|
1
|
7020
|
sub i { ref($_[0]) && ref($_[0]) ne 'PDL::Type' ? PDL::i($_[0]) : PDL->i(@_) } |
|
2997
|
|
|
|
|
|
|
sub PDL::i { |
|
2998
|
60
|
|
|
60
|
0
|
2582
|
my $class = shift; |
|
2999
|
60
|
|
|
|
|
176
|
my @args = @_; |
|
3000
|
60
|
100
|
|
|
|
168
|
if (@args) { |
|
3001
|
7
|
100
|
66
|
|
|
40
|
if (ref($args[0]) eq 'PDL::Type' and $args[0]->real) { |
|
3002
|
3
|
|
|
|
|
11
|
$args[0] = cdouble(); |
|
3003
|
|
|
|
|
|
|
} else { |
|
3004
|
4
|
|
|
|
|
18
|
unshift @args, cdouble(); |
|
3005
|
|
|
|
|
|
|
} |
|
3006
|
|
|
|
|
|
|
} else { |
|
3007
|
53
|
100
|
66
|
|
|
209
|
$class = convert $class, cdouble() if ref $class and $class->type->real; |
|
3008
|
|
|
|
|
|
|
} |
|
3009
|
60
|
100
|
|
|
|
484
|
my $pdl = scalar(@args)? $class->new_from_specification(@args) : $class->new_or_inplace; |
|
3010
|
60
|
|
|
|
|
590
|
$pdl .= PDL::_ci(); |
|
3011
|
60
|
|
|
|
|
573
|
return $pdl; |
|
3012
|
|
|
|
|
|
|
} |
|
3013
|
|
|
|
|
|
|
|
|
3014
|
|
|
|
|
|
|
=head2 reshape |
|
3015
|
|
|
|
|
|
|
|
|
3016
|
|
|
|
|
|
|
=for ref |
|
3017
|
|
|
|
|
|
|
|
|
3018
|
|
|
|
|
|
|
Change the shape (i.e. dimensions) of an ndarray, preserving contents. |
|
3019
|
|
|
|
|
|
|
|
|
3020
|
|
|
|
|
|
|
=for usage |
|
3021
|
|
|
|
|
|
|
|
|
3022
|
|
|
|
|
|
|
$x->reshape(NEWDIMS); reshape($x, NEWDIMS); |
|
3023
|
|
|
|
|
|
|
|
|
3024
|
|
|
|
|
|
|
The data elements are preserved, obviously they will wrap |
|
3025
|
|
|
|
|
|
|
differently and get truncated if the new array is shorter. |
|
3026
|
|
|
|
|
|
|
If the new array is longer it will be zero-padded. |
|
3027
|
|
|
|
|
|
|
|
|
3028
|
|
|
|
|
|
|
***Potential incompatibility with earlier versions of PDL**** |
|
3029
|
|
|
|
|
|
|
If the list of C is empty C will just drop |
|
3030
|
|
|
|
|
|
|
all dimensions of size 1 (preserving the number of elements): |
|
3031
|
|
|
|
|
|
|
|
|
3032
|
|
|
|
|
|
|
$w = sequence(3,4,5); |
|
3033
|
|
|
|
|
|
|
$y = $w(1,3); |
|
3034
|
|
|
|
|
|
|
$y->reshape(); |
|
3035
|
|
|
|
|
|
|
print $y->info; |
|
3036
|
|
|
|
|
|
|
PDL: Double D [5] |
|
3037
|
|
|
|
|
|
|
|
|
3038
|
|
|
|
|
|
|
Dimensions of size 1 will also be dropped if C is |
|
3039
|
|
|
|
|
|
|
invoked with the argument -1: |
|
3040
|
|
|
|
|
|
|
|
|
3041
|
|
|
|
|
|
|
$y = $w->reshape(-1); |
|
3042
|
|
|
|
|
|
|
|
|
3043
|
|
|
|
|
|
|
As opposed to C without arguments, C |
|
3044
|
|
|
|
|
|
|
preserves dataflow: |
|
3045
|
|
|
|
|
|
|
|
|
3046
|
|
|
|
|
|
|
$w = ones(2,1,2); |
|
3047
|
|
|
|
|
|
|
$y = $w(0)->reshape(-1); |
|
3048
|
|
|
|
|
|
|
$y++; |
|
3049
|
|
|
|
|
|
|
print $w; |
|
3050
|
|
|
|
|
|
|
[ |
|
3051
|
|
|
|
|
|
|
[ |
|
3052
|
|
|
|
|
|
|
[2 1] |
|
3053
|
|
|
|
|
|
|
] |
|
3054
|
|
|
|
|
|
|
[ |
|
3055
|
|
|
|
|
|
|
[2 1] |
|
3056
|
|
|
|
|
|
|
] |
|
3057
|
|
|
|
|
|
|
] |
|
3058
|
|
|
|
|
|
|
|
|
3059
|
|
|
|
|
|
|
Important: ndarrays are changed inplace! |
|
3060
|
|
|
|
|
|
|
|
|
3061
|
|
|
|
|
|
|
Note: If C<$x> is connected to any other PDL (e.g. if it is a slice) |
|
3062
|
|
|
|
|
|
|
then the connection is first severed. |
|
3063
|
|
|
|
|
|
|
|
|
3064
|
|
|
|
|
|
|
=for example |
|
3065
|
|
|
|
|
|
|
|
|
3066
|
|
|
|
|
|
|
pdl> $x = sequence(10) |
|
3067
|
|
|
|
|
|
|
pdl> reshape $x,3,4; p $x |
|
3068
|
|
|
|
|
|
|
[ |
|
3069
|
|
|
|
|
|
|
[0 1 2] |
|
3070
|
|
|
|
|
|
|
[3 4 5] |
|
3071
|
|
|
|
|
|
|
[6 7 8] |
|
3072
|
|
|
|
|
|
|
[9 0 0] |
|
3073
|
|
|
|
|
|
|
] |
|
3074
|
|
|
|
|
|
|
pdl> reshape $x,5; p $x |
|
3075
|
|
|
|
|
|
|
[0 1 2 3 4] |
|
3076
|
|
|
|
|
|
|
|
|
3077
|
|
|
|
|
|
|
=cut |
|
3078
|
|
|
|
|
|
|
|
|
3079
|
|
|
|
|
|
|
*reshape = \&PDL::reshape; |
|
3080
|
|
|
|
|
|
|
sub PDL::reshape :lvalue { |
|
3081
|
155
|
|
|
155
|
0
|
4225
|
my $pdl = topdl($_[0]); |
|
3082
|
155
|
100
|
100
|
|
|
670
|
if (@_ == 2 && $_[1] == -1) { # a slicing reshape that drops 1-dims |
|
3083
|
17
|
100
|
|
|
|
181
|
return $pdl->slice( map $_==1 ? [0,0,0] : [], $pdl->dims); |
|
3084
|
|
|
|
|
|
|
} |
|
3085
|
138
|
|
|
|
|
524
|
$pdl->sever; |
|
3086
|
138
|
|
|
|
|
382
|
my $nelem = $pdl->nelem; |
|
3087
|
138
|
|
|
|
|
749
|
my @dims = grep defined, @_[1..$#_]; |
|
3088
|
138
|
100
|
|
|
|
373
|
for my $dim(@dims) { barf "reshape: invalid dim size '$dim'" if $dim < 0 } |
|
|
198
|
|
|
|
|
508
|
|
|
3089
|
136
|
100
|
|
|
|
358
|
@dims = grep($_ != 1, $pdl->dims) if @dims == 0; # get rid of dims of size 1 |
|
3090
|
136
|
|
|
|
|
724
|
$pdl->setdims(\@dims); |
|
3091
|
136
|
|
|
|
|
500
|
$pdl->make_physical; |
|
3092
|
136
|
100
|
|
|
|
497
|
if ($pdl->nelem > $nelem) { |
|
3093
|
1
|
|
|
|
|
4
|
my $tmp = $pdl->flat->slice("$nelem:-1"); |
|
3094
|
1
|
|
|
|
|
4
|
$tmp .= pdl($tmp->type, 0); |
|
3095
|
|
|
|
|
|
|
} |
|
3096
|
136
|
|
|
|
|
302
|
$_[0] = $pdl; |
|
3097
|
136
|
|
|
|
|
942
|
return $pdl; |
|
3098
|
|
|
|
|
|
|
} |
|
3099
|
|
|
|
|
|
|
|
|
3100
|
|
|
|
|
|
|
=head2 squeeze |
|
3101
|
|
|
|
|
|
|
|
|
3102
|
|
|
|
|
|
|
=for ref |
|
3103
|
|
|
|
|
|
|
|
|
3104
|
|
|
|
|
|
|
eliminate all singleton dimensions (dims of size 1) |
|
3105
|
|
|
|
|
|
|
|
|
3106
|
|
|
|
|
|
|
=for example |
|
3107
|
|
|
|
|
|
|
|
|
3108
|
|
|
|
|
|
|
$y = $w(0,0)->squeeze; |
|
3109
|
|
|
|
|
|
|
|
|
3110
|
|
|
|
|
|
|
Alias for C. Removes all singleton dimensions |
|
3111
|
|
|
|
|
|
|
and preserves dataflow. A more concise interface is |
|
3112
|
|
|
|
|
|
|
provided by L via modifiers: |
|
3113
|
|
|
|
|
|
|
|
|
3114
|
|
|
|
|
|
|
use PDL::NiceSlice; |
|
3115
|
|
|
|
|
|
|
$y = $w(0,0;-); # same as $w(0,0)->squeeze |
|
3116
|
|
|
|
|
|
|
|
|
3117
|
|
|
|
|
|
|
=cut |
|
3118
|
|
|
|
|
|
|
|
|
3119
|
|
|
|
|
|
|
*squeeze = \&PDL::squeeze; |
|
3120
|
10
|
|
|
10
|
0
|
45
|
sub PDL::squeeze { return $_[0]->reshape(-1) } |
|
3121
|
|
|
|
|
|
|
|
|
3122
|
|
|
|
|
|
|
=head2 flat |
|
3123
|
|
|
|
|
|
|
|
|
3124
|
|
|
|
|
|
|
=for ref |
|
3125
|
|
|
|
|
|
|
|
|
3126
|
|
|
|
|
|
|
flatten an ndarray (alias for C<< $pdl->clump(-1) >>) |
|
3127
|
|
|
|
|
|
|
|
|
3128
|
|
|
|
|
|
|
=for example |
|
3129
|
|
|
|
|
|
|
|
|
3130
|
|
|
|
|
|
|
$srt = $pdl->flat->qsort; |
|
3131
|
|
|
|
|
|
|
|
|
3132
|
|
|
|
|
|
|
Useful method to make a 1D ndarray from an |
|
3133
|
|
|
|
|
|
|
arbitrarily sized input ndarray. Data flows |
|
3134
|
|
|
|
|
|
|
back and forth as usual with slicing routines. |
|
3135
|
|
|
|
|
|
|
Falls through if argument already == 1D. |
|
3136
|
|
|
|
|
|
|
|
|
3137
|
|
|
|
|
|
|
=cut |
|
3138
|
|
|
|
|
|
|
|
|
3139
|
|
|
|
|
|
|
*flat = \&PDL::flat; |
|
3140
|
|
|
|
|
|
|
sub PDL::flat :lvalue { # fall through if < 2D |
|
3141
|
14566
|
100
|
|
14566
|
0
|
97285
|
return my $dummy = $_[0]->getndims != 1 ? $_[0]->clump(-1) : $_[0]; |
|
3142
|
|
|
|
|
|
|
} |
|
3143
|
|
|
|
|
|
|
|
|
3144
|
|
|
|
|
|
|
=head2 convert |
|
3145
|
|
|
|
|
|
|
|
|
3146
|
|
|
|
|
|
|
=for ref |
|
3147
|
|
|
|
|
|
|
|
|
3148
|
|
|
|
|
|
|
Generic datatype conversion function |
|
3149
|
|
|
|
|
|
|
|
|
3150
|
|
|
|
|
|
|
Works on ndarrays and normal numbers. Returns input if already that |
|
3151
|
|
|
|
|
|
|
type. |
|
3152
|
|
|
|
|
|
|
|
|
3153
|
|
|
|
|
|
|
=for usage |
|
3154
|
|
|
|
|
|
|
|
|
3155
|
|
|
|
|
|
|
$y = convert($x, $newtype); |
|
3156
|
|
|
|
|
|
|
$y = $x->convert($newtype); |
|
3157
|
|
|
|
|
|
|
$x->inplace->convert($newtype); |
|
3158
|
|
|
|
|
|
|
|
|
3159
|
|
|
|
|
|
|
C<$newtype> is a type number or L object, for convenience they are |
|
3160
|
|
|
|
|
|
|
returned by C etc when called without arguments. |
|
3161
|
|
|
|
|
|
|
Can work in-place, though will C if so. |
|
3162
|
|
|
|
|
|
|
|
|
3163
|
|
|
|
|
|
|
=for example |
|
3164
|
|
|
|
|
|
|
|
|
3165
|
|
|
|
|
|
|
$y = convert $x, long; |
|
3166
|
|
|
|
|
|
|
$y = $x->convert(ushort); |
|
3167
|
|
|
|
|
|
|
$x->inplace->convert(double); |
|
3168
|
|
|
|
|
|
|
|
|
3169
|
|
|
|
|
|
|
=cut |
|
3170
|
|
|
|
|
|
|
|
|
3171
|
|
|
|
|
|
|
my $CONVERT_ERR = "Usage: \$y = convert(\$x, \$newtype)\n"; |
|
3172
|
|
|
|
|
|
|
sub PDL::convert { |
|
3173
|
601
|
50
|
|
601
|
0
|
1644
|
barf $CONVERT_ERR if @_ != 2; |
|
3174
|
601
|
|
|
|
|
1474
|
my ($pdl,$type)= @_; |
|
3175
|
601
|
|
|
|
|
1452
|
$pdl = topdl($pdl); # Allow normal numbers |
|
3176
|
601
|
100
|
|
|
|
2129
|
barf "Tried to convert(null)" if $pdl->isnull; |
|
3177
|
600
|
100
|
|
|
|
2409
|
$type = $type->enum if ref($type) eq 'PDL::Type'; |
|
3178
|
600
|
50
|
|
|
|
2504
|
barf $CONVERT_ERR unless Scalar::Util::looks_like_number($type); |
|
3179
|
600
|
100
|
|
|
|
9209
|
return $pdl if $pdl->get_datatype == $type; |
|
3180
|
344
|
100
|
|
|
|
25830
|
return $pdl->_convert_int($type)->sever if !$pdl->is_inplace; |
|
3181
|
7
|
|
|
|
|
220
|
$pdl->set_datatype($type); |
|
3182
|
7
|
|
|
|
|
26
|
$pdl; |
|
3183
|
|
|
|
|
|
|
} |
|
3184
|
|
|
|
|
|
|
|
|
3185
|
|
|
|
|
|
|
=head2 convert_flowing |
|
3186
|
|
|
|
|
|
|
|
|
3187
|
|
|
|
|
|
|
=for ref |
|
3188
|
|
|
|
|
|
|
|
|
3189
|
|
|
|
|
|
|
Generic datatype data-flowing conversion function |
|
3190
|
|
|
|
|
|
|
|
|
3191
|
|
|
|
|
|
|
=for usage |
|
3192
|
|
|
|
|
|
|
|
|
3193
|
|
|
|
|
|
|
$y = convert_flowing($x, $newtype); |
|
3194
|
|
|
|
|
|
|
$y = $x->convert_flowing($newtype); |
|
3195
|
|
|
|
|
|
|
|
|
3196
|
|
|
|
|
|
|
C<$newtype> is a type number or L object, for convenience they are |
|
3197
|
|
|
|
|
|
|
returned by C etc when called without arguments. |
|
3198
|
|
|
|
|
|
|
Only works on ndarrays, not normal numbers. Returns input if already that |
|
3199
|
|
|
|
|
|
|
type. Establishes two-way dataflow between the two ndarrays. |
|
3200
|
|
|
|
|
|
|
|
|
3201
|
|
|
|
|
|
|
=cut |
|
3202
|
|
|
|
|
|
|
|
|
3203
|
|
|
|
|
|
|
my $CONVERT_FLOW_ERR = "Usage: \$y = convert_flowing(\$pdl, \$newtype)\n"; |
|
3204
|
|
|
|
|
|
|
sub PDL::convert_flowing { |
|
3205
|
1
|
50
|
|
1
|
0
|
5
|
barf $CONVERT_FLOW_ERR if @_ != 2; |
|
3206
|
1
|
|
|
|
|
4
|
my ($pdl,$type) = @_; |
|
3207
|
1
|
50
|
|
|
|
7
|
barf $CONVERT_FLOW_ERR if !UNIVERSAL::isa($pdl, 'PDL'); |
|
3208
|
1
|
50
|
|
|
|
5
|
barf "Tried to convert_flowing(null)" if $pdl->isnull; |
|
3209
|
1
|
50
|
|
|
|
6
|
barf "Cannot convert_flowing inplace" if $pdl->is_inplace; |
|
3210
|
1
|
50
|
|
|
|
8
|
$type = $type->enum if ref($type) eq 'PDL::Type'; |
|
3211
|
1
|
50
|
|
|
|
6
|
barf $CONVERT_FLOW_ERR unless Scalar::Util::looks_like_number($type); |
|
3212
|
1
|
50
|
|
|
|
7
|
return $pdl if $pdl->get_datatype == $type; |
|
3213
|
1
|
|
|
|
|
9
|
$pdl->_convert_int($type); |
|
3214
|
|
|
|
|
|
|
} |
|
3215
|
|
|
|
|
|
|
|
|
3216
|
|
|
|
|
|
|
=head2 Datatype_conversions |
|
3217
|
|
|
|
|
|
|
|
|
3218
|
|
|
|
|
|
|
=for ref |
|
3219
|
|
|
|
|
|
|
|
|
3220
|
|
|
|
|
|
|
sbyte|byte|short|ushort|long|ulong|indx|longlong|ulonglong|float|double|ldouble|cfloat|cdouble|cldouble (shorthands to convert datatypes) |
|
3221
|
|
|
|
|
|
|
|
|
3222
|
|
|
|
|
|
|
=for usage |
|
3223
|
|
|
|
|
|
|
|
|
3224
|
|
|
|
|
|
|
$y = double $x; $y = ushort [1..10]; |
|
3225
|
|
|
|
|
|
|
# all of the above listed shorthands behave similarly |
|
3226
|
|
|
|
|
|
|
|
|
3227
|
|
|
|
|
|
|
When called with an ndarray argument, they convert to the specific |
|
3228
|
|
|
|
|
|
|
datatype. |
|
3229
|
|
|
|
|
|
|
|
|
3230
|
|
|
|
|
|
|
When called with a numeric, list, listref, or string argument they |
|
3231
|
|
|
|
|
|
|
construct a new ndarray. This is a convenience to avoid having to be |
|
3232
|
|
|
|
|
|
|
long-winded and say C<$x = long(pdl(42))> |
|
3233
|
|
|
|
|
|
|
|
|
3234
|
|
|
|
|
|
|
Thus one can say: |
|
3235
|
|
|
|
|
|
|
|
|
3236
|
|
|
|
|
|
|
$w = float(1,2,3,4); # 1D |
|
3237
|
|
|
|
|
|
|
$w = float q[1 2 3; 4 5 6]; # 2D |
|
3238
|
|
|
|
|
|
|
$w = float([1,2,3],[4,5,6]); # 2D |
|
3239
|
|
|
|
|
|
|
$w = float([[1,2,3],[4,5,6]]); # 2D |
|
3240
|
|
|
|
|
|
|
|
|
3241
|
|
|
|
|
|
|
Note the last three give identical results, and the last two are exactly |
|
3242
|
|
|
|
|
|
|
equivalent - a list is automatically converted to a list reference for |
|
3243
|
|
|
|
|
|
|
syntactic convenience. i.e. you can omit the outer C<[]> |
|
3244
|
|
|
|
|
|
|
|
|
3245
|
|
|
|
|
|
|
When called with no arguments, these functions return a special type token. |
|
3246
|
|
|
|
|
|
|
This allows syntactical sugar like: |
|
3247
|
|
|
|
|
|
|
|
|
3248
|
|
|
|
|
|
|
$x = ones byte, 1000,1000; |
|
3249
|
|
|
|
|
|
|
|
|
3250
|
|
|
|
|
|
|
This example creates a large ndarray directly as byte datatype in |
|
3251
|
|
|
|
|
|
|
order to save memory. |
|
3252
|
|
|
|
|
|
|
|
|
3253
|
|
|
|
|
|
|
In order to control how undefs are handled in converting from perl lists to |
|
3254
|
|
|
|
|
|
|
PDLs, one can set the variable C<$PDL::undefval>; |
|
3255
|
|
|
|
|
|
|
see the function L for more details. |
|
3256
|
|
|
|
|
|
|
|
|
3257
|
|
|
|
|
|
|
=for example |
|
3258
|
|
|
|
|
|
|
|
|
3259
|
|
|
|
|
|
|
pdl> p $x=sqrt float [1..10] |
|
3260
|
|
|
|
|
|
|
[1 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228] |
|
3261
|
|
|
|
|
|
|
pdl> p byte $x |
|
3262
|
|
|
|
|
|
|
[1 1 1 2 2 2 2 2 3 3] |
|
3263
|
|
|
|
|
|
|
|
|
3264
|
|
|
|
|
|
|
=head2 byte |
|
3265
|
|
|
|
|
|
|
|
|
3266
|
|
|
|
|
|
|
Convert to byte datatype |
|
3267
|
|
|
|
|
|
|
|
|
3268
|
|
|
|
|
|
|
=head2 short |
|
3269
|
|
|
|
|
|
|
|
|
3270
|
|
|
|
|
|
|
Convert to short datatype |
|
3271
|
|
|
|
|
|
|
|
|
3272
|
|
|
|
|
|
|
=head2 ushort |
|
3273
|
|
|
|
|
|
|
|
|
3274
|
|
|
|
|
|
|
Convert to ushort datatype |
|
3275
|
|
|
|
|
|
|
|
|
3276
|
|
|
|
|
|
|
=head2 long |
|
3277
|
|
|
|
|
|
|
|
|
3278
|
|
|
|
|
|
|
Convert to long datatype |
|
3279
|
|
|
|
|
|
|
|
|
3280
|
|
|
|
|
|
|
=head2 indx |
|
3281
|
|
|
|
|
|
|
|
|
3282
|
|
|
|
|
|
|
Convert to indx datatype |
|
3283
|
|
|
|
|
|
|
|
|
3284
|
|
|
|
|
|
|
=head2 longlong |
|
3285
|
|
|
|
|
|
|
|
|
3286
|
|
|
|
|
|
|
Convert to longlong datatype |
|
3287
|
|
|
|
|
|
|
|
|
3288
|
|
|
|
|
|
|
=head2 float |
|
3289
|
|
|
|
|
|
|
|
|
3290
|
|
|
|
|
|
|
Convert to float datatype |
|
3291
|
|
|
|
|
|
|
|
|
3292
|
|
|
|
|
|
|
=head2 double |
|
3293
|
|
|
|
|
|
|
|
|
3294
|
|
|
|
|
|
|
Convert to double datatype |
|
3295
|
|
|
|
|
|
|
|
|
3296
|
|
|
|
|
|
|
=head2 ldouble |
|
3297
|
|
|
|
|
|
|
|
|
3298
|
|
|
|
|
|
|
Convert to long double datatype |
|
3299
|
|
|
|
|
|
|
|
|
3300
|
|
|
|
|
|
|
=head2 cfloat |
|
3301
|
|
|
|
|
|
|
|
|
3302
|
|
|
|
|
|
|
Convert to complex float datatype |
|
3303
|
|
|
|
|
|
|
|
|
3304
|
|
|
|
|
|
|
=head2 cdouble |
|
3305
|
|
|
|
|
|
|
|
|
3306
|
|
|
|
|
|
|
Convert to complex double datatype |
|
3307
|
|
|
|
|
|
|
|
|
3308
|
|
|
|
|
|
|
=head2 cldouble |
|
3309
|
|
|
|
|
|
|
|
|
3310
|
|
|
|
|
|
|
Convert to complex long double datatype |
|
3311
|
|
|
|
|
|
|
|
|
3312
|
|
|
|
|
|
|
=head2 type |
|
3313
|
|
|
|
|
|
|
|
|
3314
|
|
|
|
|
|
|
=for ref |
|
3315
|
|
|
|
|
|
|
|
|
3316
|
|
|
|
|
|
|
return the type of an ndarray as a blessed type object |
|
3317
|
|
|
|
|
|
|
|
|
3318
|
|
|
|
|
|
|
A convenience function for use with the ndarray constructors, e.g. |
|
3319
|
|
|
|
|
|
|
|
|
3320
|
|
|
|
|
|
|
=for example |
|
3321
|
|
|
|
|
|
|
|
|
3322
|
|
|
|
|
|
|
$y = PDL->zeroes($x->type,$x->dims,3); |
|
3323
|
|
|
|
|
|
|
die "must be float" unless $x->type == float; |
|
3324
|
|
|
|
|
|
|
|
|
3325
|
|
|
|
|
|
|
See also the discussion of the C class in L. |
|
3326
|
|
|
|
|
|
|
Note that the C objects have overloaded comparison and |
|
3327
|
|
|
|
|
|
|
stringify operators so that you can compare and print types: |
|
3328
|
|
|
|
|
|
|
|
|
3329
|
|
|
|
|
|
|
$x = $x->float if $x->type < float; |
|
3330
|
|
|
|
|
|
|
$t = $x->type; print "Type is $t\n"; |
|
3331
|
|
|
|
|
|
|
|
|
3332
|
|
|
|
|
|
|
=cut |
|
3333
|
|
|
|
|
|
|
|
|
3334
|
13113
|
|
|
13113
|
0
|
99316
|
sub PDL::type { return PDL::Type->new($_[0]->get_datatype); } |
|
3335
|
|
|
|
|
|
|
|
|
3336
|
|
|
|
|
|
|
##################### Printing #################### |
|
3337
|
|
|
|
|
|
|
|
|
3338
|
|
|
|
|
|
|
=head2 string |
|
3339
|
|
|
|
|
|
|
|
|
3340
|
|
|
|
|
|
|
=for ref |
|
3341
|
|
|
|
|
|
|
|
|
3342
|
|
|
|
|
|
|
Convert ndarray to string, optionally using a C format. If such |
|
3343
|
|
|
|
|
|
|
a format is provided, it is used. If not, then the formatting variables |
|
3344
|
|
|
|
|
|
|
in L provide a default, though heuristics are attempted to |
|
3345
|
|
|
|
|
|
|
make a nice-looking output. |
|
3346
|
|
|
|
|
|
|
|
|
3347
|
|
|
|
|
|
|
=for usage |
|
3348
|
|
|
|
|
|
|
|
|
3349
|
|
|
|
|
|
|
print $x; # overloaded |
|
3350
|
|
|
|
|
|
|
print $x->string; # explicit method call |
|
3351
|
|
|
|
|
|
|
print $x->string("%5d"); # providing sprintf format |
|
3352
|
|
|
|
|
|
|
|
|
3353
|
|
|
|
|
|
|
=cut |
|
3354
|
|
|
|
|
|
|
|
|
3355
|
|
|
|
|
|
|
$PDL::_STRINGIZING = 0; |
|
3356
|
|
|
|
|
|
|
|
|
3357
|
|
|
|
|
|
|
sub PDL::string { |
|
3358
|
3494
|
|
|
3494
|
0
|
60688
|
my ($self,$format) = @_; |
|
3359
|
3494
|
|
|
|
|
6205
|
my $to_return = eval { |
|
3360
|
3494
|
100
|
|
|
|
8593
|
return "ALREADY_STRINGIZING_NO_LOOPS" if $PDL::_STRINGIZING; |
|
3361
|
1451
|
|
|
|
|
2453
|
local $PDL::_STRINGIZING = 1; |
|
3362
|
1451
|
100
|
|
|
|
4804
|
return "Null" if $self->isnull; |
|
3363
|
1443
|
100
|
|
|
|
3453
|
return "Empty[".join("x",$self->dims)."]" if $self->isempty; |
|
3364
|
1436
|
100
|
|
|
|
4895
|
return "TOO LONG TO PRINT" if $self->nelem > $PDL::toolongtoprint; |
|
3365
|
1429
|
|
|
|
|
3682
|
my $ndims = $self->getndims; |
|
3366
|
1429
|
100
|
|
|
|
3597
|
if ($ndims==0) { |
|
3367
|
1027
|
100
|
100
|
|
|
14351
|
return "BAD" if $self->badflag and $self->isbad; |
|
3368
|
1014
|
|
|
|
|
9569
|
my $x = $self->sclr; |
|
3369
|
1014
|
50
|
|
|
|
5567
|
return $format ? sprintf($format, $x) : "$x"; |
|
3370
|
|
|
|
|
|
|
} |
|
3371
|
402
|
50
|
|
|
|
1377
|
local $sep = $PDL::use_commas ? "," : " "; |
|
3372
|
402
|
50
|
|
|
|
1072
|
local $sep2 = $PDL::use_commas ? "," : ""; |
|
3373
|
402
|
100
|
|
|
|
1568
|
return str1D($self,$format) if $ndims==1; |
|
3374
|
105
|
|
|
|
|
583
|
return strND($self,$format,0); |
|
3375
|
|
|
|
|
|
|
}; |
|
3376
|
3494
|
50
|
|
|
|
16822
|
if ($@) { |
|
3377
|
|
|
|
|
|
|
# Remove reference to this line: |
|
3378
|
0
|
|
|
|
|
0
|
$@ =~ s/\s*at .* line \d+\s*\.\n*/./; |
|
3379
|
0
|
|
|
|
|
0
|
PDL::Core::barf("Stringizing problem: $@"); |
|
3380
|
|
|
|
|
|
|
} |
|
3381
|
3494
|
|
|
|
|
75051
|
return $to_return; |
|
3382
|
|
|
|
|
|
|
} |
|
3383
|
|
|
|
|
|
|
|
|
3384
|
|
|
|
|
|
|
############## Section/subsection functions ################### |
|
3385
|
|
|
|
|
|
|
|
|
3386
|
|
|
|
|
|
|
=head2 list |
|
3387
|
|
|
|
|
|
|
|
|
3388
|
|
|
|
|
|
|
=for ref |
|
3389
|
|
|
|
|
|
|
|
|
3390
|
|
|
|
|
|
|
Convert ndarray to perl list |
|
3391
|
|
|
|
|
|
|
|
|
3392
|
|
|
|
|
|
|
=for usage |
|
3393
|
|
|
|
|
|
|
|
|
3394
|
|
|
|
|
|
|
@tmp = list $x; |
|
3395
|
|
|
|
|
|
|
|
|
3396
|
|
|
|
|
|
|
Obviously this is grossly inefficient for the large datasets PDL is designed to |
|
3397
|
|
|
|
|
|
|
handle. This was provided as a get out while PDL matured. It should now be mostly |
|
3398
|
|
|
|
|
|
|
superseded by superior constructs, such as PP/broadcasting. However it is still |
|
3399
|
|
|
|
|
|
|
occasionally useful and is provided for backwards compatibility. |
|
3400
|
|
|
|
|
|
|
|
|
3401
|
|
|
|
|
|
|
=for example |
|
3402
|
|
|
|
|
|
|
|
|
3403
|
|
|
|
|
|
|
for (list $x) { |
|
3404
|
|
|
|
|
|
|
# Do something on each value... |
|
3405
|
|
|
|
|
|
|
} |
|
3406
|
|
|
|
|
|
|
|
|
3407
|
|
|
|
|
|
|
=for bad |
|
3408
|
|
|
|
|
|
|
|
|
3409
|
|
|
|
|
|
|
list converts any bad values into the string 'BAD'. |
|
3410
|
|
|
|
|
|
|
|
|
3411
|
|
|
|
|
|
|
=cut |
|
3412
|
|
|
|
|
|
|
|
|
3413
|
|
|
|
|
|
|
# No broadcasting, just the ordinary dims. |
|
3414
|
|
|
|
|
|
|
sub PDL::list{ # pdl -> @list |
|
3415
|
30
|
50
|
|
30
|
0
|
156
|
barf 'Usage: list($pdl)' if $#_!=0; |
|
3416
|
30
|
|
|
|
|
119
|
my $pdl = PDL->topdl(shift); |
|
3417
|
30
|
50
|
|
|
|
159
|
return () if nelem($pdl)==0; |
|
3418
|
30
|
|
|
|
|
57
|
@{listref_c($pdl)}; |
|
|
30
|
|
|
|
|
406
|
|
|
3419
|
|
|
|
|
|
|
} |
|
3420
|
|
|
|
|
|
|
|
|
3421
|
|
|
|
|
|
|
=head2 unpdl |
|
3422
|
|
|
|
|
|
|
|
|
3423
|
|
|
|
|
|
|
=for ref |
|
3424
|
|
|
|
|
|
|
|
|
3425
|
|
|
|
|
|
|
Convert ndarray to nested Perl array references |
|
3426
|
|
|
|
|
|
|
|
|
3427
|
|
|
|
|
|
|
=for usage |
|
3428
|
|
|
|
|
|
|
|
|
3429
|
|
|
|
|
|
|
$arrayref = unpdl $x; |
|
3430
|
|
|
|
|
|
|
|
|
3431
|
|
|
|
|
|
|
This function returns a reference to a Perl list-of-lists structure |
|
3432
|
|
|
|
|
|
|
equivalent to the input ndarray (within the limitation that while values |
|
3433
|
|
|
|
|
|
|
of elements should be preserved, the detailed datatypes will not as |
|
3434
|
|
|
|
|
|
|
perl itself basically has "number" data rather than byte, short, int... |
|
3435
|
|
|
|
|
|
|
E.g., C<< sum($x - pdl( $x->unpdl )) >> should equal 0. |
|
3436
|
|
|
|
|
|
|
|
|
3437
|
|
|
|
|
|
|
Obviously this is grossly inefficient in memory and processing for the |
|
3438
|
|
|
|
|
|
|
large datasets PDL is designed to handle. Sometimes, however, you really |
|
3439
|
|
|
|
|
|
|
want to move your data back to Perl, and with proper dimensionality, |
|
3440
|
|
|
|
|
|
|
unlike C. |
|
3441
|
|
|
|
|
|
|
|
|
3442
|
|
|
|
|
|
|
If you want to round-trip data including the use of C, |
|
3443
|
|
|
|
|
|
|
C does not support this. However, it is suggested you would |
|
3444
|
|
|
|
|
|
|
generate an index-set with C<< $pdl->whereND($pdl == $PDL::undefval) |
|
3445
|
|
|
|
|
|
|
>>, then loop over the Perl data, setting those locations to C. |
|
3446
|
|
|
|
|
|
|
|
|
3447
|
|
|
|
|
|
|
Another round-trip caveat: a zero-dimensional ndarray (a scalar) will be |
|
3448
|
|
|
|
|
|
|
returned as a single-element array-ref. This is conceptually incorrect, |
|
3449
|
|
|
|
|
|
|
but cannot now be changed due to backward compatibility. |
|
3450
|
|
|
|
|
|
|
|
|
3451
|
|
|
|
|
|
|
=for example |
|
3452
|
|
|
|
|
|
|
|
|
3453
|
|
|
|
|
|
|
use JSON; |
|
3454
|
|
|
|
|
|
|
my $json = encode_json unpdl $pdl; |
|
3455
|
|
|
|
|
|
|
|
|
3456
|
|
|
|
|
|
|
=for bad |
|
3457
|
|
|
|
|
|
|
|
|
3458
|
|
|
|
|
|
|
unpdl converts any bad values into the string 'BAD'. |
|
3459
|
|
|
|
|
|
|
|
|
3460
|
|
|
|
|
|
|
=cut |
|
3461
|
|
|
|
|
|
|
|
|
3462
|
|
|
|
|
|
|
=head2 listindices |
|
3463
|
|
|
|
|
|
|
|
|
3464
|
|
|
|
|
|
|
=for ref |
|
3465
|
|
|
|
|
|
|
|
|
3466
|
|
|
|
|
|
|
Convert ndarray indices to perl list |
|
3467
|
|
|
|
|
|
|
|
|
3468
|
|
|
|
|
|
|
=for usage |
|
3469
|
|
|
|
|
|
|
|
|
3470
|
|
|
|
|
|
|
@tmp = listindices $x; |
|
3471
|
|
|
|
|
|
|
|
|
3472
|
|
|
|
|
|
|
C<@tmp> now contains the values C<0..nelem($x)-1>. |
|
3473
|
|
|
|
|
|
|
|
|
3474
|
|
|
|
|
|
|
Obviously this is grossly inefficient for the large datasets PDL is designed to |
|
3475
|
|
|
|
|
|
|
handle. This was provided as a get out while PDL matured. It should now be mostly |
|
3476
|
|
|
|
|
|
|
superseded by superior constructs, such as PP/broadcasting. However it is still |
|
3477
|
|
|
|
|
|
|
occasionally useful and is provied for backwards compatibility. |
|
3478
|
|
|
|
|
|
|
|
|
3479
|
|
|
|
|
|
|
=for example |
|
3480
|
|
|
|
|
|
|
|
|
3481
|
|
|
|
|
|
|
for $i (listindices $x) { |
|
3482
|
|
|
|
|
|
|
# Do something on each value... |
|
3483
|
|
|
|
|
|
|
} |
|
3484
|
|
|
|
|
|
|
|
|
3485
|
|
|
|
|
|
|
=cut |
|
3486
|
|
|
|
|
|
|
|
|
3487
|
|
|
|
|
|
|
sub PDL::listindices{ # Return list of index values for 1D pdl |
|
3488
|
0
|
0
|
|
0
|
0
|
0
|
barf 'Usage: list($pdl)' if $#_!=0; |
|
3489
|
0
|
|
|
|
|
0
|
my $pdl = shift; |
|
3490
|
0
|
0
|
|
|
|
0
|
return () if nelem($pdl)==0; |
|
3491
|
0
|
0
|
|
|
|
0
|
barf 'Not 1D' if scalar(dims($pdl)) != 1; |
|
3492
|
0
|
|
|
|
|
0
|
return (0..nelem($pdl)-1); |
|
3493
|
|
|
|
|
|
|
} |
|
3494
|
|
|
|
|
|
|
|
|
3495
|
|
|
|
|
|
|
=head2 set |
|
3496
|
|
|
|
|
|
|
|
|
3497
|
|
|
|
|
|
|
=for ref |
|
3498
|
|
|
|
|
|
|
|
|
3499
|
|
|
|
|
|
|
Set a single value inside an ndarray |
|
3500
|
|
|
|
|
|
|
|
|
3501
|
|
|
|
|
|
|
=for usage |
|
3502
|
|
|
|
|
|
|
|
|
3503
|
|
|
|
|
|
|
set $ndarray, @position, $value |
|
3504
|
|
|
|
|
|
|
|
|
3505
|
|
|
|
|
|
|
C<@position> is a coordinate list, of size equal to the |
|
3506
|
|
|
|
|
|
|
number of dimensions in the ndarray. Occasionally useful, |
|
3507
|
|
|
|
|
|
|
mainly provided for backwards compatibility as superseded |
|
3508
|
|
|
|
|
|
|
by use of L and assignment operator C<.=>. |
|
3509
|
|
|
|
|
|
|
|
|
3510
|
|
|
|
|
|
|
=for example |
|
3511
|
|
|
|
|
|
|
|
|
3512
|
|
|
|
|
|
|
pdl> $x = sequence 3,4 |
|
3513
|
|
|
|
|
|
|
pdl> set $x, 2,1,99 |
|
3514
|
|
|
|
|
|
|
pdl> p $x |
|
3515
|
|
|
|
|
|
|
[ |
|
3516
|
|
|
|
|
|
|
[ 0 1 2] |
|
3517
|
|
|
|
|
|
|
[ 3 4 99] |
|
3518
|
|
|
|
|
|
|
[ 6 7 8] |
|
3519
|
|
|
|
|
|
|
[ 9 10 11] |
|
3520
|
|
|
|
|
|
|
] |
|
3521
|
|
|
|
|
|
|
|
|
3522
|
|
|
|
|
|
|
=cut |
|
3523
|
|
|
|
|
|
|
|
|
3524
|
|
|
|
|
|
|
sub PDL::set{ # Sets a particular single value |
|
3525
|
58
|
50
|
|
58
|
0
|
8797
|
barf 'Usage: set($pdl, $x, $y,.., $value)' if $#_<2; |
|
3526
|
58
|
|
|
|
|
101
|
my $self = shift; my $value = pop @_; |
|
|
58
|
|
|
|
|
113
|
|
|
3527
|
58
|
|
|
|
|
679
|
set_c ($self, [@_], $value); |
|
3528
|
57
|
|
|
|
|
200
|
return $self; |
|
3529
|
|
|
|
|
|
|
} |
|
3530
|
|
|
|
|
|
|
|
|
3531
|
|
|
|
|
|
|
=head2 at |
|
3532
|
|
|
|
|
|
|
|
|
3533
|
|
|
|
|
|
|
=for ref |
|
3534
|
|
|
|
|
|
|
|
|
3535
|
|
|
|
|
|
|
Returns a single value inside an ndarray as perl scalar. |
|
3536
|
|
|
|
|
|
|
If the ndarray is a native complex value (cdouble, cfloat), it will |
|
3537
|
|
|
|
|
|
|
be a L object. |
|
3538
|
|
|
|
|
|
|
|
|
3539
|
|
|
|
|
|
|
=for usage |
|
3540
|
|
|
|
|
|
|
|
|
3541
|
|
|
|
|
|
|
$z = at($ndarray, @position); $z=$ndarray->at(@position); |
|
3542
|
|
|
|
|
|
|
|
|
3543
|
|
|
|
|
|
|
C<@position> is a coordinate list, of size equal to the |
|
3544
|
|
|
|
|
|
|
number of dimensions in the ndarray. Occasionally useful |
|
3545
|
|
|
|
|
|
|
in a general context, quite useful too inside PDL internals. |
|
3546
|
|
|
|
|
|
|
|
|
3547
|
|
|
|
|
|
|
=for example |
|
3548
|
|
|
|
|
|
|
|
|
3549
|
|
|
|
|
|
|
pdl> $x = sequence 3,4 |
|
3550
|
|
|
|
|
|
|
pdl> p $x->at(1,2) |
|
3551
|
|
|
|
|
|
|
7 |
|
3552
|
|
|
|
|
|
|
|
|
3553
|
|
|
|
|
|
|
=for bad |
|
3554
|
|
|
|
|
|
|
|
|
3555
|
|
|
|
|
|
|
at converts any bad values into the string 'BAD'. |
|
3556
|
|
|
|
|
|
|
|
|
3557
|
|
|
|
|
|
|
=cut |
|
3558
|
|
|
|
|
|
|
|
|
3559
|
|
|
|
|
|
|
sub PDL::at { # Return value at ($x,$y,$z...) |
|
3560
|
19879
|
50
|
|
19879
|
0
|
222120
|
barf 'Usage: at($pdl, $x, $y, ...)' if $#_<0; |
|
3561
|
19879
|
|
|
|
|
30707
|
my $self = shift; |
|
3562
|
19879
|
|
|
|
|
163252
|
at_bad_c ($self, [@_]); |
|
3563
|
|
|
|
|
|
|
} |
|
3564
|
|
|
|
|
|
|
|
|
3565
|
|
|
|
|
|
|
=head2 sclr |
|
3566
|
|
|
|
|
|
|
|
|
3567
|
|
|
|
|
|
|
=for ref |
|
3568
|
|
|
|
|
|
|
|
|
3569
|
|
|
|
|
|
|
return a single value from an ndarray as a scalar, ignoring whether it is bad. |
|
3570
|
|
|
|
|
|
|
|
|
3571
|
|
|
|
|
|
|
=for example |
|
3572
|
|
|
|
|
|
|
|
|
3573
|
|
|
|
|
|
|
$val = $x(10)->sclr; |
|
3574
|
|
|
|
|
|
|
$val = sclr inner($x,$y); |
|
3575
|
|
|
|
|
|
|
|
|
3576
|
|
|
|
|
|
|
The C method is useful to turn a single-element ndarray into a normal Perl |
|
3577
|
|
|
|
|
|
|
scalar. Its main advantage over using C for this purpose is the fact |
|
3578
|
|
|
|
|
|
|
that you do not need to worry if the ndarray is 0D, 1D or higher dimensional. |
|
3579
|
|
|
|
|
|
|
Using C you have to supply the correct number of zeroes, e.g. |
|
3580
|
|
|
|
|
|
|
|
|
3581
|
|
|
|
|
|
|
$x = sequence(10); |
|
3582
|
|
|
|
|
|
|
$y = $x->slice('4'); |
|
3583
|
|
|
|
|
|
|
print $y->sclr; # no problem |
|
3584
|
|
|
|
|
|
|
print $y->at(); # error: needs at least one zero |
|
3585
|
|
|
|
|
|
|
|
|
3586
|
|
|
|
|
|
|
C is generally used when a Perl scalar is required instead |
|
3587
|
|
|
|
|
|
|
of a one-element ndarray. As of 2.064, if the input is a multielement ndarray |
|
3588
|
|
|
|
|
|
|
it will throw an exception. |
|
3589
|
|
|
|
|
|
|
|
|
3590
|
|
|
|
|
|
|
=head2 cat |
|
3591
|
|
|
|
|
|
|
|
|
3592
|
|
|
|
|
|
|
=for ref |
|
3593
|
|
|
|
|
|
|
|
|
3594
|
|
|
|
|
|
|
concatenate ndarrays to N+1 dimensional ndarray |
|
3595
|
|
|
|
|
|
|
|
|
3596
|
|
|
|
|
|
|
Takes a list of N ndarrays of same shape as argument, |
|
3597
|
|
|
|
|
|
|
returns a single ndarray of dimension N+1. |
|
3598
|
|
|
|
|
|
|
|
|
3599
|
|
|
|
|
|
|
=for example |
|
3600
|
|
|
|
|
|
|
|
|
3601
|
|
|
|
|
|
|
pdl> $x = cat ones(3,3),zeroes(3,3),rvals(3,3); p $x |
|
3602
|
|
|
|
|
|
|
[ |
|
3603
|
|
|
|
|
|
|
[ |
|
3604
|
|
|
|
|
|
|
[1 1 1] |
|
3605
|
|
|
|
|
|
|
[1 1 1] |
|
3606
|
|
|
|
|
|
|
[1 1 1] |
|
3607
|
|
|
|
|
|
|
] |
|
3608
|
|
|
|
|
|
|
[ |
|
3609
|
|
|
|
|
|
|
[0 0 0] |
|
3610
|
|
|
|
|
|
|
[0 0 0] |
|
3611
|
|
|
|
|
|
|
[0 0 0] |
|
3612
|
|
|
|
|
|
|
] |
|
3613
|
|
|
|
|
|
|
[ |
|
3614
|
|
|
|
|
|
|
[1 1 1] |
|
3615
|
|
|
|
|
|
|
[1 0 1] |
|
3616
|
|
|
|
|
|
|
[1 1 1] |
|
3617
|
|
|
|
|
|
|
] |
|
3618
|
|
|
|
|
|
|
] |
|
3619
|
|
|
|
|
|
|
|
|
3620
|
|
|
|
|
|
|
=for bad |
|
3621
|
|
|
|
|
|
|
|
|
3622
|
|
|
|
|
|
|
The output ndarray is set bad if any input ndarrays have their bad flag set. |
|
3623
|
|
|
|
|
|
|
|
|
3624
|
|
|
|
|
|
|
Similar functions include L, which |
|
3625
|
|
|
|
|
|
|
appends only two ndarrays along their first dimension, and |
|
3626
|
|
|
|
|
|
|
L, which can append more than two ndarrays |
|
3627
|
|
|
|
|
|
|
along an arbitrary dimension. |
|
3628
|
|
|
|
|
|
|
|
|
3629
|
|
|
|
|
|
|
Also consider the generic constructor L, which can handle |
|
3630
|
|
|
|
|
|
|
ndarrays of different sizes (with zero-padding), and will return a |
|
3631
|
|
|
|
|
|
|
ndarray of type 'double' by default, but may be considerably faster (up |
|
3632
|
|
|
|
|
|
|
to 10x) than cat. |
|
3633
|
|
|
|
|
|
|
|
|
3634
|
|
|
|
|
|
|
=cut |
|
3635
|
|
|
|
|
|
|
|
|
3636
|
|
|
|
|
|
|
# takes a list of array-refs with dims, returns list of maximalised |
|
3637
|
|
|
|
|
|
|
# broadcast-compatible dim lengths |
|
3638
|
|
|
|
|
|
|
sub dims_filled { |
|
3639
|
41
|
|
|
41
|
0
|
91
|
my @resdims = @{shift()}; |
|
|
41
|
|
|
|
|
148
|
|
|
3640
|
41
|
|
|
|
|
173
|
while (@_) { |
|
3641
|
52
|
|
|
|
|
83
|
my @d = @{shift()}; |
|
|
52
|
|
|
|
|
111
|
|
|
3642
|
52
|
|
|
|
|
193
|
for my $j (0..$#d) { |
|
3643
|
59
|
100
|
66
|
|
|
391
|
$resdims[$j] = $d[$j] if( !defined($resdims[$j]) or $resdims[$j]==1 ); |
|
3644
|
59
|
100
|
66
|
|
|
383
|
die "mismatched dims\n" if $d[$j] != 1 and $resdims[$j] != $d[$j]; |
|
3645
|
|
|
|
|
|
|
} |
|
3646
|
|
|
|
|
|
|
} |
|
3647
|
38
|
|
|
|
|
138
|
@resdims; |
|
3648
|
|
|
|
|
|
|
} |
|
3649
|
|
|
|
|
|
|
|
|
3650
|
|
|
|
|
|
|
sub PDL::cat { |
|
3651
|
41
|
50
|
|
41
|
0
|
233
|
barf("Called PDL::cat without any arguments") unless @_; |
|
3652
|
41
|
|
|
|
|
92
|
my (@yes_ndarray, @not_a_ndarray); |
|
3653
|
41
|
100
|
|
|
|
172
|
push @{UNIVERSAL::isa($_[$_], 'PDL')?\@yes_ndarray:\@not_a_ndarray}, $_ for 0..$#_; |
|
|
104
|
|
|
|
|
455
|
|
|
3654
|
41
|
50
|
|
|
|
160
|
barf("Called PDL::cat without any ndarray arguments") if !@yes_ndarray; |
|
3655
|
41
|
|
|
|
|
123
|
my $old_err = $@; |
|
3656
|
41
|
|
|
|
|
85
|
$@ = ''; |
|
3657
|
41
|
|
|
|
|
92
|
my @resdims = eval { dims_filled(map [$_->dims], @_[@yes_ndarray]) }; |
|
|
41
|
|
|
|
|
490
|
|
|
3658
|
41
|
100
|
100
|
|
|
294
|
if (!$@ and $yes_ndarray[0] == 0) { |
|
3659
|
36
|
|
|
|
|
306
|
my $res; |
|
3660
|
36
|
|
|
|
|
84
|
eval { |
|
3661
|
36
|
|
|
|
|
269
|
$res = $_[0]->initialize; |
|
3662
|
36
|
|
|
|
|
345
|
$res->set_datatype(max(map $_->get_datatype, @_)); |
|
3663
|
|
|
|
|
|
|
|
|
3664
|
36
|
|
|
|
|
248
|
$res->setdims([@resdims,scalar(@_)]); |
|
3665
|
36
|
|
|
|
|
568
|
my @dog = $res->dog; |
|
3666
|
36
|
|
|
|
|
296
|
$dog[$_] .= $_[$_] for 0..$#_; |
|
3667
|
|
|
|
|
|
|
|
|
3668
|
|
|
|
|
|
|
# propagate any bad flags |
|
3669
|
36
|
100
|
|
|
|
111
|
for (@_) { if ( $_->badflag() ) { $res->badflag(1); last; } } |
|
|
79
|
|
|
|
|
576
|
|
|
|
2
|
|
|
|
|
10
|
|
|
|
2
|
|
|
|
|
27
|
|
|
3670
|
|
|
|
|
|
|
}; |
|
3671
|
36
|
50
|
|
|
|
358
|
$@ = $old_err, return $res if !$@; # Restore the old error and return |
|
3672
|
|
|
|
|
|
|
} |
|
3673
|
|
|
|
|
|
|
|
|
3674
|
|
|
|
|
|
|
# If we've gotten here, then there's been an error, so check things |
|
3675
|
|
|
|
|
|
|
# and barf out a meaningful message. |
|
3676
|
|
|
|
|
|
|
|
|
3677
|
5
|
|
|
|
|
13
|
my ($first_ndarray_argument, @mismatched_dims) = $yes_ndarray[0]; |
|
3678
|
5
|
100
|
66
|
|
|
42
|
if ($@ and $@ =~ /mismatched/) { |
|
3679
|
|
|
|
|
|
|
# Get the dimensions of the first actual ndarray in the argument list: |
|
3680
|
3
|
|
|
|
|
13
|
my @dims = $_[$first_ndarray_argument]->dims; |
|
3681
|
|
|
|
|
|
|
# Figure out all the ways that the caller screwed up: |
|
3682
|
3
|
|
|
|
|
8
|
for my $i (@yes_ndarray) { |
|
3683
|
10
|
|
|
|
|
12
|
my $arg = $_[$i]; |
|
3684
|
10
|
100
|
|
|
|
22
|
if (@dims != $arg->ndims) { # Check if different number of dimensions |
|
3685
|
3
|
|
|
|
|
4
|
push @mismatched_dims, $i; |
|
3686
|
|
|
|
|
|
|
} else { # Check if size of dimensions agree |
|
3687
|
7
|
|
|
|
|
14
|
DIMENSION: for (my $j = 0; $j < @dims; $j++) { |
|
3688
|
7
|
100
|
|
|
|
23
|
next if $dims[$j] == $arg->dim($j); |
|
3689
|
2
|
|
|
|
|
4
|
push @mismatched_dims, $i; |
|
3690
|
2
|
|
|
|
|
4
|
last DIMENSION; |
|
3691
|
|
|
|
|
|
|
} |
|
3692
|
|
|
|
|
|
|
} |
|
3693
|
10
|
|
|
|
|
12
|
$i++; |
|
3694
|
|
|
|
|
|
|
} |
|
3695
|
|
|
|
|
|
|
} |
|
3696
|
|
|
|
|
|
|
# Handle the edge case that something else happened: |
|
3697
|
5
|
50
|
66
|
|
|
45
|
barf "cat: unknown error from the internals:\n$@" |
|
|
|
|
66
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
3698
|
|
|
|
|
|
|
if ($@ and $@ !~ /PDL::Ops::assgn|mismatched/) or |
|
3699
|
|
|
|
|
|
|
(!@not_a_ndarray and !@mismatched_dims); |
|
3700
|
|
|
|
|
|
|
|
|
3701
|
|
|
|
|
|
|
# Construct a message detailing the results |
|
3702
|
5
|
|
|
|
|
9
|
my $message = "bad arguments passed to function PDL::cat\n"; |
|
3703
|
5
|
100
|
|
|
|
18
|
if (@mismatched_dims > 1) { |
|
|
|
100
|
|
|
|
|
|
|
3704
|
|
|
|
|
|
|
# Many dimension mismatches |
|
3705
|
2
|
|
|
|
|
12
|
$message .= "The dimensions of arguments " |
|
3706
|
|
|
|
|
|
|
. join(', ', @mismatched_dims[0 .. $#mismatched_dims-1]) |
|
3707
|
|
|
|
|
|
|
. " and $mismatched_dims[-1] do not match the\n" |
|
3708
|
|
|
|
|
|
|
. " dimensions of the first ndarray argument (argument $first_ndarray_argument).\n"; |
|
3709
|
|
|
|
|
|
|
} elsif (@mismatched_dims) { |
|
3710
|
|
|
|
|
|
|
# One dimension mismatch |
|
3711
|
1
|
|
|
|
|
7
|
$message .= "The dimensions of argument $mismatched_dims[0] do not match the\n" |
|
3712
|
|
|
|
|
|
|
. " dimensions of the first ndarray argument (argument $first_ndarray_argument).\n"; |
|
3713
|
|
|
|
|
|
|
} |
|
3714
|
5
|
100
|
|
|
|
14
|
if (@not_a_ndarray > 1) { |
|
|
|
100
|
|
|
|
|
|
|
3715
|
|
|
|
|
|
|
# many non-ndarrays |
|
3716
|
2
|
|
|
|
|
14
|
$message .= "Arguments " . join(', ', @not_a_ndarray[0 .. $#not_a_ndarray-1]) |
|
3717
|
|
|
|
|
|
|
. " and $not_a_ndarray[-1] are not ndarrays.\n"; |
|
3718
|
|
|
|
|
|
|
} elsif (@not_a_ndarray) { |
|
3719
|
|
|
|
|
|
|
# one non-ndarray |
|
3720
|
1
|
|
|
|
|
3
|
$message .= "Argument $not_a_ndarray[0] is not an ndarray.\n"; |
|
3721
|
|
|
|
|
|
|
} |
|
3722
|
5
|
|
|
|
|
74
|
croak($message . "(Argument counting starts from zero.)"); |
|
3723
|
|
|
|
|
|
|
} |
|
3724
|
|
|
|
|
|
|
|
|
3725
|
|
|
|
|
|
|
=head2 dog |
|
3726
|
|
|
|
|
|
|
|
|
3727
|
|
|
|
|
|
|
=for ref |
|
3728
|
|
|
|
|
|
|
|
|
3729
|
|
|
|
|
|
|
Opposite of 'cat' :). Split N dim ndarray to list of N-1 dim ndarrays |
|
3730
|
|
|
|
|
|
|
|
|
3731
|
|
|
|
|
|
|
Takes a single N-dimensional ndarray and splits it into a list of N-1 dimensional |
|
3732
|
|
|
|
|
|
|
ndarrays. The breakup is done along the last dimension. |
|
3733
|
|
|
|
|
|
|
Note the dataflowed connection is still preserved by default, |
|
3734
|
|
|
|
|
|
|
e.g.: |
|
3735
|
|
|
|
|
|
|
|
|
3736
|
|
|
|
|
|
|
=for example |
|
3737
|
|
|
|
|
|
|
|
|
3738
|
|
|
|
|
|
|
pdl> $p = ones 3,3,3 |
|
3739
|
|
|
|
|
|
|
pdl> ($x,$y,$c) = dog $p |
|
3740
|
|
|
|
|
|
|
pdl> $y++; p $p |
|
3741
|
|
|
|
|
|
|
[ |
|
3742
|
|
|
|
|
|
|
[ |
|
3743
|
|
|
|
|
|
|
[1 1 1] |
|
3744
|
|
|
|
|
|
|
[1 1 1] |
|
3745
|
|
|
|
|
|
|
[1 1 1] |
|
3746
|
|
|
|
|
|
|
] |
|
3747
|
|
|
|
|
|
|
[ |
|
3748
|
|
|
|
|
|
|
[2 2 2] |
|
3749
|
|
|
|
|
|
|
[2 2 2] |
|
3750
|
|
|
|
|
|
|
[2 2 2] |
|
3751
|
|
|
|
|
|
|
] |
|
3752
|
|
|
|
|
|
|
[ |
|
3753
|
|
|
|
|
|
|
[1 1 1] |
|
3754
|
|
|
|
|
|
|
[1 1 1] |
|
3755
|
|
|
|
|
|
|
[1 1 1] |
|
3756
|
|
|
|
|
|
|
] |
|
3757
|
|
|
|
|
|
|
] |
|
3758
|
|
|
|
|
|
|
|
|
3759
|
|
|
|
|
|
|
=for options |
|
3760
|
|
|
|
|
|
|
|
|
3761
|
|
|
|
|
|
|
Break => 1 Break dataflow connection (new copy) |
|
3762
|
|
|
|
|
|
|
|
|
3763
|
|
|
|
|
|
|
=for bad |
|
3764
|
|
|
|
|
|
|
|
|
3765
|
|
|
|
|
|
|
The output ndarrays are set bad if the original ndarray has its bad flag set. |
|
3766
|
|
|
|
|
|
|
|
|
3767
|
|
|
|
|
|
|
=cut |
|
3768
|
|
|
|
|
|
|
|
|
3769
|
|
|
|
|
|
|
###################### Misc internal routines #################### |
|
3770
|
|
|
|
|
|
|
|
|
3771
|
|
|
|
|
|
|
# N-D array stringifier |
|
3772
|
|
|
|
|
|
|
|
|
3773
|
|
|
|
|
|
|
sub strND { |
|
3774
|
133
|
|
|
133
|
0
|
319
|
my($self,$format,$level)=@_; |
|
3775
|
|
|
|
|
|
|
# $self->make_physical(); |
|
3776
|
133
|
|
|
|
|
625
|
my @dims = $self->dims; |
|
3777
|
|
|
|
|
|
|
# print "STRND, $#dims\n"; |
|
3778
|
|
|
|
|
|
|
|
|
3779
|
133
|
100
|
|
|
|
379
|
if ($#dims==1) { # Return 2D string |
|
3780
|
127
|
|
|
|
|
399
|
return str2D($self,$format,$level); |
|
3781
|
|
|
|
|
|
|
} |
|
3782
|
|
|
|
|
|
|
else { # Return list of (N-1)D strings |
|
3783
|
6
|
|
|
|
|
29
|
my $secbas = join '',map {":,"} @dims[0..$#dims-1]; |
|
|
12
|
|
|
|
|
38
|
|
|
3784
|
6
|
|
|
|
|
23
|
my $ret="\n"." "x$level ."["; my $j; |
|
|
6
|
|
|
|
|
15
|
|
|
3785
|
6
|
|
|
|
|
31
|
for ($j=0; $j<$dims[$#dims]; $j++) { |
|
3786
|
28
|
|
|
|
|
54
|
my $sec = $secbas . "($j)"; |
|
3787
|
|
|
|
|
|
|
# print "SLICE: $sec\n"; |
|
3788
|
|
|
|
|
|
|
|
|
3789
|
28
|
|
|
|
|
91
|
$ret .= strND($self->slice($sec),$format, $level+1); |
|
3790
|
28
|
|
|
|
|
196
|
chop $ret; $ret .= $sep2; |
|
|
28
|
|
|
|
|
76
|
|
|
3791
|
|
|
|
|
|
|
} |
|
3792
|
6
|
50
|
|
|
|
20
|
chop $ret if $PDL::use_commas; |
|
3793
|
6
|
|
|
|
|
19
|
$ret .= "\n" ." "x$level ."]\n"; |
|
3794
|
6
|
|
|
|
|
57
|
return $ret; |
|
3795
|
|
|
|
|
|
|
} |
|
3796
|
|
|
|
|
|
|
} |
|
3797
|
|
|
|
|
|
|
|
|
3798
|
|
|
|
|
|
|
|
|
3799
|
|
|
|
|
|
|
# String 1D array in nice format |
|
3800
|
|
|
|
|
|
|
|
|
3801
|
|
|
|
|
|
|
sub str1D { |
|
3802
|
297
|
|
|
297
|
0
|
665
|
my($self,$format)=@_; |
|
3803
|
297
|
50
|
|
|
|
1149
|
barf "Not 1D" if $self->getndims != 1; |
|
3804
|
297
|
|
|
|
|
5061
|
my $x = listref_c($self); |
|
3805
|
297
|
|
|
|
|
3203
|
my $badflag = $self->badflag; |
|
3806
|
297
|
100
|
100
|
|
|
6343
|
return "[".join($sep, map |
|
|
|
100
|
|
|
|
|
|
|
3807
|
|
|
|
|
|
|
$badflag && $_ eq "BAD" ? $_ : |
|
3808
|
|
|
|
|
|
|
$format ? sprintf $format,$_ : $_, |
|
3809
|
|
|
|
|
|
|
@$x)."]"; |
|
3810
|
|
|
|
|
|
|
} |
|
3811
|
|
|
|
|
|
|
|
|
3812
|
|
|
|
|
|
|
sub str_list { |
|
3813
|
127
|
|
|
127
|
0
|
340
|
my ($x, $row_len, $format, $dtype, $badflag) = @_; |
|
3814
|
127
|
|
|
|
|
268
|
my ($len, $findmax) = (0, 1); |
|
3815
|
127
|
50
|
33
|
|
|
432
|
if (!defined $format || $format eq "") { |
|
3816
|
|
|
|
|
|
|
# Format not given? - find max length of default |
|
3817
|
127
|
|
|
|
|
3950
|
$len = max map length($_), @$x; |
|
3818
|
127
|
|
|
|
|
497
|
$format = "%".$len."s"; |
|
3819
|
127
|
100
|
|
|
|
319
|
if ($len>7) { # Too long? - perhaps try smaller format |
|
3820
|
11
|
50
|
|
|
|
50
|
if ($dtype == $PDL_F) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
3821
|
0
|
|
|
|
|
0
|
$format = $PDL::floatformat; |
|
3822
|
|
|
|
|
|
|
} elsif ($dtype == $PDL_D) { |
|
3823
|
7
|
|
|
|
|
15
|
$format = $PDL::doubleformat; |
|
3824
|
|
|
|
|
|
|
} elsif ($dtype == $PDL_IND) { |
|
3825
|
2
|
|
|
|
|
6
|
$format = $PDL::indxformat; |
|
3826
|
|
|
|
|
|
|
} else { |
|
3827
|
|
|
|
|
|
|
# Stick with default |
|
3828
|
2
|
|
|
|
|
5
|
$findmax = 0; |
|
3829
|
|
|
|
|
|
|
} |
|
3830
|
|
|
|
|
|
|
} else { |
|
3831
|
|
|
|
|
|
|
# Default ok |
|
3832
|
116
|
|
|
|
|
198
|
$findmax = 0; |
|
3833
|
|
|
|
|
|
|
} |
|
3834
|
|
|
|
|
|
|
} |
|
3835
|
127
|
100
|
|
|
|
381
|
$len = $badflag ? |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
3836
|
|
|
|
|
|
|
max $len, map $_ eq "BAD" ? 3 : length(sprintf $format,$_), @$x : |
|
3837
|
|
|
|
|
|
|
max $len, map length(sprintf $format,$_), @$x |
|
3838
|
|
|
|
|
|
|
if $findmax; # Find max length of strings in final format |
|
3839
|
127
|
|
|
|
|
209
|
my @ret; |
|
3840
|
127
|
|
|
|
|
512
|
for (my $i=0; $i<=$#$x; $i+=$row_len) { |
|
3841
|
583
|
100
|
100
|
|
|
9468
|
push @ret, "[".join($sep, map sprintf("%${len}s", $badflag && $_ eq "BAD" ? "BAD" : sprintf $format,$_), @$x[$i..$i+$row_len-1])."]"; |
|
3842
|
|
|
|
|
|
|
} |
|
3843
|
127
|
|
|
|
|
553
|
return @ret; |
|
3844
|
|
|
|
|
|
|
} |
|
3845
|
|
|
|
|
|
|
|
|
3846
|
|
|
|
|
|
|
# String 2D array in nice uniform format |
|
3847
|
|
|
|
|
|
|
|
|
3848
|
|
|
|
|
|
|
sub str2D{ |
|
3849
|
127
|
|
|
127
|
0
|
332
|
my($self,$format,$level)=@_; |
|
3850
|
127
|
|
|
|
|
365
|
my @dims = $self->dims(); |
|
3851
|
127
|
50
|
|
|
|
335
|
barf "Not 2D" if scalar(@dims)!=2; |
|
3852
|
127
|
|
|
|
|
1389
|
my $x = listref_c($self); |
|
3853
|
127
|
|
|
|
|
784
|
my @lines = str_list($x, $dims[0], $format, $self->get_datatype, $self->badflag); |
|
3854
|
127
|
|
|
|
|
363
|
my $ret = "\n" . " "x$level . "[\n"; |
|
3855
|
127
|
|
|
|
|
830
|
$ret .= join $sep2."\n", map " "x($level+1).$_, @lines; |
|
3856
|
127
|
|
|
|
|
330
|
$ret .= "\n".(" "x$level)."]\n"; |
|
3857
|
127
|
|
|
|
|
849
|
return $ret; |
|
3858
|
|
|
|
|
|
|
} |
|
3859
|
|
|
|
|
|
|
|
|
3860
|
|
|
|
|
|
|
########## Docs for functions in Core.xs ################## |
|
3861
|
|
|
|
|
|
|
# Pod docs for functions that are imported from Core.xs and are |
|
3862
|
|
|
|
|
|
|
# not documented elsewhere. Currently this is not a complete |
|
3863
|
|
|
|
|
|
|
# list. There are others. |
|
3864
|
|
|
|
|
|
|
|
|
3865
|
|
|
|
|
|
|
=head2 gethdr |
|
3866
|
|
|
|
|
|
|
|
|
3867
|
|
|
|
|
|
|
=for ref |
|
3868
|
|
|
|
|
|
|
|
|
3869
|
|
|
|
|
|
|
Retrieve header information from an ndarray |
|
3870
|
|
|
|
|
|
|
|
|
3871
|
|
|
|
|
|
|
=for example |
|
3872
|
|
|
|
|
|
|
|
|
3873
|
|
|
|
|
|
|
$pdl=rfits('file.fits'); |
|
3874
|
|
|
|
|
|
|
$h=$pdl->gethdr; |
|
3875
|
|
|
|
|
|
|
print "Number of pixels in the X-direction=$$h{NAXIS1}\n"; |
|
3876
|
|
|
|
|
|
|
|
|
3877
|
|
|
|
|
|
|
The C function retrieves whatever header information is contained |
|
3878
|
|
|
|
|
|
|
within an ndarray. The header can be set with L and is always a |
|
3879
|
|
|
|
|
|
|
hash reference or undef. |
|
3880
|
|
|
|
|
|
|
|
|
3881
|
|
|
|
|
|
|
C returns undef if the ndarray has not yet had a header |
|
3882
|
|
|
|
|
|
|
defined; compare with C and C, which are guaranteed to return a |
|
3883
|
|
|
|
|
|
|
defined value. |
|
3884
|
|
|
|
|
|
|
|
|
3885
|
|
|
|
|
|
|
Note that gethdr() works by B: you can modify the header |
|
3886
|
|
|
|
|
|
|
in-place once it has been retrieved: |
|
3887
|
|
|
|
|
|
|
|
|
3888
|
|
|
|
|
|
|
$x = rfits($filename); |
|
3889
|
|
|
|
|
|
|
$xh = $x->gethdr(); |
|
3890
|
|
|
|
|
|
|
$xh->{FILENAME} = $filename; |
|
3891
|
|
|
|
|
|
|
|
|
3892
|
|
|
|
|
|
|
It is also important to realise that in most cases the header is not |
|
3893
|
|
|
|
|
|
|
automatically copied when you copy the ndarray. See L |
|
3894
|
|
|
|
|
|
|
to enable automatic header copying. |
|
3895
|
|
|
|
|
|
|
|
|
3896
|
|
|
|
|
|
|
Here's another example: a wrapper around rcols that allows your ndarray |
|
3897
|
|
|
|
|
|
|
to remember the file it was read from and the columns could be easily |
|
3898
|
|
|
|
|
|
|
written (here assuming that no regexp is needed, extensions are left |
|
3899
|
|
|
|
|
|
|
as an exercise for the reader) |
|
3900
|
|
|
|
|
|
|
|
|
3901
|
|
|
|
|
|
|
sub ext_rcols { |
|
3902
|
|
|
|
|
|
|
my ($file, @columns)=@_; |
|
3903
|
|
|
|
|
|
|
my $header={}; |
|
3904
|
|
|
|
|
|
|
$$header{File}=$file; |
|
3905
|
|
|
|
|
|
|
$$header{Columns}=\@columns; |
|
3906
|
|
|
|
|
|
|
|
|
3907
|
|
|
|
|
|
|
@ndarrays=rcols $file, @columns; |
|
3908
|
|
|
|
|
|
|
foreach (@ndarrays) { $_->sethdr($header); } |
|
3909
|
|
|
|
|
|
|
return @ndarrays; |
|
3910
|
|
|
|
|
|
|
} |
|
3911
|
|
|
|
|
|
|
|
|
3912
|
|
|
|
|
|
|
=head2 hdr |
|
3913
|
|
|
|
|
|
|
|
|
3914
|
|
|
|
|
|
|
=for ref |
|
3915
|
|
|
|
|
|
|
|
|
3916
|
|
|
|
|
|
|
Retrieve or set header information from an ndarray |
|
3917
|
|
|
|
|
|
|
|
|
3918
|
|
|
|
|
|
|
=for example |
|
3919
|
|
|
|
|
|
|
|
|
3920
|
|
|
|
|
|
|
$pdl->hdr->{CDELT1} = 1; |
|
3921
|
|
|
|
|
|
|
|
|
3922
|
|
|
|
|
|
|
The C function allows convenient access to the header of a |
|
3923
|
|
|
|
|
|
|
ndarray. Unlike C it is guaranteed to return a defined value, |
|
3924
|
|
|
|
|
|
|
so you can use it in a hash dereference as in the example. If the |
|
3925
|
|
|
|
|
|
|
header does not yet exist, it gets autogenerated as an empty hash. |
|
3926
|
|
|
|
|
|
|
|
|
3927
|
|
|
|
|
|
|
Note that this is usually -- but not always -- What You Want. If you |
|
3928
|
|
|
|
|
|
|
want to use a tied L hash, |
|
3929
|
|
|
|
|
|
|
for example, you should either construct it yourself and use C |
|
3930
|
|
|
|
|
|
|
to put it into the ndarray, or use L instead. (Note that |
|
3931
|
|
|
|
|
|
|
you should be able to write out the FITS file successfully regardless |
|
3932
|
|
|
|
|
|
|
of whether your PDL has a tied FITS header object or a vanilla hash). |
|
3933
|
|
|
|
|
|
|
|
|
3934
|
|
|
|
|
|
|
=head2 fhdr |
|
3935
|
|
|
|
|
|
|
|
|
3936
|
|
|
|
|
|
|
=for ref |
|
3937
|
|
|
|
|
|
|
|
|
3938
|
|
|
|
|
|
|
Retrieve or set FITS header information from an ndarray |
|
3939
|
|
|
|
|
|
|
|
|
3940
|
|
|
|
|
|
|
=for example |
|
3941
|
|
|
|
|
|
|
|
|
3942
|
|
|
|
|
|
|
$pdl->fhdr->{CDELT1} = 1; |
|
3943
|
|
|
|
|
|
|
|
|
3944
|
|
|
|
|
|
|
The C function allows convenient access to the header of a |
|
3945
|
|
|
|
|
|
|
ndarray. Unlike C it is guaranteed to return a defined value, |
|
3946
|
|
|
|
|
|
|
so you can use it in a hash dereference as in the example. If the |
|
3947
|
|
|
|
|
|
|
header does not yet exist, it gets autogenerated as a tied |
|
3948
|
|
|
|
|
|
|
L hash. |
|
3949
|
|
|
|
|
|
|
|
|
3950
|
|
|
|
|
|
|
Astro::FITS::Header tied hashes are better at matching the behavior of |
|
3951
|
|
|
|
|
|
|
FITS headers than are regular hashes. In particular, the hash keys |
|
3952
|
|
|
|
|
|
|
are CAsE INsEnSItiVE, unlike normal hash keys. See |
|
3953
|
|
|
|
|
|
|
L for details. |
|
3954
|
|
|
|
|
|
|
|
|
3955
|
|
|
|
|
|
|
If you do not have Astro::FITS::Header installed, you get back a |
|
3956
|
|
|
|
|
|
|
normal hash instead of a tied object. |
|
3957
|
|
|
|
|
|
|
|
|
3958
|
|
|
|
|
|
|
=head2 sethdr |
|
3959
|
|
|
|
|
|
|
|
|
3960
|
|
|
|
|
|
|
=for ref |
|
3961
|
|
|
|
|
|
|
|
|
3962
|
|
|
|
|
|
|
Set header information of an ndarray |
|
3963
|
|
|
|
|
|
|
|
|
3964
|
|
|
|
|
|
|
=for example |
|
3965
|
|
|
|
|
|
|
|
|
3966
|
|
|
|
|
|
|
$pdl = zeroes(100,100); |
|
3967
|
|
|
|
|
|
|
$h = {NAXIS=>2, NAXIS1=>100, NAXIS=>100, COMMENT=>"Sample FITS-style header"}; |
|
3968
|
|
|
|
|
|
|
# add a FILENAME field to the header |
|
3969
|
|
|
|
|
|
|
$$h{FILENAME} = 'file.fits'; |
|
3970
|
|
|
|
|
|
|
$pdl->sethdr( $h ); |
|
3971
|
|
|
|
|
|
|
|
|
3972
|
|
|
|
|
|
|
The C function sets the header information for an ndarray. |
|
3973
|
|
|
|
|
|
|
You must feed in a hash ref or undef, and the header field of the PDL is |
|
3974
|
|
|
|
|
|
|
set to be a new ref to the same hash (or undefined). |
|
3975
|
|
|
|
|
|
|
|
|
3976
|
|
|
|
|
|
|
The hash ref requirement is a speed bump put in place since the normal |
|
3977
|
|
|
|
|
|
|
use of headers is to store fits header information and the like. Of course, |
|
3978
|
|
|
|
|
|
|
if you want you can hang whatever ugly old data structure you want off |
|
3979
|
|
|
|
|
|
|
of the header, but that makes life more complex. |
|
3980
|
|
|
|
|
|
|
|
|
3981
|
|
|
|
|
|
|
Remember that the hash is not copied -- the header is made into a ref |
|
3982
|
|
|
|
|
|
|
that points to the same underlying data. To get a real copy without |
|
3983
|
|
|
|
|
|
|
making any assumptions about the underlying data structure, you |
|
3984
|
|
|
|
|
|
|
can use one of the following: |
|
3985
|
|
|
|
|
|
|
|
|
3986
|
|
|
|
|
|
|
use PDL::IO::Dumper; |
|
3987
|
|
|
|
|
|
|
$pdl->sethdr( deep_copy($h) ); |
|
3988
|
|
|
|
|
|
|
|
|
3989
|
|
|
|
|
|
|
(which is slow but general), or |
|
3990
|
|
|
|
|
|
|
|
|
3991
|
|
|
|
|
|
|
$pdl->sethdr( PDL::_hdr_copy($h) ) |
|
3992
|
|
|
|
|
|
|
|
|
3993
|
|
|
|
|
|
|
(which uses the built-in sleazy deep copier), or (if you know that all |
|
3994
|
|
|
|
|
|
|
the elements happen to be scalars): |
|
3995
|
|
|
|
|
|
|
|
|
3996
|
|
|
|
|
|
|
{ my %a = %$h; |
|
3997
|
|
|
|
|
|
|
$pdl->sethdr(\%a); |
|
3998
|
|
|
|
|
|
|
} |
|
3999
|
|
|
|
|
|
|
|
|
4000
|
|
|
|
|
|
|
which is considerably faster but just copies the top level. |
|
4001
|
|
|
|
|
|
|
|
|
4002
|
|
|
|
|
|
|
The C function must be given a hash reference or undef. For |
|
4003
|
|
|
|
|
|
|
further information on the header, see L, L, |
|
4004
|
|
|
|
|
|
|
L and L. |
|
4005
|
|
|
|
|
|
|
|
|
4006
|
|
|
|
|
|
|
=head2 hdrcpy |
|
4007
|
|
|
|
|
|
|
|
|
4008
|
|
|
|
|
|
|
=for ref |
|
4009
|
|
|
|
|
|
|
|
|
4010
|
|
|
|
|
|
|
switch on/off/examine automatic header copying |
|
4011
|
|
|
|
|
|
|
|
|
4012
|
|
|
|
|
|
|
=for example |
|
4013
|
|
|
|
|
|
|
|
|
4014
|
|
|
|
|
|
|
print "hdrs will be copied" if $x->hdrcpy; |
|
4015
|
|
|
|
|
|
|
$x->hdrcpy(1); # switch on automatic header copying |
|
4016
|
|
|
|
|
|
|
$y = $x->sumover; # and $y will inherit $x's hdr |
|
4017
|
|
|
|
|
|
|
$x->hdrcpy(0); # and now make $x non-infectious again |
|
4018
|
|
|
|
|
|
|
|
|
4019
|
|
|
|
|
|
|
C without an argument just returns the current setting of the |
|
4020
|
|
|
|
|
|
|
flag. See also "hcpy" which returns its PDL argument (and so is useful |
|
4021
|
|
|
|
|
|
|
in method-call pipelines). |
|
4022
|
|
|
|
|
|
|
|
|
4023
|
|
|
|
|
|
|
Normally, the optional header of an ndarray is not copied automatically |
|
4024
|
|
|
|
|
|
|
in pdl operations. Switching on the hdrcpy flag using the C |
|
4025
|
|
|
|
|
|
|
method will enable automatic hdr copying. Note that an actual deep |
|
4026
|
|
|
|
|
|
|
copy gets made, which is rather processor-inefficient -- so avoid |
|
4027
|
|
|
|
|
|
|
using header copying in tight loops! |
|
4028
|
|
|
|
|
|
|
|
|
4029
|
|
|
|
|
|
|
Most PDLs have the C flag cleared by default; however, some |
|
4030
|
|
|
|
|
|
|
routines (notably L) set it by default |
|
4031
|
|
|
|
|
|
|
where that makes more sense. |
|
4032
|
|
|
|
|
|
|
|
|
4033
|
|
|
|
|
|
|
The C flag is viral: if you set it for a PDL, then derived |
|
4034
|
|
|
|
|
|
|
PDLs will get copies of the header and will also have their C |
|
4035
|
|
|
|
|
|
|
flags set. For example: |
|
4036
|
|
|
|
|
|
|
|
|
4037
|
|
|
|
|
|
|
$x = xvals(50,50); |
|
4038
|
|
|
|
|
|
|
$x->hdrcpy(1); |
|
4039
|
|
|
|
|
|
|
$x->hdr->{FOO} = "bar"; |
|
4040
|
|
|
|
|
|
|
$y = $x++; |
|
4041
|
|
|
|
|
|
|
$c = $y++; |
|
4042
|
|
|
|
|
|
|
print $y->hdr->{FOO}, " - ", $c->hdr->{FOO}, "\n"; |
|
4043
|
|
|
|
|
|
|
$y->hdr->{FOO} = "baz"; |
|
4044
|
|
|
|
|
|
|
print $x->hdr->{FOO}, " - ", $y->hdr->{FOO}, " - ", $c->hdr->{FOO}, "\n"; |
|
4045
|
|
|
|
|
|
|
|
|
4046
|
|
|
|
|
|
|
will print: |
|
4047
|
|
|
|
|
|
|
|
|
4048
|
|
|
|
|
|
|
bar - bar |
|
4049
|
|
|
|
|
|
|
bar - baz - bar |
|
4050
|
|
|
|
|
|
|
|
|
4051
|
|
|
|
|
|
|
Performing an operation in which more than one PDL has its hdrcpy flag |
|
4052
|
|
|
|
|
|
|
causes the resulting PDL to take the header of the first PDL: |
|
4053
|
|
|
|
|
|
|
|
|
4054
|
|
|
|
|
|
|
($x,$y) = sequence(5,2)->dog; |
|
4055
|
|
|
|
|
|
|
$x->hdrcpy(1); $y->hdrcpy(1); |
|
4056
|
|
|
|
|
|
|
$x->hdr->{foo} = 'a'; |
|
4057
|
|
|
|
|
|
|
$y->hdr->{foo} = 'b'; |
|
4058
|
|
|
|
|
|
|
print (($x+$y)->hdr->{foo} , ($y+$x)->hdr->{foo}); |
|
4059
|
|
|
|
|
|
|
|
|
4060
|
|
|
|
|
|
|
will print: |
|
4061
|
|
|
|
|
|
|
|
|
4062
|
|
|
|
|
|
|
a b |
|
4063
|
|
|
|
|
|
|
|
|
4064
|
|
|
|
|
|
|
=head2 hcpy |
|
4065
|
|
|
|
|
|
|
|
|
4066
|
|
|
|
|
|
|
=for ref |
|
4067
|
|
|
|
|
|
|
|
|
4068
|
|
|
|
|
|
|
Switch on/off automatic header copying, with PDL pass-through |
|
4069
|
|
|
|
|
|
|
|
|
4070
|
|
|
|
|
|
|
=for example |
|
4071
|
|
|
|
|
|
|
|
|
4072
|
|
|
|
|
|
|
$x = rfits('foo.fits')->hcpy(0); |
|
4073
|
|
|
|
|
|
|
$x = rfits('foo.fits')->hcpy(1); |
|
4074
|
|
|
|
|
|
|
|
|
4075
|
|
|
|
|
|
|
C sets or clears the hdrcpy flag of a PDL, and returns the PDL |
|
4076
|
|
|
|
|
|
|
itself. That makes it convenient for inline use in expressions. |
|
4077
|
|
|
|
|
|
|
|
|
4078
|
|
|
|
|
|
|
=cut |
|
4079
|
|
|
|
|
|
|
|
|
4080
|
|
|
|
|
|
|
# |
|
4081
|
|
|
|
|
|
|
# Sleazy hcpy saves me time typing |
|
4082
|
|
|
|
|
|
|
# |
|
4083
|
|
|
|
|
|
|
sub PDL::hcpy { |
|
4084
|
0
|
|
|
0
|
0
|
0
|
$_[0]->hdrcpy($_[1]); |
|
4085
|
0
|
|
|
|
|
0
|
$_[0]; |
|
4086
|
|
|
|
|
|
|
} |
|
4087
|
|
|
|
|
|
|
|
|
4088
|
|
|
|
|
|
|
=head2 online_cpus |
|
4089
|
|
|
|
|
|
|
|
|
4090
|
|
|
|
|
|
|
=for ref |
|
4091
|
|
|
|
|
|
|
|
|
4092
|
|
|
|
|
|
|
Returns the number of available processors cores. Used to set the number |
|
4093
|
|
|
|
|
|
|
of threads with L if C<$ENV{PDL_AUTOPTHREAD_TARG}> |
|
4094
|
|
|
|
|
|
|
is not set. |
|
4095
|
|
|
|
|
|
|
|
|
4096
|
|
|
|
|
|
|
=head2 set_autopthread_targ |
|
4097
|
|
|
|
|
|
|
|
|
4098
|
|
|
|
|
|
|
=for ref |
|
4099
|
|
|
|
|
|
|
|
|
4100
|
|
|
|
|
|
|
Set the target number of processor threads (pthreads) for multi-threaded processing. |
|
4101
|
|
|
|
|
|
|
|
|
4102
|
|
|
|
|
|
|
=for usage |
|
4103
|
|
|
|
|
|
|
|
|
4104
|
|
|
|
|
|
|
set_autopthread_targ($num_pthreads); |
|
4105
|
|
|
|
|
|
|
|
|
4106
|
|
|
|
|
|
|
C<$num_pthreads> is the target number of pthreads the auto-pthread process will try to achieve. |
|
4107
|
|
|
|
|
|
|
|
|
4108
|
|
|
|
|
|
|
See L for an overview of the auto-pthread process. |
|
4109
|
|
|
|
|
|
|
|
|
4110
|
|
|
|
|
|
|
=for example |
|
4111
|
|
|
|
|
|
|
|
|
4112
|
|
|
|
|
|
|
# Example turning on auto-pthreading for a target of 2 pthreads and for functions involving |
|
4113
|
|
|
|
|
|
|
# PDLs with greater than 1M elements |
|
4114
|
|
|
|
|
|
|
set_autopthread_targ(2); |
|
4115
|
|
|
|
|
|
|
set_autopthread_size(1); |
|
4116
|
|
|
|
|
|
|
|
|
4117
|
|
|
|
|
|
|
# Execute a pdl function, processing will split into two pthreads |
|
4118
|
|
|
|
|
|
|
$x = minimum($y); |
|
4119
|
|
|
|
|
|
|
|
|
4120
|
|
|
|
|
|
|
# Get the actual number of pthreads that were run. |
|
4121
|
|
|
|
|
|
|
$actual_pthread = get_autopthread_actual(); |
|
4122
|
|
|
|
|
|
|
|
|
4123
|
|
|
|
|
|
|
=cut |
|
4124
|
|
|
|
|
|
|
|
|
4125
|
|
|
|
|
|
|
*set_autopthread_targ = \&PDL::set_autopthread_targ; |
|
4126
|
|
|
|
|
|
|
|
|
4127
|
|
|
|
|
|
|
=head2 get_autopthread_targ |
|
4128
|
|
|
|
|
|
|
|
|
4129
|
|
|
|
|
|
|
=for ref |
|
4130
|
|
|
|
|
|
|
|
|
4131
|
|
|
|
|
|
|
Get the current target number of processor threads (pthreads) for multi-threaded processing. |
|
4132
|
|
|
|
|
|
|
|
|
4133
|
|
|
|
|
|
|
=for usage |
|
4134
|
|
|
|
|
|
|
|
|
4135
|
|
|
|
|
|
|
$num_pthreads = get_autopthread_targ(); |
|
4136
|
|
|
|
|
|
|
|
|
4137
|
|
|
|
|
|
|
C<$num_pthreads> is the target number of pthreads the auto-pthread process will try to achieve. |
|
4138
|
|
|
|
|
|
|
|
|
4139
|
|
|
|
|
|
|
See L for an overview of the auto-pthread process. |
|
4140
|
|
|
|
|
|
|
|
|
4141
|
|
|
|
|
|
|
=cut |
|
4142
|
|
|
|
|
|
|
|
|
4143
|
|
|
|
|
|
|
*get_autopthread_targ = \&PDL::get_autopthread_targ; |
|
4144
|
|
|
|
|
|
|
|
|
4145
|
|
|
|
|
|
|
=head2 get_autopthread_actual |
|
4146
|
|
|
|
|
|
|
|
|
4147
|
|
|
|
|
|
|
=for ref |
|
4148
|
|
|
|
|
|
|
|
|
4149
|
|
|
|
|
|
|
Get the actual number of pthreads executed for the last pdl processing function. |
|
4150
|
|
|
|
|
|
|
|
|
4151
|
|
|
|
|
|
|
=for usage |
|
4152
|
|
|
|
|
|
|
|
|
4153
|
|
|
|
|
|
|
$autopthread_actual = get_autopthread_actual(); |
|
4154
|
|
|
|
|
|
|
|
|
4155
|
|
|
|
|
|
|
C<$autopthread_actual> is the actual number of pthreads executed for the last pdl processing function. |
|
4156
|
|
|
|
|
|
|
|
|
4157
|
|
|
|
|
|
|
See L for an overview of the auto-pthread process. |
|
4158
|
|
|
|
|
|
|
|
|
4159
|
|
|
|
|
|
|
=cut |
|
4160
|
|
|
|
|
|
|
|
|
4161
|
|
|
|
|
|
|
*get_autopthread_actual = \&PDL::get_autopthread_actual; |
|
4162
|
|
|
|
|
|
|
|
|
4163
|
|
|
|
|
|
|
=head2 get_autopthread_dim |
|
4164
|
|
|
|
|
|
|
|
|
4165
|
|
|
|
|
|
|
=for ref |
|
4166
|
|
|
|
|
|
|
|
|
4167
|
|
|
|
|
|
|
Get the actual dimension on which pthreads were used for the last |
|
4168
|
|
|
|
|
|
|
pdl processing function. |
|
4169
|
|
|
|
|
|
|
|
|
4170
|
|
|
|
|
|
|
=for usage |
|
4171
|
|
|
|
|
|
|
|
|
4172
|
|
|
|
|
|
|
$autopthread_dim = get_autopthread_dim(); |
|
4173
|
|
|
|
|
|
|
|
|
4174
|
|
|
|
|
|
|
C<$autopthread_dim> is the actual dimension on which pthreads were |
|
4175
|
|
|
|
|
|
|
used for the last pdl processing function. |
|
4176
|
|
|
|
|
|
|
|
|
4177
|
|
|
|
|
|
|
See L for an overview of the auto-pthread process. |
|
4178
|
|
|
|
|
|
|
|
|
4179
|
|
|
|
|
|
|
=cut |
|
4180
|
|
|
|
|
|
|
|
|
4181
|
|
|
|
|
|
|
*get_autopthread_dim = \&PDL::get_autopthread_dim; |
|
4182
|
|
|
|
|
|
|
|
|
4183
|
|
|
|
|
|
|
=head2 set_autopthread_size |
|
4184
|
|
|
|
|
|
|
|
|
4185
|
|
|
|
|
|
|
=for ref |
|
4186
|
|
|
|
|
|
|
|
|
4187
|
|
|
|
|
|
|
Set the minimum size (in M-elements or 2^20 elements) of the largest PDL involved in a function where auto-pthreading will |
|
4188
|
|
|
|
|
|
|
be performed. For small PDLs, it probably isn't worth starting multiple pthreads, so this function |
|
4189
|
|
|
|
|
|
|
is used to define a minimum threshold where auto-pthreading won't be attempted. |
|
4190
|
|
|
|
|
|
|
|
|
4191
|
|
|
|
|
|
|
=for usage |
|
4192
|
|
|
|
|
|
|
|
|
4193
|
|
|
|
|
|
|
set_autopthread_size($size); |
|
4194
|
|
|
|
|
|
|
|
|
4195
|
|
|
|
|
|
|
C<$size> is the mimumum size, in M-elements or 2^20 elements (approx 1e6 elements) for the largest PDL involved in a function. |
|
4196
|
|
|
|
|
|
|
|
|
4197
|
|
|
|
|
|
|
See L for an overview of the auto-pthread process. |
|
4198
|
|
|
|
|
|
|
|
|
4199
|
|
|
|
|
|
|
=for example |
|
4200
|
|
|
|
|
|
|
|
|
4201
|
|
|
|
|
|
|
# Example turning on auto-pthreading for a target of 2 pthreads and for functions involving |
|
4202
|
|
|
|
|
|
|
# PDLs with greater than 1M elements |
|
4203
|
|
|
|
|
|
|
set_autopthread_targ(2); |
|
4204
|
|
|
|
|
|
|
set_autopthread_size(1); |
|
4205
|
|
|
|
|
|
|
|
|
4206
|
|
|
|
|
|
|
# Execute a pdl function, processing will split into two pthreads as long as |
|
4207
|
|
|
|
|
|
|
# one of the pdl-threaded dimensions is at least 2. |
|
4208
|
|
|
|
|
|
|
$x = minimum($y); |
|
4209
|
|
|
|
|
|
|
|
|
4210
|
|
|
|
|
|
|
# Get the actual number of pthreads that were run. |
|
4211
|
|
|
|
|
|
|
$actual_pthread = get_autopthread_actual(); |
|
4212
|
|
|
|
|
|
|
|
|
4213
|
|
|
|
|
|
|
=cut |
|
4214
|
|
|
|
|
|
|
|
|
4215
|
|
|
|
|
|
|
*set_autopthread_size = \&PDL::set_autopthread_size; |
|
4216
|
|
|
|
|
|
|
|
|
4217
|
|
|
|
|
|
|
=head2 get_autopthread_size |
|
4218
|
|
|
|
|
|
|
|
|
4219
|
|
|
|
|
|
|
=for ref |
|
4220
|
|
|
|
|
|
|
|
|
4221
|
|
|
|
|
|
|
Get the current autopthread_size setting. |
|
4222
|
|
|
|
|
|
|
|
|
4223
|
|
|
|
|
|
|
=for usage |
|
4224
|
|
|
|
|
|
|
|
|
4225
|
|
|
|
|
|
|
$autopthread_size = get_autopthread_size(); |
|
4226
|
|
|
|
|
|
|
|
|
4227
|
|
|
|
|
|
|
C<$autopthread_size> is the mimumum size limit for auto_pthreading to occur, in M-elements or 2^20 elements (approx 1e6 elements) for the largest PDL involved in a function |
|
4228
|
|
|
|
|
|
|
|
|
4229
|
|
|
|
|
|
|
See L for an overview of the auto-pthread process. |
|
4230
|
|
|
|
|
|
|
|
|
4231
|
|
|
|
|
|
|
=cut |
|
4232
|
|
|
|
|
|
|
|
|
4233
|
|
|
|
|
|
|
*get_autopthread_size = \&PDL::get_autopthread_size; |
|
4234
|
|
|
|
|
|
|
|
|
4235
|
|
|
|
|
|
|
=head1 AUTHOR |
|
4236
|
|
|
|
|
|
|
|
|
4237
|
|
|
|
|
|
|
Copyright (C) Karl Glazebrook (kgb@aaoepp.aao.gov.au), |
|
4238
|
|
|
|
|
|
|
Tuomas J. Lukka, (lukka@husc.harvard.edu) and Christian |
|
4239
|
|
|
|
|
|
|
Soeller (c.soeller@auckland.ac.nz) 1997. |
|
4240
|
|
|
|
|
|
|
Modified, Craig DeForest (deforest@boulder.swri.edu) 2002. |
|
4241
|
|
|
|
|
|
|
All rights reserved. There is no warranty. You are allowed |
|
4242
|
|
|
|
|
|
|
to redistribute this software / documentation under certain |
|
4243
|
|
|
|
|
|
|
conditions. For details, see the file COPYING in the PDL |
|
4244
|
|
|
|
|
|
|
distribution. If this file is separated from the PDL distribution, |
|
4245
|
|
|
|
|
|
|
the copyright notice should be included in the file. |
|
4246
|
|
|
|
|
|
|
|
|
4247
|
|
|
|
|
|
|
=cut |
|
4248
|
|
|
|
|
|
|
|
|
4249
|
|
|
|
|
|
|
# |
|
4250
|
|
|
|
|
|
|
# Easier to implement in perl than in XS... |
|
4251
|
|
|
|
|
|
|
# -- CED |
|
4252
|
|
|
|
|
|
|
# |
|
4253
|
|
|
|
|
|
|
|
|
4254
|
|
|
|
|
|
|
sub PDL::fhdr { |
|
4255
|
3
|
|
|
3
|
0
|
7
|
my $pdl = shift; |
|
4256
|
|
|
|
|
|
|
|
|
4257
|
3
|
50
|
33
|
|
|
21
|
return $pdl->hdr |
|
4258
|
|
|
|
|
|
|
if( (defined $pdl->gethdr) || |
|
4259
|
|
|
|
|
|
|
!defined $Astro::FITS::Header::VERSION |
|
4260
|
|
|
|
|
|
|
); |
|
4261
|
|
|
|
|
|
|
|
|
4262
|
|
|
|
|
|
|
# Avoid bug in 1.15 and earlier Astro::FITS::Header |
|
4263
|
3
|
|
|
|
|
9
|
my @hdr = ("SIMPLE = T"); |
|
4264
|
3
|
|
|
|
|
24
|
my $hdr = Astro::FITS::Header->new(Cards=>\@hdr); |
|
4265
|
3
|
|
|
|
|
800
|
tie my %hdr, "Astro::FITS::Header", $hdr; |
|
4266
|
3
|
|
|
|
|
42
|
$pdl->sethdr(\%hdr); |
|
4267
|
3
|
|
|
|
|
11
|
return \%hdr; |
|
4268
|
|
|
|
|
|
|
} |
|
4269
|
|
|
|
|
|
|
|
|
4270
|
|
|
|
|
|
|
sub _file_map_sv { |
|
4271
|
7
|
|
|
7
|
|
1394
|
require File::Map; |
|
4272
|
7
|
|
|
|
|
7624
|
my ($svref, $fh, $len, $shared, $writable) = @_; |
|
4273
|
7
|
50
|
|
|
|
37
|
my $prot = File::Map::PROT_READ() | ($writable ? File::Map::PROT_WRITE() : 0); |
|
4274
|
7
|
50
|
|
|
|
24
|
my $flags = ($shared ? File::Map::MAP_SHARED() : File::Map::MAP_PRIVATE()); |
|
4275
|
7
|
50
|
|
|
|
21
|
printf STDERR "_file_map_sv: calling sys_map(%s,%d,%d,%d,%s,%d)\n", |
|
4276
|
|
|
|
|
|
|
$svref, $len, $prot, $flags, $fh, 0 if $PDL::debug; |
|
4277
|
7
|
|
|
|
|
319
|
File::Map::sys_map($$svref, $len, $prot, $flags, $fh, 0); |
|
4278
|
7
|
50
|
|
|
|
84
|
printf STDERR "_file_map_sv: length \$\$svref is %d.\n", |
|
4279
|
|
|
|
|
|
|
length $$svref if $PDL::debug; |
|
4280
|
|
|
|
|
|
|
} |
|
4281
|
|
|
|
|
|
|
|
|
4282
|
|
|
|
|
|
|
sub _file_map_open { |
|
4283
|
7
|
|
|
7
|
|
55
|
require Fcntl; |
|
4284
|
7
|
|
|
|
|
24
|
my ($name, $len, $shared, $writable, $creat, $perms, $trunc) = @_; |
|
4285
|
7
|
50
|
33
|
|
|
36
|
my $mode = ($writable && $shared ? Fcntl::O_RDWR() : Fcntl::O_RDONLY()); |
|
4286
|
7
|
100
|
|
|
|
23
|
$mode |= Fcntl::O_CREAT() if $creat; |
|
4287
|
7
|
50
|
|
|
|
479
|
sysopen my $fh, $name, $mode, $perms |
|
4288
|
|
|
|
|
|
|
or die "Error opening file '$name': $!\n"; |
|
4289
|
7
|
|
|
|
|
28
|
binmode $fh; |
|
4290
|
7
|
100
|
|
|
|
20
|
if ($trunc) { |
|
4291
|
2
|
50
|
|
|
|
139
|
truncate $fh,0 or die "truncate('$name',0) failed: $!"; |
|
4292
|
2
|
50
|
|
|
|
66
|
truncate $fh,$len or die "truncate('$name',$len) failed: $!"; |
|
4293
|
|
|
|
|
|
|
} |
|
4294
|
7
|
|
|
|
|
29
|
$fh; |
|
4295
|
|
|
|
|
|
|
} |
|
4296
|
|
|
|
|
|
|
|
|
4297
|
|
|
|
|
|
|
sub PDL::set_data_by_file_map { |
|
4298
|
3
|
|
|
3
|
0
|
10
|
my ($pdl,$name,$len,$shared,$writable,$creat,$perms,$trunc) = @_; |
|
4299
|
3
|
|
|
|
|
8
|
my $fh = _file_map_open($name,$len,$shared,$writable,$creat,$perms,$trunc); |
|
4300
|
3
|
50
|
|
|
|
8
|
return $_[0] = undef if !$len; |
|
4301
|
3
|
|
|
|
|
23
|
_file_map_sv($pdl->get_dataref,$fh,$len,$shared,$writable); |
|
4302
|
3
|
|
|
|
|
17
|
$pdl->upd_data(1); |
|
4303
|
3
|
|
|
|
|
44
|
$pdl->set_donttouchdata($len); |
|
4304
|
|
|
|
|
|
|
} |
|
4305
|
|
|
|
|
|
|
|
|
4306
|
|
|
|
|
|
|
1; |