Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:34:07+00:00 2026-06-13T20:34:07+00:00

I am currently doing a python exercise for my University studies. I am very

  • 0

I am currently doing a python exercise for my University studies. I am very stuck at this task:

The taylor polynomial of degree N for the exponential function e^x is given by:

        N
p(x) = Sigma  x^k/k!  
k = 0

Make a program that (i) imports class Polynomial (found under), (ii) reads x and a series of N values from the command line, (iii) creates a Polynomial instance representing the Taylor polynomial, and (iv) prints the values of p(x) for the given N values as well as the exact value e^x. Try the program out with x = 0.5, 3, 10 and N = 2, 5, 10, 15, 25.

Polynomial.py

import numpy

class Polynomial:
def __init__(self, coefficients):
    self.coeff = coefficients

def __call__(self, x):
    """Evaluate the polynomial."""
    s = 0
    for i in range(len(self.coeff)):
        s += self.coeff[i]*x**i
    return s

def __add__(self, other):
    # Start with the longest list and add in the other
    if len(self.coeff) > len(other.coeff):
        result_coeff = self.coeff[:]  # copy!
        for i in range(len(other.coeff)):
            result_coeff[i] += other.coeff[i]
    else:
        result_coeff = other.coeff[:] # copy!
        for i in range(len(self.coeff)):
            result_coeff[i] += self.coeff[i]
    return Polynomial(result_coeff)

def __mul__(self, other):
    c = self.coeff
    d = other.coeff
    M = len(c) - 1
    N = len(d) - 1
    result_coeff = numpy.zeros(M+N+1)
    for i in range(0, M+1):
        for j in range(0, N+1):
            result_coeff[i+j] += c[i]*d[j]
    return Polynomial(result_coeff)

def differentiate(self):
    """Differentiate this polynomial in-place."""
    for i in range(1, len(self.coeff)):
        self.coeff[i-1] = i*self.coeff[i]
    del self.coeff[-1]

def derivative(self):
    """Copy this polynomial and return its derivative."""
    dpdx = Polynomial(self.coeff[:])  # make a copy
    dpdx.differentiate()
    return dpdx

def __str__(self):
    s = ''
    for i in range(0, len(self.coeff)):
        if self.coeff[i] != 0:
            s += ' + %g*x^%d' % (self.coeff[i], i)
    # Fix layout
    s = s.replace('+ -', '- ')
    s = s.replace('x^0', '1')
    s = s.replace(' 1*', ' ')
    s = s.replace('x^1 ', 'x ')
    #s = s.replace('x^1', 'x') # will replace x^100 by x^00
    if s[0:3] == ' + ':  # remove initial +
        s = s[3:]
    if s[0:3] == ' - ':  # fix spaces for initial -
        s = '-' + s[3:]
    return s

def simplestr(self):
    s = ''
    for i in range(0, len(self.coeff)):
        s += ' + %g*x^%d' % (self.coeff[i], i)
    return s


def _test():
    p1 = Polynomial([1, -1])
    p2 = Polynomial([0, 1, 0, 0, -6, -1])
    p3 = p1 + p2
print p1, '  +  ', p2, '  =  ', p3
p4 = p1*p2
print p1, '  *  ', p2, '  =  ', p4
print 'p2(3) =', p2(3)
p5 = p2.derivative()
print 'd/dx', p2, '  =  ', p5
print 'd/dx', p2,
p2.differentiate()
print '  =  ', p5
p4 = p2.derivative()
print 'd/dx', p2, '  =  ', p4

if __name__ == '__main__':
_test()

Now I’m really stuck at this, and I would love to get an explaination! I am supposed to write my code in a separate file. I’m thinking about making an instance of the Polynomial class, and sending in the list in argv[2:], but that doesn’t seem to be working. Do I have to make a def to calculate the taylor polynomial for the different values of N before sending it in to the Polynomial class?

Any help is great, thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:34:08+00:00Added an answer on June 13, 2026 at 8:34 pm

    Not quite finished, but this answers your main question I believe. Put class Polynomial in poly.p and import it.

    from poly import Polynomial as p
    from math import exp,factorial
    
    def get_input(n):
        ''' get n numbers from stdin '''
        entered = list()
        for i in range(n):
            print 'input number '
        entered.append(raw_input())
        return entered
    
    def some_input():
        return [[2,3,4],[4,3,2]]
    
    
    
    get input from cmd line                                                                                                                                   
    n = 3                                                                                                                                                     
    a = get_input(n)                                                                                                                                          
    b = get_input(n)                                                                                                                                          
    
    
    #a,b = some_input()
    
    ap = p(a)
    bp = p(b)
    
    
    print 'entered : ',a,b
    
    c = ap+bp
    
    print 'a + b = ',c
    
    print exp(3)
    
    x = ap
    print x
    
    sum = p([0])
    for k in range(1,5):
        el = x
        for j in range(1,k):
            el  el * x
            print 'el: ',el
        if el!=None and sum!=None:
            sum = sum + el
            print 'sum ',sum
    

    output

    entered :  [2, 3, 4] [4, 3, 2]
    a + b =  6*1 + 6*x + 6*x^2
    20.0855369232
    2*1 + 3*x + 4*x^2
    sum  2*1 + 3*x + 4*x^2
    el:  4*1 + 12*x + 25*x^2 + 24*x^3 + 16*x^4
    sum  6*1 + 15*x + 29*x^2 + 24*x^3 + 16*x^4
    el:  4*1 + 12*x + 25*x^2 + 24*x^3 + 16*x^4
    el:  8*1 + 36*x + 102*x^2 + 171*x^3 + 204*x^4 + 144*x^5 + 64*x^6
    sum  14*1 + 51*x + 131*x^2 + 195*x^3 + 220*x^4 + 144*x^5 + 64*x^6
    el:  4*1 + 12*x + 25*x^2 + 24*x^3 + 16*x^4
    el:  8*1 + 36*x + 102*x^2 + 171*x^3 + 204*x^4 + 144*x^5 + 64*x^6
    el:  16*1 + 96*x + 344*x^2 + 792*x^3 + 1329*x^4 + 1584*x^5 + 1376*x^6 + 768*x^7 + 256*x^8
    sum  30*1 + 147*x + 475*x^2 + 987*x^3 + 1549*x^4 + 1728*x^5 + 1440*x^6 + 768*x^7 + 256*x^8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In python, I am currently doing this: if user_can_read(request.user, b) == False: Is there
I am learning python atm, and was doing an exercise from this site --
I'm currently doing facebook integration and have gone through this tutorial . So far,
I am currently writing my python classes and instantiate them like this class calculations_class():
I'm currently doing the following to create and execute a simple python calculation, using
I am new to Python and currently am doing some translations from 2.7 to
I am currently doing some I/O intensive load-testing using python. All my program does
I am writing a website using Python / Django, and am currently doing server-side
Currently, when trying to reference some library code, I'm doing this at the top
I'm currently doing some stuffs with Python and I get some strange behavior when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.