A Mathematica interpreter in Typescript

(spakhm.com)

121 points | by lispybanana 4 days ago

7 comments

  • coffeemug 18 hours ago
    Hey, that's my project!

    I just wrote another post about perf benchmarking it against Mathematica proper-- https://www.spakhm.com/ts-wolfram-bench. Really surprised by the result, on the workload I tested Mathematica is only 2x faster than my barely optimized interpreter. A testament to the V8 engine, I didn't quite realize how ridiculously good V8 is until running this benchmark.

    • anandijain 7 hours ago
      that's awesome, I wrote a Mathematica clone too! I think it's one of the most rewarding projects I've done.

      The amazing thing is how Mathematica starts working with just a few simple ideas, evaluator, backtracking pattern matcher, and a REPL.

      were you able to implement Condition? how advanced is the pattern matcher? I got stuck after doing Blank BlankSequence and BlankNullSequence

      https://github.com/anandijain/cas3.rs https://github.com/anandijain/cas8.rs

      • coffeemug 7 hours ago
        Very cool! Pattern matcher isn't very advanced, I only spent maybe two days on it. Only supports Blank, Pattern, and PatternTest. (Also doesn't handle Flat, so for example in Mathematica `Times[x_, y_]` will match `Times[a, b, c]`, but it currently doesn't in ts-wolfram.)

        I'm new to Mathematica, so didn't know about `Condition` (thanks for mentioning it)

        It was a little hard to get going, the parsing stage usually stops me because I never learned a good parser generator well enough to just start writing code. But once I got ts-parsec working, the rest was fairly easy. I think I got `D` to work within like two days. Was also very surprised how much you can do with so little!

        Shoot me an email at [email protected], let's chat more!

  • Someone 14 hours ago
    > Certainly Mathematica’s term rewrite loop is optimized to death, and I only spent an hour or two making the most basic optimization

    I suspect this benchmarks begint libraries more than term rewriting. A way to test that may be:

      bif[1] := 0
      bif[2] := 0
      bif[n_] := bif[n-2] + bif[n-1]
    
      Timing[Do[bif[15], 1000]]
    
    You can check that neither tool is smart enough to solve that to

      bif[n_] := 0
    
    by comparing running times for different large limits.
    • bonzini 10 hours ago
      Fib(15) is just 610, so no big int involved.

      As a quick double-check, fib(n)< 2^(n-1) and 2^14 is 16384.

      • groos 8 hours ago
        This is not the Fibonoacci sequence because the first two terms are 0 and hence the entire sequence is 0.
        • bonzini 6 hours ago
          Indeed, the Fibonacci sequence is in the original benchmark and Fib(15) is not benchmarking big integer performance. It should have the same characteristics as the always-zero function.
          • spinningslate 4 hours ago
            I think gp's point is that bif[n_] == 0 for all n_. A 'smart' optimser would recognise this and so the time to compute would be (a) constant irrespective of the value of n_, and (b) instantaneous because the function call can be re-rewritten as the constant value 0.
  • lacker 8 hours ago
    I'm curious what people use Mathematica for. I know some people who use it academically. Does anyone use it in a non-academic professional context? I'd be interested to hear from anyone who does, what they are using it for.
    • jacoblambda 6 hours ago
      I have a non-commercial license that I use personally. The appeal of mathematica is that it has a library already built into it for just about everything which lets you quickly prototype and reason about fairly complex cross domain problems and then easily visualize them.

      Mathematica unfortunately has a lot of warts and rough edges clustered in specific fields (low level cryptography and binary manipulation heavy algorithms) which makes working with it a pain in the ass if you touch those fields regularly but outside of those specific fields it's fantastic.

      And I can claim praise doubly so if you get the System Modeler/Modelica license as well. With those two together you can model a project using analog circuits, digital circuits, software running on hardware/RTOS, pneumatics, hydraulics, CFD, multibody physics, etc all running in a unified environment. And then of course from a high level you can tune parameters that drive all the parts of your model and test out swapping in different parts or running under different conditions.

      You can do all the stuff that Mathematica and System Modeler are good for with other software packages cobbled together but that takes a lot more time to do and the integrations tend to not be nearly as clean. The only other product package on the market that compares would be MathWork's MATLAB + Simulink. The main difference being that Mathwork's products are more comprehensive in what all they cover but they tend to be less pleasant to work with.

      • buescher 3 hours ago
        You can do almost all of what you might do in Simulink or System Modeler in the VHDL-AMS simulator that comes with the Siemens (formerly Mentor) pcb design and layout packages, plus you can directly use SPICE models, VHDL models, and linear s-parameter models. You won’t get the control design tools of Simulink or the filter synthesis tools of a serious RF design package, though. There is a freely accessible web version here, thought, for the curious:

        https://eda.sw.siemens.com/en-US/pcb/partquest/explore/

        • jacoblambda 1 hour ago
          Yep. You can also do most of it or even all of it with a number of other tools as well.

          IMHO the main sell is that you can do "all of the above" regardless of what domains you are working with without having to switch tools or negotiate different formats, etc.

          So a systems engineer can model, simulate, and build a test framework for a project for all the bits (including the inside of the hardware and the physical world around it) without changing tools.

    • derjames 7 hours ago
      In the company I work for, I was using Mathematica as a general scripting language, for designing algorithms, plotting data in 2D/3D, photo/video editing , and some occasional algebraic manipulations. Sadly the company did not renew the license because they needed to cut costs. They asked me to port all my tooling to other languages and tools: now I have a mixture of python, matplotlib/gnuplot, ruby, maxima, ffmpeg, imagemagik and octave. So far good but I preferred to have everything under one GUI/interface and Mathematica was giving me that.
    • porcoda 8 hours ago
      I use it regularly. Mostly for visualization and prototyping ideas. I’ve tried over the years to replace it with Python/Sage, but the giant library of builtin functions always pulls me back. Plus, the lisp-ish language I prefer over Python. The visualization capabilities are quite nice too - not just data or function plots, but the ability to construct relatively complex graphics programmatically is quite useful. On a recent project where I needed to build some data structures for spatial processing in 3d, I was able to build a nice 3d visualization to help me debug my code by visualizing the calculations I was doing. I also really like the backwards compatibility of notebooks: I have mathematica notebooks dating back to the 90s, many of which still work fine (modulo some visual ugliness due to changes they made to layout and presentation over the decades). Few other tools have that longevity.
  • sillying 10 hours ago
    Mathematica has thousand of functions, this seems to be just an algorithm for defining and using rules like that in mathematica.
  • samlittlewood 17 hours ago
    So tempted to try this myself in another language - just so I can call in ‘tungsten’.
    • stared 13 hours ago
      Tungsten oxide - for one in Rust.
  • peter_d_sherman 5 hours ago
    >"Writing a toy differentiator turns out to be shockingly easy. It’s a near verbatim transcription of differentiation rules from any calculus textbook:

    D[_?NumberQ, x_Symbol] = 0;

    D[x_, x_Symbol] = 1;

    D[Times[expr1_, expr2_], x_Symbol] = D[expr1, x] expr2 + D[expr2, x] expr1;

    D[Plus[expr1_, expr2_], x_Symbol] = D[expr1, x] + D[expr2, x];

    D[Sin[x_], x_Symbol] = Cos[x];

    D[Cos[x_], x_Symbol] = -Sin[x];

    D[f_Symbol[expr_], x_Symbol] := (D[f[x], x] /. x -> expr) * D[expr, x];

    D[Power[expr_, p_Integer], x_Symbol] := p expr^(p - 1) * D[expr, x]; "

    Absolutely brilliant! And simple! And terse! And brilliant!

  • Y_Y 13 hours ago
    I applaud calling the language Mathematica, despite Stephen Mathematica's attempts to rename it a couple of years ago.

    See also: https://mathics.org/

    It's a more mature and complete reimplementation of Mathematica, though still miles behind the original.

    • jhbadger 10 hours ago
      Unfortunately when Wolfram hired Mathics' lead developer, the project pretty much stopped.
      • Y_Y 8 hours ago
        When was that?

        https://github.com/Mathics3/mathics-core/graphs/contributors

        The project seems to have had a decent level of contributions for the last couple of years.

        • jhbadger 3 hours ago
          It seems to have been restarted, which is good. But for a while (look at the the 2017-2020 gap) there was basically nothing done to it and for a while it was impossible to run on current versions of Python.
    • lupire 12 hours ago
      The name of the program is ts-wolfram.
      • Y_Y 8 hours ago
        Probably just one of those cute auto generated names like "interestingalpacagoose"
    • gklitz 7 hours ago
      Mathematica is the application. Wolframlang is the language. They are both extremely impressive each in their way, but they aren the same.