Skip to content

Commit 01a95e2

Browse files
authored
Add files via upload
0 parents  commit 01a95e2

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

Create Vector Operation.ipynb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#Created By: Gita Kartika Suriah\n",
10+
"\n",
11+
"# In[ ]:\n",
12+
"\n",
13+
"import numpy\n",
14+
"import math\n",
15+
"class Vector(object):\n",
16+
" def __init__(self,*v):\n",
17+
" self.values= v\n",
18+
"#Vector Addition\n",
19+
" def __add__(self,vector):\n",
20+
" total=tuple(a+b for a,b in zip(self.values,vector.values))\n",
21+
" return vector(*total)\n",
22+
"#Vector Substraction\n",
23+
" def __sub__(self, vector):\n",
24+
" total = tuple( a - b for a, b in zip(self.values, vector.values) )\n",
25+
" return vector(*total)\n",
26+
"#Scalar Multiplication\n",
27+
" def __rmul__(self,scalar):\n",
28+
" vector= tuple((scalar*numpy.array(self.values)).tolist())\n",
29+
" return vector(*vector)\n",
30+
"#Metric\n",
31+
" def metric(self,vector):\n",
32+
" x=tuple((a-b)**2 for a, b in zip(self.values, vector.values))\n",
33+
" y=[]\n",
34+
" for i in range (1,len(vector.values)+1):\n",
35+
" y.append(i)\n",
36+
" return math.sqrt(sum(a*b for a,b in zip(x,y)))\n",
37+
"#Norm Vector\n",
38+
" def norm(self):\n",
39+
" return math.sqrt(sum(x**2 for x in self.values))\n",
40+
"#Vector Inner Product\n",
41+
" def ip(self, vector):\n",
42+
" x=tuple(a*b for a, b in zip(self.values,vector.values))\n",
43+
" y=[]\n",
44+
" for i in range (1,len(vector.values)+1):\n",
45+
" y.append(i)\n",
46+
" return sum(a*b for a,b in zip(x,y))\n",
47+
"#Print Vector\n",
48+
" def __repr__(self):\n",
49+
" return str(self.values)\n",
50+
"\n",
51+
"# In[ ]:\n",
52+
"\n",
53+
"a=vector(4,1,3)\n",
54+
"b=vector(1,-1,1)\n",
55+
"c=vector(2,4,0)\n",
56+
"x=vector(1,0,2,0)\n",
57+
"y=vector(3,2.5,0,1.5)\n",
58+
"z=vector(0,0,3,1.5)\n",
59+
"\n",
60+
"d=a+b\n",
61+
"print(\"d:\",d)\n",
62+
"e=2*c-b\n",
63+
"print(\"e:\",e)\n",
64+
"w=3*y-2*(x+z)\n",
65+
"print(\"w:\",w)\n",
66+
"print(\"d(a,b):\",a.metric(b))\n",
67+
"print(\"d(d,a):\",d.metric(a))\n",
68+
"print(\"d(e,c):\",e.metric(c))\n",
69+
"print(\"d(x,y):\",x.metric(y))\n",
70+
"print(\"d(w,z):\",w.metric(z))\n",
71+
"print(\"Norm c:\",c.norm())\n",
72+
"print(\"Norm d:\",d.norm())\n",
73+
"print(\"Norm e:\",e.norm())\n",
74+
"print(\"Norm x:\",x.norm())\n",
75+
"print(\"Norm w:\",w.norm())\n",
76+
"print(\"<a,c>:\",a.ip(c))\n",
77+
"print(\"<d,b>:\",d.ip(b))\n",
78+
"print(\"<y,z>:\",y.ip(z))\n",
79+
"print(\"<x,w>:\",x.ip(w))"
80+
]
81+
}
82+
],
83+
"metadata": {
84+
"kernelspec": {
85+
"display_name": "Python 3",
86+
"language": "python",
87+
"name": "python3"
88+
},
89+
"language_info": {
90+
"codemirror_mode": {
91+
"name": "ipython",
92+
"version": 3
93+
},
94+
"file_extension": ".py",
95+
"mimetype": "text/x-python",
96+
"name": "python",
97+
"nbconvert_exporter": "python",
98+
"pygments_lexer": "ipython3",
99+
"version": "3.7.4"
100+
}
101+
},
102+
"nbformat": 4,
103+
"nbformat_minor": 2
104+
}

0 commit comments

Comments
 (0)