November 2022: Scilab vs Mathics

As a Master's student (hooray) in a heavily mathematical major, you'll have trouble avoiding mathematical programming languages. Honestly, a lot of those tasks can be done with Python, which in theory should make me happy. I sometimes don't feel like Python gets me the speeds I would like. Now, whether that is because I'm impatient or whether Python is actually too slow for the kind of calculations I'm trying to run, I don't know. However, there have always been two large mathematical programming suites bounding around. One is Matlab, which I would require a license for. It doesn't help that a year's subscription to matlab is in the high triple digits, and universities have a very limited amount of student licenses to give out, so that's out. The other option is Mathematica, the language running behind the Wolframalpha computation service. Mathematica is excellent, but it also requires a license, unless you own a raspberry pi, which comes with Mathematica preinstalled. I am however often on the road, and opening a raspi server at home to ssh into feels like inviting disaster. However, somebody pointed me towards Scilab, an open source mathematical programming suite, that's been in development since 1994. That means I could technically just carry it with me everywhere in my computer without issue. That's perfect, in a way. Learning my way around that should be interesting and not to mention useful, at least to the point where I can get a data-array output to plug into a python plotter. At the same time, there is another open-source project out there that uses the Mathematica syntax and brands itself as an open-source alternative to Mathematica specifically, called Mathics. It needs a little more to run, and the installation is significantly less fun than Scilab's is, but it uses the Mathematica syntax, which I've found relatively parseable, if a little confusing to remember. What I've always liked about Mathematica is it's relatively fast matrix calculations, and I've started learning General Relativity. General Relativity has something called the Christoffel-Symbol, which, to keep things short, is a part of a certain type of derivative. For people interested, and familiar with tensor calculus, this here is the definition:

The trouble is, that it's a tensor, so the computation of it is tedious once we get into the 3-index range. Maybe I can have these two softwares face off to see, which of these can faster get me to automate the computation of the Christoffel-Symbol, and maybe the Riemann-Tensor, depending on how much time we have left.

Round 1: Installation & Interface

Let's get started then. I did a from-source installation for Scilab, because the GUI runs with java, which introduces all the usual java issues, if you plan to indeed run more than one program on it. After the installation though, things went relatively smoothy. Scilab has a pdf to get newbies like me started. It's syntax is pretty clear, I think, using square brackets [...] to instance variables and square brackets (...) to pass variables to functions. Writing functions as an experience is also relatively straight-forward. It feels a little like the writing bash functions, though not as volatile. Or maybe that's just because Scilab actually throws errors and doesn't just toss it's operations into the void. I don't love that I have to use a coding suite, but it runs smoothly, for what it's worth.

Whoo, boy. So Mathics looks like it could be good from the list of features on its website. If you get it installed, that is. Installations are varied, I decided to go with pip, which is an issue of it's own, and probably mostly a direct consequence of my treating my machine like a playground rather than a work-space. The issue for me was, that the source-installation from git just didn't want to work and wasn't documented anywhere I could find, and the package manager method suggested was outdated. That left pip, which at least settles the installation process for me, but not all dependencies, which I had to fix by hand again. Anyway, the main interface, as far as I can tell, is the Django browser interface. Once the installation is done, getting to that interface is pretty straight-forward. There's a dedicated server command included in the package, which... well, it uses the 8000 port, which is a choice, but fine. I'm not going to nitpick. Like I said, I have trouble remembering Mathematica syntax, but for all intents and purposes, the Mathics interface makes things pretty easy. It's also nice to be able to zoom, so not everything is ant-script.

Round 2: Derivatives

I'll work from the generalized formula for the Christoffel-symbol, which looks like this:

Easy, right? It's technically only matrix (or rather tensor) calculus, just referencing the coordinate indices directly with those Greek letters, but there's a hitch. That curly thing in front of the gs is a derivative (contravariant) with respect to whatever coordinate is set to the index. Mathics knows what a derivative is - it says so in the screenshot. Scilab has trouble with that.

Scilab's derivative functions return a derivative at some point p which complicates things a little. The Christoffel-symbol is going to be part of the covariant derivative in curved spacetime, so we'd like to keep the variable until it's relevant to insert a concrete value, which it rarely is, if I'm honest. There are other ways of forming a derivative, that is finding computing it by hand, but it seems like an oversight not to include it. I assume Scilab only has numerical computation methods, which is why it prefers to deal with numbers.

This makes a clear point for Mathics. Now, I don't think there is any easy way to deal with tensor calculus in any algebraic programming language, so that's where coding logic will factor in.

Round 3: Ease of Coding

Generally, we need to do some basic matrix operations. For one, since the definition features both the covariant and contravariant metric tensor g, we'll have to invert a matrix. Mathics gives you that for free as well. That's not how you generally get a contravariant tensor from a covariant one, but the metric tensor is convenient like that. I made an executive decision here and assumed that we'd get the covariant tensor as an argument and derive the contravariant one. Switching them around isn't much of an effort though.

What's not quite where I'd like it to be is the lack of function options in Mathics, as well as handling strings.

gcov = {{-1,0,0}, {0,1,0}, {0,0,x^2}};
vararray = Association[<|1 -> x, 2 -> y, 3 -> z|>];
chris = 0

gcot = Inverse[gcov]


For [a = 1, a <= Length[vararray], a++,
  For [b = 1, b <= Length[vararray], b++,
    For [c = 1, c <= Length[vararray], c++,
      chris = 0
      For [d = 1, d <= Length[vararray], d++,
        chris = chris + 1/2 gcot[[a,d]] (D[gcov[[d,b]], vararray[c]] + D[gcov[[d,c]], vararray[b]] - D[gcov[[b,c]], vararray[d]])
      ]
      Print[Subscript[Superscript[ToUpperCase["\[Gamma]"], vararray[a]], vararray[b], vararray[c]]]
      Print[chris]
    ]
  ]
]

Feel free to use the above code or the Mathics Input Hash below:

#cXVlcmllcz1nY292JTIwJTNEJTIwJTdCJTdCLTElMkMwJTJDMCU3RCUyQyUyMCU3QjAlMkMxJTJDMCU3RCUyQyUyMCU3QjAlMkMwJTJDeCU1RTIlN0QlN0QlM0IlMEF2YXJhcnJheSUyMCUzRCUyMEFzc29jaWF0aW9uJTVCJTNDJTdDMSUyMC0lM0UlMjB4JTJDJTIwMiUyMC0lM0UlMjB5JTJDJTIwMyUyMC0lM0UlMjB6JTdDJTNFJTVEJTNCJTBBY2hyaXMlMjAlM0QlMjAwJTBBJTBBZ2NvdCUyMCUzRCUyMEludmVyc2UlNUJnY292JTVEJnF1ZXJpZXM9Rm9yJTIwJTVCYSUyMCUzRCUyMDElMkMlMjBhJTIwJTNDJTNEJTIwTGVuZ3RoJTVCdmFyYXJyYXklNUQlMkMlMjBhJTJCJTJCJTJDJTBBJTIwJTIwRm9yJTIwJTVCYiUyMCUzRCUyMDElMkMlMjBiJTIwJTNDJTNEJTIwTGVuZ3RoJTVCdmFyYXJyYXklNUQlMkMlMjBiJTJCJTJCJTJDJTBBJTIwJTIwJTIwJTIwRm9yJTIwJTVCYyUyMCUzRCUyMDElMkMlMjBjJTIwJTNDJTNEJTIwTGVuZ3RoJTVCdmFyYXJyYXklNUQlMkMlMjBjJTJCJTJCJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwY2hyaXMlMjAlM0QlMjAwJTBBJTIwJTIwJTIwJTIwJTIwJTIwRm9yJTIwJTVCZCUyMCUzRCUyMDElMkMlMjBkJTIwJTNDJTNEJTIwTGVuZ3RoJTVCdmFyYXJyYXklNUQlMkMlMjBkJTJCJTJCJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY2hyaXMlMjAlM0QlMjBjaHJpcyUyMCUyQiUyMDElMkYyJTIwZ2NvdCU1QiU1QmElMkNkJTVEJTVEJTIwKEQlNUJnY292JTVCJTVCZCUyQ2IlNUQlNUQlMkMlMjB2YXJhcnJheSU1QmMlNUQlNUQlMjAlMkIlMjBEJTVCZ2NvdiU1QiU1QmQlMkNjJTVEJTVEJTJDJTIwdmFyYXJyYXklNUJiJTVEJTVEJTIwLSUyMEQlNUJnY292JTVCJTVCYiUyQ2MlNUQlNUQlMkMlMjB2YXJhcnJheSU1QmQlNUQlNUQpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwUHJpbnQlNUJTdWJzY3JpcHQlNUJTdXBlcnNjcmlwdCU1QlRvVXBwZXJDYXNlJTVCJTIyJTVDJTVCR2FtbWElNUQlMjIlNUQlMkMlMjB2YXJhcnJheSU1QmElNUQlNUQlMkMlMjB2YXJhcnJheSU1QmIlNUQlMkMlMjB2YXJhcnJheSU1QmMlNUQlNUQlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjBQcmludCU1QmNocmlzJTVEJTBBJTIwJTIwJTIwJTIwJTVEJTBBJTIwJTIwJTVEJTBBJTVE

Round 4: Savefile Format

Call me old-fashioned, but I like to have direct access to all my files. It's an increasing rarity in the days of live-services, that freedom to take a file and poke around in it directly, without any safety precautions. Mathics definitely has its savefile somewhere, but I'd have to do a deep-dive on my system to find it. Scilab just gives me the path on the savefile, which I'd much rather have as an option, if I'm honest. One could treat the input hash as a savefile, though that would require me tinkering with the hash and file operations on my machine, none of which I want to do every time I'm done with a session. Point to Scilab.

Without a reliable method for derivatives, there is no good way to actually compute a Christoffel Symbol. That's of course a terrible deal-breaker for non-numeric considerations. I suppose that makes a lot of sense in a way. Scilab is probably meant more for data analysis, meaning the researcher has probably found its derivative the normal way. It won't make me any happier, considering the amount of different calculations that would leave me with. In the end, I think I prefer the experience of using Scilab, but can do more with Mathics. In my book, that makes Mathics my future weapon of choice when it comes to shortcuts.

Previous
Previous

December 2022: rust(y) Economics

Next
Next

(Surprise) November 2022: NaNoWriMo