From 9cba87405672413505495969bd989d563ff3f7ae Mon Sep 17 00:00:00 2001 From: Maximilian HUEBL Date: Sun, 25 Aug 2024 20:51:01 +0200 Subject: [PATCH 1/6] fix latexify on complex expressions --- src/latexify_recipes.jl | 6 ++++-- test/latexify.jl | 1 + test/latexify_refs/complex4.txt | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 test/latexify_refs/complex4.txt diff --git a/src/latexify_recipes.jl b/src/latexify_recipes.jl index b056252fb..2b42953d1 100644 --- a/src/latexify_recipes.jl +++ b/src/latexify_recipes.jl @@ -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] @@ -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 diff --git a/test/latexify.jl b/test/latexify.jl index 9480adc91..d5249abc8 100644 --- a/test/latexify.jl +++ b/test/latexify.jl @@ -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) diff --git a/test/latexify_refs/complex4.txt b/test/latexify_refs/complex4.txt new file mode 100644 index 000000000..d7a210d25 --- /dev/null +++ b/test/latexify_refs/complex4.txt @@ -0,0 +1,3 @@ +\begin{equation} +\mathit{i} \sqrt{2} +\end{equation} From 5318f298294a2ef1ccabc8b3a6c82b5d432ed74d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 08:12:51 +0000 Subject: [PATCH 2/6] Bump crate-ci/typos from 1.23.1 to 1.24.1 Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.23.1 to 1.24.1. - [Release notes](https://github.com/crate-ci/typos/releases) - [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md) - [Commits](https://github.com/crate-ci/typos/compare/v1.23.1...v1.24.1) --- updated-dependencies: - dependency-name: crate-ci/typos dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/SpellCheck.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/SpellCheck.yml b/.github/workflows/SpellCheck.yml index a72b85172..63a4a7ec8 100644 --- a/.github/workflows/SpellCheck.yml +++ b/.github/workflows/SpellCheck.yml @@ -10,4 +10,4 @@ jobs: - name: Checkout Actions Repository uses: actions/checkout@v4 - name: Check spelling - uses: crate-ci/typos@v1.23.1 \ No newline at end of file + uses: crate-ci/typos@v1.24.1 \ No newline at end of file From bd14f03ce56aa64ef90995a89e50f835e5fa8949 Mon Sep 17 00:00:00 2001 From: Mao Zeng Date: Mon, 26 Aug 2024 12:58:04 +0100 Subject: [PATCH 3/6] Fix `coeff(p, sym)` when `sym` is a product, by iteratively computing the coefficient w.r.t. each term in the product. --- src/utils.jl | 21 +++++++++++++++++++++ test/coeff.jl | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index c6cafdb00..61458dc9e 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -315,15 +315,36 @@ 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) sym = nothing end + return _coeff(p, sym) +end + +""" + _coeff(p, sym) + +Function used internally by `coeff(p, sym)`, after the latter function performs some initial steps and re-assigns `p, sym = value(p), value(sym)` +""" +function _coeff(p, sym) if issym(p) || isterm(p) sym === nothing ? 0 : Int(isequal(p, sym)) elseif ispow(p) diff --git a/test/coeff.jl b/test/coeff.jl index 5ae4b71bd..ea5ad7d4a 100644 --- a/test/coeff.jl +++ b/test/coeff.jl @@ -54,3 +54,9 @@ 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` From db048a09baef8ba92a9f94a8a9f89fb0558913fa Mon Sep 17 00:00:00 2001 From: Mao Zeng Date: Mon, 26 Aug 2024 13:10:32 +0100 Subject: [PATCH 4/6] Delete blank line in test/coeff.jl. --- test/coeff.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/coeff.jl b/test/coeff.jl index ea5ad7d4a..8bef7f22f 100644 --- a/test/coeff.jl +++ b/test/coeff.jl @@ -56,7 +56,6 @@ e = x*y^2 + 2x + y^3*x^3 @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` From e658d7ed8541a34492426bd943471b3962b27715 Mon Sep 17 00:00:00 2001 From: Mao Zeng Date: Mon, 26 Aug 2024 13:21:33 +0100 Subject: [PATCH 5/6] Delete function `_coeff` by merging the lines back into the function `coeff`. --- src/utils.jl | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 61458dc9e..7628e1394 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -336,15 +336,6 @@ function coeff(p, sym=nothing) sym = nothing end - return _coeff(p, sym) -end - -""" - _coeff(p, sym) - -Function used internally by `coeff(p, sym)`, after the latter function performs some initial steps and re-assigns `p, sym = value(p), value(sym)` -""" -function _coeff(p, sym) if issym(p) || isterm(p) sym === nothing ? 0 : Int(isequal(p, sym)) elseif ispow(p) From 2bcf115aea5191c77688ebbdf47f1b646cd87d15 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 27 Aug 2024 11:40:39 +0530 Subject: [PATCH 6/6] fix: call `transform` on dependent array variables in `@variables` --- src/variable.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/variable.jl b/src/variable.jl index 919260ab1..f4813fcc8 100644 --- a/src/variable.jl +++ b/src/variable.jl @@ -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