mathjs
mathjs copied to clipboard
Should `rationalize()` allow to explicitly exclude symbols from polynomial generators / include symbols in the domain of coefficients?
In SymPy, you can explicitly specify the polynomial generators and domain of coefficients. In particular, you can extend the domain of coefficients with symbols, which makes it possible to have symbolic coefficients, see:
https://docs.sympy.org/latest/modules/polys/basics.html
Is there a way to emulate this in mathjs? This would be the desired behavior (not working/pseudo code):
const ret = math.rationalize('a*x+b', {}, {variables: ["x"], domainExtensions: ["a", "b"], detailed: true})
// ret.expression="a*x+b", ret.variables = ["x"], ret.coefficients=["b", "a"]
My suspicion is that this currently is not possible due to limitations in the rewriting rules, but maybe there is some clever trick to make it work.
Sorry I don't fully understand your idea, can you elaborate?
Sure. If I understand rationalize()
correctly, the current implementation automatically assumes that any symbol in the expression is a polynomial generator. However, it can sometimes be useful to also include symbols into the domain of coefficients. The linked SymPy documentation contains some examples (e.g. using pi
as a symbolic constant). See:
e = (x + 2*pi)*y
e.as_poly()
Poly(x*y + 2*y*pi, x, y, pi, domain='ZZ')
e.as_poly(x, y)
Poly(x*y + 2*pi*y, x, y, domain='ZZ[pi]')
In the first case x
, y
, and pi
are polynomial generators, whereas in the second case only x
and y
are.
Perhaps, the current implementation should be left as is, and instead it would be better to provide an alternative function for these use cases.
Ah, so you would like to be able to treat specific symbols like pi
as a constant rather than a variable in the polynomial? That makes sense. My initial feeling is that it could be well implemented as a new option for the existing rationalize
function.
Anyone interested into trying this out? Help would be welcome.
Yes, that is another great way of putting it. I suppose (one of) the biggest question(s) would be how to distinguish "constant" vs "variable" symbols in the rules that rationalize()
uses to transform the expression. Provided that, my feeling is that the existing rules should work (with the only modification being to take this additional distinction into account). However, the rules are quite complex and I don't have the experience necessary to make the changes. There is certainly a possibility that things could break in subtle ways...
I just wanted to log this "feature request", since it seems feasible in principle, and it would be rather useful to have in certain practical applications.
Since this is more of a feature idea than a specifically actionable issue as it stands, moving it to Discussions.