Skip to content

bpo-43488 Update vector.py #24845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
What's new in Vector (vector.py)

1) Added multiplay (Vector and Vector)
2) Added division (Vector and Vector) and (Vector and scalar)
3) Added FloorDiv (Vector and Vector) and (Vector and scalar)
4) Added __mod__ (Vector and Vector) and (Vector and scalar)

This new methods is very useful!
[It is beta version! By the way we will fix bugs]
51 changes: 44 additions & 7 deletions Tools/demo/vector.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env python3
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
A demonstration of classes and their special methods in Python.
"""


class Vec:

"""A simple vector class.

Instances of the Vec class can be constructed from numbers
Expand All @@ -27,7 +30,9 @@ class Vec:
or on the right
>>> a * 3.0
Vec(3.0, 6.0, 9.0)

"""

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

Expand All @@ -50,25 +55,57 @@ def __getitem__(self, i):
return self.v[i]

def __add__(self, other):

# Element-wise addition
v = [x + y for x, y in zip(self.v, other.v)]

v = [x + y for (x, y) in zip(self.v, other.v)]
return Vec.fromlist(v)

def __sub__(self, other):

# Element-wise subtraction
v = [x - y for x, y in zip(self.v, other.v)]
return Vec.fromlist(v)

def __mul__(self, scalar):
# Multiply by scalar
v = [x * scalar for x in self.v]
v = [x - y for (x, y) in zip(self.v, other.v)]
return Vec.fromlist(v)

def __mul__(self, other):
if isinstance(other, int) or isinstance(other, float):
m = [x * other for x in self.matrix]
else:
m = [x * y for (x, y) in zip(self.matrix, other.matrix)]

return Matrix.fromlist(m)

def __truediv__(self, other):
if isinstance(other, int) or isinstance(other, float):
m = [x / other for x in self.matrix]
else:
m = [x / y for (x, y) in zip(self.matrix, other.matrix)]

return Matrix.fromlist(m)

def __floordiv__(self, other):
if isinstance(other, int) or isinstance(other, float):
m = [x // other for x in self.matrix]
else:
m = [x // y for (x, y) in zip(self.matrix, other.matrix)]

return Matrix.fromlist(m)

def __mod__(self, other):
if isinstance(other, int) or isinstance(other, float):
m = [x % other for x in self.matrix]
else:
m = [x % y for (x, y) in zip(self.matrix, other.matrix)]

return Matrix.fromlist(m)

__rmul__ = __mul__


def test():
import doctest
doctest.testmod()


test()