As with all implementation details, it's specific to the implementation (duh), but this is something that's practically universal: If the CPU supports the number type and operation (reasonably recent processors do, for floats, doubles, and the fixed-width integers), you just use that. There may be many layers in between, such as an interpreter, a JIT compiler for the IL, or something entirely else, but that's just wrappers and the actual operation is delegated to the hardware.
You'll have a very hard time beating a good hardware implementation in software, regardless of the choice of algorithm -- with one "common" exception: A slow FPU can sometimes be beaten by sacrificing some features, but that's a very low-level optimization. But that's not something language implementations usually do.
For arbitrary precision integers/numbers (such as BigInt
and BigDecimal
), you can't (entirely) rely on the hardware operations, as they are too constrained in word size. In such cases, algorithms start to matter, but again it's implementation specific and I can't give any generalizations. Note that the base is usually much greater than 10, to get the most out of the fixed-precision operations. I know that more than one highly used arbitrary precision arithmetic package (specifically, CPython's and PyPy's long
type, and the relevant piece of Mathematica) use, or at least consider, Karatsuba's algorithm.
You want to find the maximum points in both of the sets? Then the brute force search means for every point P in the first set, traverse the second set to find
the point that match with point P. It costs O(M*N), which will be suit for your case.
Of course, you can sort the points first(first by x-axis,then y-axis,then z-asix.)
Then you can traverse these two sorted set to search for the matching points.
Best Answer
Wikipedia lists four algorithms for matrix multiplication of two nxn matrices.
The classic one that a programmer would write is O(n3) and is listed as the "Schoolbook matrix multiplication". Yep. O(n3) is a bit of a hit. Lets look at the next best one.
The Strassen algorithim is O(n2.807). This one would work - it has some restrictions to it (such as the size is a power of two) and it has a caveat in the description:
For those who are interested in this algorithm and its origins, looking at How did Strassen come up with his matrix multiplication method? can be a good read. It gives a hint at the complexity of that initial O(n2) workload that is added and why this would be more expensive than just doing the classic multiplication.
So it really is O(n2 + n2.807) with that bit about lower exponent n being ignored when writing out big O. It appears that if you are working on a nice 2048x2048 matrix, this could be useful. For a 4x4 matrix, its probably going to find it slower as that overhead eats up all the other time.
And then there's the Coppersmith–Winograd algorithm which is O(n2.373) with quite a few bits of improvements. It also comes with a caveat:
So, its better when you are working on super large matrixes, but again, not useful for a 4x4 matrix.
This is again reflected in wikipedia's page in Matrix multiplication: Sub-cubic algorithms which gets at the why of things running faster:
And that gets to the core of why the algorithms are faster - you trade off some numerical stability and some additional setup. That additional setup for a 4x4 matrix is much more than the cost of doing the more multiplication.
And now, to answer your question:
No, there are no algorithms that are optimized for 4x4 matrix multiplication because the O(n3) one works quite reasonably until you start finding that you are willing to take a big hit for overhead. For your specific situation, there may be some overhead that you could incur knowing specific things beforehand about your matrixes (like how much some data will be reused), but really the easiest thing to do is write good code for the O(n3) solution, let the compiler handle it, and profile it later on to see if you actually have the code being the slow spot in the matrix multiplication.
Related on Math.SE: Minimal number of multiplications required to invert a 4x4 matrix