From 4870169261fbbd64ce2adad33236ec46262f12c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vegard=20Ven=C3=A5s?= Date: Thu, 5 Feb 2026 16:48:35 +0100 Subject: [PATCH 1/2] Add unary operator support for profiles --- Project.toml | 2 +- src/profiles.jl | 8 ++++++++ test/runtests.jl | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 918b6ce..bbdfb63 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "TimeStruct" uuid = "f9ed5ce0-9f41-4eaa-96da-f38ab8df101c" authors = ["Lars Hellemo , Truls.Flatberg "] -version = "0.9.7" +version = "0.9.8" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/profiles.jl b/src/profiles.jl index 3c21469..3417ee5 100644 --- a/src/profiles.jl +++ b/src/profiles.jl @@ -408,3 +408,11 @@ end function /(a::RepresentativeProfile{T}, b::Number) where {T} return RepresentativeProfile(a.vals ./ b) end + +-(a::FixedProfile{T}) where {T} = FixedProfile(-a.val) +-(a::OperationalProfile{T}) where {T} = OperationalProfile(-a.vals) +-(a::StrategicProfile{T}) where {T} = StrategicProfile(-a.vals) +-(a::ScenarioProfile{T}) where {T} = ScenarioProfile(-a.vals) +-(a::RepresentativeProfile{T}) where {T} = RepresentativeProfile(-a.vals) + ++(a::TimeProfile{T}) where {T} = a \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 190f844..bf4c7e9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1444,6 +1444,58 @@ end @test typeof(v) == Vector{OperationalProfile{Float64}} end +@testitem "Profile and unary operators" begin + # FixedProfile + profile = +FixedProfile(10) + @test profile.val == 10 + + profile = -FixedProfile(10) + @test profile.val == -10 + + # OperationalProfile + profile = +OperationalProfile([1, 2, 3]) + @test profile.vals == [1, 2, 3] + + profile = -OperationalProfile([1, 2, 3]) + @test profile.vals == [-1, -2, -3] + + # StrategicProfile + simple = SimpleTimes(10, 1) + ts = TwoLevel(3, 5, simple) + + profile = +StrategicProfile([1, 2, 3]) + vals = collect(profile[sp] for sp in strat_periods(ts)) + @test vals == [1, 2, 3] + + profile = -StrategicProfile([1, 2, 3]) + vals = collect(profile[sp] for sp in strat_periods(ts)) + @test vals == [-1, -2, -3] + + # ScenarioProfile + oscen = OperationalScenarios([SimpleTimes(5, 1), SimpleTimes(5, 1)]) + repr = RepresentativePeriods(2, 5, [0.6, 0.4], [oscen, oscen]) + ts = TwoLevel(3, 5, repr) + profile = +ScenarioProfile([1, 2, 3]) + vals = collect(profile[sc] for sc in opscenarios(ts)) + @test vals == [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] + + profile = -ScenarioProfile([1, 2, 3]) + vals = collect(profile[sc] for sc in opscenarios(ts)) + @test vals == [-1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2] + + # RepresentativeProfile + repr = RepresentativePeriods(2, 5, [0.6, 0.4], [SimpleTimes(5, 1), SimpleTimes(5, 1)]) + ts = TwoLevel(3, 5, repr) + + profile = +RepresentativeProfile([1, 2, 3]) + vals = collect(profile[rp] for rp in repr_periods(ts)) + @test vals == [1, 2, 1, 2, 1, 2] + + profile = -RepresentativeProfile([1, 2, 3]) + vals = collect(profile[rp] for rp in repr_periods(ts)) + @test vals == [-1, -2, -1, -2, -1, -2] +end + @testitem "Iteration utilities" begin uniform_day = SimpleTimes(24, 1) uniform_week = TwoLevel(7, 24, uniform_day) From 6c6597d8022385ee3af08aca0d797b52befba472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vegard=20Ven=C3=A5s?= Date: Fri, 6 Feb 2026 08:30:23 +0100 Subject: [PATCH 2/2] Format code --- src/profiles.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/profiles.jl b/src/profiles.jl index 3417ee5..6fe6ed7 100644 --- a/src/profiles.jl +++ b/src/profiles.jl @@ -415,4 +415,4 @@ end -(a::ScenarioProfile{T}) where {T} = ScenarioProfile(-a.vals) -(a::RepresentativeProfile{T}) where {T} = RepresentativeProfile(-a.vals) -+(a::TimeProfile{T}) where {T} = a \ No newline at end of file ++(a::TimeProfile{T}) where {T} = a