GNU Octave 10.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-2025 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-error.h"
38#include "lo-ieee.h"
39#include "lo-mappers.h"
40#include "mx-base.h"
41#include "mx-op-defs.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 ();
144
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::isinteger (val))
331 return false;
332 }
333
334 return true;
335}
336
337bool
339{
340 return test_all (octave::math::isinteger);
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
365{
366 return do_mx_cum_op<float, float> (*this, dim, mx_inline_cumprod);
367}
368
370FloatNDArray::cumsum (int dim) const
371{
372 return do_mx_cum_op<float, float> (*this, dim, mx_inline_cumsum);
373}
374
376FloatNDArray::prod (int dim) const
377{
378 return do_mx_red_op<float, float> (*this, dim, mx_inline_prod);
379}
380
382FloatNDArray::dprod (int dim) const
383{
384 return do_mx_red_op<double, float> (*this, dim, mx_inline_dprod);
385}
386
388FloatNDArray::sum (int dim) const
389{
390 return do_mx_red_op<float, float> (*this, dim, mx_inline_sum);
391}
392
394FloatNDArray::dsum (int dim) const
395{
396 return do_mx_red_op<double, float> (*this, dim, mx_inline_dsum);
397}
398
400FloatNDArray::sumsq (int dim) const
401{
402 return do_mx_red_op<float, float> (*this, dim, mx_inline_sumsq);
403}
404
406FloatNDArray::max (int dim) const
407{
408 return do_mx_minmax_op<float> (*this, dim, mx_inline_max);
409}
410
413{
414 return do_mx_minmax_op<float> (*this, idx_arg, dim, mx_inline_max);
415}
416
418FloatNDArray::min (int dim) const
419{
420 return do_mx_minmax_op<float> (*this, dim, mx_inline_min);
421}
422
425{
426 return do_mx_minmax_op<float> (*this, idx_arg, dim, mx_inline_min);
427}
428
430FloatNDArray::cummax (int dim) const
431{
432 return do_mx_cumminmax_op<float> (*this, dim, mx_inline_cummax);
433}
434
437{
438 return do_mx_cumminmax_op<float> (*this, idx_arg, dim, mx_inline_cummax);
439}
440
442FloatNDArray::cummin (int dim) const
443{
444 return do_mx_cumminmax_op<float> (*this, dim, mx_inline_cummin);
445}
446
449{
450 return do_mx_cumminmax_op<float> (*this, idx_arg, dim, mx_inline_cummin);
451}
452
455{
456 return do_mx_diff_op<float> (*this, dim, order, mx_inline_diff);
457}
458
462{
463 if (rb.numel () > 0)
464 insert (rb, ra_idx);
465 return *this;
466}
467
471{
472 FloatComplexNDArray retval (*this);
473 if (rb.numel () > 0)
474 retval.insert (rb, ra_idx);
475 return retval;
476}
477
481{
482 charNDArray retval (dims ());
483 octave_idx_type nel = numel ();
484
485 for (octave_idx_type i = 0; i < nel; i++)
486 {
487 float d = elem (i);
488
489 if (octave::math::isnan (d))
490 (*current_liboctave_error_handler)
491 ("invalid conversion from NaN to character");
492
493 octave_idx_type ival = octave::math::nint_big (d);
494
495 if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
496 // FIXME: is there something better to do? Should we warn the user?
497 ival = 0;
498
499 retval.elem (i) = static_cast<char> (ival);
500 }
501
502 if (rb.isempty ())
503 return retval;
504
505 retval.insert (rb, ra_idx);
506 return retval;
507}
508
511{
512 return do_mx_unary_op<float, FloatComplex> (a, mx_inline_real);
513}
514
517{
518 return do_mx_unary_op<float, FloatComplex> (a, mx_inline_imag);
519}
520
524{
525 Array<float>::insert (a, r, c);
526 return *this;
527}
528
532{
534 return *this;
535}
536
539{
540 return do_mx_unary_map<float, float, std::abs> (*this);
541}
542
545{
546 return do_mx_unary_map<bool, float, octave::math::isnan> (*this);
547}
548
551{
552 return do_mx_unary_map<bool, float, octave::math::isinf> (*this);
553}
554
557{
558 return do_mx_unary_map<bool, float, octave::math::isfinite> (*this);
559}
560
561void
563 const dim_vector& dimensions,
564 int start_dimension)
565{
566 ::increment_index (ra_idx, dimensions, start_dimension);
567}
568
571 const dim_vector& dimensions)
572{
573 return ::compute_index (ra_idx, dimensions);
574}
575
578{
579 return MArray<float>::diag (k);
580}
581
587
588// This contains no information on the array structure !!!
589std::ostream&
590operator << (std::ostream& os, const FloatNDArray& a)
591{
592 octave_idx_type nel = a.numel ();
593
594 for (octave_idx_type i = 0; i < nel; i++)
595 {
596 os << ' ';
597 octave::write_value<float> (os, a.elem (i));
598 os << "\n";
599 }
600 return os;
601}
602
603std::istream&
604operator >> (std::istream& is, FloatNDArray& a)
605{
606 octave_idx_type nel = a.numel ();
607
608 if (nel > 0)
609 {
610 float tmp;
611 for (octave_idx_type i = 0; i < nel; i++)
612 {
613 tmp = octave::read_value<float> (is);
614 if (is)
615 a.elem (i) = tmp;
616 else
617 return is;
618 }
619 }
620
621 return is;
622}
623
625
628
631
634
637
#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.h:130
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
Definition Array.h:507
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:525
bool test_all(F fcn) const
Size of the specified dimension.
Definition Array.h:924
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:563
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.h:652
const T * data() const
Size of the specified dimension.
Definition Array.h:665
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.h:418
FloatComplexNDArray & insert(const NDArray &a, octave_idx_type r, octave_idx_type c)
Definition fCNDArray.cc:520
bool any_element_is_positive(bool=false) const
Definition fNDArray.cc:268
boolNDArray isfinite() const
Definition fNDArray.cc:556
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition fNDArray.cc:570
FloatNDArray sum(int dim=-1) const
Definition fNDArray.cc:388
FloatNDArray min(int dim=-1) const
Definition fNDArray.cc:418
FloatComplexNDArray ifourier(int dim=1) const
Definition fNDArray.cc:89
FloatNDArray prod(int dim=-1) const
Definition fNDArray.cc:376
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 abs() const
Definition fNDArray.cc:538
FloatNDArray cumprod(int dim=-1) const
Definition fNDArray.cc:364
boolNDArray any(int dim=-1) const
Definition fNDArray.cc:358
boolNDArray isnan() const
Definition fNDArray.cc:544
boolNDArray all(int dim=-1) const
Definition fNDArray.cc:352
bool too_large_for_float() const
Definition fNDArray.cc:344
NDArray dprod(int dim=-1) const
Definition fNDArray.cc:382
FloatNDArray & insert(const FloatNDArray &a, octave_idx_type r, octave_idx_type c)
Definition fNDArray.cc:522
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
FloatNDArray cummin(int dim=-1) const
Definition fNDArray.cc:442
bool all_elements_are_zero() const
Definition fNDArray.cc:293
boolNDArray isinf() const
Definition fNDArray.cc:550
FloatNDArray cumsum(int dim=-1) const
Definition fNDArray.cc:370
FloatComplexNDArray ifourier2d() const
Definition fNDArray.cc:139
NDArray dsum(int dim=-1) const
Definition fNDArray.cc:394
FloatNDArray concat(const FloatNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition fNDArray.cc:460
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:562
FloatNDArray diff(octave_idx_type order=1, int dim=-1) const
Definition fNDArray.cc:454
FloatComplexNDArray ifourierNd() const
Definition fNDArray.cc:173
FloatComplexNDArray fourier(int dim=1) const
Definition fNDArray.cc:58
boolNDArray operator!() const
Definition fNDArray.cc:252
FloatNDArray cummax(int dim=-1) const
Definition fNDArray.cc:430
FloatNDArray sumsq(int dim=-1) const
Definition fNDArray.cc:400
FloatNDArray max(int dim=-1) const
Definition fNDArray.cc:406
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:577
friend class FloatComplexNDArray
Definition fNDArray.h:141
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:90
octave_idx_type ndims() const
Number of dimensions.
Definition dim-vector.h:253
std::ostream & operator<<(std::ostream &os, const FloatNDArray &a)
Definition fNDArray.cc:590
FloatNDArray imag(const FloatComplexNDArray &a)
Definition fNDArray.cc:516
std::istream & operator>>(std::istream &is, FloatNDArray &a)
Definition fNDArray.cc:604
FloatNDArray real(const FloatComplexNDArray &a)
Definition fNDArray.cc:510
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
bool mx_inline_any(const T *v, octave_idx_type n)
subst_template_param< std::complex, T, double >::type mx_inline_dprod(const T *v, octave_idx_type n)
void mx_inline_cummin(const T *v, T *r, octave_idx_type n)
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_max(const T *v, T *r, octave_idx_type n)
void mx_inline_not(std::size_t n, bool *r, const X *x)
void mx_inline_cummax(const T *v, T *r, octave_idx_type n)
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)
void mx_inline_min(const T *v, T *r, 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:589
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