Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/torch7.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoumith Chintala <soumith@gmail.com>2016-07-27 07:07:45 +0300
committerGitHub <noreply@github.com>2016-07-27 07:07:45 +0300
commite2f2d943e561fadf370710e0718d4d9de38765b9 (patch)
treeb9004e96297a78224c191f33af2a3dfb29563eec
parent58b9acd72bc39b3ea3b0426fc52fd28ffb0d7f97 (diff)
parent60eabb731313fccaf461f035ceb83a8cd6e1256d (diff)
Merge pull request #723 from andreaskoepf/histc_fix
Fix torch.histc problem #718
-rw-r--r--lib/TH/generic/THTensorMath.c24
1 files changed, 6 insertions, 18 deletions
diff --git a/lib/TH/generic/THTensorMath.c b/lib/TH/generic/THTensorMath.c
index 47bd6ec..c3da469 100644
--- a/lib/TH/generic/THTensorMath.c
+++ b/lib/TH/generic/THTensorMath.c
@@ -2466,10 +2466,8 @@ void THTensor_(randn)(THTensor *r_, THGenerator *_generator, THLongStorage *size
void THTensor_(histc)(THTensor *hist, THTensor *tensor, long nbins, real minvalue, real maxvalue)
{
- THTensor *clone;
real minval;
real maxval;
- real bins;
real *h_data;
THTensor_(resize1d)(hist, nbins);
@@ -2486,25 +2484,15 @@ void THTensor_(histc)(THTensor *hist, THTensor *tensor, long nbins, real minvalu
minval = minval - 1;
maxval = maxval + 1;
}
- bins = (real)(nbins)-1e-6;
-
-
- clone = THTensor_(newWithSize1d)(THTensor_(nElement)(tensor));
- THTensor_(copy)(clone,tensor);
- THTensor_(add)(clone, clone, -minval);
- THTensor_(div)(clone, clone, (maxval-minval));
- THTensor_(mul)(clone, clone, bins);
- THTensor_(floor)(clone, clone);
- THTensor_(add)(clone, clone, 1);
h_data = THTensor_(data)(hist);
- TH_TENSOR_APPLY(real, clone, \
- if ((*clone_data <= nbins) && (*clone_data >= 1)) { \
- *(h_data + (int)(*clone_data) - 1) += 1; \
- });
-
- THTensor_(free)(clone);
+ TH_TENSOR_APPLY(real, tensor,
+ if (*tensor_data >= minval && *tensor_data <= maxval) {
+ const int bin = (int)((*tensor_data-minval) / (maxval-minval) * nbins);
+ h_data[THMin(bin, nbins-1)] += 1;
+ }
+ );
}
#endif /* floating point only part */