Write a program in java for sparse polynomial
Write a

Multiplication of Sparse Polynomials
b) Program:
// SpasePolynomialsMultiplication
import java.util.ArrayList;
public class SpasePolynomialsMultiplication
{
/* create two objects for ArrayList class of type integers for two polynomial equations */
private ArrayList poly1 = new ArrayList();
private ArrayList poly2 = new ArrayList();
// parameterized constructor
public SpasePolynomialsMultiplication
{
poly1 = newPoly1;
poly2 = newPoly2;
} // end of constructor
// polynomialsMultiplication method
public ArrayList
{
ArrayList polyResult = new ArrayList();
for(int i = 0; i < poly1.size() + poly2.size()-1; i++){
polyResult.add(i, 0);
//Repeat the loop for all coefficients of the first equation.
for(int i = 0; i < poly1.size(); i++)
{
/* repeat the loop for all coefficients of the second equation */
for(int j = 0; j < poly2.size(); j++)
{
int value = poly1.get(i) * poly2.get(j);
int previousValue = polyResult.get(i + j);
//Store the resultant coefficients in the resultant equation.
polyResult.set(i + j, value + previousValue);
} // end inner for
} // end outer for
}
// return resultant equation
return polyResult;
} // end of polynomialsMultiplication method
// start main method
public static void main(String[] args)
{
//Create two objects for ArrayList class of type integers for two equations.
ArrayList equation1 = new ArrayList();
ArrayList equation2 = new ArrayList();
// In a sparse polynomial equation, the number of non-zero coefficients should be less than the number of zero-value coefficients.
// add coefficients to the first equation
equation1.add(0); // at exponential 0
equation1.add(0); // at exponential 1
equation1.add(1); // at exponential 2
equation1.add(0); // at exponential 3
equation1.add(-2); // at exponential 4
// In a sparse polynomial equation, the number of non-zero coefficients should be less than the number of zero-value coefficients.
// add coefficients to the second equation
equation2.add(-5); // at exponential 0
equation2.add(0); // at exponential 1
equation2.add(0); // at exponential 2
equation2.add(0); // at exponential 3
equation2.add(2); // at exponential 4
// Create an object for SpasePolynomialsMultiplication class with two equations.
SpasePolynomialsMultiplication spm = new SpasePolynomialsMultiplication
// Call polynomialsMultiplication method to get the resultant equation of the multiplication of the two equations.
ArrayList result = spm.polynomialsMultiplication(
// display the first equation
System.out.print("Equation 1: ");
for(int i = equation1.size() - 1; i >= 0; i--)
{
if(equation1.get(i) != 0)
{
if(equation1.get(i) > 0)
System.out.print("+" + equation1.get(i) + "x^" + i);
else
System.out.print(equation1.
}
} // end for
// display the second equation
System.out.print("\nEquation 2: ");
for(int i = equation2.size() - 1; i >= 0; i--)
{
if(equation2.get(i) != 0)
{
if(equation2.get(i) > 0)
System.out.print("+" + equation2.get(i) + "x^" + i);
else
System.out.print(equation2.
}
} // end for
// display the resultant equation
System.out.print("\nResultant Equation: ");
for(int i = result.size() - 1; i >= 0; i--)
{
if(result.get(i) != 0)
{
if(result.get(i) > 0)
System.out.print("+" + result.get(i) + "x^" + i);
else
System.out.print(result.get(i) + "x^" + i);
}
} // end for
} // end of main method
} // end of SpasePolynomialsMultiplication class
Trending now
This is a popular solution!
Step by step
Solved in 2 steps









