mathnet / mathnet-numerics

Math.NET Numerics
http://numerics.mathdotnet.com
MIT License
3.48k stars 895 forks source link

RunningStatistics Combine Method Must Always Return a New Instance #1021

Closed github-srj closed 1 year ago

github-srj commented 1 year ago

The optimization for empty instances at the beginning of the method returns a reference to one of the input instances is empty (count=0). When RunningStatistics instance(s) are contained in another class, unintended modifications can occur when the instance subsequently modified by any referencing container. The resulting problems may be difficult to locate / diagnose.

A trivial clone constructor (preferably public) would facilitate easy remediation. The Clone constructor can then be used by the optimization in the Combine method to resolve issue.

cdrnet commented 1 year ago

Yes, I see where you're coming from. A nice example of why I like Rust so much (over C#), where this problem could not appear at all, by design. I also agree with your proposed remediation, let's change it this way.