lines 5-130 of file: python/cmpad/like_numpy.xrst {xrst_begin like_numpy} {xrst_spell th op vec ndarray jax py len } The Like Numpy Vector Concept ############################# The *like_numpy* concept enables various packages to interface to the same vectorized algorithm code. It that has the following properties: Syntax ****** | |tab| *vec* = *like_numpy* . ``array`` ( *like_vec* ) | |tab| *vec* = *like_numpy* . ``concatenate`` ( *vec_list* ) | |tab| *vec* = *left* *op* *right* | |tab| *size* = ``len`` ( *vec* ) | |tab| *scalar* = *vec*. ``sum`` () | |tab| *scalar* = *vec* [ *index* ] | |tab| *vec* = *vec*. ``reshape(-1)`` like_vec ******** This can be a numeric value, a non-empty ``list`` of numeric values, or non-empty ``numpy.ndarray`` of numeric values. array ***** This syntax constructs a *like_numpy* vector. The *i*-th element of *vec* has the same precision as ``float`` and its value corresponds to the *i*-element of *like_vec* . In th special case where *like_numpy* is ``numpy`` ; see :ref:`like_numpy@reshape` below. *vec* ***** is a *like_numpy* vector. *left* ****** is a *like_numpy* vector. *right* ******* is a *like_numpy* vector. concatenate *********** The concatenates a list of vectors into one long vector. vec_list ******** is a ``list`` of *like_numpy* vectors. op ** The operator *op* can be ``+`` , ``-`` , ``*`` , or ``/`` . Either the size of *left* is one, the size of *right* is one, or the size of *left* is equal the size of *right* . #. If the size of *left* and *right* are equal: | |tab| *vec* [ *i* ] = *left* [ *i* ] *op* *right* [ *i* ] #. If the size of *left* is one: | |tab| *vec* [ *i* ] = *left* [0] *op* *right* [ *i* ] #. If the size of *right* is one: | |tab| *vec* [ *i* ] = *left* [ *i* ] *op* *right* [0] sum *** This sums the elements of *vec* . scalar ****** This object has the same (or similar) type as the elements of *like_vec* . You can convert it to a *like_numpy* vector using | |tab| *vec* = *like_numpy* . ``array`` ( *scalar* ) ``.reshape(-1)`` Note that in the ``.reshape(-1)`` is only necessary when *like_numpy* is ``numpy`` . reshape ******* Normally, this will check that *vec* is a vector; i.e, it has one dimension. In he special case where *like_numpy* is ``numpy`` , this converts the numpy array to a vector. The type of this vector depends on the package that *like_numpy* corresponds to: .. csv-table:: :header: package, constructor none, numpy.array autograd, autograd.numpy.array cppad_py, numpy.array jax, jax.numpy.array torch, torch.tensor Example ======= {xrst_literal python/cmpad/torch/like_numpy.py # BEGIN LIKE_NUMPY # END LIKE_NUMPY } {xrst_end like_numpy}