Extending integration rules

61 views
Skip to first unread message

Mr Y

unread,
Dec 10, 2024, 9:17:09 AM12/10/24
to sympy
Hi!

I'm new to SymPy so excuse me if this question is misplaced.

Basically I would like to teach SymPy how to integrate my custom function:

```
from sympy import Function

tau = symbols("tau")
fn = Function("fn")
gn = Function("gn")

result = integrate(fn(tau), tau)
```
Is there a way to add my own rules to `integrate` routine?

peter.st...@gmail.com

unread,
Dec 10, 2024, 9:25:36 AM12/10/24
to sy...@googlegroups.com

Frankly, I am not clear what you want to do.

Can you give an example?

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/6d3ef1f4-d6eb-43d0-8809-4d9fea262335n%40googlegroups.com.

Mr Y

unread,
Dec 10, 2024, 7:46:14 PM12/10/24
to sympy
Sure,

Suppose that you are integrating an equation of line L(x, x1, y1, x2, y2) which is being multiplied by a certain complicated expression. One way of representing a line is to represent through it's parameters:

def Line(x, x1, y1, x2, y2):
    return y1 + ((x - x1) (y2 - y1))/(x2 - x1)

But again in certain situations you don't want to have such detailed abstraction as it might complicate your expression especially if your expression is already too complicated. You would want the line to abstract the underlying complexity of all it's parameters and have it represented as a symbolic function which hides it's dependence on parameters x1, y1, x2, y2:

from sympy import *

tau = symbols("tau")
Line = Function("Line")
fn = Function("fn")

result = integrate(Line(tau) * fn(tau), tau)

In my specific case I would like to instruct SymPy that I would like to perform integration by parts whenever I encounter Line which should result in:

result = Line(tau) * Integral(fn(tau), tau) - Integral(Integral(fn(tau), tau) * Derivative(Line(tau), tau), tau)

Now since I know that the Derivative(Line(tau), tau) is a constant I would like to apply another rule and have the above expression to be simplified into:

result = Line(tau) * Integral(fn(tau), tau) - Derivative(Line(tau), tau) * Integral(Integral(fn(tau), tau), tau)

(This rule is universal in context governed by the definition of Line and would not work with other functions)

The major requirement for above examples is ability to operate at higher abstraction level and still be able to perform efficient manipulation and reduction.

Alternative example 
For example consider a Step, Box functions. Step(x) is a function which is equal to 1 for all x ≥ 0, zero otherwise. Box is equal to Step(x + 1/2) - Step(x - 1/2). 

There is however little sense in using above representations as they are too detailed and would complicate the result of integration so again it makes sense to maintain higher level of abstraction and to define these functions symbolically like so:

from sympy import *

tau = symbols("tau")
Step = Function("Step")
Box = Function("Box")
fn = Function("fn")

result = integrate(fn(tau) * Box(tau), tau)

and then to somehow explain to SymPy that the integration of expression that involves multiplication Box can be reduced to following form:

result = Box(tau) * (Integral(fn(tau), {tau, -1/2, 1/2}) / 2 + Integral(fn(tau), tau)) + Step(tau - 1/2) * Integral(fn(tau), {tau, -1/2, 1/2})

(this is a universal rule that comes directly from definition of Box function but SymPy doesn't know that)

Again what I'm asking is whether there is a way to extend existing integration rule system for the integrate function to account for the cases that I could provide it with. 

P.S.

I understand that I could use pattern matching to find and transform the expressions or perform monkey-patching of integrate method in order to check for above patterns but this all seems to quirky, are there any proper mechanisms to extend integrations 

Reply all
Reply to author
Forward
0 new messages