2

I want to implement the following Matlab code in Python:

x=1:100;
y=20*log10(x);

I tried using Numpy to do this:

y = numpy.zeros(x.shape)
for i in range(len(x)):
    y[i] = 20*math.log10(x[i])

But this uses a for loop; is there anyway to do a vectorized operation like in Matlab? I know for some simple math such as division and multiplication, it's possible. But what about other more sophisticated operations like logarithm here?

dckrooney
  • 2,783
  • 1
  • 19
  • 27
tonga
  • 10,669
  • 23
  • 69
  • 93

6 Answers6

3
y = numpy.log10(numpy.arange(1, 101)) * 20

In [30]: numpy.arange(1, 10)
Out[30]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [31]: numpy.log10(numpy.arange(1, 10))
Out[31]:
array([ 0.        ,  0.30103   ,  0.47712125,  0.60205999,  0.69897   ,
        0.77815125,  0.84509804,  0.90308999,  0.95424251])

In [32]: numpy.log10(numpy.arange(1, 10)) * 20
Out[32]:
array([  0.        ,   6.02059991,   9.54242509,  12.04119983,
        13.97940009,  15.56302501,  16.9019608 ,  18.06179974,  19.08485019])
Pavel Anossov
  • 54,107
  • 13
  • 131
  • 116
  • 3
    Minor: `a:b` includes `b` in Matlab. – DSM Apr 02 '13 at 21:30
  • That works. Thanks. So do all math functions in Python take vector input? – tonga Apr 02 '13 at 21:33
  • 2
    No, not all math functions in python take vector input. However within the range of mathematical operations in numpy, accepting vector input is the rule rather than the exception (as in matlab). The documentation for the functions in question will describe any exceptions. – aestrivex Apr 02 '13 at 21:37
3

Yep, there certainly is.

x = numpy.arange(1, 100)
y = 20 * numpy.log10(x)
Sajjan Singh
  • 2,399
  • 2
  • 22
  • 30
2

Numpy has a lot of built-in array operators like log10. If it's not listed in numpy's documentation and you can't generate it from combining built-in methods, then there's no easy way to do it efficiently. You can implement a C-level function to work on numpy arrays and compile that, but this is a lot more work than one or two lines of Python code.

For your case you almost have the right output already:

y = 20*numpy.log10(x)
Pyrce
  • 7,448
  • 2
  • 25
  • 44
2

You may want to take a look at the Numpy documentation. This is a good place to start:

http://docs.scipy.org/doc/numpy/reference/routines.html

And specifically related to your question:

http://docs.scipy.org/doc/numpy/reference/routines.math.html

JoshAdel
  • 57,369
  • 23
  • 130
  • 131
1

If you're not trying to do anything complicated, the original code could be implemented this way as well, without requiring the use of numpy, if I'm not mistaken.

>>> import math
>>> x = range(1, 101)
>>> y = [ 20 * math.log10(z) for z in x ]
mcmaster
  • 19
  • 1
  • 1
    This still loops over all the elements in x one at a time and isn't do array level operations. Additionally you don't output a numpy array in this case and generate a python list. The numpy implementations are optimized for full array calculations and you'll get a 10-100x speedup using them over python loops (compressed or not). – Pyrce Apr 02 '13 at 22:11
1

Apart from performing vectorized operation using numpy standard vectorized functions, you can also make your custom vectorized function using numpy.vectorize. Here is one example:

    >>> def myfunc(a, b):
    ...     "Return a-b if a>b, otherwise return a+b"
    ...     if a > b:
    ...         return a - b
    ...     else:
    ...         return a + b
    >>>
    >>> vfunc = np.vectorize(myfunc)
    >>> vfunc([1, 2, 3, 4], 2)
    array([3, 4, 1, 2])
Alok Nayak
  • 1,904
  • 19
  • 26