Skip to content

Commit

Permalink
FIX: create single branch with PosArg class (#14)
Browse files Browse the repository at this point in the history
* MAINT: remove Colab's `cellView` metadata
  • Loading branch information
redeboer authored Sep 13, 2023
1 parent 0eb0a8d commit 32f5910
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repos:
- |
cell.attachments
cell.id
cell.metadata.cellView
cell.metadata.code_folding
cell.metadata.colab
cell.metadata.editable
Expand Down
45 changes: 28 additions & 17 deletions docs/lecture17.ipynb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -294,31 +294,40 @@
" is_commutative = True\n",
" is_real = False\n",
"\n",
" def __new__(cls, z, sign, **hints) -> SignedSqrt:\n",
" return create_expression(cls, z, sign, **hints)\n",
" def __new__(cls, z, **hints) -> SignedSqrt:\n",
" return create_expression(cls, z, **hints)\n",
"\n",
" def evaluate(self) -> sp.Expr:\n",
" z, sign = self.args\n",
" return sp.sqrt(abs(z)) * sp.exp(sign * sp.I * sp.arg(z) / 2)\n",
" z = self.args[0]\n",
" return sp.sqrt(abs(z)) * sp.exp(sp.I * PosArg(z) / 2)\n",
"\n",
" def _latex(self, printer, *args) -> str:\n",
" z = printer._print(self.args[0])\n",
" sign = _render_sign(printer, self.args[1])\n",
" return Rf\"\\sqrt[{sign}]{{{z}}}\"\n",
" return Rf\"\\sqrt[+]{{{z}}}\"\n",
"\n",
"\n",
"def _render_sign(printer, sign) -> str:\n",
" if sign == +1:\n",
" return \"+\"\n",
" if sign == -1:\n",
" return \"-\"\n",
" return printer._print(sign)\n",
"@implement_doit_method\n",
"class PosArg(UnevaluatedExpression):\n",
" is_commutative = True\n",
"\n",
" def __new__(cls, z, **hints) -> SignedSqrt:\n",
" return create_expression(cls, z, **hints)\n",
"\n",
" def evaluate(self) -> sp.Expr:\n",
" z = self.args[0]\n",
" arg = sp.arg(z)\n",
" return sp.Piecewise(\n",
" (arg + 2 * sp.pi, sp.im(z) < 0),\n",
" (arg, True),\n",
" )\n",
"\n",
" def _latex(self, printer, *args) -> str:\n",
" z = printer._print(self.args[0])\n",
" return Rf\"\\arg^+\\left({z}\\right)\"\n",
"\n",
"\n",
"z = sp.Symbol(\"z\", complex=True)\n",
"neg_sqrt = SignedSqrt(z, sign=-1)\n",
"pos_sqrt = SignedSqrt(z, sign=+1)\n",
"Math(aslatex({e: e.doit() for e in [neg_sqrt, pos_sqrt]}))"
"Math(aslatex({e: e.evaluate() for e in [SignedSqrt(z), PosArg(z)]}))"
]
},
{
Expand All @@ -337,8 +346,10 @@
"outputs": [],
"source": [
"plot_riemann_surfaces(\n",
" funcs=[sp.lambdify(z, pos_sqrt.doit())],\n",
" funcs=[sp.lambdify(z, SignedSqrt(z).doit())],\n",
" func_unicode=\"⁺√z\",\n",
" mask=lambda x, y: (np.abs(y) < 1e-5) & (x > 0),\n",
" resolution=(30, 301),\n",
")"
]
},
Expand Down Expand Up @@ -393,7 +404,7 @@
"\n",
" def evaluate(self) -> sp.Expr:\n",
" s, m = self.args\n",
" return sp.sqrt(1 - 4 * m**2 / s)\n",
" return SignedSqrt(1 - 4 * m**2 / s)\n",
"\n",
" def _latex(self, printer, *args) -> str:\n",
" s = printer._print(self.args[0])\n",
Expand Down

0 comments on commit 32f5910

Please sign in to comment.