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