Related questions
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 nowThis is a popular solution!
Step by stepSolved in 2 steps
- Print numbers in for loop in Javaarrow_forwardWrite a java code for the following . Write a Java program to print the unique elements along with their frequency in the given (3x 3) integer matrix. ex. java sample outputarrow_forwardWrite a recursive C++ program that will output all the subsets of a set of n elements (without repeating any subsets).arrow_forward
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY