Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify_fractions fails #647

Open
matthiasbaitsch opened this issue Sep 9, 2024 · 0 comments
Open

simplify_fractions fails #647

matthiasbaitsch opened this issue Sep 9, 2024 · 0 comments

Comments

@matthiasbaitsch
Copy link

matthiasbaitsch commented Sep 9, 2024

Recently I had a case, where simplify_fractions (and thus simplify in Symbolics.jl) fails.

@syms a::Real b::Real c::Real d::Real e::Real
simplify_fractions(1 / (4 * a * b) * (4 * (c + d) + 4 * e / 3))

(c + d) / (a*b)

From what I could figure out, the problem originates in rm_gcds(ns, ds) where 4//1 is correctly identified as GCD but somewhere down the line (in MultivariatePolynomials.jl), div(4//3, 4//1) returns 0 such the the term with e disappears from the sum.

Maybe it would be an idea, to treat a rational divisor differently. The following code fixes the issue for the example above while the tests still pass.

_divide_by_divisor(x, y) = div(x, y)

_divide_by_divisor(x::PolyForm, y::Rational) =
    PolyForm{promote_symtype(/, symtype(x), symtype(y))}(
        MP.map_coefficients(term -> term / y, x.p), x.pvar2sym, x.sym2term
    )

function rm_gcds(ns, ds)
    ns = flatten_pows(ns)
    ds = flatten_pows(ds)
    for i = 1:length(ns)
        for j = 1:length(ds)
            g = _gcd(ns[i], ds[j])
            if !_isone(g)
                ns[i] = _divide_by_divisor(ns[i], g)
                ds[j] = _divide_by_divisor(ds[j], g)
            end
        end
    end
    filter!(!_isone, ns)
    filter!(!_isone, ds)
    ns, ds
end

However, I'm sure that I miss something, my understanding of SymbolicUtils is extremely limited.

Since I am pretty new to Julia and this is the first time I try to contribute to an open source project, any advice how to proceed is warmly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant