I am implementing a model for the units of a simple silica glass, and in doing so have come across some interesting questions on how to use keyword arguments. Essentially, there are two notations for how to represent the composition of the glass (the input to my function): x, and j.
In order to represent that, here is what I’ve done for one such unit
x(j) = j / (j + 1)
Q4(; j = 0, x = x(j)) = 0 <= x < 1 / 3 ? 1 - 3 * x : 0
It strikes me as odd that with this particular use case, I must specify a default value for j, even if it’s never used except for when a j-value is specified and x is not. Also, it’s possible, though it would error, to attempt to call this function without either j or x. This hints to me that I’m doing something “wrong,” or at least in a not very Julian/idiomatic fashion.
Additionally, when I went to plot this, I found that dot notation for vectorization doesn’t work with keyword arguments (noted in a few places a few years ago, including here: Julia: Broadcasting Functions with Keyword Arguments - Stack Overflow). Instead, I did something like this:
using Plots
xrange = 0:1/1000:2/3
plot(xrange, map(x -> Q4(x = x), xrange))
It gets a little unwieldy, but not terrible, when plotting something like the fraction of units
total(; j = 0, x = x(j)) = Q4(x = x) + Q3(x = x) + Q2(x = x) + Q1(x = x) + Q0(x = x)
plot(xrange, map(x -> Q4(x = x) / total(x = x), xrange))
Is this the preferred way to handle vectorization of functions with keyword arguments, or is there a better way?