-
-
Notifications
You must be signed in to change notification settings - Fork 7
Implement integral, indefinite integral. #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| def integral(self, a, b): | ||
| """Return the integral of self from a to b.""" | ||
| res = self.indefinite_integral | ||
| # Last term of indefinite integral is C. | ||
| res[0] = 0 | ||
| return res.calculate(b) - res.calculate(a) | ||
|
|
||
| @property | ||
| def indefinite_integral(self): | ||
| """Return the polynomial object which is the integral of self.""" | ||
| if not self: | ||
| return Polynomial("C") | ||
|
|
||
| return Polynomial( | ||
| [c/x for c, x in | ||
| zip(self, range(self.degree + 1, 0, -1))] + ["C"] | ||
| ) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest adding a parameter for the value of the integration constant, since having the string "C" prevents the polynomial from being very useful for almost all operations and is maybe a not-so-desired result, as opposed to a plain 0 for example. Maybe having the parameter like constant="C" would cover that case, also there would be no need of lines 149 and 150 then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would you cover the case of double integrals? Would we make constants a seperate polynomial that goes at the end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, currently we haven't implemented anything allowing operations between string and number coefficients, which would be needed for applying indefinite_integral to a polynomial ending with "C". So three options come in my head in this scenario:
- throwing an error (current)
or - implementing something like
nth_integralwhich returns two things: the result without the constants and a polynomial with the constants
or - implementing a class
Integralwhich handles this and can have a polynomial be easily extracted from it
This implements integral, which takes a start and end point as arguments and returns the area of the polynomial over the specified range, and indefinite_integral, which returns the indefinite integral of the polynomial.