The follow questions need to be answered in the language of
The follow question(s) need to be answered in the language of PYTHON please.
Crate a class for polynomial Mathematics. The class should be able to perform the following tasks on a polynomial:
----Addition (of one polynomial with another)
----Subtraction (of one polynomial with another)
----Multiplication (of one polynomial with another)
----Differentiate polynomial
----Integrate polynomial
----Evaluation at a point (X) through implementation of__call__magic method
----Pretty printout of Coefficients, e.g.
F(x) = a(sub 0) + a(sub 1)x^1 + … + a(sub i)x^I, for a(sub i) not 0
So basically, after you get the mathematics correct you will be implementing the following object methods
__int__ (to define your polynomial)
__add__ (to add a polynomial to self and create new polynomial
__sub__ (to subtract polynomial to self and create new polynomial
__mult__ (to multiply a polynomial to self and create a new polynomial
__str__ (to produce string representation of polynomial)
__call__ (to evaluate polynomial at point x)
__pow__ (to raise polynomial to an integral Power –not required – but will be fun to do. If you do use Horner’s Rule)
To define your polynomial class – let’s make things easier for all of us. I request that the __int__ member function have the following signature:
Def __int__ (self, coefficient_dictionary):
Where coefficient_dictionary is a dict of coefficient and exponent values.
Thus: 1+x+5x^4 would be represented as {0:1 , 1:1, 4:5} in that dict!
I also request that you provide the following get set function for you polynomial that returns the dict used to represent coeffiecient and exponent values. This will make my job of checking your answers easier.
Def get_polynomial(self):
Return dict of coefficients and exponents
Solution
Following is the required python code :
# def slice( source, start, end ):
 #    length = len(source);
 #    if start >= length:
 #        return [];
 #    if end < start or end >= length:
 #        return [];
 #    #if we reach here, we have acceptable values of start and end
#    #without using python\'s slice operator
 #    result = [];
 #    for i in range(start, end+1):
 #        result.append( source[i] );
 #    return result;
# #testing
 # myList = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\"];
 # print slice( myList, 0 , 9);
 # print slice( myList, 3 , 4);
 # print slice( myList, 4 , 3);
 # print slice( myList, 3 , 8);
 # print slice( myList, 4 , 4);
 class Mathematics:
    def __init__( self, coefficient_dictionary ):
        self.coefficient_dictionary = coefficient_dictionary;
   def __add__( self, polynomial ):
        allExponents = self.coefficient_dictionary.keys() + \\
                            polynomial.coefficient_dictionary.keys();
        allExponents = list( set( allExponents ) );
        newCoefficientDictionary = {};
        for exp in allExponents:
            value = 0;
            if self.coefficient_dictionary.has_key( exp ):
                value = value + self.coefficient_dictionary[ exp ];
            if polynomial.coefficient_dictionary.has_key( exp ):
                value = value + polynomial.coefficient_dictionary[ exp ];
            if value <> 0:
                newCoefficientDictionary[ exp ] = value;
        return Mathematics( newCoefficientDictionary );
   def __sub__( self, polynomial ):
        allExponents = self.coefficient_dictionary.keys() + \\
                            polynomial.coefficient_dictionary.keys();
        allExponents = list( set( allExponents ) );
        newCoefficientDictionary = {};
        for exp in allExponents:
            value = 0;
            if self.coefficient_dictionary.has_key( exp ):
                value = value + self.coefficient_dictionary[ exp ];
            if polynomial.coefficient_dictionary.has_key( exp ):
                value = value - polynomial.coefficient_dictionary[ exp ];
            if value <> 0:
                newCoefficientDictionary[ exp ] = value;
        return Mathematics( newCoefficientDictionary );
   def __mult__( self, polynomial ):
        selfExp = (self.coefficient_dictionary).keys();
        polyExp = (polynomial.coefficient_dictionary).keys();
        newCoefficientDictionary = {};
        for exp1 in selfExp:
            for exp2 in polyExp:
                newExp = exp1+ exp2;
                newCoeff = self.coefficient_dictionary[ exp1 ] * \\
                            polynomial.coefficient_dictionary[ exp2 ];
                if newCoefficientDictionary.has_key( newExp ):
                    newCoefficientDictionary[ newExp ] = \\
                        newCoefficientDictionary[ newExp ] + newCoeff;
                else:
                    newCoefficientDictionary[ newExp ] = newCoeff;
        return Mathematics( newCoefficientDictionary );
   def __str__( self ):
        result = \"\";
        exps = self.coefficient_dictionary.keys();
        exps.sort();
        length = len(exps);
        i = 0;
        for exp in exps:
            i = i + 1;
            result = result + str(self.coefficient_dictionary[exp]);
            if exp == 0:
                pass;
            else:
                result = result + \'x\';
                if exp <> 1:
                    result = result + \'^\' + str(exp);
            if i <> length:
                result = result + \'+\';
        return result;
   def __call__( self , x ):
        value = 0;
        for exp in self.coefficient_dictionary.keys():
            value = value + self.coefficient_dictionary[ exp ]*pow( x, exp );
        return value;
           
    def get_polynomial( self ):
        return self.coefficient_dictionary;
key = {0:1,1:3,2:4};
 z = Mathematics( key );
 key1 = {0:2,1:5,2:9};
 y = Mathematics(key1);
 print (y.__add__(z )).coefficient_dictionary;
 print (y.__sub__(z )).coefficient_dictionary;
 print (y.__mult__(z )).coefficient_dictionary;
 print (y.__str__());
 print (z.__str__());
 print (z.__call__(2));



