GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Array-d.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 // Instantiate Arrays of double values.
29 
30 #include "lo-mappers.h"
31 #include "Array.h"
32 #include "Array.cc"
33 #include "oct-locbuf.h"
34 
35 #define INLINE_ASCENDING_SORT 1
36 #define INLINE_DESCENDING_SORT 1
37 #include "oct-sort.cc"
38 
39 // Prevent implicit instantiations on some systems (Windows, others?)
40 // that can lead to duplicate definitions of static data members.
41 
42 extern template class OCTAVE_API Array<idx_vector>;
43 extern template class OCTAVE_API Array<octave_idx_type>;
44 
45 template <>
46 inline bool
48 {
49  return octave::math::isnan (x);
50 }
51 
52 static bool
53 nan_ascending_compare (double x, double y)
54 {
55  return octave::math::isnan (y) ? ! octave::math::isnan (x) : x < y;
56 }
57 
58 static bool
59 nan_descending_compare (double x, double y)
60 {
61  return octave::math::isnan (x) ? ! octave::math::isnan (y) : x > y;
62 }
63 
65 safe_comparator (sortmode mode, const Array<double>& a , bool allow_chk)
66 {
68 
69  if (allow_chk)
70  {
71  octave_idx_type k = 0;
72  for (; k < a.numel () && ! octave::math::isnan (a(k)); k++) ;
73  if (k == a.numel ())
74  {
75  if (mode == ASCENDING)
77  else if (mode == DESCENDING)
79  }
80  }
81 
82  if (! result)
83  {
84  if (mode == ASCENDING)
86  else if (mode == DESCENDING)
88  }
89 
90  return result;
91 }
92 
93 // The default solution using NaN-safe comparator is OK, but almost twice as
94 // slow than this code.
95 template <>
96 OCTAVE_API
99 {
100  octave_idx_type n = numel ();
101 
102  const double *el = data ();
103 
104  if (n <= 1)
105  return mode ? mode : ASCENDING;
106 
107  if (! mode)
108  {
109  // Auto-detect mode.
110  if (el[n-1] < el[0] || octave::math::isnan (el[0]))
111  mode = DESCENDING;
112  else
113  mode = ASCENDING;
114  }
115 
116  if (mode == DESCENDING)
117  {
118  octave_idx_type j = 0;
119  double r;
120  // Sort out NaNs.
121  do
122  r = el[j++];
123  while (octave::math::isnan (r) && j < n);
124 
125  // Orient the test so that NaN will not pass through.
126  for (; j < n; j++)
127  {
128  if (r >= el[j])
129  r = el[j];
130  else
131  {
132  mode = UNSORTED;
133  break;
134  }
135  }
136 
137  }
138  else if (mode == ASCENDING)
139  {
140  // Sort out NaNs.
141  while (n > 0 && octave::math::isnan (el[n-1]))
142  n--;
143 
144  if (n > 0)
145  {
146  // Orient the test so that NaN will not pass through.
147  double r = el[0];
148  for (octave_idx_type j = 1; j < n; j++)
149  {
150  if (r <= el[j])
151  r = el[j];
152  else
153  {
154  mode = UNSORTED;
155  break;
156  }
157  }
158  }
159  }
160 
161  return mode;
162 }
163 
164 template class OCTAVE_API octave_sort<double>;
165 
166 INSTANTIATE_ARRAY (double, OCTAVE_API);
167 
168 template OCTAVE_API std::ostream& operator << (std::ostream&,
169  const Array<double>&);
170 
171 #include "DiagArray2.h"
172 #include "DiagArray2.cc"
173 
174 template class OCTAVE_API DiagArray2<double>;
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< double > &)
bool sort_isnan< double >(double x)
Definition: Array-d.cc:47
const T * data(void) const
Definition: Array.h:582
sortmode
Definition: oct-sort.h:105
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
for large enough k
Definition: lu.cc:617
bool isnan(bool)
Definition: lo-mappers.h:187
static bool nan_descending_compare(double x, double y)
Definition: Array-d.cc:59
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
sortmode issorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2033
#define INSTANTIATE_ARRAY(T, API)
Definition: Array.cc:2768
With real return the complex result
Definition: data.cc:3260
Array< double >::compare_fcn_type safe_comparator(sortmode mode, const Array< double > &a, bool allow_chk)
Definition: Array-d.cc:65
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
static bool nan_ascending_compare(double x, double y)
Definition: Array-d.cc:53
the element is set to zero In other the statement xample y
Definition: data.cc:5264
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x