How use creating polynomial expression like function in Python?

2024/11/15 5:47:57

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
Answer
class Polynomial:def __init__(self, coeficents, degrees=None):if degrees = None:self.degree = list(reversed(range(len(coeficents))))else:self.degree = degreesself.coeficents = coeficentsdef __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.

https://en.xdnf.cn/q/120474.html

Related Q&A

how to merge two sublists sharing any number in common? [duplicate]

This question already has an answer here:Using sublists to create new lists where numbers dont repeat(1 answer)Closed 9 years ago.Given thatg=[[1,2,3,4],[4,5,6],[6,7],[10,11]]What code should I use to …

\n is treated as \ and n [duplicate]

This question already has an answer here:Compiler error "error: stray \ in program" in macro definition(1 answer)Closed 10 years ago.The following python codeenv.Command(versionFile, allSrcs …

How to locate an element and extract required text with Selenium and Python

I am using Selenium to enter data on a web page, but have run into an issue with one of the input fields. This is the HTML code causing my difficulty:<div class="form-group"> <label …

Determine type of disk on Windows with Python

I want to list all of my disk and can be able to know its type: "SATA" "NVME" "M.2" or "PCI" on Windows computer. I made some research with wmi and I get the int…

How to get a list the visible vertices and segments of a mesh

I work on pose estimation of a 3d objects. I am using CAD model of that object to generate all the possible hypothesis of its pose. I am using pyopengl to render the view of the object from a specific…

Python function calls the wrong method/target

The following program simulates a traffic light system with some buttons. The buttons appear correctly, but if Im trying to call the method to create/change the LEDs, it ends up in the wrong method. He…

How to make a triangle of xs in python?

How would I write a function that produces a triangle like this:xxxxxxxxxx xxxxxLets say the function is def triangle(n), the bottom row would have n amount of xsAll I know how to do is make a box:n = …

Pip freeze --local

I am following a video tutorial and that guy did this:$ pip freeze --local > requirement.txt $ cat requirement.txtthis is to export all these packages with their versions in another project, but how…

How to optimize this Pandas code to run faster

I have this code to create a swarmplot from data from a DataFrame:df = pd.DataFrame({"Refined__Some_ID":some_id_list,"Refined_Age":age_list,"Name":name_list …

i was creating a REST api using flask and while i was about to test it on postman I saw that error

File "c:\Users\kally\rest\code\app.py", line 3, in <module>from flask_jwt import JWTFile "C:\Users\kally\AppData\Roaming\Python\Python310\site-packages\flask_jwt\__init__.py",…