Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Private Attributes
vector.Vec Class Reference

List of all members.

Public Member Functions

def __init__
def fromlist
def __repr__
def __len__
def __getitem__
def __add__
def __sub__
def __mul__

Public Attributes

 v

Static Private Attributes

 __rmul__ = __mul__

Detailed Description

A simple vector class.

Instances of the Vec class can be constructed from numbers

>>> a = Vec(1, 2, 3)
>>> b = Vec(3, 2, 1)

added
>>> a + b
Vec(4, 4, 4)

subtracted
>>> a - b
Vec(-2, 0, 2)

and multiplied by a scalar on the left
>>> 3.0 * a
Vec(3.0, 6.0, 9.0)

or on the right
>>> a * 3.0
Vec(3.0, 6.0, 9.0)

Definition at line 7 of file vector.py.


Constructor & Destructor Documentation

def vector.Vec.__init__ (   self,
  v 
)

Definition at line 31 of file vector.py.

00031 
00032     def __init__(self, *v):
00033         self.v = list(v)

Here is the caller graph for this function:


Member Function Documentation

def vector.Vec.__add__ (   self,
  other 
)

Definition at line 52 of file vector.py.

00052 
00053     def __add__(self, other):
00054         # Element-wise addition
00055         v = [x + y for x, y in zip(self.v, other.v)]
00056         return Vec.fromlist(v)

def vector.Vec.__getitem__ (   self,
  i 
)

Definition at line 49 of file vector.py.

00049 
00050     def __getitem__(self, i):
00051         return self.v[i]

def vector.Vec.__len__ (   self)

Definition at line 46 of file vector.py.

00046 
00047     def __len__(self):
00048         return len(self.v)

def vector.Vec.__mul__ (   self,
  scalar 
)

Definition at line 62 of file vector.py.

00062 
00063     def __mul__(self, scalar):
00064         # Multiply by scalar
00065         v = [x * scalar for x in self.v]
00066         return Vec.fromlist(v)

def vector.Vec.__repr__ (   self)

Definition at line 42 of file vector.py.

00042 
00043     def __repr__(self):
00044         args = ', '.join(repr(x) for x in self.v)
00045         return 'Vec({})'.format(args)

def vector.Vec.__sub__ (   self,
  other 
)

Definition at line 57 of file vector.py.

00057 
00058     def __sub__(self, other):
00059         # Element-wise subtraction
00060         v = [x - y for x, y in zip(self.v, other.v)]
00061         return Vec.fromlist(v)

def vector.Vec.fromlist (   cls,
  v 
)

Definition at line 35 of file vector.py.

00035 
00036     def fromlist(cls, v):
00037         if not isinstance(v, list):
00038             raise TypeError
00039         inst = cls()
00040         inst.v = v
00041         return inst


Member Data Documentation

vector.Vec.__rmul__ = __mul__ [static, private]

Definition at line 67 of file vector.py.

Definition at line 32 of file vector.py.


The documentation for this class was generated from the following file: