From e2ef15a7a65dad91a0a029532fc3f4fdc2e94154 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Wed, 25 Jun 2025 14:39:53 +0200 Subject: [PATCH 1/6] feat: empty HarmonicVariable(symbol::Num) --- src/HarmonicVariable.jl | 32 +++++++++++++++++++++----------- test/HarmonicVariable.jl | 6 ++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/HarmonicVariable.jl b/src/HarmonicVariable.jl index 70c4732..9267c6c 100644 --- a/src/HarmonicVariable.jl +++ b/src/HarmonicVariable.jl @@ -19,19 +19,30 @@ mutable struct HarmonicVariable natural_variable::Num end +function HarmonicVariable(symbol::Num) + return HarmonicVariable(symbol, "", "", Num(1), Num(0)) +end + function Base.show(io::IO, hv::HarmonicVariable) - return println( - io, - "Harmonic variable ", - string.(hv.symbol) * " for harmonic ", - string(hv.ω), - " of ", - string(hv.natural_variable), - ) + if isempty(hv.type) + s = "Harmonic variable " * string.(hv.symbol) + else + s = + "Harmonic variable " * + string.(hv.symbol) * + " for harmonic " * + string(hv.ω) * + " of " * + string(hv.natural_variable) + end + return println(io, s) end """Gives the relation between `var` and the underlying natural variable.""" function _show_ansatz(var::HarmonicVariable) + if isempty(var.type) + return string(var.symbol) + end t = var.natural_variable.val.arguments t = length(t) == 1 ? string(t[1]) : error("more than 1 independent variable") ω = string(var.ω) @@ -99,6 +110,5 @@ end "Returns the symbols of a `HarmonicVariable`." Symbolics.get_variables(var::HarmonicVariable)::Num = Num(first(get_variables(var.symbol))) -Base.isequal(v1::HarmonicVariable, v2::HarmonicVariable)::Bool = isequal( - v1.symbol, v2.symbol -) +Base.isequal(v1::HarmonicVariable, v2::HarmonicVariable)::Bool = + isequal(v1.symbol, v2.symbol) diff --git a/test/HarmonicVariable.jl b/test/HarmonicVariable.jl index 9e5d5ea..066e536 100644 --- a/test/HarmonicVariable.jl +++ b/test/HarmonicVariable.jl @@ -37,6 +37,12 @@ end # Test _show_ansatz @test contains(_show_ansatz(hv), "cos(ωt)") + + @variables x + hv = HarmonicVariable(x) + @test repr(hv) == "Harmonic variable x\n" + + @test _show_ansatz(hv) == "x" end @testset "Coordinate Transforms" begin From 7ba26b048fd9ea021c8ad229afb09bdb40351128 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Wed, 25 Jun 2025 14:40:52 +0200 Subject: [PATCH 2/6] fix: update version to 0.3.4 --- Project.toml | 2 +- src/HarmonicVariable.jl | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index f57b78b..2f1a691 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "QuestBase" uuid = "7e80f742-43d6-403d-a9ea-981410111d43" authors = ["Orjan Ameye ", "Jan Kosata ", "Javier del Pino "] -version = "0.3.3" +version = "0.3.4" [deps] DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" diff --git a/src/HarmonicVariable.jl b/src/HarmonicVariable.jl index 9267c6c..0023311 100644 --- a/src/HarmonicVariable.jl +++ b/src/HarmonicVariable.jl @@ -110,5 +110,6 @@ end "Returns the symbols of a `HarmonicVariable`." Symbolics.get_variables(var::HarmonicVariable)::Num = Num(first(get_variables(var.symbol))) -Base.isequal(v1::HarmonicVariable, v2::HarmonicVariable)::Bool = - isequal(v1.symbol, v2.symbol) +Base.isequal(v1::HarmonicVariable, v2::HarmonicVariable)::Bool = isequal( + v1.symbol, v2.symbol +) From 3e9fc05afd75b81d4a77b5c7ea8806868c0ff4a8 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Wed, 25 Jun 2025 17:08:32 +0200 Subject: [PATCH 3/6] feat: add _remove_brackets function for Equation type to handle variable brackets --- src/HarmonicEquation.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/HarmonicEquation.jl b/src/HarmonicEquation.jl index cb8348a..eacf6ad 100644 --- a/src/HarmonicEquation.jl +++ b/src/HarmonicEquation.jl @@ -137,6 +137,13 @@ function _remove_brackets(eom::HarmonicEquation) return substitute_all(equations_lhs, variable_rules) end +function _remove_brackets(eom::Equation, vars::Num) + # remove brackets from the variables in the equation + variable_rules = [var => _remove_brackets(var) for var in vars] + lhs = Num.(getfield(eom, :lhs) - getfield(eom, :rhs)) + return substitute_all(lhs, variable_rules) +end + """ $(TYPEDSIGNATURES) Rearrange `eom` to the standard form, such that the derivatives of the variables are on one side. From f523f5880c15330b4a0ef2081f55baf7e35bf5b6 Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Wed, 25 Jun 2025 17:11:15 +0200 Subject: [PATCH 4/6] fix: update _remove_brackets function to handle Vector of Equations and Variables --- src/HarmonicEquation.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/HarmonicEquation.jl b/src/HarmonicEquation.jl index eacf6ad..1fe1dd9 100644 --- a/src/HarmonicEquation.jl +++ b/src/HarmonicEquation.jl @@ -137,11 +137,11 @@ function _remove_brackets(eom::HarmonicEquation) return substitute_all(equations_lhs, variable_rules) end -function _remove_brackets(eom::Equation, vars::Num) - # remove brackets from the variables in the equation - variable_rules = [var => _remove_brackets(var) for var in vars] - lhs = Num.(getfield(eom, :lhs) - getfield(eom, :rhs)) - return substitute_all(lhs, variable_rules) +function _remove_brackets(eom::Vector{Equation}, vars::Vector{Num}) + vars_ = _remove_brackets.(vars) + variable_rules = Dict(zip(var, vars_)) + equations_lhs = Num.(getfield.(eom, :lhs) - getfield.(eom, :rhs)) + return substitute_all(equations_lhs, variable_rules), vars_ end """ From a46225380694cedb9eddf0394e99be42442d3cea Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Wed, 25 Jun 2025 17:15:51 +0200 Subject: [PATCH 5/6] fix: update _remove_brackets function to handle Vector of Num instead of Vector of Equation --- src/HarmonicEquation.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HarmonicEquation.jl b/src/HarmonicEquation.jl index 1fe1dd9..c6d5468 100644 --- a/src/HarmonicEquation.jl +++ b/src/HarmonicEquation.jl @@ -137,9 +137,9 @@ function _remove_brackets(eom::HarmonicEquation) return substitute_all(equations_lhs, variable_rules) end -function _remove_brackets(eom::Vector{Equation}, vars::Vector{Num}) +function _remove_brackets(eom::Vector{Num}, vars::Vector{Num}) vars_ = _remove_brackets.(vars) - variable_rules = Dict(zip(var, vars_)) + variable_rules = Dict(zip(vars, vars_)) equations_lhs = Num.(getfield.(eom, :lhs) - getfield.(eom, :rhs)) return substitute_all(equations_lhs, variable_rules), vars_ end From 94460cc07e27dff57aedbbc81290ee98453f044f Mon Sep 17 00:00:00 2001 From: Orjan Ameye Date: Wed, 25 Jun 2025 17:18:06 +0200 Subject: [PATCH 6/6] fix: update _remove_brackets function to use eqs parameter instead of eom for better clarity --- src/HarmonicEquation.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/HarmonicEquation.jl b/src/HarmonicEquation.jl index c6d5468..4889146 100644 --- a/src/HarmonicEquation.jl +++ b/src/HarmonicEquation.jl @@ -137,11 +137,10 @@ function _remove_brackets(eom::HarmonicEquation) return substitute_all(equations_lhs, variable_rules) end -function _remove_brackets(eom::Vector{Num}, vars::Vector{Num}) +function _remove_brackets(eqs::Vector{Num}, vars::Vector{Num}) vars_ = _remove_brackets.(vars) variable_rules = Dict(zip(vars, vars_)) - equations_lhs = Num.(getfield.(eom, :lhs) - getfield.(eom, :rhs)) - return substitute_all(equations_lhs, variable_rules), vars_ + return substitute_all(eqs, variable_rules), vars_ end """