The reason why this isn't vectorized is pretty simple to explain: floating-point addition isn't associative, and vectorization changes the order in which the various terms are summed in a reduction. What you would like to have here is a variant of the + operator that licenses reassociation (just like &+ does for integers). This is implementable in the standard library leveraging builtins, but not really feasible in Swift outside of this standard library.
If this is more than curiosity, your best option today is probably to define a inline C function in a header with the desired semantics:
// myHeader.h
static inline __attribute__((inline_always))
double associative_add(double a, double b) {
#pragma clang fp reassociate(on)
return a + b;
}
// myFile.swift
func reduce(foo: [Double]) -> Double {
foo.reduce(0, associative_add)
}
Medium-term, I would consider making this and similar operations available as API in swift-numerics; feel free to open a GitHub issue to request the feature (it would be helpful to expand on the use case a bit).
Longer-term, there are a number of language features that could help here. @Joe_Groff has mentioned in the past the idea of scoped imports, which would allow you to import a different operator + that allows reassociation scoped to a block (i.e. without changing the behavior of any other code).
Another option, if you're on an Apple platform, is to use vDSP.sum(foo), which has an efficient vectorized implementation for all targets.