Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OpenVMS
Python
cpython
Commits
d348226bfb06
Commit
d348226bfb06
authored
4 years ago
by
Raymond Hettinger
Browse files
Options
Downloads
Patches
Plain Diff
Minor updates to the vector demo (GH-24853)
parent
8b54f5f9024d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Tools/demo/vector.py
+25
-5
25 additions, 5 deletions
Tools/demo/vector.py
with
25 additions
and
5 deletions
Tools/demo/vector.py
+
25
−
5
View file @
d348226b
...
...
@@ -27,4 +27,13 @@
or on the right
>>>
a
*
3.0
Vec
(
3.0
,
6.0
,
9.0
)
and dot product
>>>
a
.
dot
(
b
)
10
and printed in vector notation
>>>
print
(
a
)
<
1
2
3
>
"""
...
...
@@ -30,4 +39,5 @@
"""
def
__init__
(
self
,
*
v
):
self
.
v
=
list
(
v
)
...
...
@@ -40,8 +50,12 @@
return
inst
def
__repr__
(
self
):
args
=
'
,
'
.
join
(
repr
(
x
)
for
x
in
self
.
v
)
return
'
Vec({})
'
.
format
(
args
)
args
=
'
,
'
.
join
([
repr
(
x
)
for
x
in
self
.
v
])
return
f
'
{
type
(
self
).
__name__
}
(
{
args
}
)
'
def
__str__
(
self
):
components
=
'
'
.
join
([
str
(
x
)
for
x
in
self
.
v
])
return
f
'
<
{
components
}
>
'
def
__len__
(
self
):
return
len
(
self
.
v
)
...
...
@@ -50,8 +64,8 @@
return
self
.
v
[
i
]
def
__add__
(
self
,
other
):
#
Element-wise addition
"
Element-wise addition
"
v
=
[
x
+
y
for
x
,
y
in
zip
(
self
.
v
,
other
.
v
)]
return
Vec
.
fromlist
(
v
)
def
__sub__
(
self
,
other
):
...
...
@@ -54,9 +68,9 @@
v
=
[
x
+
y
for
x
,
y
in
zip
(
self
.
v
,
other
.
v
)]
return
Vec
.
fromlist
(
v
)
def
__sub__
(
self
,
other
):
#
Element-wise subtraction
"
Element-wise subtraction
"
v
=
[
x
-
y
for
x
,
y
in
zip
(
self
.
v
,
other
.
v
)]
return
Vec
.
fromlist
(
v
)
def
__mul__
(
self
,
scalar
):
...
...
@@ -59,10 +73,10 @@
v
=
[
x
-
y
for
x
,
y
in
zip
(
self
.
v
,
other
.
v
)]
return
Vec
.
fromlist
(
v
)
def
__mul__
(
self
,
scalar
):
#
Multiply by scalar
"
Multiply by scalar
"
v
=
[
x
*
scalar
for
x
in
self
.
v
]
return
Vec
.
fromlist
(
v
)
__rmul__
=
__mul__
...
...
@@ -64,8 +78,14 @@
v
=
[
x
*
scalar
for
x
in
self
.
v
]
return
Vec
.
fromlist
(
v
)
__rmul__
=
__mul__
def
dot
(
self
,
other
):
"
Vector dot product
"
if
not
isinstance
(
other
,
Vec
):
raise
TypeError
return
sum
(
x_i
*
y_i
for
(
x_i
,
y_i
)
in
zip
(
self
,
other
))
def
test
():
import
doctest
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment