August 2024 - Pragmatic Mathematica
Learning Mathematica couldn't really be avoided, if I'm honest, but we might as well use the time spent messing around with the thing to explore some of the deeper options. As I'm working with Mathematica notebook, I'm willing to treat it like a Jupyter notebook for now, i.e.: mostly for computation.
The good news is, that most of the things that work for Mathics, will work for Mathematica. One does get used to using the square brackets for function parameters, and defining ranges in whiskers. In the most basic form, Mathematica can be a glorified calculator, and it takes this function very seriously. There is a print statement, but that is only really relevant for formatted strings. Otherwise, Mathematica prints every line that resolves in a number, unless it's explicitly muted using a semicolon at the end of it. This means one can easily fill the screen with garbage on accident. I strongly encourage muting as many lines as possible, since it's often not immediately apparent, what output belongs to what line. The cell lines of the wolfram interpreter are numbered by kernel order, meaning the lines of an input block enter the kernel first, and are numbered accordingly, the lines of the corresponding output cell follow directly. The output line can be associated to its originator by counting the lines in both cells, but that quickly becomes confusing enough to warrant either formatting a string, or muting every line that isn't important.
The most readily important object in Mathematica is the function. It's instantiated as follows:
function_name[x_] := x + 2
and can be called by that name for any relevant variable. However, it usually, when a function is called again, it will be part of a larger expression, rather than an evaluation. Both are straight forward, just be aware that the expression function_name[y] doesn't mean anything. Instead, both function_name[2] and
Integrate[function_name[y], {y, 0, 5}]
are valid uses for the function. The integration variable and range is given in the whisker-brackets. To instead get the Integral without a numerical evaluation, use Integral[] instead.
Since I will display a fair number of graphs, there's some formatting to be learned, which is one of the new additions I had to look up. It's easy, if it's just one Plot, since this can be handled using flags, jist like it can be in matplotplib. For myself, it's also perfectly fine to just color the plots different colors using Plotstyle -> Color, but ideally I'd be able to add descriptions to each curve individually. This is a little bit less readable than good old-fashioned Show.
Ordinarily I would just list and color as such:
Show[
Plot[fx, Plotstyle -> Red],
Plot[gx, Plotstyle-> Green]
]
Doing more seems to require the use of LisPlot:
dplot = ListPlot[{data1, data2}, PlotStyle -> {Red, Green}]
dynamicLabeled[dplot, {{data1, "description1"}, {data2, "description2"}}]
Now, I'm mostly fine moving through the notebook using mostly global variables, but there will be enough applications, where I might need something akin to a programming method. Because both "function" and "method" are already taken, Mathematica introduces the concept of "Block"s, which take input variables to evaluate an expression for, before forgetting about the values. Using Blocks is sort of a squishy affair, due to the way notebooks function. Each cell remembers variables, so the scope of Block variables is set to the cell, not the Block. On the one hand, this makes it easy to access quantities defined on the fly, on the other hand, code becomes impressively confusing. When transitioning to a new cell, everything defined within a Block will be immediately gone though, which unfortunately encourages copy-pasting Blocks from one cell to another. Formally, Blocks should be used following this schema:
blockname = Block[{var1, var2},
var1 + var2
q = var1
]
var = blockname[1, 2] (* var = 3*)
In regular mathematica script, q would be out of scope the moment we leave the block.Because of the cell structure of notebooks, in Mathematica notebooks, q will be known until the we leave the cell.