︠bdbb3646-99e4-48ca-9ba8-8f2fed0f86fc︠ %md # Math 480: Open Source Mathematical Software ### 2016-04-08 ### William Stein ## **Lectures 6: Dicts and Classes (Python 3/3)** - My morning sucked: massive SageMathCloud problems. You can turn in your homework on Monday at 6pm rather than Friday at 6pm. - Screencast ︡70e03e40-9a05-428f-83b3-b56d801d9744︡︡{"done":true,"md":"# Math 480: Open Source Mathematical Software\n### 2016-04-08\n### William Stein\n\n## **Lectures 6: Dicts and Classes (Python 3/3)**\n\n- My morning sucked: massive SageMathCloud problems. You can turn in your homework on Monday at 6pm rather than Friday at 6pm.\n- Screencast"} ︠d1c71866-fbba-486f-bb07-3a6a3714abd8i︠ %md ## Another important data structure: the Python dictionary A Python dictionary is the Python version of what computer scientists might call an "associative array". It's what us mathematicians call a "function" (from one finite set to another). Except, like a list, it can be modified (to make another function). ︡14f4f227-7e2f-4233-81ca-3ec3dee9973e︡︡{"done":true,"md":"## Another important data structure: the Python dictionary\n\nA Python dictionary is the Python version of what computer scientists might call an \"associative array\".\n\nIt's what us mathematicians call a \"function\" (from one finite set to another). Except, like a list, it can be modified (to make another function)."} ︠8ffe6908-b74b-47e8-8cd9-fcdaf609fdd0s︠ d = {5:25, 10:100, 3:8} ︡85022f6e-1cd9-4256-9d4d-042d84d9939c︡︡{"done":true} ︠3cf1ddfc-81da-49f8-a3e7-134484d6dc10s︠ d ︡7d0755c0-75d7-4358-ae41-e588cc02a120︡︡{"stdout":"{10: 100, 3: 8, 5: 25}\n"}︡{"done":true} ︠18c7f593-076e-4ee0-ac59-6e9e8a2b3801︠ d[5] ︡ea46543c-6335-4a8c-96fe-20b175b697fb︡︡{"stdout":"25\n"}︡{"done":true} ︠eb2af520-e927-43e1-ae15-f0e001a58840︠ d[10] ︡181940a4-acda-48b3-810f-08a24ce6a22d︡︡{"stdout":"100\n"}︡{"done":true} ︠7f9489d4-664c-441c-b378-e905e9b0ea8c︠ d[3] ︡59544538-2f60-4182-ba92-41c7a96eecbc︡︡{"stderr":"Error in lines 1-1\nTraceback (most recent call last):\n File \"/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 904, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1, in \nNameError: name 'd' is not defined\n"}︡{"done":true} ︠159813f9-381b-46bd-a8e2-19c07ca019e1o︠ d[7] # should fail -- since we didn't say what 7 maps to. ︡e97bfc8b-545d-4762-89aa-281e64f9ab89︡︡{"stderr":"Error in lines 1-1\nTraceback (most recent call last):\n File \"/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 904, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1, in \nKeyError: 7\n"}︡{"done":true} ︠dd9611e8-6142-4752-975c-8bad8375eb6di︠ %md The keys (inputs) of a Python dictionary can be any object x in Python where `hash(x)` doesn't give an error. In practice, `hash(x)` is supposeed to work if and only if `x` is **immutable**, i.e., really can't change. ︡95bd0d19-279e-4cce-901d-0b255a9194e9︡︡{"done":true,"md":"The keys (inputs) of a Python dictionary can be any object x in Python where `hash(x)` doesn't give an error.\n\nIn practice, `hash(x)` is supposeed to work if and only if `x` is **immutable**, i.e., really can't change."} ︠5d843b8c-c518-427d-8f98-874b9318270bs︠ hash(7) ︡71f30065-f62d-456a-9b36-07f01f4f88a8︡︡{"stdout":"7\n"}︡{"done":true} ︠d3fc570b-f451-46f1-b719-3d29f753097bs︠ w = [2,3] hash(w) # better not work! ︡c9d80972-9dc4-476c-a5e1-0225a75b046b︡︡{"stderr":"Error in lines 2-2\nTraceback (most recent call last):\n File \"/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 905, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1, in \nTypeError: unhashable type: 'list'\n"}︡{"done":true} ︠1651c145-4a94-4167-8637-d960f3286e7di︠ %md Tuples -- I mentioned them very quickly last time. Tuples are sort of like lists, but you can change the objects they point to or the number of things they point to. Hence... **they have a chance to be immutable:** ︡cd8f5601-95e3-4a67-aa1c-7cc1ed4b3d0d︡︡{"done":true,"md":"Tuples -- I mentioned them very quickly last time.\n\nTuples are sort of like lists, but you can change the objects they point to or the number of things they point to.\n\nHence... **they have a chance to be immutable:**"} ︠7e53d595-f31c-43a4-b7fe-0d1d45c88496︠ w = (2,3) hash(w) # a tuple where each thing in the tuple is immutable ︡1c6027bb-885b-48d2-a51a-6a1f80b78ec2︡︡{"stdout":"3713082714463740756\n"}︡{"done":true} ︠4f50157b-145f-40a3-86c9-dba986cd5f9b︠ d = {(2,3):5, (7,19): 26} # you can use anything immutable as the inputs (or keys or domain) d[(7,19)] ︡4e06ecf4-f35d-4fb0-8c01-4a5989223ddd︡︡{"stdout":"26\n"}︡{"done":true} ︠18608d49-189f-4eb4-b3b0-1d1f8d986fd2i︠ %md Not all tuples are immutable. For example: ︡1080ca5b-c591-4b96-bf2a-e16cc496c68b︡︡{"done":true,"md":"Not all tuples are immutable. For example:"} ︠070ad738-e402-4f09-9ce9-4e978fd9ba65︠ w = ([2,3], 5) # a tuple where first thing is NOT immutable hash(w) ︡422b8072-4424-44cf-a48c-79c0d5b3b735︡︡{"stderr":"Error in lines 2-2\nTraceback (most recent call last):\n File \"/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 904, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1, in \nTypeError: unhashable type: 'list'\n"}︡{"done":true} ︠d350b0a3-07f6-4911-a703-6a9af962b8c9︠ w[0].append("Sage") w ︡05e7f6f1-8952-4786-a2fb-668045f870a9︡︡{"stdout":"([2, 3, 'Sage'], 5)\n"}︡{"done":true} ︠dcaa834d-9cc8-4db7-83de-f50b6cebed84i︠ %md **Exercise:** Which of the following are immutable, i.e., `hash`-able, so they can be used as keys for a dictionary? - the string "Foo" - the empty list [] - the number 3.14 - the dictionary {7:10, 3:8} - the tuple ('foo', 'bar') Make a dictionary that has all the immutable objects above as keys... and anything you want (hashable or not) as values. ︡7493f29b-0539-4c27-822a-e621603571bd︡︡{"done":true,"md":"\n**Exercise:** Which of the following are immutable, i.e., `hash`-able, so they can be used as keys for a dictionary?\n\n- the string \"Foo\"\n\n- the empty list []\n\n- the number 3.14\n\n- the dictionary {7:10, 3:8}\n\n- the tuple ('foo', 'bar')\n\nMake a dictionary that has all the immutable objects above as keys... and anything you want (hashable or not) as values."} ︠87c2b980-c0c7-45d0-8f2e-677788776e83︠ {"Foo": hash("Foo"), 3.14: hash(3.14), ("foo","bar"): hash(("foo","bar"))} # Lalith ︡96626c72-df54-46df-bb80-5244dc893d0b︡︡{"stdout":"{'Foo': -5036909580522842981, ('foo', 'bar'): -998648364678804268, 3.14000000000000: 3146129223}\n"}︡{"done":true} ︠3d6e0670-6b79-4972-9d98-93a4df5113ea︠ dic = {"Foo":[1],3.15:[2],('foo','bar'):{7:10,3:8}} #Joshua Mehlhaff ︡ba96fd81-bd02-46c3-a170-609add6da497︡︡{"done":true} ︠41d20d33-7fa7-4e50-aa58-9f9ad24335d4︠ #Simon Luu hash("Foo") - immutable hash(3.14) - immutable hash(("foo", "bar")) - immutable diction = {"Foo":1, 3.14:2, ("foo", "bar"):3} diction ︡1fd8b791-7bd9-4afa-8e1c-9bb45c2dbdbd︡ ︠f7740328-9e25-4bc8-a130-6126c5c52289︠ #Xiling dic = {'Foo':2, 3.14: 'a',('foo', 'bar'): {1:3} } ︡7fa636ce-ad56-483d-9ff2-a2338597cdb4︡ ︠08e50a25-2b3c-4541-a4a3-21f09c940b0c︠ hash('Foo'),hash(3.14),hash(('foo','bar')) ︡81505172-6d90-4c92-98ba-1e4e199cd5f9︡ ︠f0fddc7b-7146-4f33-9f8a-18adf648fd0c︠ d = {"Foo": 1, 3.14:2, ('foo','bar'):3} #Caitlyn Hughes ︡a446dd6c-0cda-497c-9a5e-7177d272e48b︡ ︠13732398-f635-4d68-9f5d-d1b9c2dd41ab︠ {"blah": hash("blah"), 3.14: hash(3.14), "Foo": hash("Foo"), ("foo", "bar"): hash(("foo", "bar"))} #Timothy Luong ︡f413dfd0-846a-4613-b739-596aa4376fe5︡ ︠57b6f2b5-2a9a-4a85-a030-dfe206d4ad15i︠ %md Here's a dictionary with a *function* as a value: ︡0de673e0-255e-417e-b039-70f455483798︡︡{"done":true,"md":"Here's a dictionary with a *function* as a value:"} ︠c81d36d9-3764-493a-b8bc-946d9339eb1a︠ def length(self): return sqrt(self['x']^2 + self['y']^2) self = {'x':10, 'y':15, 'length':length} self ︡4d532b2d-a0a1-42d1-b3a4-da78a8fd72d1︡︡{"stdout":"{'y': 15, 'x': 10, 'length': }\n"}︡{"done":true} ︠d34000e6-8c11-4050-bee7-49b8a531cd45︠ self['length'](self) ︡150912b3-def9-4f8f-b483-8b1d04864c8f︡︡{"stdout":"5*sqrt(13)\n"}︡{"done":true} ︠f683b6b3-a9b6-4115-8574-38fafc2b5f54i︠ %md Using dicts with data and functions you could model objects and work with them. But the notation is very awkward! Python classes do much the same thing, with vastly nicer notation! ︡a2252da7-8e39-4c81-a7e9-161a73b672e7︡︡{"done":true,"md":"Using dicts with data and functions you could model objects and work with them.\n\nBut the notation is very awkward!\n\nPython classes do much the same thing, with vastly nicer notation!"} ︠352e986e-5321-4508-99ca-def6711b7a77︠ ︡77aa5637-35a7-4dc3-8de3-ce552346fd48︡ ︠f4dd5f00-1a75-4cc5-9eca-2d84a91d360bi︠ %md ## Python Classes Python classes let you easily create custom Python objects. They are a fantastic way of organizing math-related code. Example: ︡82badfc5-4b7c-4e83-a4b1-c848d949d537︡︡{"done":true,"md":"## Python Classes\n\nPython classes let you easily create custom Python objects. They are a fantastic way of organizing math-related code.\n\nExample:"} ︠0b2d4358-f2c8-481f-a4d7-a71311a2a597︠ class Vector: def __init__(self, x, y): self.x = x self.y = y def length(self): return sqrt(self.x^2 + self.y^2) def __add__(self, right): return Vector(self.x+right.x, self.y + right.y) def __repr__(self): return "The Vector (%s, %s)"%(self.x, self.y) ︡eb6e7e6c-3a3e-4062-a659-360007298253︡︡{"done":true} ︠3f20e2d3-69cd-42f5-9373-4c52dd8d414a︠ v = Vector(10, 15) print v ︡d1e3958a-f7c5-4daa-bd06-ab0a31edfbcd︡︡{"stdout":"The Vector (10, 15)\n"}︡{"done":true} ︠153dd7d9-12cc-4bde-b875-1ce0d4232bd5︠ v.length() ︡d87ca55c-4737-4ac7-8327-a8c37af6e800︡︡{"stdout":"5*sqrt(13)\n"}︡{"done":true} ︠c459adf0-9eba-482a-b9a1-f7bf1e11c66b︠ v + v ︡4218549f-40e5-4ca2-bdcf-b6d0e2c50fd8︡︡{"stdout":"The Vector (20, 30)\n"}︡{"done":true} ︠ffcd322c-2acc-4c80-847f-17a74381a028︠ v.__dict__ ︡41429105-ef9c-42ab-9da6-f76ee99e0a8c︡︡{"stdout":"{'y': 15, 'x': 10}\n"}︡{"done":true} ︠13e278cd-28eb-48ca-a6b1-7ec8ccf1ca1di︠ %md **Exercise:** Copy the above code and make a class Vector3 that models a vector in 3-dimensional space. If you have time, also include a `__sub__` method, to subtract one vector from another, and test that v - w works. ︡4f8204a4-4314-4a77-b634-bb1bd4e86d2d︡︡{"done":true,"md":"\n**Exercise:**\n\nCopy the above code and make a class Vector3 that models a vector in 3-dimensional space.\n\nIf you have time, also include a `__sub__` method, to subtract one vector from another, and test that v - w works."} ︠88022ea7-c5fe-42e4-a0f2-ca7e2afc922b︠ #Joshua Mehlhaff class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2+ self.z^2) def __add__(self, right): return Vector(self.x + right.x, self.y + right.y, self.z + right.z) def __sub__(self, right): return Vector(self.x - right.x, self.y - right.y, self.z - right.z) def dot(self, right): return self.x*right.x + self.y*right.y + self.z*right.z def cross(self, right): return Vector(self.y*right.z-self.z*right.y, self.z*right.x-self.x*self.z, self.x*right.y-self.y*right.x) def __repr__(self): return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) ︡9c0803f8-d2f2-451d-a6dd-97704b6e316d︡ ︠4437f355-eeb9-43a8-9a4f-070979a99ceb︠ # Doudou Feng class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __add__(self, right): return Vector3(self.x + right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "The representation of the vector is (%s, %s, %s)"%(self.x, self.y, self.z) def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) ︡869fb48d-d2f7-468e-8b32-3a10b535d3b1︡ ︠a15e2560-87c9-4857-b7a2-e5f1d83b298b︠ ︡53457705-c566-49ba-ae1e-40aeacf19d6f︡ ︠8b28009a-309c-4d38-905a-795fbff1808b︠ class Vector3: #Ankit Kumar def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) def cross_prod(self, other): return Vector3(self.y * other.z - self.z * other.y, other.x * self.z - other.z * self.x, self.x * other.y - self.y * other.x) def __repr__(self): return "Ankit's Vector3 (%s, %s, %s)"%(self.x, self.y, self.z) ︡15313856-cd75-411e-8387-50cab45ecf20︡ ︠d823f572-5a85-4b43-9b1f-3172afa08dc3︠ # Mari Chinn class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __add__(self, right): return Vector3(self.x + right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "The representation of the vector is (%s, %s, %s)"%(self.x, self.y, self.z) def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) ︡84ec6b11-303a-4346-9c4f-8651f8372406︡ ︠247b2dd3-e256-4175-bd9b-8a4a18cf8582︠ #Alta Steward class Vector3: def __init__(self, x, y,z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __sub__(self, right): return Vector3(self.x-right.x, self.y-right.y, self.z-right.z) def __repr__(self): return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) ︡f316e647-f5e2-42ab-8f9a-a88c03c59057︡ ︠efba508d-32d6-4ef3-b197-5ce014c05e4d︠ ︡e7359642-75ee-4809-a5c3-05852d90de79︡ ︠35ea621a-7d9b-4e1d-b1b6-7479f5cc3229︠ ︡fabcd2eb-092b-4316-b39b-1915eb6a4721︡ ︠26fd02cc-672f-43ac-bc86-0bbfc8e012d8︠ ︡5f28f05e-5f6e-4075-aaa2-682543971ee4︡ ︠84b15dd3-225e-48be-9a99-923cb614f379︠ ︡903d573c-e4fe-4386-a9f4-bb0d85110fbf︡ ︠3dd17b42-b9fe-4406-8020-909e6ddf07c7︠ ︡54f4e268-91f0-4709-9fbd-aa9e4cfe70e9︡ ︠ce1bb73a-0009-4319-9a35-840bbde5bebe︠ ︡3f5ccb22-7dc2-464c-aab1-b05ec09e0ac8︡ ︠1277fe2a-0734-4a0b-b25d-18d1db0cbef0︠ ︡6ada49d0-3826-4d16-b8c7-6886db829718︡ ︠c41098fc-601d-420d-88e2-63ec51c960a9︠ ︡c8df2735-d428-4f3f-b968-7fbb59877ee7︡ ︠d6975f8f-e7cf-4692-9131-6c1243be7dee︠ ︡1163fefa-198f-4184-a7c4-d89bf4ac3c34︡ ︠307ad889-1127-42d9-ba0e-85e394edb401︠ ︡5bc0b48b-58f2-4f11-8991-9b5db4afdc5e︡ ︠aa36f3ea-4bac-43fa-a230-ae49c4f9d2da︠ ︡9e495d72-f45d-4c31-bed5-5aa2d4f6ff38︡ ︠8ff64ec6-c37e-4a37-ba09-fd3e5195c1f3︠ ︡5cb84949-8816-4f16-98e4-daaf827fd38c︡ ︠1306ea47-22c4-4448-8e67-1aedf2294921︠ ︡a470f789-bc8d-45de-ae5b-3f6167b12cf7︡ ︠dd1c191c-a07d-4f71-932a-07bf1ed5dbed︠ ︡d1d0478b-63b6-47cf-a0f4-489c6652bd4f︡ ︠9b540c43-caf7-4c07-8e21-a3f3cb3b7d2c︠ ︡602f9714-e3a9-4160-87a9-bd0af204290b︡ ︠3b4920ad-a06d-4df2-8319-55b3ab959510︠ ︡4afbed90-432d-4da6-aafc-3a16abb94797︡ ︠db401090-20ae-46d3-ae00-7cfb2f14da35︠ ︡e7df54f3-d295-4013-ad52-cd0551a1a093︡ ︠78a5edbc-0579-424f-8cab-9c837e244f93︠ # Vinh X Mao class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x + right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "Vinh's Vector3 (%s, %s, %s)"%(self.x, self.y, self.z) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) def __cross__(self, right): return Vector3(self.y * right.z - aelf.z * right.y, self.x * right.z - self.z * right.x, self.x * right.y - self.y * right.x) ︡6d3b04e4-d258-4837-9361-5952c5373518︡ ︠65ae6e21-5bdb-4281-81b7-61f1bb8b8afa︠ ︡d07a03c4-63e8-479c-beaa-e38643ad3463︡ ︠b12bf54c-8aa9-4896-a90e-94957d122ed7︠ ︡0f54c5d5-e2fb-4f40-83c3-5ecd1bf837ed︡ ︠bfb14f82-bea4-4f10-bf03-0f79ba15bd73︠ ︡e507e759-5593-4a2e-a615-422f55ad0a92︡ ︠2de0e989-3cd2-4b72-991c-be0ba3a32f80︠ ︡ea5626a8-757a-4e1c-9d17-712d33ba26ab︡ ︠79e60c01-e03f-44a1-8ebb-af34145a36ec︠ ︡8afa0d01-b4e8-4666-89e2-9d1ded1bd03a︡ ︠2275c36c-53c9-4c2f-a4f8-c1f64091de8c︠ ︡5d545220-5542-44ad-b96a-233fe3ebc1c8︡ ︠9a58d8e2-f3f5-4fea-9744-11156a938959︠ ︡e7666b3e-73dd-474a-87a4-82d308fd9883︡ ︠4ab4e115-974e-4e75-8a45-36a804d4cc70︠ ︡d83d8957-1c56-40e3-b45d-2bf3b1c843af︡ ︠1d0b1073-6169-4614-8830-3e4cc1ab7802︠ ︡7e75d94d-33bf-40f1-9480-efac4a939882︡ ︠2a2ba17b-9efc-45b9-a2c5-e8e484a0ba6d︠ ︡217512fd-08b0-4503-b9f0-60d091cf36e9︡ ︠67a25bc5-7fb4-4101-afe3-91e4357701a9︠ ︡3a0ba0db-b0aa-43ba-a753-85ad5bebe777︡ ︠37543012-8712-4c1d-aae3-bed9c9992531︠ ︡976a08ab-4d75-48bb-a2f9-bd04fe82dcb6︡ ︠8aaa3c70-a368-49ca-bb18-53c0bd37ea27︠ ︡4dd0f2d7-c77f-463f-bc2e-ef7e3e7884e8︡ ︠3f61a1bd-71e4-4389-8075-be4bf3c3eb82︠ ︡f3e51266-d5e2-4406-bb6f-264478b1ef2e︡ ︠0d638442-f230-4ed3-bed7-092d3953ec11︠ ︡d95d1a40-08d5-496c-b0b8-de619104eb6c︡ ︠4e72258a-f682-415f-8f98-b86b525bb493︠ ︡707b4184-1b03-4e96-bf44-ef9fc5d80c58︡ ︠08373eba-3365-4698-a59c-ffac63865e89︠ ︡0b745548-f1ae-4707-b0fb-bf32abf98088︡ ︠d8d5792b-32f2-4c51-a1f5-7319d4bb9ab8︠ #Simon Luu class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) def __cross__(self, right): return Vector3(self.y * right.z - self.z * right.y, self.x * right.z - self.z * right.x, self.x * right.y - self.y * right.x) def __repr__(self): return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) ︡82c00a02-58c0-4fbc-aa62-1bd3e0c7568b︡ ︠9c03c203-479a-4488-9d28-7cd9ed6d4cfd︠ ︡40ef6e5d-e7b7-4cb3-9211-4d563d23d476︡ ︠dbd15101-914e-44b7-9083-60c6c50d7a8e︠ ︡fd1dfb28-1cde-4a79-96d2-044f50146e1e︡ ︠65eeaab8-4f11-43c6-91a2-1e4f67023d6a︠ ︡54ca0de9-0758-4449-81c4-5049cd083193︡ ︠026cc1e7-bbd4-4d8d-9a6d-1dc30b148ab1︠ ︡94c93bc2-38d8-4b37-a58d-76382f8760b5︡ ︠b1a0cc6a-a976-4900-8ba8-a644135e3a88︠ ︡ecf8ac01-5d26-4f32-acc2-368a600dc6c2︡ ︠2b08abb0-2c6f-4574-88f9-4013ff9d9c1c︠ ︡ed71bf8c-2eab-4029-ba00-2a1321a94a19︡ ︠8b557541-29fc-466f-bec4-8f1f909bc738︠ ︡f50264ae-d9f7-4c6c-af2b-5cb677696440︡ ︠786b21b3-d95a-4385-8297-5f75af14d67a︠ ︡50ff5921-4035-4f38-bb4a-1a36d4fee374︡ ︠7de00139-67c8-446a-9c5f-72907c4ad8ad︠ ︡ae982165-5e99-415a-aee3-d966e83bc317︡ ︠0cc73a5e-8429-4c1e-8d6f-b64be111cc7b︠ ︡7d444a28-b28f-46a8-93a7-7305fe071654︡ ︠8dae4e76-b3eb-42f0-bf84-8449037a3c80︠ ︡01cdbd8e-186d-41b4-8e62-f701566dd6bc︡ ︠dac83834-870d-4e7a-a935-99339fc21581︠ ︡c362c214-724e-4cee-b5dd-4683c67995ed︡ ︠4a04e99e-17eb-4e77-806a-588c1c3b5dcc︠ ︡ddf92b07-dd67-4583-9fc5-7e7232713013︡ ︠46718f38-122f-4832-9191-f65a4fba892a︠ ︡d0443d65-fbf2-4ee2-84d1-d433f4cb3023︡ ︠cdd32948-59ab-45ef-9f92-4b72c7bd192b︠ ︡afe51b70-56fc-4861-8905-3339c514ca47︡ ︠fa5173e8-d8b2-4ba6-9fc8-b1426865d4f6︠ ︡39decb4d-aaf1-4e7b-87b0-2a31928f0108︡ ︠7bc17498-b24d-4663-999b-5148ae32cdda︠ ︡f5cc49ae-f790-45b5-a94b-404063e172e3︡ ︠549c835b-e2a1-44bc-ae24-53c7fdfa619f︠ #Mac Hollister class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x + right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) def __sub__(self, other): return Vector3(self.x-other.x, self.y-other.y, self.z-other.z) def cross(self, other): return Vector3(self.y * other.z - self.z * other.y, self.x * other.z - self.z * other.x, self.x * other.y - self.y * other.x) ︡750aa15e-9e4b-4cbb-9760-f5f67ff7b2bc︡ ︠2b83f030-4d95-4234-9151-c64593b7c60f︠ ︡d5bd7373-4ec0-433d-aec7-3afc21556088︡ ︠0ce53b53-9c24-4dfd-a0f4-44cf4fcaaf5c︠ ︡0da30128-fe37-4c9a-94ee-6367f2848208︡ ︠6a10c191-a5b3-4aa6-8a75-0fdcbcfbc411︠ ︡a5398356-9a84-4d2f-9ffb-0e3dd457335c︡ ︠8b8381e6-2cd2-4a10-8782-beabc9478bae︠ ︡301957db-b99f-48aa-af01-b65d44e020a2︡ ︠ac17ad8a-2658-46b1-b78b-597246f37c3b︠ ︡8aa2e8b9-1add-49b4-8539-c4e90b88034d︡ ︠f3b9990c-6d88-434d-9913-382ccf5935ea︠ ︡ff946529-4d13-4b32-9bc6-aa8f731b51d7︡ ︠0a04c5a5-8166-42ef-99bc-c06fe0c1d413︠ ︡220ccda7-0fb7-4877-9e20-c18e8b77ebfb︡ ︠bfc11451-f2aa-431d-a690-a4b6f02864ea︠ ︡9e23537e-1ef0-47b6-9194-c4ab0fe7389c︡ ︠c4e8a91e-bbf0-4a41-a65e-c438877d9689︠ ︡4c4d9d85-3f2d-46a9-84e2-7ea46ccc41a0︡ ︠621817cd-f3cd-4682-a813-7f9515101fa4︠ ︡ba107118-de1a-4fee-b520-a4c72dc67d7d︡ ︠06ce1040-f65b-446f-87db-cbd3c0495aca︠ ︡c771ef59-6161-4435-8086-f7b001d1adf3︡ ︠88f0c9e5-2668-4033-be1c-adfebe8f4fb6︠ ︡b44b159a-05c6-46bd-a5f4-f31f19e4c794︡ ︠54a7bbf8-0bdd-4870-bbb2-e34a5cf5fe64︠ ︡f8dd5d98-dd50-4801-b9b8-fa28905ab6c5︡ ︠4ab824b7-7ac6-4e3c-98e5-c7de653b7f2b︠ ︡a98aaf6c-a242-4bb1-afdf-f1f76242e73c︡ ︠e09babf0-cbbb-487e-b3d9-8a1f297b38ad︠ ︡d90eba7e-1974-4770-886d-aa7d38054ec8︡ ︠ea129279-6e45-4af8-9556-628080050702︠ ︡90834f17-4a71-45bf-a8d3-d15fded85915︡ ︠05ad78f5-78e9-4714-9fd3-7583fb29896b︠ class Vector:#John Menefee def __init__(self, x, y, z): self.x = x self.y = y self.z = z print "I just initialized myself" def length(self): #length of vector return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right):#adds a vector return Vector(self.x + right.x, self.y + right.y, self.z + right.z) def __sub__(self, right):#subtracts a vector return Vector(self.x - right.x, self.y - right.y, self.z - right.z) def __crossprod__(self, right):#cross product return Vector(self.x * right.x, self.y * right.y, self.z * right. z) def __repr__(self):# string representation return "John's Vector (%s, %s, %s)"%(self.x, self.y, self.z) ︡c7943847-4778-4e79-8691-7c3c55f0b50a︡ ︠37e7229c-60a3-42af-9373-dd82bd6ab118︠ #Caitlyn Hughes class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return (self.x^2 + self.y^2 + self.z^2)^(1/2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "The 3D Vector is (%s, %s, %s)"%(self.x, self.y, self.z) def __sub__(self,right): return Vector3(self.x-right.x, self.y - right.y, self.z - right.z) def __crossproduct__(self,right): return Vector3(self.y*right.z - self.z*right.y, self.z*right.x - self.x*right.z, self.x*right.y - self.y*right.x) ︡d995331d-caae-4246-88db-783f9b378d00︡ ︠de70d27b-0536-4794-8928-5fd697fbad0f︠ ︡2129fd81-f9c7-44cf-8eec-105e79850695︡ ︠ac598758-98a6-4287-a492-8e21ad6d3699︠ ︡6b7e2d6e-7125-4e46-899c-4303131e2e6e︡ ︠c7ff6ca7-575a-4aac-9625-63282a7e6286︠ ︡5ba0efb3-bb1d-445e-82d0-205294709905︡ ︠fcbb2bc4-6295-4cf6-8c94-7a2f9e03aa54︠ ︡3569a715-9b8f-4a4b-81cf-1208b791699c︡ ︠e8eabb56-0ca9-4ceb-bb71-c83c2476c5a8︠ ︡716020e1-c463-40f8-b5d0-3842dd0ec155︡ ︠c6ac2647-5b37-4390-bd5f-88ed55ab949d︠ ︡f40e0997-c31b-4e96-b742-5be04b0785dc︡ ︠5e0dda6e-0209-404e-9f72-9055dacc4b57︠ ︡0f6fae16-35c2-4ee5-8058-0020c8c47d3b︡ ︠c6497f8e-96a4-4b29-ba44-d84275f12f14︠ ︡2f50cc4c-57a3-4876-a44b-34f566342f8e︡ ︠0fd0f22c-e390-4db6-b96a-e9aed8c032ad︠ ︡76f88815-61c8-485d-9499-4c74947b0525︡ ︠56a1d222-10c9-41b7-a59c-4b2e3c5b387f︠ ︡074d8806-f1b0-4cb4-b4cc-88d7c775dee5︡ ︠f41eb313-b371-417c-a8fc-c4486d6b7ca0︠ ︡9eac3a09-c97f-44ef-8b5b-57064cffec69︡ ︠482b2734-73ae-4dcf-a058-f1d5bb434d6c︠ ︡79e1f703-c2c4-465a-89a4-93f17aa5ea3f︡ ︠1b03ee6f-1c6c-4bd2-86ef-cc6856fdf4dd︠ ︡b844fe7a-42f8-40e7-8de7-094d61b9410d︡ ︠c5fb171b-aa74-414b-ade9-efd23cdc5191︠ ︡9a7ab5dd-05d8-4ce1-b70b-77b4c28d66ac︡ ︠3f2bbc03-fa77-42cb-b6b7-b63964d0d4b8︠ ︡a8aed5dd-a0aa-4690-aa2d-e6dad1b26bed︡ ︠49608711-49ce-488a-a909-7bf5920bb16d︠ ︡4790a84b-500d-4a6c-940a-9a7874d628d8︡ ︠5b6beb71-565d-46ee-8e45-187f0a945c81︠ ︡846df9e8-27d7-4fc9-a30b-d1f474691867︡ ︠e2270a90-f695-4b42-89b7-3a816c4beae4︠ ︡326b7b44-9444-4087-a800-c52e4169b269︡ ︠ce9c17da-7ea4-4945-8cb4-64ffe03f6385︠ class Vector3: #Timothy Luong def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) ︡a2113b2f-48a5-4874-b961-f6e8abc0ed75︡︡{"stderr":"Error in lines 1-1\nTraceback (most recent call last):\n File \"/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 904, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 1\n ︠\n ^\nSyntaxError: invalid syntax\n"}︡{"done":true} ︠68eb8a90-9b92-4c2c-bfb4-4ee8df3c7eff︠ ︡6a1cea70-23e9-4788-bde9-05086634301e︡ ︠d8e17471-e9d1-48c1-8f43-396d1a0cf1e1︠ ︡c631265f-15f2-4b77-ac69-4f6ee699f284︡ ︠6fb118d5-f040-4b0b-865b-fa0d72ab1cbf︠ ︡d47d54c6-21d3-4392-8ccf-7346e1f4dba2︡ ︠fc0b37bc-3892-402c-b1d9-4bd1b90e14e8︠ ︡c0f68dfa-93ce-47df-8030-a5641f460477︡ ︠104ab655-4d33-4e3d-80a0-62f85260ff71︠ ︡66f1f3a1-f9f3-4a9f-b540-b7db0c98d542︡ ︠36508cb7-bc92-4044-8ab7-a92e9f8d61d2︠ ︡f5d09112-bece-47ac-b163-65a9332ae14a︡ ︠da4d96f9-aeb7-47af-98b8-4221e6d8100e︠ ︡ea8363ff-75b0-4b4a-9a03-2be38516bdcf︡ ︠811a0096-98b4-4765-81c1-d2588ffea871︠ ︡faa8cc50-7c0c-4e11-8a58-d92beebe7a53︡ ︠e9c3486b-e332-4459-a9a9-5a04f8df416c︠ ︡136a5761-0135-425e-b95c-94c14ee39833︡ ︠4aaf2d6b-b6b2-4370-a102-995b8986c6c3︠ ︡69737815-0cec-48ce-8438-63a83bd1a45b︡ ︠c1d9b6f1-7122-484d-b83f-01c6346607c1︠ ︡735034df-f96f-455c-9933-5e4f7693c7b4︡ ︠db182b8e-c44a-4234-8e34-a4d8c1ba2532︠ ︡0d16b2d1-1001-4234-a46e-22391aa7b32a︡ ︠e648fbd7-1552-47b4-b11d-5c140cebc503︠ ︡87e8553d-8352-4026-b0b8-84410a77f7f7︡ ︠137d7b9f-8feb-44ee-a837-d7ffd18d72d8︠ ︡632dd1a2-460d-4135-9b61-b67c76d19439︡ ︠737a16d8-58ae-4f66-a982-74a20f806c16︠ ︡ea0ca988-3cf7-454d-b57f-d2e5764a7469︡ ︠7e0a92f2-7a44-4d41-801a-6156df07a8cc︠ ︡2b79cc1c-edbe-428a-b1e3-7145d69d7dbc︡ ︠f3ed18fd-09cb-43fa-bba2-7ce0352aa7e4︠ ︡70c28177-f8c0-4bde-af0b-7c3487fb7579︡ ︠eaf78f59-301c-417d-8698-dc572dee661d︠ #Boyu Deng class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __repr__(self): return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) def __sub__(self): return Vector3(self.x-right.x, self.y-right.y, self.z-right.z) ︡af68e339-0d5b-4bb8-8558-3d944a0b4cf9︡︡{"done":true} ︠e0fff018-b14c-4c5e-b6da-1f24d2addb68︠ ︡cc1d2aa3-b82e-4cab-8360-b44eeccd89fd︡ ︠9872c394-d747-4ef9-b02a-8d96a13c05e4︠ ︡d270a919-bcaa-4e52-9f6a-0f351ef80129︡ ︠6ca8f83e-e88a-435d-8561-2c9cf75ed67e︠ ︡982c3b9a-b6bd-40a9-81c5-e1ebebb9b5bf︡ ︠3f530c23-fb99-4252-bc41-e89ff3af7c8d︠ ︡15d148d6-4b0d-4183-b309-f161c27ff1fc︡ ︠a17c2605-a07d-48c4-988a-6188b85cdd9e︠ ︡dba4b677-8144-4917-bbe8-ae8872ad6136︡ ︠15282e37-7fd0-4f28-ac28-bbe8a266b069︠ ︡9db5aa01-06d1-4a0a-bed0-6d3e74c0ddbd︡ ︠46d9dfde-32ca-4936-b790-831f850ad6d5︠ ︡46de9376-ea8b-4e20-8959-da8754206f5c︡ ︠3845d6c8-1f60-4f68-b973-c9256578b9b8︠ ︡254cc5c7-8966-4b62-a233-2b4c0e3e119b︡ ︠4a6662ec-8234-4cc7-bd88-06ef63c7734f︠ ︡b0672fd7-2da1-47f5-b5db-a5d2890eae17︡ ︠8309ee7a-b100-4453-abac-d6da7fdca16b︠ ︡a7c25c96-1dc4-4a52-b528-018e7fa6f378︡ ︠e85b51cd-a54d-4868-838d-2ce9c0137d08︠ ︡a4bfd310-3b49-44bf-809c-0a64e704f5e1︡ ︠492cd57d-15b7-436b-9aff-540a6b5f02f3︠ ︡6eb8a48a-8738-416e-ad70-75d75f38929e︡ ︠4d0839bf-352e-4a7f-b61e-f853242e0953︠ ︡6f73a471-bd72-4fc2-8697-030adcf28782︡ ︠b7f5cd70-f402-4044-86c6-45b773fc9511︠ ︡902fa973-33db-4164-899d-46c34f02c343︡ ︠40ba93db-9704-4363-ae2b-1e6fd8f3893a︠ ︡051cacaa-bda4-472c-8022-4372bbde151a︡ ︠fc71b73f-ef4a-44ce-a02a-109d87071e1d︠ ︡5ef91e35-8ee5-4990-89ac-60ba8109f530︡ ︠dcdbcc0b-1ca0-4914-8030-d768e4751135︠ ︡5ac111df-2342-4737-8b76-35573264a029︡ ︠66cf4fd0-eac6-4be7-8a72-181dd42ba7c2︠ ︡a69fdb18-2a32-4b91-9b3f-362c383aff0e︡ ︠91795d74-6a60-424e-94e6-ddea3fb3ea32︠ ︡df2e4092-639a-4208-a3b2-a568d22de8f8︡ ︠d2eda2fb-ab6c-4dbe-8ff7-6a7294762cbf︠ ︡b01e043c-0926-4f2e-bad0-c1c59e1a1352︡ ︠44bdeedc-baa1-4d7b-a6d0-bb5b3eb73c50︠ ︡1b607abf-efb5-41d3-b84c-8ebcbff4f1d5︡ ︠345107f7-f06f-487b-99bb-0174ad72486c︠ ︡ade875b7-74a5-42a0-a704-4b9fe291fb41︡ ︠603dec0b-0b61-4bab-bfbc-ad7a543f0f3d︠ ︡d034f530-de47-45a6-a67b-db198935c2f8︡ ︠b3522f23-f822-4e4b-aa0f-61bd1d963507︠ ︡234b6dc4-70b1-4e4a-a82f-55777e41afce︡ ︠4bedf45f-8440-479d-bfc0-67b6a7dbba1f︠ ︡cda98e80-1306-4167-8de5-28dcef82e102︡ ︠1bb2fcb8-8d68-4e0a-8feb-826bf3b08b14︠ ︡11b8381d-ab9f-4982-a012-21c917be903b︡ ︠de1a34b7-28ca-4032-819a-61e58abeb616︠ ︡04b269a7-3c9f-402e-b7de-43922592476f︡ ︠0307aba4-6f87-415d-be8f-81c915b50cfd︠ ︡44968f0a-25c6-4c8d-86df-fbfa3cfe57fb︡ ︠252c27d5-5f0d-4c7c-a2e8-4139e6e4a348︠ ︡0e0d3f81-7a3f-4b07-91f1-f5d0b6f58086︡ ︠613db620-016f-4fed-a414-d7740c4fb8f4︠ ︡3702bbf6-aa67-4829-a6a6-5e60680acc56︡ ︠4b5b3976-8ecb-49f2-969c-33361e5fe1c1︠ ︡140b50de-cdb3-47b7-ac37-3cab603d9d92︡ ︠26368f20-a846-4b36-92d7-2b3d417a3ca7︠ ︡c3c4c992-86ab-43fa-97a9-4dc1156d37d3︡ ︠b3b4fed9-effa-4ee3-a96a-608831e766b5︠ ︡302a8083-d8e8-46b6-96bb-faf4cb6d52e5︡ ︠7618c6bf-4833-47f9-92da-3af9e7d421cd︠ ︡17d043e4-7161-49c0-8b5d-ed64adf1cbe1︡︡{"done":true} ︠e997cc80-9b13-4aff-b8bb-2b1342f66dbb︠ class Vector3: # Lalith def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): # length of vector return sqrt(self.x**2 + self.y**2 + self.z**2) def __add__(self, right): # adds a vector return Vector3(self.x + right.x, self.y + right.y, self.z + right.z) def __sub__(self, right): # subtracts a vector return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) def cross(self, right): # computes cross product return Vector3(self.y * right.z - self.z * right.y, right.x * self.z - right.z * self.x, self.x * right.y - self.y * right.x) def __eq__(self, other): return self.x == other.x and self.y == other.y and self.z == other.z def __repr__(self): # string representation return "The Vector (%s, %s, %s)"%(self.x, self.y, self.z) # Quan Nguyen class Vector3: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def length(self): return sqrt(self.x^2 + self.y^2 + self.z^2) def __add__(self, right): return Vector3(self.x+right.x, self.y + right.y, self.z + right.z) def __sub__(self, right): return Vector3(self.x - right.x, self.y - right.y, self.z - right.z) def __repr__(self): return "My Vector3 (%s, %s, %s)"%(self.x, self.y, self.z) q = Vector3(1,5,99) ︡d7770daa-ffc8-4eea-b30e-1ea418aa7b7b︡{"done":true}︡ ︠1617969b-87e8-458f-935a-e8dccb6c067a︠ q+q ︡dcd23ac6-b626-451b-bea5-27b5eba9e90a︡ ︠f3d43ff7-3cc6-493e-bbf2-1cc7e4c1703f︠