Skip to content

Commit 6c2a2cb

Browse files
committed
format
1 parent 5115ec1 commit 6c2a2cb

5 files changed

Lines changed: 66 additions & 52 deletions

File tree

src/Symbolics/Symbolics_utils.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ _apply_termwise(f, x::Num) = wrap(_apply_termwise(f, unwrap(x)))
44

55
# Symbolics v7 note: `unwrap` now always returns `BasicSymbolic`; use `Symbolics.value`
66
# when you want the underlying literal number (via `Const`).
7-
_literal_number(x::Num) = (v = Symbolics.value(x); v isa Number ? v : nothing)
8-
_literal_number(x::BasicSymbolic) =
9-
(v = SymbolicUtils.unwrap_const(x); v isa Number ? v : nothing)
10-
is_literal_zero(x) = (v = _literal_number(x); v !== nothing && iszero(v))
7+
_literal_number(x::Num) = (v=Symbolics.value(x); v isa Number ? v : nothing)
8+
function _literal_number(x::BasicSymbolic)
9+
(v=SymbolicUtils.unwrap_const(x); v isa Number ? v : nothing)
10+
end
11+
is_literal_zero(x) = (v=_literal_number(x); v !== nothing && iszero(v))
1112

1213
"Expands using SymbolicUtils.expand and expand_exp_power (changes exp(x)^n to exp(x*n)"
1314
function expand_all(x)
@@ -145,7 +146,7 @@ function is_harmonic(x::Num, t::Num)::Bool
145146
isempty(t_terms) && return true
146147
trigs = is_trig.(t_terms)
147148

148-
if !all(trigs)
149+
if !all(trigs)
149150
return false
150151
else
151152
powers = [max_power(first(arguments(term.val)), t) for term in t_terms[trigs]]
@@ -165,10 +166,11 @@ Counts the number of derivatives of a symbolic variable.
165166
function count_derivatives(x::BasicSymbolic)
166167
if Symbolics.is_derivative(x)
167168
arg = first(arguments(x))
168-
(issym(arg) ||
169-
Symbolics.is_derivative(arg) ||
170-
(isterm(arg) && issym(operation(arg)))) ||
171-
error("The input is not a single term or symbol")
169+
(
170+
issym(arg) ||
171+
Symbolics.is_derivative(arg) ||
172+
(isterm(arg) && issym(operation(arg)))
173+
) || error("The input is not a single term or symbol")
172174
D = operation(x)
173175
return D.order + count_derivatives(arg)
174176
end

src/Symbolics/drop_powers.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ function drop_powers(expr::Num, vars::Vector{Num}, deg::Int)
1717
Symbolics.@variables ϵ
1818
subs_expr = deepcopy(expr)
1919
rules = Dict([var => ϵ * var for var in unique(vars)])
20-
subs_expr = Symbolics.expand(substitute_all(subs_expr, rules; include_derivatives=false))
20+
subs_expr = Symbolics.expand(
21+
substitute_all(subs_expr, rules; include_derivatives=false)
22+
)
2123
max_deg = max_power(subs_expr, ϵ)
2224
removal = Dict([ϵ^d => Num(0) for d in deg:max_deg])
2325
res = substitute_all(

src/Symbolics/fourier.jl

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,45 @@ end
5555

5656
function _trig_expand_products(x::BasicSymbolic)
5757
# Expand trig products/powers into sums so `get_independent` can isolate constants.
58-
y = Postwalk(ex -> begin
59-
if ispow(ex)
60-
base, exponent = arguments(ex)
61-
exp_val = SymbolicUtils.unwrap_const(exponent)
62-
if exp_val isa Integer && exp_val == 2 && _is_sin_cos(base)
63-
arg = first(arguments(base))
64-
if operation(base) === cos
65-
return (1 + cos(2 * arg)) / 2
66-
else
67-
return (1 - cos(2 * arg)) / 2
58+
y = Postwalk(
59+
ex -> begin
60+
if ispow(ex)
61+
base, exponent = arguments(ex)
62+
exp_val = SymbolicUtils.unwrap_const(exponent)
63+
if exp_val isa Integer && exp_val == 2 && _is_sin_cos(base)
64+
arg = first(arguments(base))
65+
if operation(base) === cos
66+
return (1 + cos(2 * arg)) / 2
67+
else
68+
return (1 - cos(2 * arg)) / 2
69+
end
6870
end
69-
end
70-
elseif ismul(ex)
71-
# In SymbolicUtils v4, `arguments(ismul(...))` includes the numeric coefficient
72-
# even though `ex.coeff` also stores it. Avoid double-counting it.
73-
factors = BasicSymbolic[
74-
f for f in arguments(ex) if !(SymbolicUtils.unwrap_const(f) isa Number)
75-
]
76-
trig_idx = findall(_is_sin_cos, factors)
77-
if length(trig_idx) >= 2
78-
i, j = trig_idx[1], trig_idx[2]
79-
repl = _trig_mul_to_sum(factors[i], factors[j])
80-
if repl !== nothing
81-
others = BasicSymbolic[]
82-
for (k, f) in pairs(factors)
83-
(k == i || k == j) && continue
84-
push!(others, f)
71+
elseif ismul(ex)
72+
# In SymbolicUtils v4, `arguments(ismul(...))` includes the numeric coefficient
73+
# even though `ex.coeff` also stores it. Avoid double-counting it.
74+
factors = BasicSymbolic[
75+
f for f in arguments(ex) if !(SymbolicUtils.unwrap_const(f) isa Number)
76+
]
77+
trig_idx = findall(_is_sin_cos, factors)
78+
if length(trig_idx) >= 2
79+
i, j = trig_idx[1], trig_idx[2]
80+
repl = _trig_mul_to_sum(factors[i], factors[j])
81+
if repl !== nothing
82+
others = BasicSymbolic[]
83+
for (k, f) in pairs(factors)
84+
(k == i || k == j) && continue
85+
push!(others, f)
86+
end
87+
coeff = ex.coeff
88+
return coeff * prod(others; init=1) * repl
8589
end
86-
coeff = ex.coeff
87-
return coeff * prod(others; init=1) * repl
8890
end
8991
end
90-
end
91-
return ex
92-
end)(x)
92+
return ex
93+
end,
94+
)(
95+
x
96+
)
9397
return SymbolicUtils.expand(y)
9498
end
9599
_trig_expand_products(x::Num) = wrap(_trig_expand_products(unwrap(x)))
@@ -227,7 +231,10 @@ function _strip_real_imag(x::BasicSymbolic)
227231
if coeff_val isa Number
228232
r = real(coeff_val)
229233
rest = prod(
230-
(f for f in arguments(ex) if !(SymbolicUtils.unwrap_const(f) isa Number));
234+
(
235+
f for
236+
f in arguments(ex) if !(SymbolicUtils.unwrap_const(f) isa Number)
237+
);
231238
init=1,
232239
)
233240
return iszero(r) ? 0 : r * rest
@@ -249,7 +256,10 @@ function _strip_real_imag(x::BasicSymbolic)
249256
if coeff_val isa Number
250257
i = imag(coeff_val)
251258
rest = prod(
252-
(f for f in arguments(ex) if !(SymbolicUtils.unwrap_const(f) isa Number));
259+
(
260+
f for
261+
f in arguments(ex) if !(SymbolicUtils.unwrap_const(f) isa Number)
262+
);
253263
init=1,
254264
)
255265
return iszero(i) ? 0 : i * rest

test/DifferentialEquations.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ using QuestBase:
4141
expr = d(x, t, 2) + ω0^2 * x
4242
diff_eq3 = DifferentialEquation(expr, x)
4343
@test length(diff_eq3.equations) == 1
44-
rhs_simplified = Symbolics.simplify(diff_eq3.equations[x].rhs)
45-
rhs_val = Symbolics.value(rhs_simplified)
46-
@test rhs_val isa Number && iszero(rhs_val)
44+
rhs_simplified = Symbolics.simplify(diff_eq3.equations[x].rhs)
45+
rhs_val = Symbolics.value(rhs_simplified)
46+
@test rhs_val isa Number && iszero(rhs_val)
4747

4848
# Test empty constructor
4949
diff = DifferentialEquation()

test/symbolics.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,10 @@ end
148148

149149
# try something harder!
150150
term = (a + b * cos(f * t + θ)^2)^3 * sin(f * t)
151-
@eqtest fourier_sin_term(term, f, t) ==
152-
expand(
153-
a^3 + a^2 * b * 3//2 + 9//8 * a * b^2 + 5//16 * b^3 -
154-
3//64 * b * (16 * a^2 + 16 * a * b + 5 * b^2) * cos(2 * θ),
155-
)
151+
@eqtest fourier_sin_term(term, f, t) == expand(
152+
a^3 + a^2 * b * 3//2 + 9//8 * a * b^2 + 5//16 * b^3 -
153+
3//64 * b * (16 * a^2 + 16 * a * b + 5 * b^2) * cos(2 * θ),
154+
)
156155

157156
@eqtest fourier_cos_term(term, f, t) ==
158157
expand(-3//64 * b * (16 * a^2 + 16 * a * b + 5 * b^2) * sin(2 * θ))
@@ -220,7 +219,8 @@ end
220219
@eqtest sort(get_all_terms(a + b + c); by=string) == sort([a, b, c]; by=string)
221220
@eqtest sort(get_all_terms(a * b * c); by=string) == sort([a, b, c]; by=string)
222221
@eqtest sort(get_all_terms(a / b); by=string) == sort([a, b]; by=string)
223-
@eqtest sort(get_all_terms(a^2 + b^2 + c^2); by=string) == sort([a^2, b^2, c^2]; by=string)
222+
@eqtest sort(get_all_terms(a^2 + b^2 + c^2); by=string) ==
223+
sort([a^2, b^2, c^2]; by=string)
224224
@eqtest sort(get_all_terms(a^2 / b^2); by=string) == sort([a^2, b^2]; by=string)
225225
@eqtest sort(get_all_terms(2 * b^2); by=string) == sort([2, b^2]; by=string)
226226
@eqtest sort(get_all_terms(2 * b^2 ~ a); by=string) == sort([2, b^2, a]; by=string)

0 commit comments

Comments
 (0)