Skip to content

Conversation

@philippeitis
Copy link
Contributor

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.

a = Polynomial(1, 2, 3)
a.indefinite_integral
>>> 0.3333333333333333x^3 + x^2 + 3.0x + C
a.integral(0, 2)
>>> 12.666666666666666

a = Constant(1)
a.indefinite_integral
>>> x + C
a.integral(-10, 10)
>>> 20.0

a = Monomial(1, 2)
a.indefinite_integral
>>> 0.3333333333333333x^3 + C
a.integral(-11, -10.5)
>>> 57.79166666666663

a = ZeroPolynomial()
a.indefinite_integral
>>> C
a.integral(-1000, 1000)
>>> 0

Comment on lines +146 to +163
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"]
)

Copy link
Owner

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.

Copy link
Contributor Author

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?

Copy link
Owner

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:

  1. throwing an error (current)
    or
  2. implementing something like nth_integral which returns two things: the result without the constants and a polynomial with the constants
    or
  3. implementing a class Integral which handles this and can have a polynomial be easily extracted from it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants