Skip to content

Commit

Permalink
docs: rewrite double pendulum example using vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumJHays committed Oct 5, 2022
1 parent 4f12990 commit 787a6e5
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 324 deletions.
141 changes: 72 additions & 69 deletions examples/braking_deceleration.ipynb
Original file line number Diff line number Diff line change
@@ -1,69 +1,72 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution( t = 1.1176 seconds )\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1397}{1250} \\hspace{1.25em} \\text{s}$"
],
"text/plain": [
"1.1176 seconds"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from mathpad import *\n",
"\n",
"t = \"t\" * seconds # \"t seconds\"\n",
"\n",
"velocity = 5 * miles / hour # \"5 miles/hour\"\n",
"\n",
"deceleration = 2 * meters / second ** 2 # \"10 meters/second**2\"\n",
"\n",
"solution = solve(t == velocity / deceleration, t) # \"Solution(t = 1.1176 seconds)\"\n",
"print(solution)\n",
"\n",
"solution[t]"
]
}
],
"metadata": {
"interpreter": {
"hash": "bec35ff8d3ceac10428ff23ff3d495acaf5c2f0370f6a03008458d4f010ac403"
},
"kernelspec": {
"display_name": "Python 3.9.1 64-bit ('mathpad-tPciuzSw-py3.9': poetry)",
"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.1"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution( t = 1.1176 seconds )\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{1397}{1250} \\hspace{1.25em} \\text{s}$"
],
"text/plain": [
"1.1176 seconds"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from mathpad import *\n",
"\n",
"t = \"t\" * seconds # \"t seconds\"\n",
"\n",
"velocity = 5 * miles / hour # \"5 miles/hour\"\n",
"\n",
"deceleration = 2 * meters / second ** 2 # \"10 meters/second**2\"\n",
"\n",
"solution = solve(t == velocity / deceleration, t) # \"Solution(t = 1.1176 seconds)\"\n",
"print(solution)\n",
"\n",
"solution[t]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 ('mathpad-IMbbmyaw-py3.8')",
"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.8.10"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "c68f6f75b9b5074b455bff57bf843aee0a1a4f29736bbb117f262860b9efa145"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
18 changes: 8 additions & 10 deletions examples/cart_spring_cargo.ipynb

Large diffs are not rendered by default.

144 changes: 10 additions & 134 deletions examples/cart_spring_pendulum.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/double_pendulum.ipynb

Large diffs are not rendered by default.

Binary file modified examples/imgs/cart_spring_pendulum_simulation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/imgs/double_pendulum_simulation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mathpad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from mathpad.functions import piecewise, sqrt
from mathpad.calculus import diff, integral
from mathpad.trigonometry import cos, sin, tan, magnitude, hypotenuse
from mathpad.trigonometry import cos, sin, tan
from mathpad.vector_space import VectorSpace, R2, R3
from mathpad.simulate_dynamic_system import simulate_dynamic_system

Expand Down
17 changes: 2 additions & 15 deletions mathpad/trigonometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ def cos(x: Q[Angle]) -> Dimensionless:
def tan(x: Q[Angle]) -> Dimensionless:
return Dimensionless(1, sympy.tan(x.val)) # type: ignore


def magnitude(*xs: Q[ValT]) -> ValT:
"x's are lengths in orthogonal directions. ie (i, j k)"
c_2 = 0
for x in xs:
c_2 += x ** 2
return sqrt(c_2)


def hypotenuse(a: Q[Length], b: Q[Length]) -> Length:
return cartesian_distance(a, b)


# TODO: add more trig functions


Expand All @@ -37,7 +24,7 @@ def sine_rule(
beta: Q[Angle], # internal angle opposite to side b
) -> Equation:
"Relates lengths of two sides of any triangle the internal angle opposite"
return a == b * sin(alpha) / sin(beta)
return a == b * sin(alpha) / sin(beta) # type: ignore


def cosine_rule(
Expand All @@ -46,4 +33,4 @@ def cosine_rule(
c: Q[Length], # length of side c
C: Q[Angle], # internal angle opposite to side c
) -> Equation:
return c ** 2 == a ** 2 + b ** 2 - 2 * a * b * cos(C)
return c ** 2 == a ** 2 + b ** 2 - 2 * a * b * cos(C) # type: ignore
92 changes: 0 additions & 92 deletions mathpad/vec3.py

This file was deleted.

17 changes: 15 additions & 2 deletions mathpad/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,20 @@ def __neg__(self) -> Self:
return self.__class__(self.space, -self.val) # type: ignore

def __abs__(self):
from mathpad.trigonometry import magnitude
return magnitude(*self) # type: ignore
"""
Returns the norm / magnitude of this vector
Raises:
ValueError: if the vector does not have uniform dimensionality (each base_unit in the vector space must be equivalent)
"""
from mathpad.functions import sqrt

try:
norm_squared = sum(val ** 2 for val in self)
except SumDimensionsMismatch:
raise ValueError("Cannot take the norm of a vector with non-uniform units")

return sqrt(norm_squared)

def __mul__(self, other: Q[Val]):
return Vec(
Expand Down Expand Up @@ -199,6 +211,7 @@ def __repr__(self) -> str:

# TODO: extend this to higher dimensions and other bases somehow
# PS: technically cross product is only defined for 3D and 7D, but the concept of orthogonal basis vectors is more general
# TODO: should this only be defined for R3?
def cross(self, other: Self) -> 'Vec':
"""
Cross product of two vectors.
Expand Down

1 comment on commit 787a6e5

@petercorke
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see this. The reference frame thing is cute as well.

Please sign in to comment.