symengine.R icon indicating copy to clipboard operation
symengine.R copied to clipboard

factor function

Open elfunesto opened this issue 5 years ago • 11 comments

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

elfunesto avatar Jan 23 '20 16:01 elfunesto

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

Marlin-Na avatar Jan 23 '20 17:01 Marlin-Na

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

elfunesto avatar Jan 23 '20 19:01 elfunesto

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

mattfidler avatar Jan 24 '20 22:01 mattfidler

@mattfidler, is this univariate polynomial factoring?

isuruf avatar Jan 24 '20 22:01 isuruf

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.

mattfidler avatar Jan 24 '20 22:01 mattfidler

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.

isuruf avatar Jan 24 '20 22:01 isuruf

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

mattfidler avatar Jan 24 '20 23:01 mattfidler

@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?

elfunesto avatar Jan 25 '20 07:01 elfunesto

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 avatar Jan 27 '20 19:01 mattfidler

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)

mattfidler avatar Jan 27 '20 19:01 mattfidler

Thank you very much!

elfunesto avatar Jan 27 '20 19:01 elfunesto