Also if k is known in advance, you could precompute the signs and map to
the sample.
# clumsy computation of all vectors of length k with 1/-1 as entries using
powerset.
signs(k) = [[(-1)^(i in s) for i = 1:k] for s=powerset(1:k)]
# now return a closure
sdifsums(k) = let s=signs(k) ; a->map(x-
I use the following to get the (untyped) powerset of an array:
function powerset (x)
result = {{}}
for i in x, j = 1:length(result)
push!(result, [result[j],i])
end
result
end
julia> show(powerset(1:3))
{{},{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3}}
Then sdifsums can be written as
sdifs
I also cleaned up the first function
function signedsum(v::Vector, j::Uint)
s = zero(eltype(v))
for i in 1:length(v)
if (j & one(Uint)) == zero(Uint)
s += v[i]
else
s -= v[i]
end
j >>= 1
end
s
end
I updated that code but forgot to copy it over. The second function should
be defined as
function sdifsums(v::Vector)
[signedsum(v,j) for j in uint(0:(2^length(v) - 1))]
end
On Tuesday, April 1, 2014 2:51:13 PM UTC-5, Douglas Bates wrote:
>
> In
>
> http://nbviewer.ipython.org/gist/dmbate