mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Should `rationalize()` allow to explicitly exclude symbols from polynomial generators / include symbols in the domain of coefficients?

Open filonik opened this issue 2 years ago • 4 comments

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.

filonik avatar Jun 15 '22 22:06 filonik

Sorry I don't fully understand your idea, can you elaborate?

josdejong avatar Jun 22 '22 13:06 josdejong

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.

filonik avatar Jun 25 '22 08:06 filonik

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.

josdejong avatar Jun 28 '22 07:06 josdejong

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.

filonik avatar Jun 28 '22 10:06 filonik

Since this is more of a feature idea than a specifically actionable issue as it stands, moving it to Discussions.

gwhitney avatar Oct 03 '23 01:10 gwhitney