factor() gives up on any float coefficient, even 2.0, and returns an unevaluated node in which the coefficients have been rewritten as integers

Open
#381 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start by tracing the factor evaluation path and how it handles numeric coefficients, then compare it with the discriminant path, which the issue says accepts these floats. Reproduce the listed float and integer inputs in the R binding and find the relevant tests. Done when exact integer-valued floats factor correctly, or unsupported coefficients produce a domain error without changing the unevaluated expression.

Written by the indexing model from the issue text.

Description

area/symbolic help wanted kind/contract severity/3-major

factor(expr) is documented as "factors a polynomial or rational expression into irreducible factors over the rationals". Any coefficient written as a float makes it give up, even when the value is an exact integer:

library(rssn)
compute(parse_term("factor(x^2 - 2*x + 1)"))$text
# "(x - 1)^2"
compute(parse_term("factor(x^2 - 2.0*x + 1.0)"))$text
# "factor(x^2 - 2*x + 1)"          <- unevaluated, reduced = TRUE, value = NA
compute(parse_term("factor(x^2 - 4.0*x + 3.0)"))$text
# "factor(x^2 - 4*x + 3)"
compute(parse_term("factor(2.0*x^2 - 2.0)"))$text
# "factor(2*x^2 - 2)"
compute(parse_term("factor(1.0*x^2 - 1.0)"))$text
# "factor(x^2 - 1)"

The integer forms of the same four inputs all factor correctly ((x - 1)^2, (x - 3)*(x - 1), 2*(x - 1)*(x + 1), (x - 1)*(x + 1)), so the algorithm is there and only the float coefficients stop it.

Two things make this worse than a plain "unsupported input":

  1. The result is not a faithful echo. factor(x^2 - 2.0*x + 1.0) comes back as factor(x^2 - 2*x + 1) — the 2.0 has been silently rewritten as 2 in the unevaluated node, so the returned term no longer denotes the input expression.
  2. It hits the R binding's default numeric type. In R every numeric literal is a double, so a polynomial built the ordinary way (x^2 - 2*x + 1 with R numbers) always carries float coefficients and always fails; the user has to spell the expression with integer literals inside a string to get a factorization.

discriminant already handles the same float coefficients (discriminant(x^2 + 2.0*x + 1.0, x) = 0), so the polynomial family is inconsistent about it.

Expected: normalise exact float coefficients to rationals before factoring (2.0 → 2), or reject the input with a domain error naming the coefficient — not return an unevaluated call whose arguments have been changed.

rssn 0.3.0, R 4.6.2 binding, Windows x86_64.

Dominant language
Rust
Stars
36
Forks
3
Avg merge
14h 51m
Merged PRs (30d)
29

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Apich-Organization/rssn

All issues in Apich-Organization/rssn

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.