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:
-rw-r--r--TensorMath.lua8
-rwxr-xr-xdoc/maths.md37
-rw-r--r--lib/TH/generic/THTensorMath.c39
-rw-r--r--lib/TH/generic/THTensorMath.h1
-rw-r--r--test/test.lua12
5 files changed, 97 insertions, 0 deletions
diff --git a/TensorMath.lua b/TensorMath.lua
index 682de23..e8cb97b 100644
--- a/TensorMath.lua
+++ b/TensorMath.lua
@@ -1088,6 +1088,14 @@ static void THTensor_random1__(THTensor *self, THGenerator *gen, long b)
{name="long",default=100},
{name="double",default=0},
{name="double",default=0}})
+
+ wrap("histc2",
+ cname("histc2"),
+ {{name=Tensor, default=true, returned=true},
+ {name=Tensor},
+ {name="long",default=100},
+ {name="double",default=0},
+ {name="double",default=0}})
wrap("norm",
cname("normall"),
diff --git a/doc/maths.md b/doc/maths.md
index 252b52d..433bf52 100755
--- a/doc/maths.md
+++ b/doc/maths.md
@@ -150,6 +150,43 @@ By default the elements are sorted into 100 equally spaced bins between the mini
`y = torch.histc(x, n, min, max)` same as above with `n` bins and `[min, max]` as elements range.
+<a name="torch.histc2"></a>
+### [res] torch.histc2([res,] x [,nbins, min_value, max_value]) ###
+<a name="torch.histc2"></a>
+
+`y = torch.histc2(x)` returns the histogram of the elements in 2d tensor `x` along the last dimension.
+By default the elements are sorted into 100 equally spaced bins between the minimum and maximum values of `x`.
+
+`y = torch.histc(x, n)` same as above with `n` bins.
+
+`y = torch.histc(x, n, min, max)` same as above with `n` bins and `[min, max]` as elements range.
+
+```lua
+x =torch.Tensor(3, 6)
+
+> x[1] = torch.Tensor{ 2, 4, 2, 2, 5, 4 }
+> x[2] = torch.Tensor{ 3, 5, 1, 5, 3, 5 }
+> x[3] = torch.Tensor{ 3, 4, 2, 5, 5, 1 }
+
+> x
+ 2 4 2 2 5 4
+ 3 5 1 5 3 5
+ 3 4 2 5 5 1
+[torch.DoubleTensor of size 3x6]
+
+> torch.histc2(x, 5, 1, 5)
+ 0 3 0 2 1
+ 1 0 2 0 3
+ 1 1 1 1 2
+[torch.DoubleTensor of size 3x5]
+
+> y = torch.Tensor(1, 6):copy(x[1])
+
+> torch.histc2(y, 5)
+ 3 0 2 0 1
+[torch.DoubleTensor of size 1x5]
+```
+
<a name="torch.linspace"></a>
### [res] torch.linspace([res,] x1, x2, [,n]) ###
<a name="torch.linspace"></a>
diff --git a/lib/TH/generic/THTensorMath.c b/lib/TH/generic/THTensorMath.c
index e04d3b6..765f9c4 100644
--- a/lib/TH/generic/THTensorMath.c
+++ b/lib/TH/generic/THTensorMath.c
@@ -2538,5 +2538,44 @@ void THTensor_(histc)(THTensor *hist, THTensor *tensor, long nbins, real minvalu
);
}
+void THTensor_(histc2)(THTensor *hist, THTensor *tensor, long nbins, real minvalue, real maxvalue)
+{
+ THArgCheck(THTensor_(nDimension)(tensor) < 3, 2, "invalid dimension %d, the input must be a 2d tensor", THTensor_(nDimension)(tensor));
+
+ int dimension = 1;
+ THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(tensor), 2, "invalid dimension %d",
+ dimension + TH_INDEX_BASE);
+
+ real minval;
+ real maxval;
+ real *h_data;
+
+ THTensor_(resize2d)(hist, tensor->size[0], nbins);
+ THTensor_(zero)(hist);
+
+ minval = minvalue;
+ maxval = maxvalue;
+ if (minval == maxval)
+ {
+ minval = THTensor_(minall)(tensor);
+ maxval = THTensor_(maxall)(tensor);
+ }
+ if (minval == maxval)
+ {
+ minval = minval - 1;
+ maxval = maxval + 1;
+ }
+
+ TH_TENSOR_DIM_APPLY2(real, tensor, real, hist, dimension, long i;
+ for(i = 0; i < tensor_size; i++)
+ {
+ if(tensor_data[i*tensor_stride] >= minval && tensor_data[i*tensor_stride] <= maxval) {
+ const int bin = (int)((tensor_data[i*tensor_stride]-minval) / (maxval-minval) * nbins);
+ hist_data[THMin(bin, nbins-1)] += 1;
+ }
+ }
+ );
+}
+
#endif /* floating point only part */
#endif
diff --git a/lib/TH/generic/THTensorMath.h b/lib/TH/generic/THTensorMath.h
index 87f1616..3908c27 100644
--- a/lib/TH/generic/THTensorMath.h
+++ b/lib/TH/generic/THTensorMath.h
@@ -163,6 +163,7 @@ TH_API void THTensor_(norm)(THTensor *r_, THTensor *t, real value, int dimension
TH_API void THTensor_(renorm)(THTensor *r_, THTensor *t, real value, int dimension, real maxnorm);
TH_API accreal THTensor_(dist)(THTensor *a, THTensor *b, real value);
TH_API void THTensor_(histc)(THTensor *hist, THTensor *tensor, long nbins, real minvalue, real maxvalue);
+TH_API void THTensor_(histc2)(THTensor *hist, THTensor *tensor, long nbins, real minvalue, real maxvalue);
TH_API accreal THTensor_(meanall)(THTensor *self);
TH_API accreal THTensor_(varall)(THTensor *self);
diff --git a/test/test.lua b/test/test.lua
index 3eb119f..4ab2f4a 100644
--- a/test/test.lua
+++ b/test/test.lua
@@ -1355,6 +1355,18 @@ function torchtest.histc()
local z = torch.Tensor{ 0, 3, 0, 2, 1 }
mytester:assertTensorEq(y,z,precision,'error in torch.histc')
end
+function torchtest.histc2()
+ local x = torch.Tensor(3, 6)
+ x[1] = torch.Tensor{ 2, 4, 2, 2, 5, 4 }
+ x[2] = torch.Tensor{ 3, 5, 1, 5, 3, 5 }
+ x[3] = torch.Tensor{ 3, 4, 2, 5, 5, 1 }
+ local y = torch.histc2(x, 5, 1, 5) -- nbins, min, max
+ local z = torch.Tensor(3, 5)
+ z[1] = torch.Tensor{ 0, 3, 0, 2, 1 }
+ z[2] = torch.Tensor{ 1, 0, 2, 0, 3 }
+ z[3] = torch.Tensor{ 1, 1, 1, 1, 2 }
+ mytester:assertTensorEq(y,z,precision,'error in torch.histc2 in last dimension')
+end
function torchtest.ones()
local mx = torch.ones(msize,msize)
local mxx = torch.Tensor()