Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SciProg2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
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
pclark3
SciProg2024
Commits
fd5e5d2c
Commit
fd5e5d2c
authored
1 year ago
by
cprutean
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
1d3e3819
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
CodeExamples/Vector.ipynb
+147
-0
147 additions, 0 deletions
CodeExamples/Vector.ipynb
with
147 additions
and
0 deletions
CodeExamples/Vector.ipynb
0 → 100644
+
147
−
0
View file @
fd5e5d2c
{
"cells": [
{
"cell_type": "markdown",
"id": "80ff35bf",
"metadata": {},
"source": [
"# Vector"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bdf8ec5",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\"\n",
" A more developed Vector class for simple 3d vectors\n",
"\"\"\"\n",
"\n",
"import math\n",
"class Vector3d(object):\n",
" \"\"\"\n",
" Simple vector 3d class\n",
" \"\"\"\n",
" def __init__(self, x, y, z):\n",
" \"\"\"\n",
" Constructor to form a vector\n",
" \"\"\"\n",
" self.x = float(x)\n",
" self.y = float(y)\n",
" self.z = float(z)\n",
"\n",
" def __str__(self): \n",
" \"\"\"\n",
" Method to format a vector as a string (implments str()) \n",
" \"\"\"\n",
" return \"[\" + str(self.x) + \", \" + str(self.y) + \", \" + str(self.z) + \"]\"\n",
"\n",
" def copy(self):\n",
" \"\"\"\n",
" Method to return a copy of current vector\n",
" \"\"\"\n",
" return Vector3d(self.x,self.y,self.z)\n",
"\n",
" def magnitude(self):\n",
" \"\"\"\n",
" Return the magnitude\n",
" \"\"\"\n",
" return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z)\n",
"\n",
" def distance(self,b):\n",
" \"\"\"\n",
" Distance from self to vector b\n",
" \"\"\"\n",
" dx = self.x - b.x\n",
" dy = self.y - b.y\n",
" dz = self.z - b.z\n",
" return math.sqrt(dx*dx + dy*dy + dz*dz)\n",
"\n",
"\n",
" def normalise(self):\n",
" \"\"\"\n",
" Normlaise current vector so its magnitude is 1.0 (unit vector)\n",
" \"\"\"\n",
" mag = self.magnitude()\n",
" if mag != 0.0 : # if not already zero\n",
" self.x /= mag # dveive throgh by mag\n",
" self.y /= mag\n",
" self.z /= mag\n",
"\n",
" def add(self,b):\n",
" \"\"\"\n",
" Add a vector b to self and return a new vector.\n",
" \"\"\"\n",
" x = self.x + b.x\n",
" y = self.y + b.y\n",
" z = self.z + b.z\n",
" return Vector3d(x,y,z)\n",
"\n",
" def addTo(self,b):\n",
" \"\"\"\n",
" Add a vector b to self in place.\n",
" \"\"\"\n",
" self.x += b.x\n",
" self.y += b.y\n",
" self.z += b.z\n",
"\n",
"\n",
" def dot(self,b):\n",
" \"\"\"\n",
" Form dot product between self and vector b\n",
" \"\"\"\n",
" return self.x*b.x + self.y*b.y + self.z*b.z\n",
"\n",
" def cross(self,b):\n",
" \"\"\"\n",
" Form cross product between self and vector b.\n",
" \"\"\"\n",
" x = self.y*b.z - self.z*b.y\n",
" y = self.z*b.x - self.x*b.z\n",
" z = self.x*b.y - self.y*b.x\n",
" return Vector3d(x,y,z)\n",
"\n",
"def main(): \n",
" \"\"\"\n",
" Simple test program to test the vectors\n",
" \"\"\"\n",
" a = Vector3d(1,2,3)\n",
" b = Vector3d(4,5,6)\n",
"\n",
" print(\"Magnitde of a is \" + str(a.magnitude()))\n",
"\n",
" c = a.cross(b) # Form cross product\n",
" #\n",
" # the str() function call the __str__(self) method\n",
" print(\"Cross product of a x b is : \" + str(c)) \n",
"\n",
"\n",
"\n",
"#main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:markdown id:80ff35bf tags:
# Vector
%% Cell type:code id:1bdf8ec5 tags:
```
python
""""
A more developed Vector class for simple 3d vectors
"""
import
math
class
Vector3d
(
object
):
"""
Simple vector 3d class
"""
def
__init__
(
self
,
x
,
y
,
z
):
"""
Constructor to form a vector
"""
self
.
x
=
float
(
x
)
self
.
y
=
float
(
y
)
self
.
z
=
float
(
z
)
def
__str__
(
self
):
"""
Method to format a vector as a string (implments str())
"""
return
"
[
"
+
str
(
self
.
x
)
+
"
,
"
+
str
(
self
.
y
)
+
"
,
"
+
str
(
self
.
z
)
+
"
]
"
def
copy
(
self
):
"""
Method to return a copy of current vector
"""
return
Vector3d
(
self
.
x
,
self
.
y
,
self
.
z
)
def
magnitude
(
self
):
"""
Return the magnitude
"""
return
math
.
sqrt
(
self
.
x
*
self
.
x
+
self
.
y
*
self
.
y
+
self
.
z
*
self
.
z
)
def
distance
(
self
,
b
):
"""
Distance from self to vector b
"""
dx
=
self
.
x
-
b
.
x
dy
=
self
.
y
-
b
.
y
dz
=
self
.
z
-
b
.
z
return
math
.
sqrt
(
dx
*
dx
+
dy
*
dy
+
dz
*
dz
)
def
normalise
(
self
):
"""
Normlaise current vector so its magnitude is 1.0 (unit vector)
"""
mag
=
self
.
magnitude
()
if
mag
!=
0.0
:
# if not already zero
self
.
x
/=
mag
# dveive throgh by mag
self
.
y
/=
mag
self
.
z
/=
mag
def
add
(
self
,
b
):
"""
Add a vector b to self and return a new vector.
"""
x
=
self
.
x
+
b
.
x
y
=
self
.
y
+
b
.
y
z
=
self
.
z
+
b
.
z
return
Vector3d
(
x
,
y
,
z
)
def
addTo
(
self
,
b
):
"""
Add a vector b to self in place.
"""
self
.
x
+=
b
.
x
self
.
y
+=
b
.
y
self
.
z
+=
b
.
z
def
dot
(
self
,
b
):
"""
Form dot product between self and vector b
"""
return
self
.
x
*
b
.
x
+
self
.
y
*
b
.
y
+
self
.
z
*
b
.
z
def
cross
(
self
,
b
):
"""
Form cross product between self and vector b.
"""
x
=
self
.
y
*
b
.
z
-
self
.
z
*
b
.
y
y
=
self
.
z
*
b
.
x
-
self
.
x
*
b
.
z
z
=
self
.
x
*
b
.
y
-
self
.
y
*
b
.
x
return
Vector3d
(
x
,
y
,
z
)
def
main
():
"""
Simple test program to test the vectors
"""
a
=
Vector3d
(
1
,
2
,
3
)
b
=
Vector3d
(
4
,
5
,
6
)
print
(
"
Magnitde of a is
"
+
str
(
a
.
magnitude
()))
c
=
a
.
cross
(
b
)
# Form cross product
#
# the str() function call the __str__(self) method
print
(
"
Cross product of a x b is :
"
+
str
(
c
))
#main()
```
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