This non-padding version miraculously works out:
f(a,b)=total(ab)/(a^2.totalb^2.total)^{.5}
Try It On Desmos!
Try It On Desmos! - Prettified
I originally had constructed an answer that was 123 bytes (around 3 times longer than with no padding!!!) long that did actually do the padding, but then I realized, to my complete surprise, that the test case outputs were matching on both my padding and no-padding answers (which is this 42 byte answer), even for test cases that I made up myself. This led me to dig a bit deeper into why this was the case, resulting in the explanation below.
Explanation
The question essentially asks to pad the shorter list with zeros at the end to make both lists the same, and then multiply each element of the lists together and sum them, then divide it by the square root of the product of the sums of the elements of each of the two lists squared.
Now let's say that the unpadded, original input lists are a=[a_1,a_2,...,a_A]
and b=[b_1,b_2,...,b_B]
, where the capitalized version of the list represents the length of the list. WLOG, assume that A<B
. Now let c
and d
be the padded versions of a
and b
respectively, meaning c=[a_1,a_2,...,a_A,0,...,0]
, where there are B-A
zeros, and d=[b_1,b_2,...,b_B]
, which is b
unchanged.
Now to explain my non-padding answer first:
In Desmos, every time an operation is applied to two lists, it will almost always clip the resulting list to the length of the shorter list out of the two lists. This includes multiplication (implicitly vectorized), which is used in this answer.
For the numerator of the formula given in the question, it is essentially equivalent to using vectorized multiplication on the two lists (which will multiply the lists element-by-element), and then taking the total sum of that vectorized product. We know that vectorized multiplication will clip the lists to the length of the shorter lists, which means that when we take the product a*b
, we will get the result [a_1 * b_1, a_2 * b_2,...,a_A * b_A]
, where it is clipped off at the shorter list a
. Taking the total of this would give a_1 * b_1 + a_2 * b_2 + ... + a_A * b_A
.
Wait! Now let's take a look at what happens when we take the product c*d
and sum that list instead. Recall that c=[a_1,a_2,...,a_A,0,...,0]
and d=[b_1,b_2,...,b_B]
, so c*d
would be [a_1 * b_1, a_2 * b_2,...,a_A * b_A, 0 * b_(A+1),...,0 * b_B]
. Taking the sum of all those terms gives a_1 * b_1 + a_2 * b_2 + ... + a_A * b_A
, due to all the zeros vanishing from the sum. But this is the exact same sum that we got without padding!!! This indicates that padding at least does not affect the final result of the numerator of the formula, so it is fine to take the sum without padding the two lists beforehand, as it will result in the same answer as with padding anyways.
Now let's consider what is happening in the denominator. In my answer with no padding, I am simply just multiplying each of the lists by themselves (i.e. squaring them), and then summing the resulting elements. So basically, we have that a^2 = [(a_1)^2, (a_2)^2,...,(a_A)^2]
and b^2 = [(b_1)^2, (b_2)^2,...,(b_B)^2]
, which means that their sums are (a_1)^2 + (a_2)^2 + ... + (a_A)^2
and (b_1)^2 + (b_2)^2 + ... + (b_B)^2
respectively.
But now consider the sums when there is padding involved. We then have that c^2 = [(a_1)^2, (a_2)^2,...,(a_A)^2, 0^2,...,0^2]
, and d^2
would be the same as b^2
because as stated earlier, d
is the same as b
unchanged. So, the sum of d^2
would obviously match that of b^2
. But what about the sum of c^2
? We have that the sum of c^2
is (a_1)^2 + (a_2)^2 + ... + (a_A)^2 + 0^2 + ... + 0^2
, but because all the zeros just vanish, the sum simplifies down to (a_1)^2 + (a_2)^2 + ... + (a_A)^2
. Comparing this to the sum that we got with no padding, we can see that they are exactly the same!!!
Obviously, we have assumed that A<B
at the very start of our proof. But what happens if B>A
? Then simply just switch all the a
's and b
's, A
and B
's, and also c
and d
and you will have the proof for the B>A
case.
Lastly, what happens if A=B
; in other words, what happens if they are both the same length? The clipping will shorten the longer list into the same length of the shorter list, but because they are the same length, this will not affect either of the lists. Similarly, padding will lengthen the shorter length to match the length of the longer list, but again, because the two lists are the exact same length, this won't affect either of the lists. Because both clipping and padding preserve the original lists, both of them will obviously result in the same calculations and therefore the same outputs as well.
Thus, by proving that the sums in both the numerator and denominator are the same with both padding (which is what the question is asking for) and with no padding (which is what is happening in my answer), my answer is valid, even though I am not actually doing any padding like the question asks for.