-3

I'd like to write a program in Python where user define a deegre of polynomial and coefficients (a,b,c). When program create a polynomial expression with this data I'd like to use it like function because I need this to other operations. How can i get it? For example when I have polynomial= x^n+a^n-1+b^n-2+c^-3 I'd like to use it in polynomial(x) to calculate value.

Now the creating polynomial method looks:

def polynomial(n,a,b,c):
    return a*x**n+b*x**3-c*x
  • What have you tried so far? StackOverflow is not your personal coding service, but we do love to help those who already have tried to solve their own coding problems. – JCJ Jan 12 '18 at 14:05
  • Some people have trouble writing an understandable question, let alone write any code. – heroworkshop Jan 12 '18 at 14:13
  • I write some code... –  Jan 12 '18 at 14:17
  • If I am not mistaken, you asked the same question yesterday and got some answers. Why did you delete it and ask the same question again? – Mr. T Jan 12 '18 at 14:29

2 Answers2

1
class Polynomial:

    def __init__(self, coeficents, degrees=None):
        if degrees = None:
            self.degree = list(reversed(range(len(coeficents))))
        else:
            self.degree = degrees
        self.coeficents = coeficents

    def __call__(self, x):
        print(self.coeficents)
        print(self.degree)
        return sum([self.coeficents[i]*x**self.degree[i] for i in range(len(self.coeficents))])



p = Polynomial([1,2,4],[10,2,0])
print(p(2))

This will compute the polynomial x^10 + 2x^2 + 4 at x = 2. It should be very clear how to use with your example.

modesitt
  • 7,052
  • 2
  • 34
  • 64
  • Ok i find this too but can I use it from my defined polynomial? –  Jan 12 '18 at 14:21
  • `degree` could be defined as a simple `range` with step -1. – Mad Physicist Jan 12 '18 at 14:22
  • 1
    @Piotr. Where did you find it, if I might ask? – Mad Physicist Jan 12 '18 at 14:23
  • @piotr. don't know how you would "find" it. Also your polynomial of the form `x^n+a^n-1+b^n-2+c^-3` is really `(x+a+b)^n + c^-3 - 3`. You should really do this yourself, but I will show you. – modesitt Jan 12 '18 at 14:25
  • @Mad Physicist I find something in documentation in numPy to creating a polynomial but i'd like to define my own and this is a problem –  Jan 12 '18 at 14:26
  • This is defining your own @PiotrStelmach. I extended the answer to include arbitrary degrees. This should be very clear. – modesitt Jan 12 '18 at 14:27
  • @PiotrStelmach can you please accept the answer if this works. – modesitt Jan 12 '18 at 14:38
-1

The best strategy is not to pass in n, but you will need to pass in x. You should instead pass the coefficients in as a list. You don't need to pass in n as it is calculated from the length of the list.

  def poly(coefs, x):
      result=0
      N=len(coefs)
      n=0
      while N-n>0:
        result+=coefs[n]*(x**(N-n-1))
        n+=1
      return result

So if you want to calculate, for example x^2 + 3x -5 where x=5, you would use this line:

print(poly([1,3,-5], 5))
heroworkshop
  • 365
  • 2
  • 6
  • Ok but I'd like to pass the n because I need this from my task –  Jan 12 '18 at 14:34
  • Then you can add a parameter n. You then just need to decide if you want to simply ignore it or if you want to check that it matches the length and throw an error if it doesn't. – heroworkshop Jan 12 '18 at 14:37