obliczenia_naukowe/l1/5.jl

152 lines
3.9 KiB
Julia
Raw Permalink Normal View History

2024-10-27 21:56:24 +01:00
#!/usr/bin/env julia
# Jacek Poziemski 272389
"""
2024-11-08 10:54:40 +01:00
sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
2024-10-27 21:56:24 +01:00
Calculate the scalar sum of `x` and `y`,
going from the first element to the last.
# Arguments
2024-11-08 10:54:40 +01:00
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function sumvectorsforwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::T = zero(T)
2024-10-27 21:56:24 +01:00
2024-11-08 10:54:40 +01:00
for i::Int in 1:(length(x))
2024-10-27 21:56:24 +01:00
s += x[i] * y[i]
end
return s
end
"""
2024-11-08 10:54:40 +01:00
sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
2024-10-27 21:56:24 +01:00
Calculate the scalar sum of `x` and `y`,
going from the last element to the first.
# Arguments
2024-11-08 10:54:40 +01:00
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function sumvectorsbackwards(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::T = zero(T)
2024-10-27 21:56:24 +01:00
2024-11-08 10:54:40 +01:00
for i::Int in (length(x)):-1:1
2024-10-27 21:56:24 +01:00
s += x[i] * y[i]
end
return s
end
"""
2024-11-08 10:54:40 +01:00
sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
2024-10-27 21:56:24 +01:00
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
2024-11-08 10:54:40 +01:00
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function sumvectorsdecreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::Vector{T} = zeros(T, length(x))
2024-10-27 21:56:24 +01:00
2024-11-08 10:54:40 +01:00
for i::Int in 1:(length(x))
2024-10-27 21:56:24 +01:00
s[i] = x[i] * y[i]
end
2024-11-10 19:45:31 +01:00
spos::Vector{T} = s[s .> zero(T)]
2024-10-27 21:56:24 +01:00
sort!(spos, rev = true)
2024-11-10 19:45:31 +01:00
sneg::Vector{T} = s[s .<= zero(T)]
2024-10-27 21:56:24 +01:00
sort!(sneg)
2024-11-08 10:54:40 +01:00
neg::T = zero(T)
for x::T in sneg
neg += x
2024-10-27 21:56:24 +01:00
end
2024-11-08 10:54:40 +01:00
pos::T = zero(T)
for x::T in spos
pos += x
2024-10-27 21:56:24 +01:00
end
return pos + neg
end
"""
2024-11-08 10:54:40 +01:00
sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
2024-10-27 21:56:24 +01:00
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
2024-11-08 10:54:40 +01:00
- `x::Vector{T}`: first of the 2 vectors
- `y::Vector{T}`: second of the 2 vectors
2024-10-27 21:56:24 +01:00
"""
2024-11-08 10:54:40 +01:00
function sumvectorsincreasing(x::Vector{T}, y::Vector{T})::T where T <: AbstractFloat
s::Vector{T} = zeros(T, length(x))
2024-10-27 21:56:24 +01:00
2024-11-08 11:01:17 +01:00
for i::Int in 1:(length(x))
2024-10-27 21:56:24 +01:00
s[i] = x[i] * y[i]
end
2024-11-10 19:45:31 +01:00
spos::Vector{T} = s[s .> zero(T)]
2024-10-27 21:56:24 +01:00
sort!(spos)
2024-11-10 19:45:31 +01:00
sneg::Vector{T} = s[s .<= zero(T)]
2024-10-27 21:56:24 +01:00
sort!(sneg, rev = true)
2024-11-08 10:54:40 +01:00
neg::T = zero(T)
for x::T in sneg
neg += x
2024-10-27 21:56:24 +01:00
end
2024-11-08 10:54:40 +01:00
pos::T = zero(T)
for x::T in spos
pos += x
2024-10-27 21:56:24 +01:00
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]
2024-11-08 10:54:40 +01:00
expected::Float64 = 1.00657107000000 * 10^(11)
2024-10-27 21:56:24 +01:00
2024-11-08 10:54:40 +01:00
res::Float64 = sumvectorsforwards(x32, y32)
2024-10-27 21:56:24 +01:00
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)")