like_numpy

View page source

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

      vec = like_numpy . array ( like_vec )
      vec = like_numpy . concatenate ( vec_list )
      vec = left op right
      size = len ( vec )
      scalar = vec. sum ()
      scalar = vec [ index ]
      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 reshape below.

vec

is a like_numpy vector.

left

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 .

  1. If the size of left and right are equal:

          vec [ i ] = left [ i ] op right [ i ]
  2. If the size of left is one:

          vec [ i ] = left [0] op right [ i ]
  3. If the size of right is one:

          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

      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:

package

constructor

none

numpy.array

autograd

autograd.numpy.array

cppad_py

numpy.array

jax

jax.numpy.array

torch

torch.tensor

Example

import torch
#
class like_numpy :
   type_list = [ torch.Tensor ]
   #
   def __init__(self, like_vec) :
      if type(like_vec) in self.type_list :
         self.vec = like_vec
      else :
         self.vec = torch.tensor(like_vec, dtype=float)
      if len( self.vec.shape ) != 1 :
         self.vec = self.vec.reshape(-1)
   #
   def __add__(self, other) :
      return like_numpy( self.vec + other.vec )
   #
   def __sub__(self, other) :
      return like_numpy( self.vec - other.vec )
   #
   def __mul__(self, other) :
      return like_numpy( self.vec * other.vec )
   #
   def __truediv__(self, other) :
      return like_numpy( self.vec / other.vec )
   #
   def array(vec) :
      return like_numpy(vec)
   #
   def sum(self) :
      return self.vec.sum()
   #
   def __len__(self) :
      return len(self.vec)
   #
   def __getitem__(self, index) :
      return self.vec[index]
   #
   def concatenate(like_list) :
      vec_list = list()
      for like in like_list :
         vec_list.append(like.vec)
      return like_numpy( torch.cat(vec_list) )
   #
   def concatenate(like_list) :
      vec_list = list()
      for like in like_list :
         vec_list.append(like.vec)
      return like_numpy( torch.cat(vec_list) )
   #
   def reshape(self, shape) :
      assert shape == -1
      assert self.vec.shape == ( len(self.vec), )
      return like_numpy( self.vec )