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

implement coeff #677

Merged
merged 2 commits into from
Aug 10, 2022
Merged

implement coeff #677

merged 2 commits into from
Aug 10, 2022

Conversation

t-bltg
Copy link
Contributor

@t-bltg t-bltg commented Jul 29, 2022

Hi,

This is a proposition in order to obtain the coefficients from Symbolic expressions (focusing on monomials).

using Symbolics
@variables a x y

julia> coeff(2a, x)
0
julia> coeff(a*x, x)
a
julia> coeff(3x + 2y, x)
3
julia> coeff(x * y, x)
y
julia> coeff(x^2 + y, x^2)
1

Fix #216.
Fix #374.
Fix #236.

I've also rewritten degree as a nfc in the same pattern as coeff for consistency (enhance after #241).

Open question (which needs to be settled for coeff & degree): how do we handle SymbolicUtils.Div ?
degree(x / y, x) currently throws a StackOverflowError (added as a @test_broken in test/degree.jl).

EDIT: handling Div is outside the scope of this PR.

@t-bltg t-bltg force-pushed the coefficients branch 4 times, most recently from bf53d3d to 7594e71 Compare July 29, 2022 18:11
@bowenszhu
Copy link
Member

I am wondering the expected results of the following cases.

coeff(((x + 1)^4 + x)^3, x^2)
coeff((x^2 - 1) / (x - 1), x)
coeff((x^(1//2) + y^0.5)^2, x)

@bowenszhu
Copy link
Member

Open question (which needs to be settled for coeff & degree): how do we handle SymbolicUtils.Div ?

degree(x / y, x) currently throws a StackOverflowError (added as a @test_broken in test/degree.jl).

This is tricky. What do you expect for this case?

p = (y^2 + y + 1) / (y + 1)

@t-bltg
Copy link
Contributor Author

t-bltg commented Aug 1, 2022

Thanks for the comments.

coeff(((x + 1)^4 + x)^3, x^2)
coeff((x^2 - 1) / (x - 1), x)

These examples work if you expand or simplify the given expression prior to feeding coeff:

julia> ((x + 1)^4 + x)^3 |> expand |> e -> coeff(e, x^2)
93
julia> (x^2 - 1) / (x - 1) |> simplify |> e -> coeff(e, x)
1

I believe it should be up to the user to expand or simplify the given expression first before feeding coeff. Doing that in the background in coeff is not a good idea: e.g. when querying x, x^2, or more coeffs it would result in un-necessary duplicate (potentially costly) expand - simplify calls.

coeff((x^(1//2) + y^0.5)^2, x)

Thanks, I'll have a look: sympy returns 0 on that one too.

julia> (x^(1//2) + y^0.5)^2 |> expand |> e -> coeff(e, x)
1

Works after expansion too.

This is tricky. What do you expect for this case?

Agreed, so maybe let's focus on examples not involving quotients (monomials). This PR isn't meant to be exhaustive, but incremental.

Honestly, I don't this PR enhanced #241.

Maybe the wording wasn't right, but to me clarity of the code is an enhancement. Consistency is also imo a necessity. I'll rephrase that.
I also show in #685 that this PR enhances #241 from a performance pov.

@t-bltg t-bltg force-pushed the coefficients branch 2 times, most recently from 84518ca to 5f9b686 Compare August 1, 2022 09:58
@bowenszhu
Copy link
Member

Honestly, I don't this PR enhanced #241.

Maybe the wording wasn't right, but to me clarity of the code is an enhancement. Consistency is also imo a necessity. I'll rephrase that. I also show in #685 that this PR enhances #241 from a performance pov.

True. Sorry about that.

@shashi
Copy link
Member

shashi commented Aug 1, 2022

Nice! Could you add doc strings for coeff and degree. I think we don't want to export degree anymore since it conflicts with exported names in another commonly used package.

@shashi shashi requested a review from YingboMa August 1, 2022 19:09
@t-bltg
Copy link
Contributor Author

t-bltg commented Aug 1, 2022

I think we don't want to export degree anymore since it conflicts with exported names in another commonly used package.

We have the same problem of too many exported symbols in Plots.jl ;) Removed.

Could you add doc strings for coeff and degree.

Added.

@codecov-commenter
Copy link

codecov-commenter commented Aug 1, 2022

Codecov Report

Merging #677 (ebdb7e4) into master (5b61308) will decrease coverage by 0.05%.
The diff coverage is 95.23%.

@@            Coverage Diff             @@
##           master     #677      +/-   ##
==========================================
- Coverage   76.99%   76.93%   -0.06%     
==========================================
  Files          23       23              
  Lines        2712     2710       -2     
==========================================
- Hits         2088     2085       -3     
- Misses        624      625       +1     
Impacted Files Coverage Δ
src/Symbolics.jl 71.42% <ø> (ø)
src/utils.jl 75.78% <95.23%> (-1.15%) ⬇️
src/semipoly.jl 96.66% <0.00%> (ø)

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@bowenszhu
Copy link
Member

resolve #612

@shashi shashi merged commit c893846 into JuliaSymbolics:master Aug 10, 2022
@shashi
Copy link
Member

shashi commented Aug 10, 2022

Thank you!!

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