NZMATH  1.2.0
About: NZMATH is a Python based number theory oriented calculation system.
  Fossies Dox: NZMATH-1.2.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

vector.py
Go to the documentation of this file.
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  """
nzmath.vector.Vector.__neg__
def __neg__(self)
Definition: vector.py:51
nzmath.bigrange.range
def range(start, stop=None, step=None)
Definition: bigrange.py:19
nzmath.vector.Vector.__truediv__
def __truediv__(self, other)
Definition: vector.py:68
nzmath.vector.Vector.indexOfNoneZero
def indexOfNoneZero(self)
Definition: vector.py:96
nzmath.vector.Vector.__mod__
def __mod__(self, other)
Definition: vector.py:73
nzmath.vector.Vector.__add__
def __add__(self, other)
Definition: vector.py:32
nzmath.matrix
Definition: matrix.py:1
nzmath.vector.Vector.compo
compo
Definition: vector.py:8
nzmath.vector.Vector.set
def set(self, compo)
Definition: vector.py:90
nzmath.vector.Vector.__str__
def __str__(self)
Definition: vector.py:83
nzmath.vector.Vector.__mul__
def __mul__(self, other)
Definition: vector.py:54
nzmath.vector.innerProduct
def innerProduct(bra, ket)
Definition: vector.py:114
nzmath.vector.Vector
Definition: vector.py:3
nzmath.vector.Vector.__iter__
def __iter__(self)
Definition: vector.py:19
nzmath.vector.Vector.__eq__
def __eq__(self, other)
Definition: vector.py:22
nzmath.vector.Vector.__setitem__
def __setitem__(self, index, value)
Definition: vector.py:13
nzmath.vector.Vector.__init__
def __init__(self, compo)
Definition: vector.py:7
nzmath.vector.Vector.__rmul__
def __rmul__(self, other)
Definition: vector.py:62
nzmath.vector.ismatrix
def ismatrix(obj)
Definition: vector.py:129
nzmath.vector.Vector.__repr__
def __repr__(self)
Definition: vector.py:80
nzmath.vector.Vector.__getitem__
def __getitem__(self, index)
Definition: vector.py:10
nzmath.vector.Vector.__len__
def __len__(self)
Definition: vector.py:16
nzmath.vector.VectorSizeError
Definition: vector.py:136
nzmath.vector.Vector.toMatrix
def toMatrix(self, as_column=False)
Definition: vector.py:102
nzmath.vector.Vector.__hash__
def __hash__(self)
Definition: vector.py:25
nzmath.vector.Vector.__sub__
def __sub__(self, other)
Definition: vector.py:42
nzmath.vector.Vector.copy
def copy(self)
Definition: vector.py:87
nzmath.vector.Vector.__ne__
def __ne__(self, other)
Definition: vector.py:29