Skip to content

Commit

Permalink
returning correct dtype from unitvec
Browse files Browse the repository at this point in the history
matutils.unitvec currently returns a unitvector of a different dtype from the input vector if the input dtype isn't np.float.

we should make the return type consistent with the input type.

fixes piskvorky#1722
  • Loading branch information
accraze committed Dec 12, 2017
1 parent 48249bb commit 90b02cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gensim/matutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def unitvec(vec, norm='l2'):
return vec

if isinstance(vec, np.ndarray):
vec = np.asarray(vec, dtype=float)
vec = np.asarray(vec, dtype=vec.dtype)
if norm == 'l1':
veclen = np.sum(np.abs(vec))
if norm == 'l2':
Expand Down
27 changes: 27 additions & 0 deletions gensim/test/test_matutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html

"""
Automated tests for checking various matutils functions.
"""

import logging
import unittest

import numpy as np

from gensim import matutils


class TestMatutils(unittest.TestCase):
def test_unitvec(self):
input_vector = np.random.uniform(size=(100,)).astype(np.float32)
unit_vector = matutils.unitvec(input_vector)
self.assertEqual(input_vector.dtype, unit_vector.dtype)


if __name__ == '__main__':
logging.root.setLevel(logging.WARNING)
unittest.main()

0 comments on commit 90b02cd

Please sign in to comment.