I can see that this implementation should be sufficient for simple cases.
This is not my area of expertise, so I do not feel confident of judging whether it is sufficient or optimal as the number of terms becomes very large or the magnitude of the individual terms becomes large.
I worry that sequential multiplication can lead to overflow more often than sequential addition. There also may be concerns about loss of precision. For example, I wonder if there is a good way to decide when it would be better to evaluate a sum of logarithms instead?
Good point about handling terms that are less than zero.
Another question:
What should happen if the end is less than the start?
For example
A = prod [ i = 5 : 4 ] i
The code in the patch sets A to 1.0, but this is non-intuitive.
I would expect it to set A to 5.0 or to report an error and set A to NaN or <undefined>.</undefined>
Alternatively, the code could rearrange the terms so that the sequence of multiplications runs from min(start,end) to max(start,end). But this would be different from what "sum" currently does.
Last edit: Ethan Merritt 2025-05-10
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I can see that this implementation should be sufficient for simple cases.
This is not my area of expertise, so I do not feel confident of judging whether it is sufficient or optimal as the number of terms becomes very large or the magnitude of the individual terms becomes large.
I worry that sequential multiplication can lead to overflow more often than sequential addition. There also may be concerns about loss of precision. For example, I wonder if there is a good way to decide when it would be better to evaluate a sum of logarithms instead?
Does anyone know what other programs do? R? Mathematica? Octave?
I think the implementation by exp and log is not sufficient because each term may has minus sign.
For example: Lagrange interpolation:
Good point about handling terms that are less than zero.
Another question:
What should happen if the end is less than the start?
For example
A = prod [ i = 5 : 4 ] i
The code in the patch sets A to 1.0, but this is non-intuitive.
I would expect it to set A to 5.0 or to report an error and set A to NaN or <undefined>.</undefined>
Alternatively, the code could rearrange the terms so that the sequence of multiplications runs from min(start,end) to max(start,end). But this would be different from what "sum" currently does.
Last edit: Ethan Merritt 2025-05-10
I think it is reasonable that "prod [i=5:4] i" returns 1, since it is reasonable that "sum [i=5:4] i" returns 0.
This behavior is also used in the Lagrange interpolation I showed, and it may be useful.
OK.
Added to the git master by commit 2b27736ab8
I added a description in the documentation.