Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mathematical function input
#1
What is the best way to input a mathematical function (for example 5x^3+2x-5) in python? Also, can I somehow get a random number of polynoms? (for example up to x^5 or x^6) without defining the numbers of them inbefore?
Thanks for all help in advance,
Till
Reply
#2
The best way to do a mathematical function with variables would be to use a string, otherwise, Python will give you a syntax error. As far as I know, Python does not work with Polynomials. There could however be a library. You could also make your own.
Reply
#3
numpy can do polynomials: https://docs.scipy.org/doc/numpy/referen...mpy.poly1d

Your example, 5x^3+2x-5, would look like this:
>>> import numpy
>>> eq = numpy.poly1d([5, 0, 2, -5])
>>> print(eq)
   3
5 x + 2 x - 5
>>> for i in range(5):
...   print(f"x={i}: {eq(i)}")
...
x=0: -5
x=1: 2
x=2: 39
x=3: 136
x=4: 323
It'll work with any length of polynomial (so yes, x^5 or x^6), since it's based on the length of the list you pass it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to properly extract mathematical equations and images from PDF for a Python RAG c IchNar 2 97 Jan-27-2026, 11:53 PM
Last Post: Pedroski55
Question Two arguments in input function Alfredd 4 811 Nov-09-2025, 12:56 AM
Last Post: Pedroski55
  Using a For Loop to subtract numbers from an input function. Anunderling 9 2,819 Sep-22-2025, 08:56 PM
Last Post: deanhystad
  Input function oldschool 1 1,255 Sep-14-2024, 01:02 PM
Last Post: deanhystad
  difference between forms of input a list to function akbarza 6 3,499 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  plotting based on the results of mathematical formulas Timur 1 1,342 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Taking Mathematical Expressions from Strings quest 2 2,358 Jul-02-2023, 01:38 PM
Last Post: Pedroski55
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 3,131 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Showing an empty chart, then input data via function kgall89 0 1,902 Jun-02-2022, 01:53 AM
Last Post: kgall89
  input function question barryjo 12 5,872 Jan-18-2022, 12:11 AM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020