when can numpy ufuncs be used, when must they be UTPM

59 views
Skip to first unread message

Mark Mikofski

unread,
Apr 6, 2016, 5:21:15 AM4/6/16
to algopy
I can't determine when I must use UTPM operations like sin, cos, exp, dot, inv, etc. and when the numpy ufuncs are okay.

The documentation shows examples of both.

For example if I try the following:

import numpy as np
from algopy import UTPM, zeros
import numdifftools.nd_algopy as nda
import numdifftools as nd


def f(x):
    nobs = x.shape[1:]
    f0 = x[0]**2 * np.sin(x[1])**2
    f1 = x[0]**2 * np.cos(x[1])**2
    out = zeros((2,) + nobs, dtype=x)
    out[0,:] = f0
    out[1,:] = f1
    return out

x = np.array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype=float)
y = f(x)

xj = UTPM.init_jacobian(x)
j = UTPM.extract_jacobian(f(xj))

I get a nasty stack trace that says binary operations with dtype objects don't work

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-31-a606a9700adf> in <module>()
     19
     20 xj = UTPM.init_jacobian(x)
---> 21 j = UTPM.extract_jacobian(f(xj))
     22
     23 print "x =\n%r\n" % x

<ipython-input-31-a606a9700adf> in f(x)
      8     nobs = x.shape[1:]
      9     print "nobs: %r" % nobs
---> 10     f0 = x[0]**2 * np.sin(x[1])**2
     11     f1 = x[0]**2 * np.cos(x[1])**2
     12     out = zeros((2,) + nobs, dtype=x)

c:\python27\lib\site-packages\algopy\utpm\utpm.pyc in __mul__(self, rhs)
    364             else:
    365                 err_str = 'binary operations between UTPM instances and object arrays are not supported'
--> 366                 raise NotImplementedError(err_str)
    367
    368         elif isinstance(rhs,numpy.ndarray):

NotImplementedError: binary operations between UTPM instances and object arrays are not supported

But if I just import sin and cos from algopy, and use them instead of np.sin and np.cos, then it works fine.

The documentation shows several examples (getting started, taylor series, derivatives, error propagation, etc.) that use numpy ufuncs in the return value.

When I step through the above example, I can see that when __mul__() is called it sees the rhs as

rhs
array([ UTPM([[ 0.91953576  0.91953576  0.91953576  0.91953576  0.91953576  0.91953576   0.91953576  0.91953576]
              [ 0.          0.          0.          0.         -0.54402111  0.          0.   0.        ]]),
        UTPM([[ 0.07807302  0.07807302  0.07807302  0.07807302  0.07807302  0.07807302   0.07807302  0.07807302]
              [ 0.          0.          0.          0.          0.         -0.53657292   0.          0.        ]]),
        UTPM([[ 0.43163139  0.43163139  0.43163139  0.43163139  0.43163139  0.43163139   0.43163139  0.43163139]
              [ 0.          0.          0.          0.          0.          0.   0.99060736  0.        ]]),
        UTPM([[ 0.97882974  0.97882974  0.97882974  0.97882974  0.97882974  0.97882974   0.97882974  0.97882974]
              [ 0.          0.          0.          0.          0.          0.          0.  -0.28790332]])],
    dtype=object)

so that's the problem which using UTPM.as_utpm() seems to fix. whenever the argument is UTPM, then the UTPM method is called instead of the ufunc even though numpy.sin or some other numpy method is initially called. But then UTPM.<ufunc> is mishandling the initialized jacobian input when UTPM.sin is called


Sebastian Walter

unread,
Apr 6, 2016, 6:34:26 AM4/6/16
to alg...@googlegroups.com
Hello Mark,

IIRC, numpy.{sin,cos,etc} were not a problem for AlgoPy before numpy implemented a generic ufunc backend.
I believe, the documentation you reference was written before the changes in numpy. When I find some spare time, I'll update the docs.

Generally, one should avoid to use numpy arrays of UTPM instances and instead use UTPM instances are replacement for numpy.ndarray.

Best
Sebastian


--
You received this message because you are subscribed to the Google Groups "algopy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to algopy+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages