From 34394726f437eecad952377f8c3db63ebabbbaa0 Mon Sep 17 00:00:00 2001 From: Ehsonjon Gadoev <59335419+icoder-new@users.noreply.github.com> Date: Sat, 13 Mar 2021 23:10:06 +0500 Subject: [PATCH 1/6] Update vector.py --- Tools/demo/vector.py | 60 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/Tools/demo/vector.py b/Tools/demo/vector.py index da5b3891d18c62f..c6eab0354bf0add 100755 --- a/Tools/demo/vector.py +++ b/Tools/demo/vector.py @@ -27,6 +27,27 @@ class Vec: or on the right >>> a * 3.0 Vec(3.0, 6.0, 9.0) + + lenght of Vector + >>> len(a) + 3 + + printing argument of Vector by index + >>> a[1] + 2 + + New!! + >>> a * b + Vec(3, 4, 3) + + >>> a / b + Vec(0.3333333333333333, 1.0, 3.0) + + >>> a // b + Vec(1, 0, 0) + + >>> a % 2 + Vec(1, 0, 1) """ def __init__(self, *v): self.v = list(v) @@ -53,18 +74,45 @@ def __add__(self, other): # Element-wise addition 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] - 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__ + __rmul__ = __mul__ def test(): From 70daaa37c8be058e0ea0cf7907a9c8d18f43ca32 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 13 Mar 2021 18:22:59 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Tools-Demos/2021-03-13-18-22-58.bpo-43488.bzjZFP.rst | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2021-03-13-18-22-58.bpo-43488.bzjZFP.rst diff --git a/Misc/NEWS.d/next/Tools-Demos/2021-03-13-18-22-58.bpo-43488.bzjZFP.rst b/Misc/NEWS.d/next/Tools-Demos/2021-03-13-18-22-58.bpo-43488.bzjZFP.rst new file mode 100644 index 000000000000000..5e107a15a0382b0 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2021-03-13-18-22-58.bpo-43488.bzjZFP.rst @@ -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] \ No newline at end of file From e155653c6038c8a092009f5b4e91e75195790b0c Mon Sep 17 00:00:00 2001 From: Ehsonjon Gadoev <59335419+icoder-new@users.noreply.github.com> Date: Sat, 13 Mar 2021 23:35:57 +0500 Subject: [PATCH 3/6] Update vector.py --- Tools/demo/vector.py | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/Tools/demo/vector.py b/Tools/demo/vector.py index c6eab0354bf0add..8072283467f46ea 100755 --- a/Tools/demo/vector.py +++ b/Tools/demo/vector.py @@ -27,27 +27,7 @@ class Vec: or on the right >>> a * 3.0 Vec(3.0, 6.0, 9.0) - - lenght of Vector - >>> len(a) - 3 - - printing argument of Vector by index - >>> a[1] - 2 - - New!! - >>> a * b - Vec(3, 4, 3) - - >>> a / b - Vec(0.3333333333333333, 1.0, 3.0) - - >>> a // b - Vec(1, 0, 0) - - >>> a % 2 - Vec(1, 0, 1) + """ def __init__(self, *v): self.v = list(v) From df8de5513b190c2a0dfac31d6a23e3ee92053898 Mon Sep 17 00:00:00 2001 From: Ehsonjon Gadoev <59335419+icoder-new@users.noreply.github.com> Date: Sat, 13 Mar 2021 23:54:18 +0500 Subject: [PATCH 4/6] Update vector.py --- Tools/demo/vector.py | 52 +++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/Tools/demo/vector.py b/Tools/demo/vector.py index 8072283467f46ea..93d14c7cef2c897 100755 --- a/Tools/demo/vector.py +++ b/Tools/demo/vector.py @@ -4,6 +4,8 @@ A demonstration of classes and their special methods in Python. """ + + class Vec: """A simple vector class. @@ -60,39 +62,39 @@ def __sub__(self, other): 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)] + 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) + 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)] + 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) + 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)] + 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) + 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)] + 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) + return Matrix.fromlist(m) - __rmul__ = __mul__ + __rmul__ = __mul__ def test(): From 9d808e77dc302d9b685020d6fb965b664d9db7b5 Mon Sep 17 00:00:00 2001 From: Ehsonjon Gadoev <59335419+icoder-new@users.noreply.github.com> Date: Sun, 14 Mar 2021 00:09:21 +0500 Subject: [PATCH 5/6] Update vector.py --- Tools/demo/vector.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tools/demo/vector.py b/Tools/demo/vector.py index 93d14c7cef2c897..030466232bbd9d7 100755 --- a/Tools/demo/vector.py +++ b/Tools/demo/vector.py @@ -5,7 +5,6 @@ """ - class Vec: """A simple vector class. From 9b7c71f42e542d1cf3ddf5e607eb41de6cf1d90c Mon Sep 17 00:00:00 2001 From: Ehsonjon Gadoev <59335419+icoder-new@users.noreply.github.com> Date: Sun, 14 Mar 2021 00:16:52 +0500 Subject: [PATCH 6/6] Update vector.py --- Tools/demo/vector.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Tools/demo/vector.py b/Tools/demo/vector.py index 030466232bbd9d7..b5a132b67c22d16 100755 --- a/Tools/demo/vector.py +++ b/Tools/demo/vector.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python3 +#!/usr/bin/python +# -*- coding: utf-8 -*- """ A demonstration of classes and their special methods in Python. @@ -6,6 +7,7 @@ class Vec: + """A simple vector class. Instances of the Vec class can be constructed from numbers @@ -30,6 +32,7 @@ class Vec: Vec(3.0, 6.0, 9.0) """ + def __init__(self, *v): self.v = list(v) @@ -52,20 +55,24 @@ 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)] + + 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)] + m = [x * y for (x, y) in zip(self.matrix, other.matrix)] return Matrix.fromlist(m) @@ -73,7 +80,7 @@ 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)] + m = [x / y for (x, y) in zip(self.matrix, other.matrix)] return Matrix.fromlist(m) @@ -81,7 +88,7 @@ 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)] + m = [x // y for (x, y) in zip(self.matrix, other.matrix)] return Matrix.fromlist(m) @@ -89,7 +96,7 @@ 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)] + m = [x % y for (x, y) in zip(self.matrix, other.matrix)] return Matrix.fromlist(m) @@ -100,4 +107,5 @@ def test(): import doctest doctest.testmod() + test()