Skip to content

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

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.

Project coverage is 76.93%. Comparing base (5b61308) to head (ebdb7e4).
Report is 1520 commits behind head on master.

Files with missing lines Patch % Lines
src/utils.jl 95.23% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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

Development

Successfully merging this pull request may close these issues.

SymPy x.coeff like functionality Add a coefficient function Extracting degree and coefficients of a polynomial

4 participants