{"id":1488,"date":"2021-10-30T14:05:47","date_gmt":"2021-10-30T13:05:47","guid":{"rendered":"https:\/\/www.nobugs.org\/blog\/?p=1488"},"modified":"2021-10-30T14:25:41","modified_gmt":"2021-10-30T13:25:41","slug":"computational-algebra","status":"publish","type":"post","link":"https:\/\/www.nobugs.org\/blog\/archives\/2021\/10\/30\/computational-algebra\/","title":{"rendered":"Computational algebra"},"content":{"rendered":"\n<p>I&#8217;ve been studying electromagnetism recently, and consequently been doing more maths-by-hand. Every time I do this, I think about using computer algebra systems to check my working and help me with calculus. But the only computer algebra system I&#8217;ve used was back at uni (Maple, I think) &#8211; and whilst Mathematica looks whizzy it&#8217;s also quite pricey. So I thought I&#8217;d give a few &#8216;free&#8217; ones a go.<\/p>\n\n\n\n<p>I have a particular &#8216;test&#8217; problem in mind, because I&#8217;ve just worked through it by hand whilst reading <a href=\"https:\/\/www.feynmanlectures.caltech.edu\/II_20.html\">Feynman Vol 2<\/a>. It&#8217;s calculating the Laplacian of a radially symmetric potential &#8211; which involves a nice mixture of partial and total derivatives, chain rules and product rules. Turns out, I do actually remember how to do all this stuff, but having done it by hand it makes a nice concrete example for doing it on computer.<\/p>\n\n\n\n<p>Another motivation is that I hate the imprecise notation that physicists and some mathematician use. What is the derivative of phi(r) with respect to x? My brain says that phi is a unary function with parameter r, so the only thing that can cause it to change is changes in it&#8217;s input r. In physics you have to &#8216;understand&#8217; that this means phi is really a function over R^3 (space) that&#8217;s defined like phi(x,y,z) = g(sqrt(x^2+y^2+z^2)) with the helper g(r)=&#8230; telling you how things change with distance.  And quite often, phi will also quietly become a function of time too. In <a href=\"https:\/\/mitpress.mit.edu\/sites\/default\/files\/titles\/content\/sicm_edition_2\/book.html\">SICM<\/a> they fix this problem by using a scheme-based computer algebra system. Here I&#8217;m trying to do the same thing but using a more mainstream maths package.<\/p>\n\n\n\n<p>I just ran sagemath via docker, rather than worry about &#8216;installing it&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -it sagemath\/sagemath:latest<\/code><\/pre>\n\n\n\n<p>And so we start to tell it about our world &#8230;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><code>%display latex\n\n# We'll use g(r) as our \"how things change with distance\" function, and we'll leave it abstrac\n\ng = function(\"g\")\n\n# Now we can define phi to be a concrete function over all space that delegates to g\n\nphi(x,y,z) = g(sqrt(x^2+y^2+z^2))<\/code>\nphi.differentiate(x,2)\n \n# result\n# x^2*D&#91;0, 0](g)(sqrt(x^2 + y^2 + z^2))\/(x^2 + y^2 + z^2) - x^2*D&#91;0](g)(sqrt(x^2 + y^2 + z^2))\/(x^2 + y^2 + z^2)^(3\/2) + D&#91;0](g)(sqrt(x^2 + y^2 + z^2))\/sqrt(x^2 + y^2 + z^2)<\/pre>\n\n\n\n<p>Assuming that D[0] means &#8216;derivative with respect to 0th arg&#8217; then this is <i>right<\/i> but boy is it ugly!<\/p>\n\n\n\n<p>Specifically, we&#8217;re not giving a short name &#8216;r&#8217; to the expression sqrt(x^2+y^2+z^2). We can try to cajoule sagemath along:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>r = var('r')\nphi.differentiate(x,2).subs({sqrt(x^2+y^2+z^2): r})<\/code><\/pre>\n\n\n\n<p>Sadly that only improves the g(r) expressions, but not the usages in the denominators.<\/p>\n\n\n\n<p>Perhaps we should tell it about r^2 rather than r?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><code>phi.differentiate(x,2).subs({(x^2+y^2+z^2): r^2})<\/code><\/pre>\n\n\n\n<p>This does affect the denominator, but weirdly leaves lots of sqrt(x^2) terms. Odd.<\/p>\n\n\n\n<p id=\"block-21727ba2-8c72-47a7-8586-046c00c7959c\">We can blunder on ahead and complete the Laplacian:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><code>(phi.differentiate(x,2) + phi.differentiate(y,2) + phi.differentiate(z,2)).simplify_full()<\/code><\/pre>\n\n\n\n<p id=\"block-21727ba2-8c72-47a7-8586-046c00c7959c\">.. which yields the right answer, but again in a not-very-simplified form.  Still, this is a pretty impressive feat.  It takes a lots of pencil + paper to do these three second derivatives, chain rules and everything else.  Doing it in one line shows that sagemath is a pretty useful maths assistant!<\/p>\n\n\n\n<p>So far, we&#8217;ve just modelled phi as a three-arg function &#8211; but sagemath actually knows what scalar and vector fields are, and can already do operations on them!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sage.manifolds.operators import *\n<\/code><code>E.&lt;x,y,z> = EuclideanSpace()\ng = function(\"g\")\nphi = E.scalar_field(g(sqrt(x^2+y^2+z^2)), name='phi')\nphi.laplacian().display()<\/code><\/pre>\n\n\n\n<p id=\"block-02bf2bb3-bf08-4518-b705-d6d17e6b6bfa\">&#8230; which gives the same answer as above, but now we&#8217;ve got &#8220;batteries included&#8221; and can calculate div\/grad\/curl and all that.<\/p>\n\n\n\n<p>So this is now good enough to solve electrostatic problems, where the laplacian of the scalar potential is proportional to charge density. For dynamic cases however, we&#8217;ll need a function of x,y,z and also t. Not sure how that&#8217;ll work with the manifold support. Do I need a function from time to scalar fields? A field of time-to-scalar functions? A 4d spacetime structure?<\/p>\n\n\n\n<p>This <a href=\"https:\/\/github.com\/ernestyalumni\/Propulsion\/blob\/master\/EM\/EMsage.ipynb\">page<\/a> is working with E\/B fields using 4d manifolds.  I&#8217;ve done courses on special relativity, so I can handle spacetime but the language of differential geometry (manifolds, 2-forms) is beyond me currently (although Sussman of SICP fame has a <a href=\"http:\/\/web.mit.edu\/wisdom\/www\/AIM-2005-003.pdf\">similarly computational approach to it<\/a>).  Perhaps it&#8217;s better to leave the sage.manifolds alone for now and return to explicit x\/y\/z\/t approach (hey, it was good enough for Maxwell!).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been studying electromagnetism recently, and consequently been doing more maths-by-hand. Every time I do this, I think about using computer algebra systems to check my working and help me with calculus. But the only computer algebra system I&#8217;ve used was back at uni (Maple, I think) &#8211; and whilst Mathematica looks whizzy it&#8217;s also [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1488","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/1488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/comments?post=1488"}],"version-history":[{"count":14,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/1488\/revisions"}],"predecessor-version":[{"id":1504,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/1488\/revisions\/1504"}],"wp:attachment":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/media?parent=1488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/categories?post=1488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/tags?post=1488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}