symengine.R
symengine.R copied to clipboard
factor function
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
I think both factor
and simplify
are currently missing from symengine C++ library.
That was my guess. Hope it will be implemented one day, this would be very helpful. Thank you for your answer
In RxODE, we did factoring by symengine -> expand -> dsl parsing. It is possible, but it would be done outside of symengine.
@mattfidler, is this univariate polynomial factoring?
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.
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.
Not for my purposes. You are right, of course.
@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?
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
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)
Thank you very much!