Skip to content

Instantly share code, notes, and snippets.

@eayoungs
Created June 30, 2015 01:45
Show Gist options
  • Save eayoungs/a61c7b09ec6f2e4af567 to your computer and use it in GitHub Desktop.
Save eayoungs/a61c7b09ec6f2e4af567 to your computer and use it in GitHub Desktop.
numpy multiply function
def multiply(a, i):
"""
Return (a * i), that is string multiple concatenation,
element-wise.
Values in `i` of less than 0 are treated as 0 (which yields an
empty string).
Parameters
----------
a : array_like of str or unicode
i : array_like of ints
Returns
-------
out : ndarray
Output array of str or unicode, depending on input types
"""
a_arr = numpy.asarray(a)
i_arr = numpy.asarray(i)
if not issubclass(i_arr.dtype.type, integer):
raise ValueError("Can only multiply by integers")
out_size = _get_num_chars(a_arr) * max(long(i_arr.max()), 0)
return _vec_string(
a_arr, (a_arr.dtype.type, out_size), '__mul__', (i_arr,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment