GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Array-f.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2017 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 the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 // Instantiate Arrays of float 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 (float x, float y)
54 {
55  return octave::math::isnan (y) ? ! octave::math::isnan (x) : x < y;
56 }
57 
58 static bool
59 nan_descending_compare (float x, float y)
60 {
61  return octave::math::isnan (x) ? ! octave::math::isnan (y) : x > y;
62 }
63 
65 safe_comparator (sortmode mode, const Array<float>& 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)
85  result = nan_ascending_compare;
86  else if (mode == DESCENDING)
87  result = nan_descending_compare;
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 float *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  float 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  float 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<float>;
165 
166 INSTANTIATE_ARRAY (float, OCTAVE_API);
167 
168 template OCTAVE_API std::ostream& operator << (std::ostream&,
169  const Array<float>&);
170 
171 #include "DiagArray2.h"
172 #include "DiagArray2.cc"
173 
174 template class OCTAVE_API DiagArray2<float>;
sortmode
Definition: oct-sort.h:105
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
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:6386
bool isnan(double x)
Definition: lo-mappers.cc:347
for large enough k
Definition: lu.cc:606
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:398
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< float > &)
const T * data(void) const
Definition: Array.h:582
#define INSTANTIATE_ARRAY(T, API)
Definition: Array.cc:2767
With real return the complex result
Definition: data.cc:3375
sortmode is_sorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2052
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
static bool nan_ascending_compare(float x, float y)
Definition: Array-f.cc:53
static bool nan_descending_compare(float x, float y)
Definition: Array-f.cc:59
the element is set to zero In other the statement xample y
Definition: data.cc:5342
Array< float >::compare_fcn_type safe_comparator(sortmode mode, const Array< float > &a, bool allow_chk)
Definition: Array-f.cc:65
bool sort_isnan< float >(float x)
Definition: Array-f.cc:47
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x