Ricky Teachey writes:
Well keep in mind that generally an engineer submits calculations-- not code-- to be reviewed. This leaves me with a few options:
[...]
Hiding the portion of code that looks too much like programming (and not enough like equations) takes some time, but if I do not do this it will inevitably lead to a 40 minute phone call [...].
Ah, I misinterpreted your statement that engineers don't much use linear algebra. You *do* use it for *presentation*, but it's not very useful in the programming that gets your results.
Furthermore, the same NYDoT reviewer might come back and say:
*"Rejected: calculation does not take into account change in friction angle with depth."*
The official would be saying that friction (μ_s) is not a scalar, it should be another array variable, because the soils apply different friction along the shaft depth.
I don't think Steven's proposed matrix class can substitute for numpy in general. It will work for your case because '*' can be both scalar multiplication and matrix multiplication. That is, the scalar μ_s (float, I presume?) doesn't know how to self.__mul__(q_s), but q_s.__rmul__(μ_s) works fine. Then tmp.__mul__(C_shaft) (C_shaft a scalar or a vector of the same length) works. But if you have *two* variables, and the reviewer claims they have different (scalar) coefficients, you're going to need numpy-style broadcasting. Also, you're going to run into the issue that Steven is probably going to use '@' for matrix multiplication for numpy compatibility as requested by several people, while * will be elementwise multiplication. Finally you lose the convenience function like linspace. Overall, it may still be worthwhile, and Steven may give you the operator you want (whether it's '*' or '@' for matrix multiplication).
The answer is yes. It's a good question on the linspace() function -- I don't think so. [...] However I would certainly hide this part of the code, and include the copy and pasted result from this:
print(f"γ_per_ft = ({', '.join(f'{v:.0f}' for v in z)}) pcf")
Aaarrrrrggggggh! What we do for money and to please reviewers ....
Sidebar: it would be pretty fantastic if a native matrix library could display values using f-strings like this:
x = 1.1 print(f"{x=:.0f} pcf") 1 pcf γ_per_ft = matrix( [120, 122, 118, 117] ) print(f"{γ_per_ft=:.0f} pcf") (120, 122, 118, 117) pcf
The idea here is that python knows the datatype of γ_per_ft is float, so it can format all the members of the array *one at a time*.
I think all you need to do is provide an appropriate __format__ method on matrix objects. That method will have access to the format spec (ie, everything after the colon), and if there isn't a parser you can just import, I'm sure there are plenty you can copy. At least for this case you can just apply it to the individual elements. It's not obvious to me that elementwise is the "right" way to implement matrix.__format__ (there may be "matrix-level" formatting that takes precedence, for example how to treat rows wider than the page width), but it's quite plausible. Of course you can also derive a simple subclass with elementwise __format__.