Example #1
0
        # Read a poly until it works.  An EOF will except out of the
        # program.
        while 1:
            try:
                poly = polynomial.read('Enter a polynomial coefficients: ')
            except EOFError:
                raise
            except:
                print('Conversion failed.  Please try again.')
            else:
                break

        # Read and evaluate x values until the user types a blank line.
        # Again, and EOF will except out of the pgm.
        while 1:
            resp = input('Enter x value or blank line: ')
            if resp == 'quit': raise EOFError
            if not resp: break
            try:
                x = int(resp)
            except ValueError:
                print("That doesn't look like an integer.  Please try again.")
            else:
                print('p(x) =', polynomial.srep(poly))
                print('p(' + str(x) + ') =', polynomial.eval(x, poly))

except (EOFError, KeyboardInterrupt):
    # Exit without error for EOF or ^C.  Print a blank line to clear after
    # any prompt.
    print()
Example #2
0
__author__ = 'Nathaniel Klein - Coded on my MBPro - 06/04/15'

from polynomial import bisection
from polynomial import eval

a = 1
b = 7
poly = [-119, 3, -4, 0, 7]
tolerance = 1e-12

# Prints the value of the root
print(bisection(a, b, poly, tolerance))

# Stores the returned value of the root in rootval
rootval = (bisection(a, b, poly, tolerance))

# Evaluates the polynomial at the value of the root
print(eval(rootval,poly))
Example #3
0
__author__ = 'Nathaniel Klein - Coded on my MBPro - 06/04/15'

import re
from polynomial import eval

x = 17

list = [2, 3, 4, 0, 7]

print("The polynomial evaluates to ", eval(x,list))
print(" at x =", x)

    # Repeat until an exception or quit gets us out.
    while 1:
        # Read a poly until it works.  An EOF will except out of the
        # program.
        while 1:
            try:
                poly = polynomial.read('Enter a polynomial coefficients: ')
            except:
                print 'Conversion failed.  Please try again.'
            else:
                break

        # Read and evaluate x values until the user types a blank line.
        # Again, and EOF will except out of the pgm.
        while 1:
            resp = raw_input('Enter x value or blank line: ')
            if resp == 'quit': raise EOFError
            if not resp: break
            try:
                x = int(resp)
            except ValueError:
                print "That doesn't look like an integer.  Please try again."
            else:
                print 'p(x) =', polynomial.srep(poly)
                print 'p(' + str(x) + ') =', polynomial.eval(x, poly)

except (EOFError, KeyboardInterrupt):
    # Exit without error for EOF or ^C.  Print a blank line to clear after
    # any prompt.
    print
        except TypeError as msg:
            response = "X:TypeError - " + str(msg)

        except ValueError as msg:
            response = "X:ValueError - " + str(msg)

        # if an error has not already occurred
        if response is "":
            # Error check: must have params length of at least 2
            if values[0][:1] is "E":
                if len(data) < 2:
                    response = "X:Invalid argument length - must have (x, poly)"
                else:
                    x = data[0]
                    poly = data[1:]
                    response = "E" + str(eval(x,poly))

            # Error check: must have params length of at least
            elif values[0][:1] is "S":
                if len(data) < 4:
                    response = "X:Invalid argument length - must have (a, b, poly, tolerance)"
                else:
                    a = data[0]
                    b = data[1]
                    poly = data[2: len(data)-1]
                    tolerance = data[len(data) -1]
                    response = "S" + str(bisection(a, b, poly, tolerance))

            else:
                response = "X:Invalid Operation Code |" + values[0][:1] + "|"