gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 4845486 2/2: Library: gal_statistics_unique fi


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 4845486 2/2: Library: gal_statistics_unique finds the unique elements of the input
Date: Tue, 5 Nov 2019 12:48:04 -0500 (EST)

branch: master
commit 4845486ebe03dd7b6696f6dcaeba09b5c611c2ba
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Library: gal_statistics_unique finds the unique elements of the input
    
    Until now this function was only available in the Arithmetic program. But
    it can be useful in many other contexts also.
    
    With this commit, it has been moved to the library and given this new name.
---
 NEWS                        |  1 +
 bin/arithmetic/arithmetic.c | 46 ++-------------------------------------
 doc/gnuastro.texi           |  6 +++++
 lib/gnuastro/statistics.h   |  3 +++
 lib/statistics.c            | 53 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 65 insertions(+), 44 deletions(-)

diff --git a/NEWS b/NEWS
index 7375d0a..9224f06 100644
--- a/NEWS
+++ b/NEWS
@@ -115,6 +115,7 @@ See the end of the file for license conditions.
    - gal_binary_connected_indexs: store indexs of connected components.
    - gal_box_bound_ellipsoid_extent: Extent of 3D ellipsoid.
    - gal_box_bound_ellipsoid: Bounding box for a 3D ellipsoid.
+   - gal_statistics_unique: Return unique (non-blank) elements of the input.
 
 ** Removed features
 
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 82bbba3..39dec1a 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -882,53 +882,11 @@ arithmetic_tofile(struct arithmeticparams *p, char 
*token, int freeflag)
 
 
 
-/* Pull out unique elements */
-#define UNIQUE_BYTYPE(TYPE) {                                           \
-    size_t i, j;                                                        \
-    TYPE *a=input->array, b;                                            \
-                                                                        \
-    /* Write the blank value for this type into `b'. */                 \
-    gal_blank_write(&b, input->type);                                   \
-                                                                        \
-    /* Go over the elements, and set the duplicates to blank. */        \
-    /* Note that for integers and floats, the behavior of blank/NaN */  \
-    /* differs: for floats (NaN), we can identify a blank using the  */ \
-    /* fact that by definition, NaN!=NaN. */                            \
-    if(b==b)                                                            \
-      for(i=0;i<input->size;++i)                                        \
-        { if(a[i]!=b)    for(j=i+1;j<input->size;++j) if(a[i]==a[j]) a[j]=b;} \
-    else                                                                \
-      for(i=0;i<input->size;++i)                                        \
-        { if(a[i]==a[i]) for(j=i+1;j<input->size;++j) if(a[i]==a[j]) a[j]=b;} \
-  }
-
 void
 arithmetic_unique(struct arithmeticparams *p, char *token, int operator)
 {
-  gal_data_t *input = operands_pop(p, token);
-
-  /* Remove the duplicates based on size. */
-  switch(input->type)
-    {
-    case GAL_TYPE_UINT8:   UNIQUE_BYTYPE( uint8_t  ); break;
-    case GAL_TYPE_INT8:    UNIQUE_BYTYPE( int8_t   ); break;
-    case GAL_TYPE_UINT16:  UNIQUE_BYTYPE( uint16_t ); break;
-    case GAL_TYPE_INT16:   UNIQUE_BYTYPE( int16_t  ); break;
-    case GAL_TYPE_UINT32:  UNIQUE_BYTYPE( uint32_t ); break;
-    case GAL_TYPE_INT32:   UNIQUE_BYTYPE( int32_t  ); break;
-    case GAL_TYPE_UINT64:  UNIQUE_BYTYPE( uint64_t ); break;
-    case GAL_TYPE_INT64:   UNIQUE_BYTYPE( int64_t  ); break;
-    case GAL_TYPE_FLOAT32: UNIQUE_BYTYPE( float    ); break;
-    case GAL_TYPE_FLOAT64: UNIQUE_BYTYPE( double   ); break;
-    default:
-      error(EXIT_FAILURE, 0, "the `unique' operator doesn't support type "
-            "code `%u'", input->type);
-    }
-
-  /* Remove all blank elements. */
-  gal_blank_remove(input);
-
-  /* Add the collapsed dataset to the top of the operands. */
+  /* Pass the popped operand to the statistics library. */
+  gal_data_t *input = gal_statistics_unique(operands_pop(p, token), 1);
   operands_add(p, NULL, input);
 }
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index bd9d0b4..48a03b6 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -23715,6 +23715,12 @@ than the input's largest element, then the returned 
value is 1. See
 @code{gal_statistics_median} for a description of @code{inplace}.
 @end deftypefun
 
+@deftypefun {gal_data_t *} gal_statistics_unique (gal_data_t @code{*input}, 
int @code{inplace})
+Return a 1D dataset with the same numeric data type as the input, but only 
containing its unique elements and without any (possible) blank/NaN elements.
+Note that the input's number of dimensions is irrelevant for this function.
+If @code{inplace} is not zero, then the unique values will over-write the 
allocated space of the input, otherwise a new space will be allocated and the 
input will not be touched.
+@end deftypefun
+
 @deftypefun {gal_data_t *} gal_statistics_mode (gal_data_t @code{*input}, 
float @code{mirrordist}, int @code{inplace})
 Return a four-element (@code{double} or @code{float64}) dataset that
 contains the mode of the @code{input} distribution. This function
diff --git a/lib/gnuastro/statistics.h b/lib/gnuastro/statistics.h
index 9dcf35a..f128f82 100644
--- a/lib/gnuastro/statistics.h
+++ b/lib/gnuastro/statistics.h
@@ -108,6 +108,9 @@ gal_data_t *
 gal_statistics_quantile_function(gal_data_t *input, gal_data_t *value,
                                  int inplace);
 
+gal_data_t *
+gal_statistics_unique(gal_data_t *input, int inplace);
+
 
 
 
diff --git a/lib/statistics.c b/lib/statistics.c
index c942126..47f22a9 100644
--- a/lib/statistics.c
+++ b/lib/statistics.c
@@ -537,6 +537,59 @@ gal_statistics_quantile_function(gal_data_t *input, 
gal_data_t *value,
 
 
 
+/* Pull out unique elements */
+#define UNIQUE_BYTYPE(TYPE) {                                           \
+    size_t i, j;                                                        \
+    TYPE *a=out->array, b;                                              \
+                                                                        \
+    /* Write the blank value for this type into `b'. */                 \
+    gal_blank_write(&b, out->type);                                     \
+                                                                        \
+    /* Go over the elements, and set the duplicates to blank. */        \
+    /* Note that for integers and floats, the behavior of blank/NaN */  \
+    /* differs: for floats (NaN), we can identify a blank using the  */ \
+    /* fact that by definition, NaN!=NaN. */                            \
+    if(b==b)                                                            \
+      for(i=0;i<out->size;++i)                                          \
+        { if(a[i]!=b)    for(j=i+1;j<out->size;++j) if(a[i]==a[j]) a[j]=b;} \
+    else                                                                \
+      for(i=0;i<out->size;++i)                                          \
+        { if(a[i]==a[i]) for(j=i+1;j<out->size;++j) if(a[i]==a[j]) a[j]=b;} \
+  }
+
+gal_data_t *
+gal_statistics_unique(gal_data_t *input, int inplace)
+{
+  gal_data_t *out = inplace ? input : gal_data_copy(input);
+
+  /* Remove the duplicates based on size. */
+  switch(out->type)
+    {
+    case GAL_TYPE_UINT8:   UNIQUE_BYTYPE( uint8_t  ); break;
+    case GAL_TYPE_INT8:    UNIQUE_BYTYPE( int8_t   ); break;
+    case GAL_TYPE_UINT16:  UNIQUE_BYTYPE( uint16_t ); break;
+    case GAL_TYPE_INT16:   UNIQUE_BYTYPE( int16_t  ); break;
+    case GAL_TYPE_UINT32:  UNIQUE_BYTYPE( uint32_t ); break;
+    case GAL_TYPE_INT32:   UNIQUE_BYTYPE( int32_t  ); break;
+    case GAL_TYPE_UINT64:  UNIQUE_BYTYPE( uint64_t ); break;
+    case GAL_TYPE_INT64:   UNIQUE_BYTYPE( int64_t  ); break;
+    case GAL_TYPE_FLOAT32: UNIQUE_BYTYPE( float    ); break;
+    case GAL_TYPE_FLOAT64: UNIQUE_BYTYPE( double   ); break;
+    default:
+      error(EXIT_FAILURE, 0, "the `unique' operator doesn't support type "
+            "code `%u'", out->type);
+    }
+
+  /* Remove all blank elements (note that `gal_blank_remove' also corrects
+     the size of the dataset and sets it to 1D). */
+  gal_blank_remove(out);
+  return out;
+}
+
+
+
+
+
 
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]