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

factor function #81

Open
elfunesto opened this issue Jan 23, 2020 · 11 comments
Open

factor function #81

elfunesto opened this issue Jan 23, 2020 · 11 comments

Comments

@elfunesto
Copy link

Hello,
Is there an equivalent of the factor function provided by in sympy package (i.e. the opposition of the expand function? https://www.geeksforgeeks.org/python-sympy-factor-method/ ? If not, is there plan to implement it? I guess this depends on the availability in the symengine engine? Anyway, thank you for your brilliant package.
Hilaire

@Marlin-Na
Copy link
Member

I think both factor and simplify are currently missing from symengine C++ library.

@elfunesto
Copy link
Author

That was my guess. Hope it will be implemented one day, this would be very helpful. Thank you for your answer

@mattfidler
Copy link
Contributor

In RxODE, we did factoring by symengine -> expand -> dsl parsing. It is possible, but it would be done outside of symengine.

@isuruf
Copy link
Member

isuruf commented Jan 24, 2020

@mattfidler, is this univariate polynomial factoring?

@mattfidler
Copy link
Contributor

mattfidler commented Jan 24, 2020

It is multivariate polynomial factoring. However, in principle it is the same as univariate polynomial factoring applied to each term in a polynomial or non-polynomial expression. I used it for inductive linearization.

@isuruf
Copy link
Member

isuruf commented Jan 24, 2020

Yeah, a generic factoring would be able to factor, sin(x)**2 + 2*sin(x)*log(x) + log(x)**2, but I guess that's not needed here.

@mattfidler
Copy link
Contributor

Not for my purposes. You are right, of course.

@elfunesto
Copy link
Author

@mattfidler , thank you for your suggestion. I have never used RxODE so I will have a look. Do you have a piece of code to illustrate the procedure?

@mattfidler
Copy link
Contributor

Hm. It is buried in the package (and it is off-topic for this repository)

First after using symengine's expand, I split it with rxSplitPlusQ for the + and - operators:

https://github.com/nlmixrdevelopment/RxODE/blob/b0e77313a4127c5b41c1805ea89d002205a35a2f/R/symengine.R#L2108-L2190

I can then use that same function to split with the * and / operators and then see if they match what I'm factoring.

This is used in the mutiple item factoring found here:

https://github.com/nlmixrdevelopment/RxODE/blob/pruneBranch/R/rxIndLin.R#L27-L129

@mattfidler
Copy link
Contributor

library(RxODE);
lapply(rxSplitPlusQ("a*exp(b+c)+d*log(e-f)-g*f"),function(x){eval(parse(text=sprintf("rxSplitPlusQ(quote(%s),mult=TRUE)",x)))})
#> [[1]]
#> [1] "a"          "exp(b + c)"
#> 
#> [[2]]
#> [1] "d"          "log(e - f)"
#> 
#> [[3]]
#> [1] "-g" "f"

Created on 2020-01-27 by the reprex package (v0.3.0)

@elfunesto
Copy link
Author

Thank you very much!

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

4 participants