Well I am trying to write a simple "polynomial engine" that has two functions:

  • simplifyPolynomial
  • solvePolynomial

I just don't know where to start:(
I can't think of a method to use. I don't think there are any polynomial libraries in java?
My question is: do any of you have any idea on a simple way to simplify polynomials?

Please help!

Thanks in advanced,
Andrew

Dani AI

Generated

Good start, — parsing and combining like terms is the right idea. Your current method works for simple tests but is fragile: avoid hard-coded lengths (use x.length), avoid direct float == 0 checks (use an epsilon or BigDecimal), prefer && for boolean logic, and remove duplicated/unreachable else if branches (the first matching branch wins, so later coeff == 0 checks may never run). Also JOptionPane.showConfirmDialog returns an int; use showMessageDialog to display values if that was your intent. As suggested, instrument the code first (print xs, x2s, and the arrays) to confirm inputs.

A simpler, robust workflow is: parse each term, determine its exponent (0 if no "x", 1 if "x" without "^"), parse the numeric coefficient (default 1 or -1 when omitted), then accumulate coefficients by exponent in a map. After accumulation, iterate exponents descending and format the polynomial while skipping near-zero coefficients. Example of the accumulation step in Java:

Map<Integer, BigDecimal> coeffs = new TreeMap<>(Collections.reverseOrder());
coeffs.merge(exp, coeff, BigDecimal::add);  // add coefficients for same exponent

Troubleshooting tips: write unit tests for inputs like "x+2x-3", "-x^2 + x - 1", and "2x^2 + 0x -2". Use an epsilon (e.g. 1e-9) or BigDecimal to decide when a coefficient is effectively zero, and format output to hide ".0" for integers. If you need full-featured polynomial arithmetic later, note mentioned a Java package for polynomial ops and pointed to homotopy solvers for systems — those are options once parsing/combining is solid.

Recommended Answers

All 5 Replies

So a basic question, you won't find any polynomial library
What is the probelm with the code you wrote ?

That's the problem, I don't have any code I have written! I am very talented at math or at least I was when I was doing algebra in 5th grade. So I keep going through the steps of evaluating:

  1. Combine alike terms
  2. Re-order alphabetically

I get stuck at the very first item "Combine alike terms" I just don't know how I would do that, since the user, is supposed to enter the polynomial, and I get it as a string.

I know that if I can evaluate the polynomial then solving it becomes easy if you know the unknowns.

Help please,

Andrew

I don't know if I should make a new thread for this so I will use this one which I have created!

Ok well I wrote a class that simplifies polynomials, but I always get weird answers. Take a look at the code and then at the bottom of the post.

package polynomialhelper;
import javax.swing.*;

/**
 * This class helps with polynomials!
 * @author Andrew
 */
public class polynomialEngine {

    /**
     * Class constructor nothing interesting.
     */
    public polynomialEngine() {
    }

    /**
     *
     * @param x the array of x coefficients
     * @param x2 the array of x<sup>2</sup> coefficients
     * @param coeff coefficient at the end
     * @return polynomial in string state.
     */
    public String simplifyPolynomial(float[] x, float[] x2, float coeff) {
        String polynomial = "Error";
        float xs = 0, x2s = 0;

        for(int i = 0; i <3; i++) {
            xs = xs + x[i];
            x2s += x2[i];
        }

        JOptionPane.showConfirmDialog(null, x2s);

        if ( xs != 0 & x2s !=0 ) {
        polynomial = "<html>" + String.valueOf(x2s) + "x<sup>2</sup>+" + String.valueOf(xs) + "x+" + String.valueOf(coeff) + "</html>";
        }

 else if (xs != 0 & x2s == 0) {
        polynomial = "<html>" + String.valueOf(xs) + "x" + "+" + String.valueOf(coeff) + "</html>";
        }

 else if (xs == 0 & x2s != 0) {
        polynomial = "<html>" + String.valueOf(x2s) + "x<sup>2</sup>+" + String.valueOf(coeff) + "</html>";
        }

 else if (xs == 0 & x2s == 0) {
            polynomial = "<html>" + String.valueOf(coeff) + "</html>";
        }

 else if (xs != 0 & x2s != 0 & coeff == 0) {
        polynomial = "<html>" + String.valueOf(x2s) + "x<sup>2</sup>+" + String.valueOf(xs) + "x" + "</html>";
        }

 else if (xs != 0 & x2s == 0 & coeff == 0) {
        polynomial ="<html>" +  String.valueOf(xs) + "x" + "</html>";
        }

 else if (xs == 0 & x2s != 0) {
        polynomial = "<html>" +  String.valueOf(x2s) + "x<sup>2</sup>" + "</html>";
        }

else {
           polynomial = "Error please check data!";
            JOptionPane.showMessageDialog(null, "Error please check the data you enter!", "Polynomial Helper", JOptionPane.ERROR_MESSAGE);



}


        return polynomial;

    }
}

Please note that I am using this class in an other program, it has a limit of three coefficients per-variable and here are some of the test values I passed and here is what I got:

1st x value: 1
2nd x value: 2
3rd x value: 3
1st x^2 value: 1
2nd x^2 value: 2
3rd x^3 value: 3
Coeff. value : 4

Answer: 6x^2 + 6x + 4 (totally right)


1st x value: 1
2nd x value: 3
3rd x value: 5
1st x^2 value: 2
2nd x^2 value: 4
3rd x^3 value: 6
Coeff. value : 7

Answer: 11x^2 + 9x + 7

What is wrong with my code?:(

My package com.perisic.ring (available at ring.perisic.com) provides polynomial arithmetic.

If you are running on windows you should use a system call to bertini32 which solves polynomial systems using homotopic continuation. It will run slower but will find much more difficult polynomial systems.

See:

http://dl.acm.org/citation.cfm?id=1473186]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.