Davide Modesto
Davide Modesto

Reputation: 225

Integrate over 2d polygon quadpy


I am trying to integrate a function on a 2d polygon described by its vertices as follow

import numpy as np
import quadpy


def f(x):
    return x[0]


poly = np.array([[0.0, 0.0], [1.0, 0.0], [0, 1], [1,1]])

scheme = quadpy.t2.get_good_scheme(10)
val = scheme.integrate(f, poly)

But I get

QuadpyError: Wrong domain shape.

I really appreciate any kind of help

Upvotes: 0

Views: 341

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84519

quad.t2 is for triangles, three points are expected. Your polygon is a square, you have to use quad.c2.

import quadpy

scheme = quadpy.c2.get_good_scheme(7)
val = scheme.integrate(
    lambda x : x[0],
    [ [[0.0, 0.0], [1.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]] ]
)
val

This gives 0.5, which is easy to get mathematically.

See the link for the way to specify the quadrilateral.

Upvotes: 1

Related Questions