[lua-torch-torch7] 03/04: Imported Upstream version 0~20160803-g17ff317

Zhou Mo cdluminate-guest at moszumanska.debian.org
Mon Aug 8 14:20:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

cdluminate-guest pushed a commit to branch master
in repository lua-torch-torch7.

commit 4e36b0bddff9e92957062a6ac725929bdbd01313
Author: Zhou Mo <cdluminate at gmail.com>
Date:   Mon Aug 8 14:18:49 2016 +0000

    Imported Upstream version 0~20160803-g17ff317
---
 TensorMath.lua                |   6 +--
 doc/maths.md                  |  10 ++--
 doc/tensor.md                 |  16 +++++-
 generic/TensorOperator.c      |   2 +-
 generic/luaG.h                |  15 +++++-
 init.lua                      |   3 ++
 lib/TH/THGeneral.h.in         |   4 ++
 lib/TH/generic/THBlas.c       |  25 +++++++--
 lib/TH/generic/THTensorMath.c | 114 +++++++++++++++++++-----------------------
 9 files changed, 117 insertions(+), 78 deletions(-)

diff --git a/TensorMath.lua b/TensorMath.lua
index 5a37b12..6b79237 100644
--- a/TensorMath.lua
+++ b/TensorMath.lua
@@ -61,7 +61,7 @@ static const void* torch_istensorarray(lua_State *L, int idx)
 
   lua_checkstack(L, 3);
   lua_rawgeti(L, idx, 1);
-  tensor_idx = lua_gettop(L);  
+  tensor_idx = lua_gettop(L);
   tname = (torch_istensortype(L, luaT_typename(L, -1)));
   lua_remove(L, tensor_idx);
   return tname;
@@ -316,7 +316,7 @@ for _,Tensor in ipairs({"ByteTensor", "CharTensor",
         {{name=Tensor, default=true, returned=true, method={default='nil'}},
          {name=Tensor, method={default=1}},
          {name=real}})
- 
+
    -- mod alias
    wrap("mod",
         cname("fmod"),
@@ -652,7 +652,7 @@ wrap("topk",
         {{name=Tensor, default=true, returned=true},
          {name="IndexTensor", default=true, returned=true, noreadadd=true},
          {name=Tensor},
-         {name="index"},
+         {name="long"},
          {name="index", default=lastdim(3)}})
 
    wrap("mode",
diff --git a/doc/maths.md b/doc/maths.md
index 5916409..fa322e0 100755
--- a/doc/maths.md
+++ b/doc/maths.md
@@ -1014,7 +1014,9 @@ Sizes must respect the matrix-multiplication operation: if `mat` is a `n × m` m
 
 `x:addmv(y, z)` accumulates `y * z` into `x`.
 
-`r:addmv(x, y, z)` puts the result of `x + y * z` into `r`.
+`r:addmv(x, y, z)` puts the result of `x + y * z` into `r` if `x` is a vector.
+
+`r:addmv(s, y, z)` puts the result of `s * r + y * z` into `r` if `s` is a scalar.
 
 
 <a name="torch.addr"></a>
@@ -1073,7 +1075,7 @@ If `vec1` is a vector of size `n` and `vec2` is a vector of size `m`, then `mat`
 
 
 <a name="torch.addmm"></a>
-### [res] torch.addmm([res,] [beta,] [v1,] M [v2,] mat1, mat2) ###
+### [res] torch.addmm([res,] [beta,] [v1,] M, [v2,] mat1, mat2) ###
 <a name="torch.addmm"></a>
 
 Performs a matrix-matrix multiplication between `mat1` (2D `Tensor`) and `mat2` (2D `Tensor`).
@@ -1101,7 +1103,7 @@ If `mat1` is a `n × m` matrix, `mat2` a `m × p` matrix, `M` must be a `n × p`
 
 
 <a name="torch.addbmm"></a>
-### [res] torch.addbmm([res,] [v1,] M [v2,] batch1, batch2) ###
+### [res] torch.addbmm([res,] [v1,] M, [v2,] batch1, batch2) ###
 <a name="torch.addbmm"></a>
 
 Batch matrix matrix product of matrices stored in `batch1` and `batch2`, with a reduced add step (all matrix multiplications get accumulated in a single place).
@@ -1123,7 +1125,7 @@ res = (v1 * M) + (v2 * sum(batch1_i * batch2_i, i = 1, b))
 
 
 <a name="torch.baddbmm"></a>
-### [res] torch.baddbmm([res,] [v1,] M [v2,] batch1, batch2) ###
+### [res] torch.baddbmm([res,] [v1,] M, [v2,] batch1, batch2) ###
 <a name="torch.baddbmm"></a>
 
 Batch matrix matrix product of matrices stored in `batch1` and `batch2`, with batch add.
diff --git a/doc/tensor.md b/doc/tensor.md
index 931d0a6..fabaaa7 100644
--- a/doc/tensor.md
+++ b/doc/tensor.md
@@ -1583,12 +1583,26 @@ Also note how an existing tensor `z` can be used to store the results.
 <a name="torch.Tensor.maskedCopy"></a>
 ### [Tensor] maskedCopy(mask, tensor) ###
 
-Copies the masked elements of `tensor` into itself. The masked elements are those elements having a
+Copies the elements of `tensor` into `mask` locations of itself. The masked elements are those elements having a
 corresponding `1` in the `mask` Tensor. This `mask` is a `torch.ByteTensor`
 of zeros and ones. The destination `Tensor` and the `mask` Tensor should have the same number of elements.
 The source `tensor` should have at least as many elements as the number of 1s in the `mask`.
 
 ```lua
+x = torch.Tensor({0, 0, 0, 0})
+mask = torch.ByteTensor({0, 1, 0, 1})
+y = torch.Tensor({10, 20})
+x:maskedCopy(y)
+print(x)
+
+  0
+ 10
+  0
+ 20
+[torch.DoubleTensor of size 4]
+```
+
+```lua
 x = torch.range(1,4):double():resize(2,2)
 > x
  1  2
diff --git a/generic/TensorOperator.c b/generic/TensorOperator.c
index 22506c0..eba6c81 100644
--- a/generic/TensorOperator.c
+++ b/generic/TensorOperator.c
@@ -149,7 +149,7 @@ static int torch_TensorOperator_(__div__)(lua_State *L)
 
   THTensor_(resizeAs)(r, tensor);
   THTensor_(copy)(r, tensor);
-  THTensor_(mul)(r, r, 1/lua_tonumber(L, 2));
+  THTensor_(div)(r, r, lua_tonumber(L, 2));
 
   return 1;
 }
diff --git a/generic/luaG.h b/generic/luaG.h
index fc1a3cc..950eae9 100644
--- a/generic/luaG.h
+++ b/generic/luaG.h
@@ -17,10 +17,21 @@ static void luaG_(pushreal)(lua_State *L, accreal n) {
 }
 
 static real luaG_(checkreal)(lua_State *L, int idx) {
-#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE) || LUA_VERSION_NUM < 503
+#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE)
 	return (lua_Number)luaL_checknumber(L, idx);
 #elif defined(TH_REAL_IS_BYTE) || defined(TH_REAL_IS_CHAR) || defined(TH_REAL_IS_SHORT) || defined(TH_REAL_IS_INT) || defined(TH_REAL_IS_LONG)
-	return (lua_Integer)luaL_checkinteger(L, idx);
+        int type = lua_type(L, idx);
+        if (type == LUA_TSTRING) {
+          const char *str = lua_tolstring(L, idx, NULL);
+          long int num = strtol(str, NULL, 0);
+          return (real) num;
+        } else {
+#if LUA_VERSION_NUM < 503
+          return (lua_Number)luaL_checkinteger(L, idx);
+#else
+          return (lua_Integer)luaL_checkinteger(L, idx);
+#endif
+        }
 #else
 	#error "unhandled real type in luaG_checkreal"
 #endif
diff --git a/init.lua b/init.lua
index d2eccc1..517f4c8 100644
--- a/init.lua
+++ b/init.lua
@@ -186,4 +186,7 @@ end
 -- alias for convenience
 torch.Tensor.isTensor = torch.isTensor
 
+-- remove this line to disable automatic heap-tracking for garbage collection
+torch.setheaptracking(true)
+
 return torch
diff --git a/lib/TH/THGeneral.h.in b/lib/TH/THGeneral.h.in
index 5c19da2..1b68f5e 100644
--- a/lib/TH/THGeneral.h.in
+++ b/lib/TH/THGeneral.h.in
@@ -41,6 +41,10 @@
 # define M_PI 3.14159265358979323846
 #endif
 
+#ifndef TH_INDEX_BASE
+#define TH_INDEX_BASE 1
+#endif
+
 TH_API double THLog1p(const double x);
 TH_API void _THError(const char *file, const int line, const char *fmt, ...);
 TH_API void _THAssertionFailed(const char *file, const int line, const char *exp, const char *fmt, ...);
diff --git a/lib/TH/generic/THBlas.c b/lib/TH/generic/THBlas.c
index 759689f..6452f94 100644
--- a/lib/TH/generic/THBlas.c
+++ b/lib/TH/generic/THBlas.c
@@ -214,7 +214,10 @@ void THBlas_(gemv)(char trans, long m, long n, real alpha, real *a, long lda, re
         real *row_ = a+lda*i;
         for(j = 0; j < m; j++)
           sum += x[j*incx]*row_[j];
-        y[i*incy] = beta*y[i*incy] + alpha*sum;
+	if (beta == 0)
+	  y[i*incy] = alpha*sum;
+	else
+	  y[i*incy] = beta*y[i*incy] + alpha*sum;
       }
     }
     else
@@ -329,7 +332,10 @@ void THBlas_(gemm)(char transa, char transb, long m, long n, long k, real alpha,
           for(l = 0; l < k; l++)
             sum += a_[l*lda]*b_[l];
           b_ += ldb;
-          c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
+	  if (beta == 0)
+	    c[j*ldc+i] = alpha*sum;
+	  else
+	    c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
         }
         a_++;
       }
@@ -346,7 +352,10 @@ void THBlas_(gemm)(char transa, char transb, long m, long n, long k, real alpha,
           for(l = 0; l < k; l++)
             sum += a_[l]*b_[l];
           b_ += ldb;
-          c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
+	  if (beta == 0)
+	    c[j*ldc+i] = alpha*sum;
+	  else
+	    c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
         }
         a_ += lda;
       }
@@ -363,7 +372,10 @@ void THBlas_(gemm)(char transa, char transb, long m, long n, long k, real alpha,
           for(l = 0; l < k; l++)
             sum += a_[l*lda]*b_[l*ldb];
           b_++;
-          c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
+	  if (beta == 0)
+	    c[j*ldc+i] = alpha*sum;
+	  else
+	    c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
         }
         a_++;
       }
@@ -380,7 +392,10 @@ void THBlas_(gemm)(char transa, char transb, long m, long n, long k, real alpha,
           for(l = 0; l < k; l++)
             sum += a_[l]*b_[l*ldb];
           b_++;
-          c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
+	  if (beta == 0)
+	    c[j*ldc+i] = alpha*sum;
+	  else
+	    c[j*ldc+i] = beta*c[j*ldc+i]+alpha*sum;
         }
         a_ += lda;
       }
diff --git a/lib/TH/generic/THTensorMath.c b/lib/TH/generic/THTensorMath.c
index 19843a8..c3da469 100644
--- a/lib/TH/generic/THTensorMath.c
+++ b/lib/TH/generic/THTensorMath.c
@@ -128,7 +128,7 @@ void THTensor_(indexSelect)(THTensor *tensor, THTensor *src, int dim, THLongTens
   real *tensor_data, *src_data;
 
   THArgCheck(index->nDimension == 1, 3, "Index is supposed to be a vector");
-  THArgCheck(dim < src->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim+1);
+  THArgCheck(dim < src->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim + TH_INDEX_BASE);
   THArgCheck(src->nDimension > 0,2,"Source tensor is empty");
 
   numel = THLongTensor_nElement(index);
@@ -149,9 +149,9 @@ void THTensor_(indexSelect)(THTensor *tensor, THTensor *src, int dim, THLongTens
     long rowsize = THTensor_(nElement)(src) / src->size[0];
 
     // check that the indices are within range
-    long max = src->size[0];
+    long max = src->size[0] - 1 + TH_INDEX_BASE;
     for (i=0; i<numel; i++) {
-      if (index_data[i] < 1 || index_data[i] > max) {
+      if (index_data[i] < TH_INDEX_BASE || index_data[i] > max) {
         THLongTensor_free(index);
         THError("index out of range");
       }
@@ -160,17 +160,17 @@ void THTensor_(indexSelect)(THTensor *tensor, THTensor *src, int dim, THLongTens
     if (src->nDimension == 1) {
       #pragma omp parallel for if(numel > TH_OMP_OVERHEAD_THRESHOLD) private(i)
       for (i=0; i<numel; i++)
-        tensor_data[i] = src_data[index_data[i]-1];
+        tensor_data[i] = src_data[index_data[i] - TH_INDEX_BASE];
     } else {
       #pragma omp parallel for if(numel*rowsize > TH_OMP_OVERHEAD_THRESHOLD) private(i)
       for (i=0; i<numel; i++)
-        memcpy(tensor_data + i*rowsize, src_data + (index_data[i]-1)*rowsize, rowsize*sizeof(real));
+        memcpy(tensor_data + i*rowsize, src_data + (index_data[i] - TH_INDEX_BASE)*rowsize, rowsize*sizeof(real));
     }
   }
   else if (src->nDimension == 1)
   {
     for (i=0; i<numel; i++)
-      THTensor_(set1d)(tensor,i,THTensor_(get1d)(src,index_data[i]-1));
+      THTensor_(set1d)(tensor,i,THTensor_(get1d)(src,index_data[i] - TH_INDEX_BASE));
   }
   else
   {
@@ -179,7 +179,7 @@ void THTensor_(indexSelect)(THTensor *tensor, THTensor *src, int dim, THLongTens
       tSlice = THTensor_(new)();
       sSlice = THTensor_(new)();
       THTensor_(select)(tSlice, tensor, dim, i);
-      THTensor_(select)(sSlice, src, dim, index_data[i]-1);
+      THTensor_(select)(sSlice, src, dim, index_data[i] - TH_INDEX_BASE);
       THTensor_(copy)(tSlice, sSlice);
       THTensor_(free)(tSlice);
       THTensor_(free)(sSlice);
@@ -197,7 +197,7 @@ void THTensor_(indexCopy)(THTensor *tensor, int dim, THLongTensor *index, THTens
 
   numel = THLongTensor_nElement(index);
   THArgCheck(index->nDimension == 1, 3, "Index is supposed to be a vector");
-  THArgCheck(dim < src->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim+1);
+  THArgCheck(dim < src->nDimension, 4, "Indexing dim %d is out of bounds of tensor", dim + TH_INDEX_BASE);
   THArgCheck(numel == src->size[dim],4,"Number of indices should be equal to source:size(dim)");
 
   index = THLongTensor_newContiguous(index);
@@ -210,7 +210,7 @@ void THTensor_(indexCopy)(THTensor *tensor, int dim, THLongTensor *index, THTens
 
     for (i=0; i<numel; i++)
     {
-      THTensor_(select)(tSlice, tensor, dim, index_data[i]-1);
+      THTensor_(select)(tSlice, tensor, dim, index_data[i] - TH_INDEX_BASE);
       THTensor_(select)(sSlice, src, dim, i);
       THTensor_(copy)(tSlice, sSlice);
     }
@@ -222,7 +222,7 @@ void THTensor_(indexCopy)(THTensor *tensor, int dim, THLongTensor *index, THTens
   {
     for (i=0; i<numel; i++)
     {
-      THTensor_(set1d)(tensor,index_data[i]-1,THTensor_(get1d)(src,i));
+      THTensor_(set1d)(tensor, index_data[i] - TH_INDEX_BASE, THTensor_(get1d)(src,i));
     }
   }
   THLongTensor_free(index);
@@ -236,20 +236,20 @@ void THTensor_(indexAdd)(THTensor *tensor, int dim, THLongTensor *index, THTenso
 
   numel = THLongTensor_nElement(index);
   THArgCheck(index->nDimension == 1, 3, "Index is supposed to be a vector");
-  THArgCheck(dim < src->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim+1);
+  THArgCheck(dim < src->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim + TH_INDEX_BASE);
   THArgCheck(numel == src->size[dim],4,"Number of indices should be equal to source:size(dim)");
 
   index = THLongTensor_newContiguous(index);
   index_data = THLongTensor_data(index);
 
-  if (tensor->nDimension > 1 )
+  if (tensor->nDimension > 1)
   {
     tSlice = THTensor_(new)();
     sSlice = THTensor_(new)();
 
     for (i=0; i<numel; i++)
     {
-      THTensor_(select)(tSlice, tensor, dim, index_data[i]-1);
+      THTensor_(select)(tSlice, tensor, dim, index_data[i] - TH_INDEX_BASE);
       THTensor_(select)(sSlice, src, dim, i);
       THTensor_(cadd)(tSlice, tSlice, 1.0, sSlice);
     }
@@ -261,7 +261,9 @@ void THTensor_(indexAdd)(THTensor *tensor, int dim, THLongTensor *index, THTenso
   {
     for (i=0; i<numel; i++)
     {
-      THTensor_(set1d)(tensor,index_data[i]-1,THTensor_(get1d)(src,i) + THTensor_(get1d)(tensor,index_data[i]-1));
+      THTensor_(set1d)(tensor,
+              index_data[i] - TH_INDEX_BASE,
+              THTensor_(get1d)(src,i) + THTensor_(get1d)(tensor,index_data[i] - TH_INDEX_BASE));
     }
   }
   THLongTensor_free(index);
@@ -275,23 +277,23 @@ void THTensor_(indexFill)(THTensor *tensor, int dim, THLongTensor *index, real v
 
   numel = THLongTensor_nElement(index);
   THArgCheck(index->nDimension == 1, 3, "Index is supposed to be a vector");
-  THArgCheck(dim < tensor->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim+1);
+  THArgCheck(dim < tensor->nDimension, 4,"Indexing dim %d is out of bounds of tensor", dim + TH_INDEX_BASE);
 
   index = THLongTensor_newContiguous(index);
   index_data = THLongTensor_data(index);
 
   for (i=0; i<numel; i++)
   {
-    if (tensor->nDimension > 1 )
+    if (tensor->nDimension > 1)
     {
       tSlice = THTensor_(new)();
-      THTensor_(select)(tSlice, tensor,dim,index_data[i]-1);
+      THTensor_(select)(tSlice, tensor,dim,index_data[i] - TH_INDEX_BASE);
       THTensor_(fill)(tSlice, val);
       THTensor_(free)(tSlice);
     }
     else
     {
-      THTensor_(set1d)(tensor,index_data[i]-1,val);
+      THTensor_(set1d)(tensor, index_data[i] - TH_INDEX_BASE, val);
     }
   }
   THLongTensor_free(index);
@@ -313,12 +315,12 @@ void THTensor_(gather)(THTensor *tensor, THTensor *src, int dim, THLongTensor *i
                        for (i = 0; i < elems_per_row; ++i)
                        {
                          idx = *(index_data + i*index_stride);
-                         if (idx < 1 || idx > src_size)
+                         if (idx < TH_INDEX_BASE || idx >= src_size + TH_INDEX_BASE)
                          {
                            THFree(TH_TENSOR_DIM_APPLY_counter);
                            THError("Invalid index in gather");
                          }
-                         *(tensor_data + i*tensor_stride) = src_data[(idx - 1) * src_stride];
+                         *(tensor_data + i*tensor_stride) = src_data[(idx - TH_INDEX_BASE) * src_stride];
                        })
 }
 
@@ -338,12 +340,12 @@ void THTensor_(scatter)(THTensor *tensor, int dim, THLongTensor *index, THTensor
                        for (i = 0; i < elems_per_row; ++i)
                        {
                          idx = *(index_data + i*index_stride);
-                         if (idx < 1 || idx > tensor_size)
+                         if (idx < TH_INDEX_BASE || idx >= tensor_size + TH_INDEX_BASE)
                          {
                            THFree(TH_TENSOR_DIM_APPLY_counter);
                            THError("Invalid index in scatter");
                          }
-                         tensor_data[(idx - 1) * tensor_stride] = *(src_data + i*src_stride);
+                         tensor_data[(idx - TH_INDEX_BASE) * tensor_stride] = *(src_data + i*src_stride);
                        })
 }
 
@@ -361,12 +363,12 @@ void THTensor_(scatterFill)(THTensor *tensor, int dim, THLongTensor *index, real
                        for (i = 0; i < elems_per_row; ++i)
                        {
                          idx = *(index_data + i*index_stride);
-                         if (idx < 1 || idx > tensor_size)
+                         if (idx < TH_INDEX_BASE || idx >= tensor_size + TH_INDEX_BASE)
                          {
                            THFree(TH_TENSOR_DIM_APPLY_counter);
                            THError("Invalid index in scatter");
                          }
-                         tensor_data[(idx - 1) * tensor_stride] = val;
+                         tensor_data[(idx - TH_INDEX_BASE) * tensor_stride] = val;
                        })
 }
 
@@ -1061,7 +1063,7 @@ void THTensor_(max)(THTensor *values_, THLongTensor *indices_, THTensor *t, int
   long i;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -1097,7 +1099,7 @@ void THTensor_(min)(THTensor *values_, THLongTensor *indices_, THTensor *t, int
   long i;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -1130,7 +1132,7 @@ void THTensor_(sum)(THTensor *r_, THTensor *t, int dimension)
   THLongStorage *dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -1150,7 +1152,7 @@ void THTensor_(prod)(THTensor *r_, THTensor *t, int dimension)
   THLongStorage *dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -1169,7 +1171,7 @@ void THTensor_(prod)(THTensor *r_, THTensor *t, int dimension)
 void THTensor_(cumsum)(THTensor *r_, THTensor *t, int dimension)
 {
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   THTensor_(resizeAs)(r_, t);
 
@@ -1186,7 +1188,7 @@ void THTensor_(cumsum)(THTensor *r_, THTensor *t, int dimension)
 void THTensor_(cumprod)(THTensor *r_, THTensor *t, int dimension)
 {
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   THTensor_(resizeAs)(r_, t);
 
@@ -1273,9 +1275,9 @@ void THTensor_(cross)(THTensor *r_, THTensor *a, THTensor *b, int dimension)
   }
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(a), 3, "dimension %d out of range",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
   THArgCheck(THTensor_(size)(a, dimension) == 3, 3, "dimension %d does not have size 3",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   THTensor_(resizeAs)(r_, a);
 
@@ -1650,7 +1652,7 @@ static void THTensor_(quicksortdescend)(real *arr, long *idx, long elements, lon
 void THTensor_(sort)(THTensor *rt_, THLongTensor *ri_, THTensor *t, int dimension, int descendingOrder)
 {
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "invalid dimension %d",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   THTensor_(resizeAs)(rt_, t);
   THTensor_(copy)(rt_, t);
@@ -1802,7 +1804,7 @@ void THTensor_(kthvalue)(THTensor *values_, THLongTensor *indices_, THTensor *t,
   long t_size_dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 3, "dimension out of range");
-  THArgCheck(k >= 0 && k < t->size[dimension], 2, "selected index out of range");
+  THArgCheck(k > 0 && k <= t->size[dimension], 2, "selected index out of range");
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -1826,9 +1828,9 @@ void THTensor_(kthvalue)(THTensor *values_, THLongTensor *indices_, THTensor *t,
                           temp__data[i] = t_data[i*t_stride];
                        for(i = 0; i < t_size_dim; i++)
                           tempi__data[i] = i;
-                       THTensor_(quickselect)(temp__data, tempi__data, k, t_size_dim, 1);
-                       *values__data = temp__data[k];
-                       *indices__data = tempi__data[k];);
+                       THTensor_(quickselect)(temp__data, tempi__data, k - 1, t_size_dim, 1);
+                       *values__data = temp__data[k-1];
+                       *indices__data = tempi__data[k-1];);
 
   THTensor_(free)(temp_);
   THLongTensor_free(tempi_);
@@ -1843,7 +1845,7 @@ void THTensor_(median)(THTensor *values_, THLongTensor *indices_, THTensor *t, i
   t_size_dim = THTensor_(size)(t, dimension);
   k = (t_size_dim-1) >> 1; /* take middle or one-before-middle element */
 
-  THTensor_(kthvalue)(values_, indices_, t, k, dimension);
+  THTensor_(kthvalue)(values_, indices_, t, k+1, dimension);
 }
 
 void THTensor_(topk)(THTensor *rt_, THLongTensor *ri_, THTensor *t, long k, int dim, int dir, int sorted)
@@ -1993,7 +1995,7 @@ void THTensor_(catArray)(THTensor *result, THTensor **inputs, int numInputs, int
   }
 
   THArgCheck(numInputs > 0, 3, "invalid number of inputs %d", numInputs);
-  THArgCheck(dimension >= 0, 4, "invalid dimension %d", dimension+1);
+  THArgCheck(dimension >= 0, 4, "invalid dimension %d", dimension + TH_INDEX_BASE);
 
   size = THLongStorage_newWithSize(ndim);
   for(i = 0; i < ndim; i++)
@@ -2182,7 +2184,7 @@ void THTensor_(mean)(THTensor *r_, THTensor *t, int dimension)
   THLongStorage *dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 2, "invalid dimension %d",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -2202,7 +2204,7 @@ void THTensor_(std)(THTensor *r_, THTensor *t, int dimension, int flag)
   THLongStorage *dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 3, "invalid dimension %d",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -2243,7 +2245,7 @@ void THTensor_(var)(THTensor *r_, THTensor *t, int dimension, int flag)
   THLongStorage *dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 3, "invalid dimension %d",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -2284,7 +2286,7 @@ void THTensor_(norm)(THTensor *r_, THTensor *t, real value, int dimension)
   THLongStorage *dim;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(t), 3, "invalid dimension %d",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
 
   dim = THTensor_(newSizeOf)(t);
   THLongStorage_set(dim, dimension, 1);
@@ -2332,7 +2334,7 @@ void THTensor_(renorm)(THTensor *res, THTensor *src, real value, int dimension,
   THTensor *rowR, *rowS;
 
   THArgCheck(dimension >= 0 && dimension < THTensor_(nDimension)(src), 3, "invalid dimension %d",
-      dimension+1);
+      dimension + TH_INDEX_BASE);
   THArgCheck(value > 0, 2, "non-positive-norm not supported");
   THArgCheck(THTensor_(nDimension)(src) > 1, 1, "need at least 2 dimensions, got %d dimensions",
       THTensor_(nDimension)(src));
@@ -2464,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);
@@ -2484,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 */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/lua-torch-torch7.git



More information about the debian-science-commits mailing list