It’s shorter, no doubt, but it’s also way denser. Originally, I wrote out the 9 terms without any use of :
, and I think that’s the way I ultimately prefer, but I had to go back and forth a bit, in case there were some performance tricks that would only happen with a specific syntax (and wouldn’t you know!)
Here’s my stream of consciousness:
[ xs[1,1]-xs[1,4] xs[1,2]-xs[1,4] xs[1,3]-xs[1,4] ;
xs[2,1]-xs[2,4] xs[2,2]-xs[2,4] xs[2,3]-xs[2,4] ;
xs[3,1]-xs[3,4] xs[3,2]-xs[3,4] xs[3,3]-xs[3,4] ]
oh, it’s a 3x3 matrix, and A[i,j] =
…uuh lets see … xs[i,j] - xs[i,4]
.
[xs[:,1].-xs[:,4] xs[:,2].-xs[:,4] xs[:,3].-xs[:,4]]
Okay so it’s an array, and the elements are, oh no wait, xs[:,1]
is a vector, so we’ll end up with a matrix where … uuh … the vectors here become the columns?, and then we broadcast xs[:,4]
which is also a vector, so the broadcasting is really just element wise, okay.
xs[:, 1:3] .- xs[:, 4]
Okay so we index the vector, oh with ranges in both places, okay, I guess we’ll get the sub matrix? and then we’ll broadcast sub xs[:,4]
which is a vector, and subtraction of a vector needs another vector, so probably this will go across the columns? uuh wait xs
is 3 high and so xs[:,4]
is 3 long or uuh high, and so we’ll have to go across the columns in xs
.
Whether I write the second one, or eventually get to the last one, isn’t really important to me, since I find the first version way clearer. In addition, I would think that spelling it out explicitly would make Julia’s job of generating good code for this easier, so that I don’t have to rewrite all of my functions when I realize that bad code is being generated. Somehow I got it completely backwards this time?
Anyways, thanks for the help!