[HN Gopher] Learn Physics with Functional Programming
       ___________________________________________________________________
        
       Learn Physics with Functional Programming
        
       Author : privong
       Score  : 157 points
       Date   : 2023-09-05 13:13 UTC (9 hours ago)
        
 (HTM) web link (nostarch.com)
 (TXT) w3m dump (nostarch.com)
        
       | ogogmad wrote:
       | Expressing physics with more rigorous mathematical or
       | computational formalism might be good: For instance, a force is a
       | triple (sending object, receiving object, force vector). Newton's
       | third law says that whenever there is a force (S,R,F), then there
       | is another force (R,S,-F). Also, this makes clear that Newton's
       | 2nd law is _not_ a definition of force, because it misses out on
       | two of the three components of a force triple (-- by the way, is
       | there a formal term for what I 'm calling a force triple?)
        
         | lagrange77 wrote:
         | Yes and it could help with a shortcoming of traditional
         | representations of physical phenomena: defining cause and
         | effect.
        
       | daft_pink wrote:
       | I'd love a book like this written in Python. Just feedback. I
       | clicked the link and was about to purchase this. The idea of
       | learning physics sounds great. The idea of spending time learning
       | a new programming language that I'm unlikely to use for anything
       | else sounds terrible.
        
         | wrycoder wrote:
         | You can learn Scheme in two hours. Haskell, not so much. Stick
         | to SICM.
        
         | greenie_beans wrote:
         | learning another programming language that you will never use
         | still has value. it makes you a better python programmer.
        
         | forgotpwd16 wrote:
         | Another win if Python was used will be showing how functional
         | programming is done in Python. That said I'm fine with it being
         | based on Haskell. Presenting a specific programming language
         | doesn't seem to be the main point. Also someone can follow book
         | with Python and typing/mypy as exercise.
        
         | tobinfricke wrote:
         | I had the opposite reaction. I already know physics, but I want
         | to learn Haskell.
        
           | daft_pink wrote:
           | Maybe I will take the plunge. Is there any real value to
           | learning Haskell? There are so many different things I'd love
           | to study.
        
             | nequo wrote:
             | This is a very valid consideration.
             | 
             | At the least, Haskell is much faster than Python, the
             | language itself is not that complicated (learning a whole
             | new ecosystem is a different story), FP might lend itself
             | better to translating physics into code, and some elements
             | of it carry over to other languages, for example, type
             | classes and Rust's traits.
        
               | petemir wrote:
               | > and some elements of it carry over to other languages,
               | for example, type classes and Rust's traits.
               | 
               | Add react to the list
        
               | acchow wrote:
               | > the language itself is not that complicated
               | 
               | I find Haskell quite complicated, involving abstract
               | concepts you would likely not consciously encounter in
               | other languages (lenses, monad transformers, free monads,
               | etc).
        
               | nequo wrote:
               | Lenses and monad transformers are libraries, not part of
               | the language. It doesn't look like the book uses them, so
               | you don't need to understand them in order to follow it:
               | 
               | https://nostarch.com/learn-physics-functional-programming
        
               | [deleted]
        
             | michaelsbradley wrote:
             | [flagged]
        
             | weatherlight wrote:
             | I get your reluctance to dive into a new language just for
             | one project. But here's the deal: Haskell's strong type
             | system and emphasis on pure functions can actually make you
             | a better programmer in any language. Its lazy evaluation is
             | a unique feature that optimizes performance, and it's built
             | with concurrency in mind. Plus, in Haskell, you really
             | can't cheat your way out of functional programming. The
             | language enforces it, so the code you write will be
             | functional, unlike in Python where you can mix paradigms.
             | Even if you're a Python devotee, the principles you'll
             | learn from Haskell can offer a new lens for problem-
             | solving. It's not just about learning a new language; it's
             | about broadening your skill set.
        
             | 7thaccount wrote:
             | There's always some value in learning a new paradigm, but
             | Haskell always felt less useful to me than learning other
             | tools and esoteric technologies. That's a personal belief
             | though and it's subjective. Some people love Haskell and
             | get a lot of power from it.
             | 
             | Python mostly hits an optimal point for my needs though and
             | I didn't have to read 50 articles trying to explain what a
             | Monad was. Scripting languages just click a lot better for
             | me (less ceremony). There are some weird languages I like
             | such as APL. APL is also very mathy, but there's
             | essentially no ceremony (just some funny symbols that are
             | easy enough to learn).
        
               | chowells wrote:
               | Ah. You found Haskell hard because you were trying to
               | learn it out of order. Demanding to understand what a
               | monad is before learning how higher-order functions and
               | higher-kinded polymorphism work in Haskell is like
               | demanding to know what an Iterator is before learning
               | what a variable is in Java.
               | 
               | The thing about "Monad" is that it's an absolutely
               | trivial interface. You'll know you actually understand it
               | properly when you go "oh, that's all." Is it hard to
               | describe? Not when you understand the underlying
               | concepts. But if you demand to skip them, you're only
               | going to confuse yourself.
        
               | babuloseo wrote:
               | https://bartoszmilewski.com/2021/02/16/functorio/
        
       | alex_lav wrote:
       | How much of either do I need to know to find value out of this
       | book?
        
       | archarios wrote:
       | Calculus makes a lot more sense to me as code so this seems
       | promising :D
        
       | MissingAFew wrote:
       | Funny, I was thinking of doing just this exact thing to learn
       | haskell.
        
       | tobinfricke wrote:
       | Does this implement symbolic algebra/calculus, or is it entirely
       | numerical?
        
         | tikhonj wrote:
         | No idea if this book does it, but this seems like a perfect fit
         | for automatic differentiation. You can get a simple
         | implementation of automatic differentiation for one dimension
         | in a small amount of surprisingly clear code, without needing
         | to pull in any dependencies or extraneous concepts.
        
           | tobinfricke wrote:
           | That is along the lines of the motivation for my question.
           | Automatic differentiation is such a natural fit with
           | Haskell...
        
           | lagrange77 wrote:
           | > this seems like a perfect fit for automatic
           | differentiation.
           | 
           | Why?
        
             | tikhonj wrote:
             | It would let you represent physical systems as normal
             | Haskell functions and get accurate derivatives "for free".
             | At least in the one-dimensional case it's probably
             | _simpler_ than numeric differentiation while also letting
             | you worry far less about accuracy and stability.
             | 
             | I've never worked this through to a full conclusion, but
             | you could even write it in a way that would let you get
             | symbolic differentiation out of it too.
        
               | lagrange77 wrote:
               | Makes sense. I initially thought, that automatic
               | differentiation would be mostly useful for long
               | derivative chains, but of course even for single
               | derivatives it does have advantages.
        
       | Pet_Ant wrote:
       | I wish the title said "Haskell" instead of "Function
       | Programming". I mean the two aren't interchangeable. Was actually
       | hoping for Clojure personally.
        
         | [deleted]
        
       | galp76 wrote:
       | I have been looking for a excuse to try again to learn Haskell.
       | Also looking for some interesting book to learn material in order
       | to try to get my son interested in science. Thanks for this.
        
       | footy wrote:
       | This is an incredibly cool concept and I would have loved to have
       | this book when I was in undergrad (for Physics).
        
       | [deleted]
        
       | [deleted]
        
       | bumbledraven wrote:
       | I would like to see Learn Physics with APL.
        
       | latenightcoding wrote:
       | Structure and Interpretation of Classical Mechanics published in
       | 2001 uses Scheme (There is a second edition released more
       | recently)
        
         | hcarvalhoalves wrote:
         | See also: https://nextjournal.com/try/samritchie/sicmutils
        
       | bowsamic wrote:
       | I feel like the screenshots don't make the book seem particularly
       | novel or interesting
        
       | noelwelsh wrote:
       | Sounds similar to https://tgvaughan.github.io/sicm/toc.html
       | Should be fun if you're physics inclined.
        
         | tobinfricke wrote:
         | Official version: https://mitp-content-
         | server.mit.edu/books/content/sectbyfn/b...
         | 
         | I'd really like to see a "spiritual successor" to Structure and
         | Interpretation of Classical Mechanics--something that can take
         | off and achieve a life of its own.
         | 
         | SICM is open-source, and many people have implemented their own
         | versions of parts of it, but I would love to see a vibrant and
         | active community develop around such a beautiful computer
         | algebra / computer-physics system.
         | 
         | SICM goes far beyond simple Newtonian mechanics, implementing
         | calculus, Lagrangian and Hamiltonian mechanics, and
         | differential geometry, and probably a whole lot more that you
         | just have to spelunk into the source code to discover.
         | 
         | (Here's a book about the differential geometry implementation
         | in scmutils: https://mitpress.mit.edu/9780262019347/functional-
         | differenti... as seen in HN:
         | https://news.ycombinator.com/item?id=7884551 )
        
           | Y_Y wrote:
           | Really we need somehow to rejuvenate scmutils, either with a
           | faithful port to a modern language, or a spiritual successor
           | of similar calibre.
        
             | sritchie wrote:
             | It's fully rejuvenated in Clojure as "Emmy", with Sussman's
             | support and a bunch of 2D and 3D graphing extensions. See
             | Emmy-Viewers: https://emmy-viewers.mentat.org/ and Emmy:
             | https://emmy.mentat.org/
             | 
             | Thanks to https://2.maria.cloud, everything in SICM and FDG
             | works in the browser as well:
             | https://2.maria.cloud/gist/d3c76ee5e9eaf6b3367949f43873e8b2
        
               | tobinfricke wrote:
               | Wow, thanks for this!
        
               | sritchie wrote:
               | Of course! And referencing your other comment, during the
               | ~2 year period I've been working on Emmy (on top of work
               | by Colin Smith), I was keen to make the implementation
               | more accessible and well-documented than the original.
               | 
               | There's still not a great map of the project (from
               | primitives to general relativity), but many of the
               | namespaces are written as literate programming
               | explorations: https://emmy.mentat.org/#explore-the-
               | project
               | 
               | Here's the automatic differentiation
               | implementation/essay, for example: https://sritchie.githu
               | b.io/emmy/src/emmy/differential.html
               | 
               | A rough sketch of the tower is:
               | 
               | - `emmy.value` and `emmy.generic` implement the
               | extensible generic operations
               | 
               | - `emmy.ratio`, `emmy.complex` and `emmy.numbers` fleshes
               | out the numeric tower
               | 
               | - `emmy.expression` and `emmy.abstract.number` add
               | support for symbolic literals
               | 
               | Next we need an algebraic simplifier...
               | 
               | - `emmy.pattern.{match,rule,syntax} give us a pattern
               | matching language
               | 
               | - `emmy.simplify.rules` adds a ton of simplification
               | rules, out of which
               | 
               | - `emmy.simplify` builds a simplification engine
               | 
               | Actually the simplifier has three parts... the first two
               | start in `emmy.rational-function` and `emmy.polynomial`
               | and involve converting an expression into either a
               | polynomial or a rational function and then back out,
               | putting them into "canonical form" in the process. That
               | will send you down the rabbit hole of polynomial GCD
               | etc...
               | 
               | And on and on! I'm happy to facilitate any code reading
               | journey you go on or chat about Emmy or the original
               | scmutils, feel free to write at sam [at] mentat.org, or
               | else visit the Discord I run for the project at
               | https://discord.gg/hsRBqGEeQ4.
        
           | marshallward wrote:
           | I know someone who took that course. They did not have fond
           | memories of it.
           | 
           | My impression is that it can be a very frustrating way to
           | learn mechanics if you don't have much interest in functional
           | programming.
        
             | tobinfricke wrote:
             | It's not for everyone.
             | 
             | I cut my teeth on SICP before going into physics, so I was
             | perhaps the exact target audience.
             | 
             | I also see Scheme as an improvement over its successor
             | languages.
        
           | sritchie wrote:
           | What would you build / create / write if you had a web-
           | enabled build of SICM (well, scmutils I guess) in hand? I'd
           | love to hear more about your thoughts on how to build a
           | community around these tools and ideas.
        
             | tobinfricke wrote:
             | I would love to explore statistical mechanics, quantum
             | field theory, and/or general relativity through a similar
             | lens.
             | 
             | But I am also quite interested in learning more about the
             | "under the hood" workings and software craftsmanship of
             | scmutils. The textbook _uses_ scmutils to explore classical
             | mechanics.
             | 
             | But it does not delve into the implementation details of
             | scmutils itself, which interest me.
        
           | lrc wrote:
           | Have a look at Emmy: https://emmy.mentat.org
        
         | cipherself wrote:
         | SICM Is more like a 2nd physics course as it uses the
         | Lagrangian interpretation of classical mechanics and starts
         | with it whereas OP seems to start from the basics.
        
       ___________________________________________________________________
       (page generated 2023-09-05 23:00 UTC)