Short snippet in C# to do this classic trick, I could not find one on the web. Little, (even none!) error checking is done here.

Member Avatar
Member Avatar
+0 forum 3

This snippet shows how to find the complex roots of a polynomial using python (tested with python 2.6). You need the scipy or numpy module.

Member Avatar
Member Avatar
+2 forum 6

Here is a simple Polynomial class. Its purpose is to show how one can [B]simply [/B]create a Polynomial class without using abstract datastructures such as linked lists etc. Each term is printed out in order of its power - from highest to lowest. Inspiration was taken from a Java source code, the link appears to now be dead.

Member Avatar
Member Avatar
+2 forum 9

Don’t know if this is ever been done before, or even if it is a good way to do it. I’ll just await your comments if any. What I mostly found on the web is that if you want to define a polynomial you need to have an array containing the coefficients. The place of the coefficient in the array determines the power of the unknown, you have to include a 0(zero) in the array for the powers that are not in your polynomial. So I tried to do it using a list of tuples, with one element containing the …

Member Avatar
+0 forum 0

need a program to perform polynomial division with linked lists or arrays. i already tried multiplication,subtraction and addition but stucked in division can anyone help me out on this.

Member Avatar
+0 forum 0

Hi, in my polynomial class all the terms consist of a List of tuples (double, uint), representing the coefficient and the exponent; a real and a natural number. The +operator implementation works great, but I was wondering if I could avoid to write two times `grouping.Sum(s => s.Item1)` It somehow feels not good, but I can't seem find a way to circumvent it. If any LINQ guru out there, could provide me with a helping hand that would be nice! :) Here is the code: public static tuplePolynomial operator +(tuplePolynomial tp1, tuplePolynomial tp2) { tuplePolynomial Result = new tuplePolynomial(); Result.Terms …

Member Avatar
Member Avatar
+0 forum 2

I wrote this code for mutipicate two polynomials but I want change it as it can get two polynomials and then multicipate them for example: input: 5x -2x^2 +x -1 -4 -2x output: 4x^3 -4x^2 -22x +4 public class Polynomial{ public int[b+1]; coef[b] = a; deg = degree(); } // return the degree of this polynomial (0 for the zero polynomial) public int degree() { int d = 0; for (int i = 0; i < coef.length; i++) if (coef[i] != 0) d = i; return d; } // return c = a + b public Polynomial plus(Polynomial b) { …

Member Avatar
Member Avatar
-1 forum 4

Hello, I have to write a program that will solve polynomials by user input, my program compiles, but gets an exception error. Here is my code: import java.io.*; import java.util.Scanner; import java.util.LinkedList; public class PolynomialList { private Node first = new Node(0, 0); private Node last = first; private static class Node { int co_eff; int expo; Node next; Node(int co_eff, int expo) { this.co_eff = co_eff; this.expo = expo; } } private PolynomialList() { } public PolynomialList(int co_eff, int expo) { last.next = new Node(co_eff, expo); last = last.next; } public PolynomialList plus(PolynomialList b) { PolynomialList a = this; …

Member Avatar
Member Avatar
+0 forum 1

Hello, I have used taylor polynomials to approximate floating-point values before. I am wondering whether there is a way to still apply them to functions with complex arguments, and if there is a way what its constraints would be. For example, to calculate the sine of a floating-point number to a specific precision I keep adding terms until the error value is less than the precision. However, I noticed (by testing it with the taylor expansion of ln(x) ) that this gives the wrong value when I use complex numbers (with the standard operators appropriately defined). For example: ln(0.5)~=-(0.5+(0.5^2)/2+(0.5^3)/3) --- …

Member Avatar
Member Avatar
+0 forum 1

I want to make two polynomials and then add them. We have two lists, [1,2,0,3] and [2,3,0,0,1]. The polynomials are as followed: 3x^3+2x+1 and x^4+3x+2. I have the following code, but I'm getting stuck. Any advice? I'm new to python. def polyAdd(poly1,poly2): for i in range(len(poly1)): z = poly1[i] return z for j in range(len(poly2)): y = poly2[j] return y if i == j: coef = y + z print(coef)

Member Avatar
Member Avatar
+0 forum 1

Here is my first Code Snippet contribution: a C++ sub-routine that computes the roots of a quadratic equation. Notes: * This sub-routine checks if a = 0. * IF a = 0, then the equation is actually not a quadratic equation; it is a linear equation with one--if any--root. * IF the user misses the output statement alerting the user to this fact, the second root has been explicitly assigend a NaN value so that the result is not mistaken for a number.

Member Avatar
Member Avatar
+1 forum 10

I am creating a method to multiply 2 polynomial expressions together such that: `3x^5 * 2x^3 = 6x^8` -> Where the coefficients are multiplied and the exponents are added together. My test case for this would look something like the following @Test public void times01() throws TError { assertEquals(Term.Zero, Term.Zero.times(Term.Zero)); } I should also add that `Term.Zero = (0,0)` and `Term.Unit = (1,0)` So anything multiplied by `Term.Zero` is `Term.Zero` and anything multiplied by `Term.Unit` returns itself as `Term.Unit` effectively is 1. public Term times(Term that) throws CoefficientOverflow, ExponentOverflow, NegativeExponent { return null; } This is the `times` method. I'm …

Member Avatar
Member Avatar
+0 forum 7

I have a Java class called Term holding polynomials like below public Term(int c, int e) throws NegativeExponent { if (e < 0) throw new NegativeExponent(); coef = c; expo = (coef == 0) ? 1 : e; } I also have an equals method in the same class like below @Override public boolean equals(Object obj) { } I am stuck with how to code how to compare these 2 Term objects Within my JUnit test file I am using the test below to try and test the equals method static org.junit.Assert.*; import org.junit.Test; public class ConEqTest { private int …

Member Avatar
Member Avatar
+0 forum 4

There was discussion thread http://www.daniweb.com/software-development/python/threads/424953/polynomial-division for class based implementation for polynomials, and I developed a version utilizing the magic methods to enable operation with normal arithmetic operation. I only inherited sequence structure from list type. Interoperability with scalar types is not implemented to avoid too complicated type checks of the arithmetic operations. **Division will be added** when the OP has made his own solution of it ready. Tests are continued to be run and I will update the code with bug fixes if and when I find any. I have done informal tests but not yet exhaustive ones. Multiplying by …

Member Avatar
Member Avatar
+1 forum 9

Hi everyone, this is my first post and I am new to C++. I am trying to figure out how to do this project. Newton’s method is an algorithm that can be used to solve an equation of the form f(x) = 0 using a sequence of approximations. Here’s how it works: You make a reasonable guess for a solution, x0. You should create a function which calculates the value of the polynomial at a specific x value (the inputs for the function should be the array representing the polynomial, its size, and the x-value to evaluate at), and a …

Member Avatar
Member Avatar
+0 forum 7

Newton's Method to find polynomial solution Hi everyone, this is my first post and I am new to C++. I am trying to figure out how to do this project. Newton’s method is an algorithm that can be used to solve an equation of the form f(x) = 0 using a sequence of approximations. Here’s how it works: You make a reasonable guess for a solution, x0. You should create a function which calculates the value of the polynomial at a specific x value (the inputs for the function should be the array representing the polynomial, its size, and the …

Member Avatar
Member Avatar
+0 forum 1

Hello to everyone! This is my first post here, I hope you can help me. I'm making a class work in python and I'm a little lost: I have to use classes to represent monomials and polynomials, and the classes should contain methods to add, substract, multiply and divide them. So far, I've been able to create the whole monomials class, and almost all the polynomial class, but I can't find a way to code polynomial division. Surfing the web I've seen some methods based on binary conversion and things like that, but that doesn't seem to work for me …

Member Avatar
Member Avatar
+0 forum 9

Hello problem i am having is when trying to multiply polynomial 1 by polynomial 2 polynomial 1 gets mutliplied by the first term in polynomial 2 and not by each term in polynomial 2. Below is my code: ~~~~ /********************************************************************************************* * Multiplies Polynomial 1 to Polynomial 2 * The method does not change the original polynomial. **********************************************************************************************/ public Polynomial multiply(Polynomial poly) { Polynomial res = clone(); for(Monomial tmp2 = poly.head; tmp2 != null; tmp2 = tmp2.next) //-------- LINE B for(Monomial tmp = res.head; tmp != null; tmp = tmp.next) res.addTerm(tmp.coeff *= poly.head.coeff, tmp.deg += poly.head.deg); double num = 0.5; for(Monomial …

Member Avatar
Member Avatar
+0 forum 1

I need to Compute 2x4 +7x3 -15x2 +3x+2 Using Horners Method. When I run this in QTSpim it doesn't give me any errors but I'm just wondering if everything is running completely right. For instance did I call "x" correctly? I'm trying to load the value for x from memory by using an lw instruction. Set up a base register to use with the instruction. After the polynomial was calculated I stored it in memory with an sw instruction (using the same base register). I just want to be sure I'm doing this right. Thank you for your assistance. [CODE] …

Member Avatar
+0 forum 0

I have to do my first assignment for my java class for friday this week. So due in 3 days. I wrote the polynomial class but haven't done the driver for it yet as i don't know if my polynomial class is good. Please check and let me know if i have done correctly according to my assignment details (assignment pdf file is attached). I am missing the numberOfTerms as i can't seem to figure that out. Please help me out. Polynomial.java [CODE] public class Polynomial { private int[] coeff = new int[5]; public Polynomial() { coeff = null; } …

Member Avatar
-1 forum 0

Hi all, I just make a program and I get in trouble about set method.so anyone can help me? [CODE] import java.util.LinkedList; public class Polynomial { private LinkedList<Integer> coeff = new LinkedList<Integer>(); private int deg = 0; /* Constructs a new polynomial of degree zero */ public Polynomial() { } /* Returns an integer representing the coefficient * of the x^power term */ int getCoefficients(int power) { return coeff.get(power); } /* Sets the coefficient of the x^power term to coef.*/ void setCoefficients(int coef, int power) { } /* Returns the String representation of the polynomial. * For example, a polynomial …

Member Avatar
Member Avatar
+0 forum 1

#include<iostream> using namespace std; class polynomial { private: struct p { int degree; float coefficient; p *link; }*head; public: polynomial() { head=NULL; } void getdegree(); void getpolynomial(int d); void display(); void addition(polynomial p1,polynomial p2); }; void polynomial::getdegree() { int degree1; cout<<"\n Enter the highest degree of your polynomial:"; cin>>degree1; getpolynomial(degree1); } void polynomial::getpolynomial(int degree1) { int i=0; p *temp,*data; float *arr; arr=new float[degree1+1]; for(i=degree1;i>=0;i--) { if(i==0) { cout<<"\n Enter the constant term:"; cin>>arr[i]; } else { cout<<"\n Enter the coefficient for degree x^"<<i<<":"; cin>>arr[i]; } } for(i=degree1;i>=0;i--) { if(head==NULL) { head=new p; head->coefficient=arr[i]; head->degree=i; head->link=NULL; } else { temp=head; while(temp->link!=NULL) …

Member Avatar
Member Avatar
+0 forum 1

hi in my text book i have this problem. how to program this [ICODE] Define a class Quadratic_int that stores the coefficients of a quadratic polynomial in a dynamically allocated array of integers. Supply the "big three" memory management functions. Use this class to demonstrate (a) the difference between initialization Quadratic_int s; Quadratic_int t = s; and assignment operation Quadratic_int s; Quadratic_int t; s = t; (b) the fact that all constructed objects are automatically destroyed (c) the fact that the copy constructor is invoked if an object is passed by value to a function (d) the fact that the …

Member Avatar
Member Avatar
-2 forum 5

[CODE]ifndef POLY_H #define POLY_H class Poly { public: Poly() : first(0) { } void addTerm(double coeff, unsigned exponent); // add term (coeff^exponent) to the polynomial; // might just change coefficient of existing term, // or might insert new term; maintains terms // in order from greatest to least exponent private: struct Term { double coeff; unsigned exponent; Term *next; Term(double c=0, unsigned e=0) // Term constructor done : coeff(c), exponent(e), next(0) { } }; Term *first; // pointer to first polynomial term void printAbsTerm(Term *t) const; }; #endif [/CODE] I need some help so I can understand what I need …

Member Avatar
Member Avatar
+0 forum 4

So this is my final project for my class. The idea is to use my RationalNum class (which is complete and works fine... I just didn't attach the .cpp since it was only the driver) and use it to add, sub, & multi polynomials (RationalNum being the coefficients i.e. 1/4x^3 + 1/2x^2 + 1/3x, etc.). My over all goal is to input the amount of terms I want and then input the rational coefficients and then the exponets.... the rest will be automatically calculated from there. I am in a sense just starting this out and my mind is completely …

Member Avatar
Member Avatar
+0 forum 7

I have an assignment where I need to do some operations on polynomials (add, multiply, ...) which are represented by linked lists. Part of the assignment is to make an inner class Term containing two int fields and a constructor with coef and exp ints) I also need an inner class Node that has a term and node field, along with a constructor that also inludes term and node parameters. I also need to have a private Node field that will represent the polynomial as a singly linked list with a head (and ordered by exponent from smallest to largest). …

Member Avatar
Member Avatar
+0 forum 1

Polynomial hash code in hashing is used to convert character strings to numerical values. Use Horner’s rule to implement this algorithm, and also perform the character conversion using ASCII format. Conduct experiments to study the collision rates for this hash code by using different values (prime or non-prime numbers) for parameter a. Test this polynomial hash code on English document not less than 15000 words. [B][COLOR="Red"]What I just want to know, can anyone here tell me the "idea" how to implement this thing?[/COLOR][/B]

Member Avatar
+0 forum 0

[CODE] #ifndef POLYNOMIAL_H #define POLYNOMIAL_H class Polynomial { public: Polynomial(); Polynomial(int ,int []); ~Polynomial(); const Polynomial operator+(Polynomial& p) const;// addition const Polynomial operator- (Polynomial& p ) const; // subtraction int getExponent(); //void setExponent(); void setCoefficient(); int getCoefficient(); void printPolynomial(); private: int exponent; // exponent int coefficient[ 10 ]; // coefficients array }; #endif //end of class definition [/CODE] [CODE] //Polynomial.cpp #include "Polynomial.h" #include <iostream> using std::cout; using std::endl; using std::cin; Polynomial::Polynomial() //default constructor { exponent = 0; coefficient[10] = 0; } Polynomial::Polynomial(int x,int c[]) //constructor { if(x>=0 && x<10){ exponent=x; for(int i=0;i<=exponent;i++) coefficient[i]=c[i]; for(int i=exponent+1;i<10;i++) coefficient[i]=0; } else { exponent=0; …

Member Avatar
Member Avatar
+0 forum 2

Here some experiments to pretty print polynomials with help of vegaseat's tip of wx.lib.fancytext.

Member Avatar
Member Avatar
+1 forum 1

Hey there, I'm having trouble creating a multiplication function for the program as I have two classes to keep in mind (Monomial and Polynomial). And if possible, a little advice on how to make my for statements in the arithmetic operators more flexible to user input such as i<PolynomialSize, which I just can't seem to find a way around it. So any help or hints would be helpful. My functions work partially, by the way. Here's what I got so far: [CODE]/*-- Polynomial.h --------------------------------------------------------- Defines the monomial and polynomial classes. Polynomial is implemented basically as a list of monomials. ---------------------------------------------------------------------------*/ …

Member Avatar
Member Avatar
+0 forum 2

The End.