"Fossies" - the Fresh Open Source Software Archive

Member "NZMATH-1.2.0/nzmath/vector.py" (19 Nov 2012, 4058 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "vector.py" see the Fossies "Dox" file reference documentation.

    1 from __future__ import division
    2 
    3 class Vector (object):
    4     """
    5     Class Vector is an elemental class of vector.
    6     """
    7     def __init__(self, compo):
    8         self.compo = list(compo)
    9 
   10     def __getitem__(self, index):
   11         return self.compo[index - 1]
   12 
   13     def __setitem__(self, index, value):
   14         self.compo[index - 1] = value
   15 
   16     def __len__(self):
   17         return len(self.compo)
   18 
   19     def __iter__(self):
   20         return iter(self.compo)
   21 
   22     def __eq__(self, other):
   23         return self.compo == other.compo
   24 
   25     def __hash__(self):
   26         val = sum([hash(ele) for ele in self.compo]) 
   27         return val
   28 
   29     def __ne__(self, other):
   30         return self.compo != other.compo
   31 
   32     def __add__(self, other):
   33         if isinstance(other, Vector):
   34             if len(self) == len(other):
   35                 tmp = [s + c for (s, c) in zip(self.compo, other.compo)]
   36                 return self.__class__(tmp)
   37             else:
   38                 raise VectorSizeError("unable to add vectors with different sizes")
   39         else:
   40             raise TypeError("unable to add")
   41 
   42     def __sub__(self, other):
   43         if isinstance(other, Vector):
   44             if len(self) == len(other):
   45                 return self.__class__([s - c for (s, c) in zip(self.compo, other.compo)])
   46             else:
   47                 raise VectorSizeError("unable to subtract vectors with different sizes")
   48         else:
   49             raise TypeError("unable to subtract")
   50 
   51     def __neg__(self):
   52         return self.__class__([-c for c in self.compo])
   53 
   54     def __mul__(self, other):
   55         if isinstance(other, Vector):
   56             raise TypeError("no multiplication")
   57         elif ismatrix(other):
   58             return NotImplemented
   59         else:
   60             return self.__class__([c * other for c in self.compo])
   61 
   62     def __rmul__(self, other):
   63         if ismatrix(other):
   64             return NotImplemented
   65         else:
   66             return self.__class__([other * c for c in self.compo])
   67 
   68     def __truediv__(self, other):
   69         return self.__class__([c / other for c in self.compo])
   70 
   71     __div__ = __truediv__ # for backward compatibility
   72 
   73     def __mod__(self,other):
   74         if isinstance(other, Vector):
   75             return TypeError
   76         else:
   77             modulus = int(other)
   78             return self.__class__([int(c) % modulus for c in self.compo])
   79 
   80     def __repr__(self):
   81         return "Vector(" + repr(self.compo) + ")"
   82 
   83     def __str__(self):
   84         return str(self.compo)
   85 
   86 # utility methods ----------------------------------------------------
   87     def copy(self):
   88         return self.__class__(self.compo)
   89 
   90     def set(self, compo):
   91         if isinstance(compo, list):
   92             self.compo = compo
   93         else:
   94             raise ValueError
   95 
   96     def indexOfNoneZero(self):
   97         for c, entry in enumerate(self.compo):
   98             if entry:
   99                 return c + 1
  100         raise ValueError("all zero")
  101 
  102     def toMatrix(self, as_column=False):
  103         """
  104         toMatrix(as_column): convert to Matrix representation.
  105         If as_column is True, return column matrix, otherwise row matrix.
  106         """
  107         import nzmath.matrix as matrix
  108         if as_column:
  109             return matrix.createMatrix(len(self), 1, self.compo)
  110         else:
  111             return matrix.createMatrix(1, len(self), self.compo)
  112 
  113 
  114 def innerProduct(bra, ket):
  115     """
  116     Return the result of inner product of two vectors, whose
  117     components are real or complex numbers.
  118     """
  119     if len(bra) != len(ket):
  120         raise VectorSizeError("no inner product with different sized vectors")
  121     v = 0
  122     for i in range(1, len(bra) + 1):
  123         try:
  124             v += bra[i] * ket[i].conjugate()
  125         except AttributeError:
  126             v += bra[i] * ket[i]
  127     return v
  128 
  129 def ismatrix(obj):
  130     """
  131     If the given obj is a matrix then return True.  False, otherwise.
  132     """
  133     return hasattr(obj, "row") and hasattr(obj, "column")
  134 
  135 
  136 class VectorSizeError(Exception):
  137     """
  138     An exception raised when two vector operands for an operator
  139     mismatches in size.
  140     """