Skip to content
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

[v1.0.1] Drop quadpy dependency #95

Closed
adtzlr opened this issue Oct 25, 2021 · 4 comments
Closed

[v1.0.1] Drop quadpy dependency #95

adtzlr opened this issue Oct 25, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@adtzlr
Copy link
Owner

adtzlr commented Oct 25, 2021

As mentioned in #92 quadpy increases the import time drastically. We can drop that dependency and use built-in functions of numpy.

@adtzlr adtzlr added the enhancement New feature or request label Oct 25, 2021
@adtzlr
Copy link
Owner Author

adtzlr commented Oct 25, 2021

this would be a simple replacement

def gauss_legendre(order, dim):
    "Gauss-Legendre Points and Weights."
    
    x, w = np.polynomial.legendre.leggauss(order)
    
    points = np.stack(
        np.meshgrid(*([x] * dim), indexing="ij")
    )[::-1].reshape(dim, -1).T
    
    idx = ["i", "j", "k"][:dim]
    weights = np.einsum(", ".join(idx), *([w] * dim)).ravel()
    
    return points, weights

@adtzlr
Copy link
Owner Author

adtzlr commented Oct 25, 2021

even nicer because it works on arbitrary dimensions...

import numpy as np
from string import ascii_lowercase


def gauss_legendre(order, dim):
    "Gauss-Legendre Points and Weights."
    
    x, w = np.polynomial.legendre.leggauss(order)
    
    points = np.stack(
        np.meshgrid(*([x] * dim), indexing="ij")
    )[::-1].reshape(dim, -1).T
    
    idx = list(ascii_lowercase)[: dim]
    weights = np.einsum(", ".join(idx), *([w] * dim)).ravel()
    
    return points, weights

@adtzlr adtzlr mentioned this issue Oct 27, 2021
@adtzlr
Copy link
Owner Author

adtzlr commented Oct 27, 2021

see

class GaussLegendre(Scheme):
"A n-dimensional Gauss-Legendre quadrature rule."
def __init__(self, order: int, dim: int):
"""Arbitrary `order` Gauss-Legendre quadrature rule of `dim` 1, 2 or 3
on the interval [-1, 1] as approximation of
∫ f(x) dx ≈ ∑ f(x_a) w_a (1)
with `a` quadrature points `x_a` and corresponding weights `w_a`.
"""
# integration point weights and coordinates
if dim not in [1, 2, 3]:
raise ValueError("Wrong dimension.")
x, w = np.polynomial.legendre.leggauss(1 + order)
points = (
np.stack(np.meshgrid(*([x] * dim), indexing="ij"))[::-1].reshape(dim, -1).T
)
idx = list(ascii_lowercase)[:dim]
weights = np.einsum(", ".join(idx), *([w] * dim)).ravel()
super().__init__(points, weights)

@adtzlr
Copy link
Owner Author

adtzlr commented Oct 27, 2021

fixed by #96

@adtzlr adtzlr closed this as completed Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant