Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 51 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()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.