GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
fNDArray.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <istream>
31#include <limits>
32#include <ostream>
33
34#include "Array-util.h"
35#include "f77-fcn.h"
36#include "fNDArray.h"
37#include "lo-ieee.h"
38#include "mappers.h"
39#include "mx-base.h"
40#include "mx-op-defs.h"
41#include "oct-error.h"
42#include "oct-fftw.h"
43#include "oct-locbuf.h"
44
45#include "bsxfun-defs.cc"
46
48 : MArray<float> (a.dims ())
49{
50 octave_idx_type n = a.numel ();
51 for (octave_idx_type i = 0; i < n; i++)
52 xelem (i) = static_cast<unsigned char> (a(i));
53}
54
55#if defined (HAVE_FFTW)
56
58FloatNDArray::fourier (int dim) const
59{
60 const dim_vector& dv = dims ();
61
62 if (dim > dv.ndims () || dim < 0)
63 return FloatComplexNDArray ();
64
65 octave_idx_type stride = 1;
66 octave_idx_type n = dv(dim);
67
68 for (int i = 0; i < dim; i++)
69 stride *= dv(i);
70
71 octave_idx_type howmany = numel () / dv(dim);
72 howmany = (stride == 1 ? howmany : (howmany > stride ? stride : howmany));
73 octave_idx_type nloop = (stride == 1 ? 1 : numel () / dv(dim) / stride);
74 octave_idx_type dist = (stride == 1 ? n : 1);
75
76 const float *in (data ());
77 FloatComplexNDArray retval (dv);
78 FloatComplex *out (retval.rwdata ());
79
80 // Need to be careful here about the distance between fft's
81 for (octave_idx_type k = 0; k < nloop; k++)
82 octave::fftw::fft (in + k * stride * n, out + k * stride * n,
83 n, howmany, stride, dist);
84
85 return retval;
86}
87
90{
91 const dim_vector& dv = dims ();
92
93 if (dim > dv.ndims () || dim < 0)
94 return FloatComplexNDArray ();
95
96 octave_idx_type stride = 1;
97 octave_idx_type n = dv(dim);
98
99 for (int i = 0; i < dim; i++)
100 stride *= dv(i);
101
102 octave_idx_type howmany = numel () / dv(dim);
103 howmany = (stride == 1 ? howmany : (howmany > stride ? stride : howmany));
104 octave_idx_type nloop = (stride == 1 ? 1 : numel () / dv(dim) / stride);
105 octave_idx_type dist = (stride == 1 ? n : 1);
106
107 FloatComplexNDArray retval (*this);
108 FloatComplex *out (retval.rwdata ());
109
110 // Need to be careful here about the distance between fft's
111 for (octave_idx_type k = 0; k < nloop; k++)
112 octave::fftw::ifft (out + k * stride * n, out + k * stride * n,
113 n, howmany, stride, dist);
114
115 return retval;
116}
117
120{
121 const dim_vector& dv = dims ();
122 if (dv.ndims () < 2)
123 return FloatComplexNDArray ();
124
125 dim_vector dv2 (dv(0), dv(1));
126 const float *in = data ();
127 FloatComplexNDArray retval (dv);
128 FloatComplex *out = retval.rwdata ();
129 octave_idx_type howmany = numel () / dv(0) / dv(1);
130 octave_idx_type dist = dv(0) * dv(1);
131
132 for (octave_idx_type i=0; i < howmany; i++)
133 octave::fftw::fftNd (in + i*dist, out + i*dist, 2, dv2);
134
135 return retval;
136}
137
140{
141 const dim_vector& dv = dims ();
142 if (dv.ndims () < 2)
143 return FloatComplexNDArray ();
145 dim_vector dv2 (dv(0), dv(1));
146 FloatComplexNDArray retval (*this);
147 FloatComplex *out = retval.rwdata ();
148 octave_idx_type howmany = numel () / dv(0) / dv(1);
149 octave_idx_type dist = dv(0) * dv(1);
150
151 for (octave_idx_type i=0; i < howmany; i++)
152 octave::fftw::ifftNd (out + i*dist, out + i*dist, 2, dv2);
153
154 return retval;
155}
156
159{
160 const dim_vector& dv = dims ();
161 int rank = dv.ndims ();
162
163 const float *in (data ());
164 FloatComplexNDArray retval (dv);
165 FloatComplex *out (retval.rwdata ());
166
167 octave::fftw::fftNd (in, out, rank, dv);
168
169 return retval;
170}
171
174{
175 const dim_vector& dv = dims ();
176 int rank = dv.ndims ();
177
178 FloatComplexNDArray tmp (*this);
179 FloatComplex *in (tmp.rwdata ());
180 FloatComplexNDArray retval (dv);
181 FloatComplex *out (retval.rwdata ());
182
183 octave::fftw::ifftNd (in, out, rank, dv);
184
185 return retval;
186}
187
188#else
189
191FloatNDArray::fourier (int dim) const
192{
193 octave_unused_parameter (dim);
194
195 (*current_liboctave_error_handler)
196 ("support for FFTW was unavailable or disabled when liboctave was built");
197
198 return FloatComplexNDArray ();
199}
200
202FloatNDArray::ifourier (int dim) const
203{
204 octave_unused_parameter (dim);
205
206 (*current_liboctave_error_handler)
207 ("support for FFTW was unavailable or disabled when liboctave was built");
208 return FloatComplexNDArray ();
209}
210
213{
214 (*current_liboctave_error_handler)
215 ("support for FFTW was unavailable or disabled when liboctave was built");
216
217 return FloatComplexNDArray ();
218}
219
222{
223 (*current_liboctave_error_handler)
224 ("support for FFTW was unavailable or disabled when liboctave was built");
225
226 return FloatComplexNDArray ();
227}
228
231{
232 (*current_liboctave_error_handler)
233 ("support for FFTW was unavailable or disabled when liboctave was built");
234
235 return FloatComplexNDArray ();
236}
237
240{
241 (*current_liboctave_error_handler)
242 ("support for FFTW was unavailable or disabled when liboctave was built");
243
244 return FloatComplexNDArray ();
245}
246
247#endif
248
249// unary operations
250
253{
254 if (any_element_is_nan ())
255 octave::err_nan_to_logical_conversion ();
256
257 return do_mx_unary_op<bool, float> (*this, mx_inline_not);
258}
259
260bool
262{
263 return (neg_zero ? test_all (octave::math::negative_sign)
264 : do_mx_check<float> (*this, mx_inline_any_negative));
265}
266
267bool
269{
270 return (neg_zero ? test_all (octave::math::positive_sign)
271 : do_mx_check<float> (*this, mx_inline_any_positive));
272}
273
274bool
276{
277 return do_mx_check<float> (*this, mx_inline_any_nan);
278}
279
280bool
282{
283 return ! do_mx_check<float> (*this, mx_inline_all_finite);
284}
285
286bool
288{
289 return ! test_all (octave::is_one_or_zero);
290}
291
292bool
294{
295 return test_all (octave::is_zero);
296}
297
298bool
300{
301 return test_all (octave::is_int_or_inf_or_nan);
302}
303
304// Return nonzero if any element of M is not an integer. Also extract
305// the largest and smallest values and return them in MAX_VAL and MIN_VAL.
306
307bool
308FloatNDArray::all_integers (float& max_val, float& min_val) const
309{
310 octave_idx_type nel = numel ();
311
312 if (nel > 0)
313 {
314 max_val = elem (0);
315 min_val = elem (0);
316 }
317 else
318 return false;
319
320 for (octave_idx_type i = 0; i < nel; i++)
321 {
322 float val = elem (i);
323
324 if (val > max_val)
325 max_val = val;
326
327 if (val < min_val)
328 min_val = val;
329
330 if (! octave::math::is_integer (val))
331 return false;
332 }
333
334 return true;
335}
336
337bool
339{
340 return test_all (octave::math::is_integer);
341}
342
343bool
345{
346 return false;
347}
348
349// FIXME: this is not quite the right thing.
350
352FloatNDArray::all (int dim) const
353{
354 return do_mx_red_op<bool, float> (*this, dim, mx_inline_all);
355}
356
358FloatNDArray::any (int dim) const
359{
360 return do_mx_red_op<bool, float> (*this, dim, mx_inline_any);
361}
362
364FloatNDArray::flip (int dim) const
365{
366 return do_mx_flip_op<float, float> (*this, dim, mx_inline_flip);
367}
368
370FloatNDArray::cumprod (int dim, bool nanflag) const
371{
372 return do_mx_cum_op<float, float> (*this, dim, nanflag, mx_inline_cumprod);
373}
374
376FloatNDArray::cumsum (int dim, bool nanflag) const
377{
378 return do_mx_cum_op<float, float> (*this, dim, nanflag, mx_inline_cumsum);
379}
380
382FloatNDArray::prod (int dim, bool nanflag) const
383{
384 return do_mx_red_op<float, float> (*this, dim, nanflag, mx_inline_prod);
385}
386
388FloatNDArray::dprod (int dim, bool nanflag) const
389{
390 return do_mx_red_op<double, float> (*this, dim, nanflag, mx_inline_dprod);
391}
392
394FloatNDArray::sum (int dim, bool nanflag) const
395{
396 return do_mx_red_op<float, float> (*this, dim, nanflag, mx_inline_sum);
397}
398
400FloatNDArray::dsum (int dim, bool nanflag) const
401{
402 return do_mx_red_op<double, float> (*this, dim, nanflag, mx_inline_dsum);
403}
404
406FloatNDArray::sumsq (int dim, bool nanflag) const
407{
408 return do_mx_red_op<float, float> (*this, dim, nanflag, mx_inline_sumsq);
409}
410
412FloatNDArray::dsumsq (int dim, bool nanflag) const
413{
414 return do_mx_red_op<double, float> (*this, dim, nanflag, mx_inline_dsumsq);
415}
416
418FloatNDArray::max (int dim, bool nanflag, bool realabs) const
419{
420 return do_mx_minmax_op<float> (*this, dim, nanflag, realabs, mx_inline_max);
421}
422
425 int dim, bool nanflag, bool realabs) const
426{
427 return do_mx_minmax_op<float> (*this, idx_arg, dim,
428 nanflag, realabs, mx_inline_max);
429}
430
432FloatNDArray::min (int dim, bool nanflag, bool realabs) const
433{
434 return do_mx_minmax_op<float> (*this, dim, nanflag, realabs, mx_inline_min);
435}
436
439 int dim, bool nanflag, bool realabs) const
440{
441 return do_mx_minmax_op<float> (*this, idx_arg, dim,
442 nanflag, realabs, mx_inline_min);
443}
444
446FloatNDArray::cummax (int dim, bool nanflag, bool realabs) const
447{
448 return do_mx_cumminmax_op<float> (*this, dim, nanflag,
449 realabs, mx_inline_cummax);
450}
451
454 int dim, bool nanflag, bool realabs) const
455{
456 return do_mx_cumminmax_op<float> (*this, idx_arg, dim, nanflag,
457 realabs, mx_inline_cummax);
458}
459
461FloatNDArray::cummin (int dim, bool nanflag, bool realabs) const
462{
463 return do_mx_cumminmax_op<float> (*this, dim, nanflag,
464 realabs, mx_inline_cummin);
465}
466
469 int dim, bool nanflag, bool realabs) const
470{
471 return do_mx_cumminmax_op<float> (*this, idx_arg, dim, nanflag,
472 realabs, mx_inline_cummin);
473}
474
477{
478 return do_mx_diff_op<float> (*this, dim, order, mx_inline_diff);
479}
480
484{
485 if (rb.numel () > 0)
486 insert (rb, ra_idx);
487 return *this;
488}
489
493{
494 FloatComplexNDArray retval (*this);
495 if (rb.numel () > 0)
496 retval.insert (rb, ra_idx);
497 return retval;
498}
499
503{
504 charNDArray retval (dims ());
505 octave_idx_type nel = numel ();
506
507 for (octave_idx_type i = 0; i < nel; i++)
508 {
509 float d = elem (i);
510
511 if (octave::math::isnan (d))
512 (*current_liboctave_error_handler)
513 ("invalid conversion from NaN to character");
514
515 octave_idx_type ival = octave::math::nint_big (d);
516
517 if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
518 // FIXME: is there something better to do? Should we warn the user?
519 ival = 0;
520
521 retval.elem (i) = static_cast<char> (ival);
522 }
523
524 if (rb.isempty ())
525 return retval;
526
527 retval.insert (rb, ra_idx);
528 return retval;
529}
530
533{
534 return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
535}
536
539{
540 return do_mx_unary_op<float, FloatComplex> (a, mx_inline_imag);
541}
542
546{
547 Array<float>::insert (a, r, c);
548 return *this;
549}
550
554{
556 return *this;
557}
558
561{
562 return do_mx_unary_map<float, float, std::abs> (*this);
563}
564
567{
568 return do_mx_unary_map<bool, float, octave::math::isnan> (*this);
569}
570
573{
574 return do_mx_unary_map<bool, float, octave::math::isinf> (*this);
575}
576
579{
580 return do_mx_unary_map<bool, float, octave::math::isfinite> (*this);
581}
582
583void
585 const dim_vector& dimensions,
586 int start_dimension)
587{
588 ::increment_index (ra_idx, dimensions, start_dimension);
589}
590
593 const dim_vector& dimensions)
594{
595 return ::compute_index (ra_idx, dimensions);
596}
597
600{
601 return MArray<float>::diag (k);
602}
603
609
610// This contains no information on the array structure !!!
611std::ostream&
612operator << (std::ostream& os, const FloatNDArray& a)
613{
614 octave_idx_type nel = a.numel ();
615
616 for (octave_idx_type i = 0; i < nel; i++)
617 {
618 os << ' ';
619 octave::write_value<float> (os, a.elem (i));
620 os << "\n";
621 }
622 return os;
623}
624
625std::istream&
626operator >> (std::istream& is, FloatNDArray& a)
627{
628 octave_idx_type nel = a.numel ();
629
630 if (nel > 0)
631 {
632 float tmp;
633 for (octave_idx_type i = 0; i < nel; i++)
634 {
635 tmp = octave::read_value<float> (is);
636 if (is)
637 a.elem (i) = tmp;
638 else
639 return is;
640 }
641 }
642
643 return is;
644}
645
647
650
653
656
659
#define BSXFUN_STDREL_DEFS_MXLOOP(ARRAY)
#define BSXFUN_STDOP_DEFS_MXLOOP(ARRAY)
#define BSXFUN_OP2_DEF_MXLOOP(OP, ARRAY, ARRAY1, ARRAY2, LOOP)
#define BSXFUN_OP_DEF_MXLOOP(OP, ARRAY, LOOP)
N Dimensional Array with copy-on-write semantics.
Definition Array-base.h:130
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
Definition Array-base.h:529
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition Array-base.h:547
bool test_all(F fcn) const
Size of the specified dimension.
Definition Array-base.h:952
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition Array-base.h:585
Array< T, Alloc > & insert(const Array< T, Alloc > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
bool isempty() const
Size of the specified dimension.
Definition Array-base.h:674
const T * data() const
Size of the specified dimension.
Definition Array-base.h:687
T * rwdata()
Size of the specified dimension.
Array< T, Alloc > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
octave_idx_type numel() const
Number of elements in the array.
Definition Array-base.h:440
FloatComplexNDArray & insert(const NDArray &a, octave_idx_type r, octave_idx_type c)
Definition fCNDArray.cc:548
bool any_element_is_positive(bool=false) const
Definition fNDArray.cc:268
FloatNDArray cummax(int dim=-1, bool nanflag=true, bool realabs=true) const
Definition fNDArray.cc:446
FloatNDArray max(int dim=-1, bool nanflag=true, bool realabs=true) const
Definition fNDArray.cc:418
NDArray dsum(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:400
NDArray dprod(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:388
boolNDArray isfinite() const
Definition fNDArray.cc:578
FloatNDArray cumsum(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:376
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition fNDArray.cc:592
NDArray dsumsq(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:412
FloatComplexNDArray ifourier(int dim=1) const
Definition fNDArray.cc:89
bool any_element_not_one_or_zero() const
Definition fNDArray.cc:287
bool any_element_is_negative(bool=false) const
Definition fNDArray.cc:261
FloatNDArray sumsq(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:406
FloatNDArray abs() const
Definition fNDArray.cc:560
FloatNDArray cummin(int dim=-1, bool nanflag=true, bool realabs=true) const
Definition fNDArray.cc:461
boolNDArray any(int dim=-1) const
Definition fNDArray.cc:358
boolNDArray isnan() const
Definition fNDArray.cc:566
boolNDArray all(int dim=-1) const
Definition fNDArray.cc:352
FloatNDArray min(int dim=-1, bool nanflag=true, bool realabs=true) const
Definition fNDArray.cc:432
bool too_large_for_float() const
Definition fNDArray.cc:344
FloatNDArray & insert(const FloatNDArray &a, octave_idx_type r, octave_idx_type c)
Definition fNDArray.cc:544
bool any_element_is_nan() const
Definition fNDArray.cc:275
bool all_integers() const
Definition fNDArray.cc:338
bool any_element_is_inf_or_nan() const
Definition fNDArray.cc:281
bool all_elements_are_zero() const
Definition fNDArray.cc:293
boolNDArray isinf() const
Definition fNDArray.cc:572
FloatComplexNDArray ifourier2d() const
Definition fNDArray.cc:139
FloatNDArray concat(const FloatNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition fNDArray.cc:482
FloatComplexNDArray fourier2d() const
Definition fNDArray.cc:119
FloatComplexNDArray fourierNd() const
Definition fNDArray.cc:158
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition fNDArray.cc:584
FloatNDArray diff(octave_idx_type order=1, int dim=-1) const
Definition fNDArray.cc:476
FloatComplexNDArray ifourierNd() const
Definition fNDArray.cc:173
FloatNDArray sum(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:394
FloatNDArray flip(int dim=-1) const
Definition fNDArray.cc:364
FloatComplexNDArray fourier(int dim=1) const
Definition fNDArray.cc:58
FloatNDArray cumprod(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:370
boolNDArray operator!() const
Definition fNDArray.cc:252
bool all_elements_are_int_or_inf_or_nan() const
Definition fNDArray.cc:299
FloatNDArray diag(octave_idx_type k=0) const
Definition fNDArray.cc:599
FloatNDArray prod(int dim=-1, bool nanflag=false) const
Definition fNDArray.cc:382
friend class FloatComplexNDArray
Definition fNDArray.h:147
Template for N-dimensional array classes with like-type math operators.
Definition MArray.h:61
charNDArray & insert(const charNDArray &a, octave_idx_type r, octave_idx_type c)
Definition chNDArray.cc:166
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
octave_idx_type ndims() const
Number of dimensions.
Definition dim-vector.h:263
std::ostream & operator<<(std::ostream &os, const FloatNDArray &a)
Definition fNDArray.cc:612
FloatNDArray imag(const FloatComplexNDArray &a)
Definition fNDArray.cc:538
std::istream & operator>>(std::istream &is, FloatNDArray &a)
Definition fNDArray.cc:626
FloatNDArray real(const FloatComplexNDArray &a)
Definition fNDArray.cc:532
bool mx_inline_any(const T *v, octave_idx_type n)
subst_template_param< std::complex, T, double >::type mx_inline_dsumsq(const T *v, octave_idx_type n)
void mx_inline_cummin(const T *v, T *r, octave_idx_type n, const bool nanflag, const bool realabs)
void mx_inline_cummax(const T *v, T *r, octave_idx_type n, const bool nanflag, const bool realabs)
subst_template_param< std::complex, T, double >::type mx_inline_dprod(const T *v, octave_idx_type n)
void mx_inline_min(const T *v, T *r, octave_idx_type n, const bool nanflag, const bool realabs)
void mx_inline_cumprod(const T *v, T *r, octave_idx_type n)
void mx_inline_cumsum(const T *v, T *r, octave_idx_type n)
bool mx_inline_any_nan(std::size_t n, const T *x)
void mx_inline_not(std::size_t n, bool *r, const X *x)
void mx_inline_flip(const T *v, T *r, octave_idx_type n)
void mx_inline_max(const T *v, T *r, octave_idx_type n, const bool nanflag, const bool realabs)
bool mx_inline_all(const T *v, octave_idx_type n)
void mx_inline_real(std::size_t n, T *r, const std::complex< T > *x)
T mx_inline_sumsq(const T *v, octave_idx_type n)
bool mx_inline_any_positive(std::size_t n, const T *x)
void mx_inline_imag(std::size_t n, T *r, const std::complex< T > *x)
T mx_inline_sum(const T *v, octave_idx_type n)
T mx_inline_prod(const T *v, octave_idx_type n)
void mx_inline_diff(const T *v, T *r, octave_idx_type n, octave_idx_type order)
bool mx_inline_all_finite(std::size_t n, const T *x)
bool mx_inline_any_negative(std::size_t n, const T *x)
void mx_inline_pow(std::size_t n, R *r, const X *x, const Y *y)
subst_template_param< std::complex, T, double >::type mx_inline_dsum(const T *v, octave_idx_type n)
#define NDND_BOOL_OPS(ND1, ND2)
Definition mx-op-defs.h:350
#define NDS_BOOL_OPS(ND, S)
Definition mx-op-defs.h:256
#define NDND_CMP_OPS(ND1, ND2)
Definition mx-op-defs.h:333
#define SND_BOOL_OPS(S, ND)
Definition mx-op-defs.h:303
#define NDS_CMP_OPS(ND, S)
Definition mx-op-defs.h:239
#define SND_CMP_OPS(S, ND)
Definition mx-op-defs.h:286
#define MINMAX_FCNS(T, S)
Definition mx-op-defs.h:697
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
const octave_base_value const Array< octave_idx_type > & ra_idx
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d