File: fCNDArray.cc

package info (click to toggle)
octave 10.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,484 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (680 lines) | stat: -rw-r--r-- 16,777 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1996-2025 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <complex>
#include <istream>
#include <ostream>

#include "Array-util.h"
#include "f77-fcn.h"
#include "fCNDArray.h"
#include "lo-ieee.h"
#include "lo-mappers.h"
#include "mx-base.h"
#include "mx-op-defs.h"
#include "mx-fcnda-fs.h"
#include "oct-fftw.h"
#include "oct-locbuf.h"

#include "bsxfun-defs.cc"

FloatComplexNDArray::FloatComplexNDArray (const charNDArray& a)
  : MArray<FloatComplex> (a.dims ())
{
  octave_idx_type n = a.numel ();
  for (octave_idx_type i = 0; i < n; i++)
    xelem (i) = static_cast<unsigned char> (a(i));
}

#if defined (HAVE_FFTW)

FloatComplexNDArray
FloatComplexNDArray::fourier (int dim) const
{
  const dim_vector& dv = dims ();

  if (dim > dv.ndims () || dim < 0)
    return FloatComplexNDArray ();

  octave_idx_type stride = 1;
  octave_idx_type n = dv(dim);

  for (int i = 0; i < dim; i++)
    stride *= dv(i);

  octave_idx_type howmany = numel () / dv(dim);
  howmany = (stride == 1 ? howmany : (howmany > stride ? stride : howmany));
  octave_idx_type nloop = (stride == 1 ? 1 : numel () / dv(dim) / stride);
  octave_idx_type dist = (stride == 1 ? n : 1);

  const FloatComplex *in (data ());
  FloatComplexNDArray retval (dv);
  FloatComplex *out (retval.rwdata ());

  // Need to be careful here about the distance between fft's
  for (octave_idx_type k = 0; k < nloop; k++)
    octave::fftw::fft (in + k * stride * n, out + k * stride * n,
                       n, howmany, stride, dist);

  return retval;
}

FloatComplexNDArray
FloatComplexNDArray::ifourier (int dim) const
{
  const dim_vector& dv = dims ();

  if (dim > dv.ndims () || dim < 0)
    return FloatComplexNDArray ();

  octave_idx_type stride = 1;
  octave_idx_type n = dv(dim);

  for (int i = 0; i < dim; i++)
    stride *= dv(i);

  octave_idx_type howmany = numel () / dv(dim);
  howmany = (stride == 1 ? howmany : (howmany > stride ? stride : howmany));
  octave_idx_type nloop = (stride == 1 ? 1 : numel () / dv(dim) / stride);
  octave_idx_type dist = (stride == 1 ? n : 1);

  const FloatComplex *in (data ());
  FloatComplexNDArray retval (dv);
  FloatComplex *out (retval.rwdata ());

  // Need to be careful here about the distance between fft's
  for (octave_idx_type k = 0; k < nloop; k++)
    octave::fftw::ifft (in + k * stride * n, out + k * stride * n,
                        n, howmany, stride, dist);

  return retval;
}

FloatComplexNDArray
FloatComplexNDArray::fourier2d () const
{
  const dim_vector& dv = dims ();
  if (dv.ndims () < 2)
    return FloatComplexNDArray ();

  dim_vector dv2 (dv(0), dv(1));
  const FloatComplex *in = data ();
  FloatComplexNDArray retval (dv);
  FloatComplex *out = retval.rwdata ();
  octave_idx_type howmany = numel () / dv(0) / dv(1);
  octave_idx_type dist = dv(0) * dv(1);

  for (octave_idx_type i=0; i < howmany; i++)
    octave::fftw::fftNd (in + i*dist, out + i*dist, 2, dv2);

  return retval;
}

FloatComplexNDArray
FloatComplexNDArray::ifourier2d () const
{
  const dim_vector& dv = dims ();
  if (dv.ndims () < 2)
    return FloatComplexNDArray ();

  dim_vector dv2 (dv(0), dv(1));
  const FloatComplex *in = data ();
  FloatComplexNDArray retval (dv);
  FloatComplex *out = retval.rwdata ();
  octave_idx_type howmany = numel () / dv(0) / dv(1);
  octave_idx_type dist = dv(0) * dv(1);

  for (octave_idx_type i=0; i < howmany; i++)
    octave::fftw::ifftNd (in + i*dist, out + i*dist, 2, dv2);

  return retval;
}

FloatComplexNDArray
FloatComplexNDArray::fourierNd () const
{
  const dim_vector& dv = dims ();
  int rank = dv.ndims ();

  const FloatComplex *in (data ());
  FloatComplexNDArray retval (dv);
  FloatComplex *out (retval.rwdata ());

  octave::fftw::fftNd (in, out, rank, dv);

  return retval;
}

FloatComplexNDArray
FloatComplexNDArray::ifourierNd () const
{
  const dim_vector& dv = dims ();
  int rank = dv.ndims ();

  const FloatComplex *in (data ());
  FloatComplexNDArray retval (dv);
  FloatComplex *out (retval.rwdata ());

  octave::fftw::ifftNd (in, out, rank, dv);

  return retval;
}

#else

FloatComplexNDArray
FloatComplexNDArray::fourier (int dim) const
{
  octave_unused_parameter (dim);

  (*current_liboctave_error_handler)
    ("support for FFTW was unavailable or disabled when liboctave was built");

  return FloatComplexNDArray ();
}

FloatComplexNDArray
FloatComplexNDArray::ifourier (int dim) const
{
  octave_unused_parameter (dim);

  (*current_liboctave_error_handler)
    ("support for FFTW was unavailable or disabled when liboctave was built");

  return FloatComplexNDArray ();
}

FloatComplexNDArray
FloatComplexNDArray::fourier2d () const
{
  (*current_liboctave_error_handler)
    ("support for FFTW was unavailable or disabled when liboctave was built");

  return FloatComplexNDArray ();
}

FloatComplexNDArray
FloatComplexNDArray::ifourier2d () const
{
  (*current_liboctave_error_handler)
    ("support for FFTW was unavailable or disabled when liboctave was built");

  return FloatComplexNDArray ();
}

FloatComplexNDArray
FloatComplexNDArray::fourierNd () const
{
  (*current_liboctave_error_handler)
    ("support for FFTW was unavailable or disabled when liboctave was built");

  return FloatComplexNDArray ();
}

FloatComplexNDArray
FloatComplexNDArray::ifourierNd () const
{
  (*current_liboctave_error_handler)
    ("support for FFTW was unavailable or disabled when liboctave was built");

  return FloatComplexNDArray ();
}

#endif

// unary operations

boolNDArray
FloatComplexNDArray::operator ! () const
{
  if (any_element_is_nan ())
    octave::err_nan_to_logical_conversion ();

  return do_mx_unary_op<bool, FloatComplex> (*this, mx_inline_not);
}

// FIXME: this is not quite the right thing.

bool
FloatComplexNDArray::any_element_is_nan () const
{
  return do_mx_check<FloatComplex> (*this, mx_inline_any_nan);
}

bool
FloatComplexNDArray::any_element_is_inf_or_nan () const
{
  return ! do_mx_check<FloatComplex> (*this, mx_inline_all_finite);
}

// Return true if no elements have imaginary components.

bool
FloatComplexNDArray::all_elements_are_real () const
{
  return do_mx_check<FloatComplex> (*this, mx_inline_all_real);
}

// Return nonzero if any element of CM has a non-integer real or
// imaginary part.  Also extract the largest and smallest (real or
// imaginary) values and return them in MAX_VAL and MIN_VAL.

bool
FloatComplexNDArray::all_integers (float& max_val, float& min_val) const
{
  octave_idx_type nel = numel ();

  if (nel > 0)
    {
      FloatComplex val = elem (0);

      float r_val = val.real ();
      float i_val = val.imag ();

      max_val = r_val;
      min_val = r_val;

      if (i_val > max_val)
        max_val = i_val;

      if (i_val < max_val)
        min_val = i_val;
    }
  else
    return false;

  for (octave_idx_type i = 0; i < nel; i++)
    {
      FloatComplex val = elem (i);

      float r_val = val.real ();
      float i_val = val.imag ();

      if (r_val > max_val)
        max_val = r_val;

      if (i_val > max_val)
        max_val = i_val;

      if (r_val < min_val)
        min_val = r_val;

      if (i_val < min_val)
        min_val = i_val;

      if (octave::math::x_nint (r_val) != r_val
          || octave::math::x_nint (i_val) != i_val)
        return false;
    }

  return true;
}

bool
FloatComplexNDArray::too_large_for_float () const
{
  return false;
}

boolNDArray
FloatComplexNDArray::all (int dim) const
{
  return do_mx_red_op<bool, FloatComplex> (*this, dim, mx_inline_all);
}

boolNDArray
FloatComplexNDArray::any (int dim) const
{
  return do_mx_red_op<bool, FloatComplex> (*this, dim, mx_inline_any);
}

FloatComplexNDArray
FloatComplexNDArray::cumprod (int dim) const
{
  return do_mx_cum_op<FloatComplex, FloatComplex> (*this, dim,
                                                   mx_inline_cumprod);
}

FloatComplexNDArray
FloatComplexNDArray::cumsum (int dim) const
{
  return do_mx_cum_op<FloatComplex, FloatComplex> (*this, dim,
                                                   mx_inline_cumsum);
}

FloatComplexNDArray
FloatComplexNDArray::prod (int dim) const
{
  return do_mx_red_op<FloatComplex, FloatComplex> (*this, dim, mx_inline_prod);
}

ComplexNDArray
FloatComplexNDArray::dprod (int dim) const
{
  return do_mx_red_op<Complex, FloatComplex> (*this, dim, mx_inline_dprod);
}

FloatComplexNDArray
FloatComplexNDArray::sum (int dim) const
{
  return do_mx_red_op<FloatComplex, FloatComplex> (*this, dim, mx_inline_sum);
}

ComplexNDArray
FloatComplexNDArray::dsum (int dim) const
{
  return do_mx_red_op<Complex, FloatComplex> (*this, dim, mx_inline_dsum);
}

FloatComplexNDArray
FloatComplexNDArray::sumsq (int dim) const
{
  return do_mx_red_op<float, FloatComplex> (*this, dim, mx_inline_sumsq);
}

FloatComplexNDArray
FloatComplexNDArray::diff (octave_idx_type order, int dim) const
{
  return do_mx_diff_op<FloatComplex> (*this, dim, order, mx_inline_diff);
}

FloatComplexNDArray
FloatComplexNDArray::concat (const FloatComplexNDArray& rb,
                             const Array<octave_idx_type>& ra_idx)
{
  if (rb.numel () > 0)
    insert (rb, ra_idx);
  return *this;
}

FloatComplexNDArray
FloatComplexNDArray::concat (const FloatNDArray& rb,
                             const Array<octave_idx_type>& ra_idx)
{
  FloatComplexNDArray tmp (rb);
  if (rb.numel () > 0)
    insert (tmp, ra_idx);
  return *this;
}

FloatComplexNDArray
concat (NDArray& ra, FloatComplexNDArray& rb,
        const Array<octave_idx_type>& ra_idx)
{
  FloatComplexNDArray retval (ra);
  if (rb.numel () > 0)
    retval.insert (rb, ra_idx);
  return retval;
}

static const FloatComplex FloatComplex_NaN_result (octave::numeric_limits<float>::NaN (),
                                                   octave::numeric_limits<float>::NaN ());

FloatComplexNDArray
FloatComplexNDArray::max (int dim) const
{
  return do_mx_minmax_op<FloatComplex> (*this, dim, mx_inline_max);
}

FloatComplexNDArray
FloatComplexNDArray::max (Array<octave_idx_type>& idx_arg, int dim) const
{
  return do_mx_minmax_op<FloatComplex> (*this, idx_arg, dim, mx_inline_max);
}

FloatComplexNDArray
FloatComplexNDArray::min (int dim) const
{
  return do_mx_minmax_op<FloatComplex> (*this, dim, mx_inline_min);
}

FloatComplexNDArray
FloatComplexNDArray::min (Array<octave_idx_type>& idx_arg, int dim) const
{
  return do_mx_minmax_op<FloatComplex> (*this, idx_arg, dim, mx_inline_min);
}

FloatComplexNDArray
FloatComplexNDArray::cummax (int dim) const
{
  return do_mx_cumminmax_op<FloatComplex> (*this, dim, mx_inline_cummax);
}

FloatComplexNDArray
FloatComplexNDArray::cummax (Array<octave_idx_type>& idx_arg, int dim) const
{
  return do_mx_cumminmax_op<FloatComplex> (*this, idx_arg, dim,
                                           mx_inline_cummax);
}

FloatComplexNDArray
FloatComplexNDArray::cummin (int dim) const
{
  return do_mx_cumminmax_op<FloatComplex> (*this, dim, mx_inline_cummin);
}

FloatComplexNDArray
FloatComplexNDArray::cummin (Array<octave_idx_type>& idx_arg, int dim) const
{
  return do_mx_cumminmax_op<FloatComplex> (*this, idx_arg, dim,
                                           mx_inline_cummin);
}

FloatNDArray
FloatComplexNDArray::abs () const
{
  return do_mx_unary_map<float, FloatComplex, std::abs> (*this);
}

boolNDArray
FloatComplexNDArray::isnan () const
{
  return do_mx_unary_map<bool, FloatComplex, octave::math::isnan> (*this);
}

boolNDArray
FloatComplexNDArray::isinf () const
{
  return do_mx_unary_map<bool, FloatComplex, octave::math::isinf> (*this);
}

boolNDArray
FloatComplexNDArray::isfinite () const
{
  return do_mx_unary_map<bool, FloatComplex, octave::math::isfinite> (*this);
}

FloatComplexNDArray
conj (const FloatComplexNDArray& a)
{
  return do_mx_unary_map<FloatComplex, FloatComplex, std::conj<float>> (a);
}

FloatComplexNDArray&
FloatComplexNDArray::insert (const NDArray& a,
                             octave_idx_type r, octave_idx_type c)
{
  const dim_vector& a_dv = a.dims ();
  const dim_vector& dv = dims ();

  int n = a_dv.ndims ();

  if (n == dv.ndims ())
    {
      Array<octave_idx_type> a_ra_idx (dim_vector (a_dv.ndims (), 1), 0);

      a_ra_idx.elem (0) = r;
      a_ra_idx.elem (1) = c;

      for (int i = 0; i < n; i++)
        {
          if (a_ra_idx(i) < 0 || (a_ra_idx(i) + a_dv(i)) > dv(i))
            (*current_liboctave_error_handler)
              ("Array<T>::insert: range error for insert");
        }

      a_ra_idx.elem (0) = 0;
      a_ra_idx.elem (1) = 0;

      octave_idx_type n_elt = a.numel ();

      // IS make_unique () NECESSARY HERE?

      for (octave_idx_type i = 0; i < n_elt; i++)
        {
          Array<octave_idx_type> ra_idx = a_ra_idx;

          ra_idx.elem (0) = a_ra_idx(0) + r;
          ra_idx.elem (1) = a_ra_idx(1) + c;

          elem (ra_idx) = a.elem (a_ra_idx);

          increment_index (a_ra_idx, a_dv);
        }
    }
  else
    (*current_liboctave_error_handler)
      ("Array<T>::insert: invalid indexing operation");

  return *this;
}

FloatComplexNDArray&
FloatComplexNDArray::insert (const FloatComplexNDArray& a,
                             octave_idx_type r, octave_idx_type c)
{
  Array<FloatComplex>::insert (a, r, c);
  return *this;
}

FloatComplexNDArray&
FloatComplexNDArray::insert (const FloatComplexNDArray& a,
                             const Array<octave_idx_type>& ra_idx)
{
  Array<FloatComplex>::insert (a, ra_idx);
  return *this;
}

void
FloatComplexNDArray::increment_index (Array<octave_idx_type>& ra_idx,
                                      const dim_vector& dimensions,
                                      int start_dimension)
{
  ::increment_index (ra_idx, dimensions, start_dimension);
}

octave_idx_type
FloatComplexNDArray::compute_index (Array<octave_idx_type>& ra_idx,
                                    const dim_vector& dimensions)
{
  return ::compute_index (ra_idx, dimensions);
}

FloatComplexNDArray
FloatComplexNDArray::diag (octave_idx_type k) const
{
  return MArray<FloatComplex>::diag (k);
}

FloatComplexNDArray
FloatComplexNDArray::diag (octave_idx_type m, octave_idx_type n) const
{
  return MArray<FloatComplex>::diag (m, n);
}

// This contains no information on the array structure !!!
std::ostream&
operator << (std::ostream& os, const FloatComplexNDArray& a)
{
  octave_idx_type nel = a.numel ();

  for (octave_idx_type i = 0; i < nel; i++)
    {
      os << ' ';
      octave::write_value<Complex> (os, a.elem (i));
      os << "\n";
    }
  return os;
}

std::istream&
operator >> (std::istream& is, FloatComplexNDArray& a)
{
  octave_idx_type nel = a.numel ();

  if (nel > 0)
    {
      FloatComplex tmp;
      for (octave_idx_type i = 0; i < nel; i++)
        {
          tmp = octave::read_value<FloatComplex> (is);
          if (is)
            a.elem (i) = tmp;
          else
            return is;
        }
    }

  return is;
}

MINMAX_FCNS (FloatComplexNDArray, FloatComplex)

NDS_CMP_OPS (FloatComplexNDArray, FloatComplex)
NDS_BOOL_OPS (FloatComplexNDArray, FloatComplex)

SND_CMP_OPS (FloatComplex, FloatComplexNDArray)
SND_BOOL_OPS (FloatComplex, FloatComplexNDArray)

NDND_CMP_OPS (FloatComplexNDArray, FloatComplexNDArray)
NDND_BOOL_OPS (FloatComplexNDArray, FloatComplexNDArray)

FloatComplexNDArray& operator *= (FloatComplexNDArray& a, float s)
{
  if (a.is_shared ())
    a = a * s;
  else
    do_ms_inplace_op<FloatComplex, float> (a, s, mx_inline_mul2);
  return a;
}

FloatComplexNDArray&
operator /= (FloatComplexNDArray& a, float s)
{
  if (a.is_shared ())
    a = a / s;
  else
    do_ms_inplace_op<FloatComplex, float> (a, s, mx_inline_div2);
  return a;
}

BSXFUN_STDOP_DEFS_MXLOOP (FloatComplexNDArray)
BSXFUN_STDREL_DEFS_MXLOOP (FloatComplexNDArray)

BSXFUN_OP_DEF_MXLOOP (pow, FloatComplexNDArray, mx_inline_pow)