obliczenia_naukowe/l2/1.jl

152 lines
3.9 KiB
Julia
Raw Normal View History

2024-11-08 12:04:48 +01:00
#!/usr/bin/env julia
# Jacek Poziemski 272389
"""
sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
Calculate the scalar sum of `x` and `y`,
going from the first element to the last.
# Arguments
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::T = zero(T)
for i::Int in 1:(length(x))
s += x[i] * y[i]
end
return s
end
"""
sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
Calculate the scalar sum of `x` and `y`,
going from the last element to the first.
# Arguments
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::T = zero(T)
for i::Int in (length(x)):-1:1
s += x[i] * y[i]
end
return s
end
"""
sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: 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{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::Vector{T} = zeros(T, length(x))
for i::Int in 1:(length(x))
s[i] = x[i] * y[i]
end
spos::Vector{T} = s[s .> 0]
sort!(spos, rev = true)
sneg::Vector{T} = s[s .<= 0]
sort!(sneg)
neg::T = zero(T)
for x::T in sneg
neg += x
end
pos::T = zero(T)
for x::T in spos
pos += x
end
return pos + neg
end
"""
sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: 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{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
"""
function sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::Vector{T} = zeros(T, length(x))
for i::Int in 1:(length(x))
s[i] = x[i] * y[i]
end
spos::Vector{T} = s[s .> 0]
sort!(spos)
sneg::Vector{T} = s[s .<= 0]
sort!(sneg, rev = true)
neg::T = zero(T)
for x::T in sneg
neg += x
end
pos::T = zero(T)
for x::T in spos
pos += x
end
return pos + neg
end
x32::Vector{Float32} = [2.718281828, 3.141592654, 1.414213562, 0.577215664, 0.301029995]
y32::Vector{Float32} = [1486.2497, 878366.9879, 22.37492, 4773714.647, 0.000185049]
x64::Vector{Float64} = [2.718281828, 3.141592654, 1.414213562, 0.577215664, 0.301029995]
y64::Vector{Float64} = [1486.2497, 878366.9879, 22.37492, 4773714.647, 0.000185049]
expected::Float64 = 1.00657107000000 * 10^(11)
res::Float64 = 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)")