151 lines
3.9 KiB
Julia
Executable file
151 lines
3.9 KiB
Julia
Executable file
#!/usr/bin/env julia
|
||
|
||
# Jacek Poziemski 272389
|
||
|
||
"""
|
||
sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
|
||
Calculate the scalar sum of `x` and `y`,
|
||
going from the first element to the last.
|
||
|
||
# Arguments
|
||
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
|
||
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
|
||
"""
|
||
function sumvectorsforwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
s = 0.0
|
||
|
||
for i in 1:(length(x))
|
||
s += x[i] * y[i]
|
||
end
|
||
|
||
return s
|
||
end
|
||
|
||
"""
|
||
sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
|
||
Calculate the scalar sum of `x` and `y`,
|
||
going from the last element to the first.
|
||
|
||
# Arguments
|
||
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
|
||
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
|
||
"""
|
||
function sumvectorsbackwards(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
s = 0.0
|
||
|
||
for i in (length(x)):-1:1
|
||
s += x[i] * y[i]
|
||
end
|
||
|
||
return s
|
||
end
|
||
|
||
"""
|
||
sumvectorsdecreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
|
||
Calculate the scalar sum of `x` and `y` by calculating
|
||
the partial sums of positive and negative numbers,
|
||
summing up the partial sums using a decreasing order
|
||
according to their absolute values.
|
||
|
||
# Arguments
|
||
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
|
||
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
|
||
"""
|
||
function sumvectorsdecreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
s = zeros(length(x))
|
||
|
||
for i in 1:(length(x))
|
||
s[i] = x[i] * y[i]
|
||
end
|
||
|
||
spos = s[s .> 0]
|
||
sort!(spos, rev = true)
|
||
|
||
sneg = s[s .<= 0]
|
||
sort!(sneg)
|
||
|
||
neg = 0
|
||
for i in sneg
|
||
neg += i
|
||
end
|
||
|
||
pos = 0
|
||
for i in spos
|
||
pos += i
|
||
end
|
||
|
||
return pos + neg
|
||
end
|
||
|
||
"""
|
||
sumvectorsincreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
|
||
Calculate the scalar sum of `x` and `y` by calculating
|
||
the partial sums of positive and negative numbers,
|
||
summing up the partial sums using an increasing order
|
||
according to their absolute values.
|
||
|
||
# Arguments
|
||
- `x::Vector{<: AbstractFloat}`: first of the 2 vectors
|
||
- `y::Vector{<: AbstractFloat}`: second of the 2 vectors
|
||
"""
|
||
function sumvectorsincreasing(x::Vector{<: AbstractFloat}, y::Vector{<: AbstractFloat})
|
||
s = zeros(length(x))
|
||
|
||
for i in 1:(length(x))
|
||
s[i] = x[i] * y[i]
|
||
end
|
||
|
||
spos = s[s .> 0]
|
||
sort!(spos)
|
||
|
||
sneg = s[s .<= 0]
|
||
sort!(sneg, rev = true)
|
||
|
||
neg = 0
|
||
for i in sneg
|
||
neg += i
|
||
end
|
||
|
||
pos = 0
|
||
for i in spos
|
||
pos += i
|
||
end
|
||
|
||
return pos + neg
|
||
end
|
||
|
||
x32::Vector{Float32} = [2.718281828, −3.141592654, 1.414213562, 0.5772156649, 0.3010299957]
|
||
y32::Vector{Float32} = [1486.2497, 878366.9879, −22.37492, 4773714.647, 0.000185049]
|
||
|
||
x64::Vector{Float64} = [2.718281828, −3.141592654, 1.414213562, 0.5772156649, 0.3010299957]
|
||
y64::Vector{Float64} = [1486.2497, 878366.9879, −22.37492, 4773714.647, 0.000185049]
|
||
|
||
expected = −1.00657107000000 * 10^(−11)
|
||
|
||
res = sumvectorsforwards(x32, y32)
|
||
println("summing x32 and y32 forwards: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsbackwards(x32, y32)
|
||
println("summing x32 and y32 backwards: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsdecreasing(x32, y32)
|
||
println("summing x32 and y32 decreasing: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsincreasing(x32, y32)
|
||
println("summing x32 and y32 increasing: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsforwards(x64, y64)
|
||
println("summing x64 and y64 forwards: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsbackwards(x64, y64)
|
||
println("summing x64 and y64 backwards: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsdecreasing(x64, y64)
|
||
println("summing x64 and y64 decreasing: $res; correct: $(res == expected)")
|
||
|
||
res = sumvectorsincreasing(x64, y64)
|
||
println("summing x64 and y64 increasing: $res; correct: $(res == expected)")
|