Skip to content

Commit

Permalink
Merge branch 'JuliaSymbolics:master' into multivar_stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rbed authored Aug 27, 2024
2 parents 8aac598 + b9d68b7 commit af41dfd
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/SpellCheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
- name: Checkout Actions Repository
uses: actions/checkout@v4
- name: Check spelling
uses: crate-ci/typos@v1.23.1
uses: crate-ci/typos@v1.24.1
6 changes: 4 additions & 2 deletions src/latexify_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ function _toexpr(O)
end
end

if isempty(numer) || !isone(abs(m.coeff))
if !isreal(m.coeff)
numer_expr = Expr(:call, :*, m.coeff, numer...)
elseif isempty(numer) || !isone(abs(m.coeff))
numer_expr = Expr(:call, :*, abs(m.coeff), numer...)
else
numer_expr = length(numer) > 1 ? Expr(:call, :*, numer...) : numer[1]
Expand All @@ -181,7 +183,7 @@ function _toexpr(O)
frac_expr = Expr(:call, :/, numer_expr, denom_expr)
end

if m.coeff < 0
if isreal(m.coeff) && m.coeff < 0
return Expr(:call, :-, frac_expr)
else
return frac_expr
Expand Down
12 changes: 12 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,21 @@ julia> Symbolics.coeff(3x + 2y, y)
julia> Symbolics.coeff(x^2 + y, x^2)
1
julia> Symbolics.coeff(2*x*y + y, x*y)
2
```
"""
function coeff(p, sym=nothing)
# if `sym` is a product, iteratively compute the coefficient w.r.t. each term in `sym`
if iscall(value(sym)) && operation(value(sym)) === (*)
for t in arguments(value(sym))
@assert !(t isa Number) "`coeff(p, sym)` does not allow `sym` containing numerical factors"
p = coeff(p, t)
end
return p
end

p, sym = value(p), value(sym)

if isequal(sym, 1)
Expand Down
4 changes: 1 addition & 3 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ function construct_dep_array_vars(macroname, lhs, type, call_args, indices, val,

ex = :($wrap($ex))

if call_args[1] == :..
ex = :($transform($ex))
end
ex = :($transform($ex))
if isruntime
lhs = gensym(lhs)
end
Expand Down
5 changes: 5 additions & 0 deletions test/coeff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,8 @@ e = x*y^2 + 2x + y^3*x^3
@test isequal(coeff(x / 5, x), 1//5)
@test isequal(coeff(x / y, x), 1/y)
@test isequal(coeff(x * 5y / (1 + y + z) , x), 5y / (1 + y + z))

# issue #1041 - coefficient of cross term in multivariate polynomial
@test isequal(coeff(2*x*y + y, x*y), 2)
@test isequal(coeff(2*x^2*y + y, x^2*y), 2)
@test_throws AssertionError coeff(2*x*y + y, 2*x*y) # numerical factors not allowed in second argument of `coeff`
1 change: 1 addition & 0 deletions test/latexify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Dy = Differential(y)
@test_reference "latexify_refs/complex1.txt" latexify(x^2-y^2+2im*x*y)
@test_reference "latexify_refs/complex2.txt" latexify(3im*x)
@test_reference "latexify_refs/complex3.txt" latexify(1 - x + (1+2x)*im; imaginary_unit="\\mathbb{i}")
@test_reference "latexify_refs/complex4.txt" latexify(im * Symbolics.Term(sqrt, [2]))

@test_reference "latexify_refs/indices1.txt" latexify(h[10,10])
@test_reference "latexify_refs/indices2.txt" latexify(h[10,10], index=:bracket)
3 changes: 3 additions & 0 deletions test/latexify_refs/complex4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\begin{equation}
\mathit{i} \sqrt{2}
\end{equation}

0 comments on commit af41dfd

Please sign in to comment.