I am looking for a correct implement of dot product using mpmath, but they give me different results, which I don't understand why.
sum = mp.mpf(0.)
for i in range(n):
sum += mp.mpf(x[i]) * mp.mpf(y[i]) # 1st method
# sum += mp.mpf(str(x[i])) * mp.mpf(str(y[i])) # 2nd mehtod
I am using them to solve ill-conditioned dot products and the two give very different results. This is very important because I am comparing them with my another accurate dot product algorithm, which agrees with the 1st method , and disagree with the 2nd method. So I want to understand if I should trust the answer from the 1st.
Whether you should trust answer from the 1st depend on the precision, and accumulation of numerical error. But at least, that is the correct way.
The second method (besides being convoluted and expansive: it forces the creation of a human readable string, which is normally only for humans to read, and then parse it again, which is normally only for trying to understand numbers given by humans. I know I am saying this like if I were asking "why robots in Star Wars speak to each other in English when they surely have more efficient robot languages", but really, it is just about the unnecessary cost of printing and parsing strings), the second method loose accuracy. Because str
shows only digits it is sure about. So, for example
mp.mp.dps=5
a=mp.mpf(1)
b=mp.mp2(3)
a/b
# 0.33333349
str(a/b)
# 0.33333
As you can see, we loose the 3 last digits 349
. The 3
is a serious loss. But even the 49
is: sure, digits are not really 49
in the real value of 1/3
, but they aren't 00
neither, and correct value is closer to 49
that to 00
Still, it is a good idea for str
(and printing functions) to show only correct digits (the 5 digits of precision I've asked for). But only because they are used not for computation but for printing. So that is another reason for my initial rant on "don't use human/machine function for machine/machine computation": not only, as I said, that is unnecessary work, but also, humans want to see true digits, when machines want to play with all information present, even the one with some error is still better that nothing.