Python numpy.polynomial.legendre.leggauss() Examples

The following are code examples for showing how to use numpy.polynomial.legendre.leggauss(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: lega   Author: MiroK   File: legendre_basis.py    MIT License 6 votes vote down vote up
def backward_transformation_matrix(N):
    '''
    Compute NxN matrix with values N_ij = L_i(x_j) where L_i are N Legendre
    polynomials and x_j are N GL quadrature points. This matrix is used for
    backward Legendre transformation: Suppose function f is represented in
    the wave number space by a vector F and let BL be the backward transformation
    matrix. Then f(x_j) = F.BL[:, j] or f = F.BL or BL.T.F, and vector f
    represents f in the real space.
    '''
    BL = np.zeros((N, N))
    # Get points of the guadrature
    points, _ = leggauss(N)
    for i in range(N):
        c = np.zeros(i+1)
        c[-1] = 1
        # Evaluate the i-th polynomial at all the points
        row = legval(points, c)

        BL[i, :] = row

    return BL 
Example 2
Project: shenfun   Author: spectralDNS   File: bases.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def points_and_weights(self, N=None, map_true_domain=False, weighted=True, **kw):
        if N is None:
            N = self.N
        if self.quad == "LG":
            points, weights = leg.leggauss(N)
        elif self.quad == "GL":
            points, weights = legendre_lobatto_nodes_and_weights(N)
        else:
            raise NotImplementedError

        if map_true_domain is True:
            points = self.map_true_domain(points)

        return points, weights 
Example 3
Project: LaserTOF   Author: kyleuckert   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 4
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 5
Project: recruit   Author: Frank-qlu   File: test_legendre.py    Apache License 2.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 6
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 7
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 8
Project: MARRtino-2.0   Author: DaniAffCH   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 9
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 10
Project: vnpy_crypto   Author: birforce   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 11
Project: ble5-nrf52-mac   Author: tomasero   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 12
Project: Computable   Author: ktraunmueller   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 13
Project: poker   Author: surgebiswas   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 14
Project: P3_image_processing   Author: latedude2   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 15
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 16
Project: 3dprinteros-client   Author: panasevychol   File: test_legendre.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 17
Project: 3dprinteros-client   Author: panasevychol   File: test_legendre.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 18
Project: lie_learn   Author: AMLab-Amsterdam   File: S2.py    MIT License 5 votes vote down vote up
def linspace(b, grid_type='Driscoll-Healy'):
    if grid_type == 'Driscoll-Healy':
        beta = np.arange(2 * b) * np.pi / (2. * b)
        alpha = np.arange(2 * b) * np.pi / b
    elif grid_type == 'SOFT':
        beta = np.pi * (2 * np.arange(2 * b) + 1) / (4. * b)
        alpha = np.arange(2 * b) * np.pi / b
    elif grid_type == 'Clenshaw-Curtis':
        # beta = np.arange(2 * b + 1) * np.pi / (2 * b)
        # alpha = np.arange(2 * b + 2) * np.pi / (b + 1)
        # Must use np.linspace to prevent numerical errors that cause beta > pi
        beta = np.linspace(0, np.pi, 2 * b + 1)
        alpha = np.linspace(0, 2 * np.pi, 2 * b + 2, endpoint=False)
    elif grid_type == 'Gauss-Legendre':
        x, _ = leggauss(b + 1)  # TODO: leggauss docs state that this may not be only stable for orders > 100
        beta = np.arccos(x)
        alpha = np.arange(2 * b + 2) * np.pi / (b + 1)
    elif grid_type == 'HEALPix':
        #TODO: implement this here so that we don't need the dependency on healpy / healpix_compat
        from healpix_compat import healpy_sphere_meshgrid
        return healpy_sphere_meshgrid(b)
    elif grid_type == 'equidistribution':
        raise NotImplementedError('Not implemented yet; see Fast evaluation of quadrature formulae on the sphere.')
    else:
        raise ValueError('Unknown grid_type:' + grid_type)
    return beta, alpha 
Example 19
Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: test_legendre.py    Apache License 2.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 20
Project: fund   Author: Frank-qlu   File: test_legendre.py    Apache License 2.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 21
Project: pySINDy   Author: luckystarufo   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 22
Project: Programming-for-Non-Technical-Roles-   Author: PacktPublishing   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 23
Project: linear_neuron   Author: uglyboxer   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 24
Project: facethin   Author: ParkerGod   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 25
Project: islam-buddy   Author: hamir   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 26
Project: mxnet-lambda   Author: awslabs   File: test_legendre.py    Apache License 2.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 27
Project: Deribit_funding_rate_indicator   Author: Dimasik007   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 28
Project: psychrometric-chart-makeover   Author: buds-lab   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 29
Project: wine-ml-on-aws-lambda   Author: pierreant   File: test_legendre.py    Apache License 2.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 30
Project: linux-cross-gcc   Author: nmercier   File: test_legendre.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 31
Project: SignLanguage_ML   Author: mareep-raljodid   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 32
Project: ImageFusion   Author: pfchai   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 33
Project: sarah   Author: ChonchoFronto   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 34
Project: honours_project   Author: JFriel   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 35
Project: honours_project   Author: JFriel   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 36
Project: Blackjack-Tracker   Author: martinabeleda   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 37
Project: PYPIC   Author: max614   File: test_legendre.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 38
Project: PYPIC   Author: max614   File: test_legendre.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 39
Project: PYPIC   Author: max614   File: test_legendre.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 40
Project: Renormalizer   Author: shuaigroup   File: lib.py    Apache License 2.0 5 votes vote down vote up
def legendre(self, nb, x0, x1, ifsort=True):
        """
        Legendre polynomial fit [x0, x1] to [-1,1]
        omega_m is the cutoff
        """
        omega_value, w = le.leggauss(nb)
        omega_value = (omega_value + (x1 + x0) / (x1 - x0)) * (x1 - x0) / 2.
        c_j2 = w * (x1 - x0) / 2. * self.alpha * omega_value ** 2 * np.exp(-omega_value / self.omega_c)

        return self.post_process(omega_value, c_j2, ifsort) 
Example 41
Project: lega   Author: MiroK   File: integration.py    MIT License 5 votes vote down vote up
def __init__(self, N):
        '''Quadrature formulat using N points.'''
        self.points, self.weights = leggauss(N) 
Example 42
Project: lega   Author: MiroK   File: integration.py    MIT License 5 votes vote down vote up
def __init__(self, N):
        '''Quadrature formulat using N x Npoints.'''
        points, weights = leggauss(N)

        # Let's do the tensor product
        XY = np.array([list(xy) for xy in product(points, points)])
        self.X = XY[:, 0]
        self.Y = XY[:, 1]

        self.weights = np.array([w0*w1 for w0, w1 in product(weights, weights)]) 
Example 43
Project: lega   Author: MiroK   File: legendre_basis.py    MIT License 5 votes vote down vote up
def forward_transformation_matrix(N):
    '''
    For any function f, we define its interpolant f_N as \sum_{i=0}^{N-1}F_i*L_i,
    where L_i is the i-th Legendre polynomial and the coeffcients F_i are given
    as F_i=\sum_{j=0}^{n-1}*f(xj)*w_j*L_i(x_j)/(L_i, L_i). The interpolant is
    thus a polynomial of degree N-1. The reasoning behind the definition is that
    is f were a polynomial of degre N-1 the integrals (f, L_i) having an integrand
    of max degree 2N-2 would be exactly evaluated by the N-1 point GL gradrature.
    Vector F is a representation of function f in the wave number space.
    Computing F can be represented as matrix-vector product and is reffered to
    as a forward Legendre transformation. Here we get the
    matrix for the operatation FL.
    '''
    # Note that each row of FL could be computed by taking a dot of row of
    # matrix BL.inv(M) with the vector of weight.
    FL = np.zeros((N, N))
    # Get point and weights of the guadrature
    points, weights = leggauss(N)
    for i in range(N):
        c = np.zeros(i+1)
        c[-1] = 1
        # Evaluate te the i-th polynomial at all the points
        row = legval(points, c)
        # Now the element-wise with with weights, i.e. dot with weight vector
        row *= weights
        # Finally the (Li, Li) term, i.e. the inv(M)
        row /= 2/(2*i+1)

        FL[i, :] = row

    return FL 
Example 44
Project: lega   Author: MiroK   File: legendre_basis.py    MIT License 5 votes vote down vote up
def __init__(self, N):
        '''Compute the evaluation points.'''
        if not isinstance(N, list):
            N = [N]

        self.dim = len(N)
        # This would work for any dim but since only 1d and 2d is supported in
        # FLT and BLT I see no points in supporting it here.
        assert self.dim < 3
        self.shape = tuple(N)
        # Get points for components
        points_i = [leggauss(n)[0] for n in N]
        # Combine as cartesian product
        self.points = np.array([list(pis) for pis in product(*points_i)]) 
Example 45
Project: offlow   Author: satwikkansal   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 46
Project: lambda-tensorflow-object-detection   Author: mikylucky   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 47
Project: elasticintel   Author: securityclippy   File: test_legendre.py    GNU General Public License v3.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 48
Project: cnidaria   Author: sauloal   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 49
Project: AutoDraw   Author: yemi-Jump   File: test_legendre.py    MIT License 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 50
Project: fund-rank-dashboard   Author: 1pani   File: test_legendre.py    Apache License 2.0 5 votes vote down vote up
def test_100(self):
        x, w = leg.leggauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = leg.legvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = 2.0
        assert_almost_equal(w.sum(), tgt) 
Example 51
Project: florence   Author: romeric   File: NumericIntegrator.py    MIT License 4 votes vote down vote up
def GaussQuadrature(N,a=-1,b=1):
    if a==-1 and b==1:
        return leggauss(N)
    # The following is for historical purposes and when the range is different from [-1,1]
    N0=N-1
    N1 = N0+1
    N2 = N0+2
    xu = np.linspace(-1.,1.,N1)
    
    # Legendre-Gauss-Vandermonde Matrix
    L = 1.0*np.zeros((N1,N2))
    # Derivative of Legendre-Gauss-Vandermonde Matrix
    Lp = 1.0*np.zeros(N1)

    dum = np.linspace(0,N0,N1)
    y=np.cos((2*dum+1)*np.pi/(2*N0+2))+(0.27/N1)*np.sin(np.pi*xu*N0/N2)
    # PI = np.pi
    # y=ne.evaluate("cos((2*dum+1)*PI/(2*N0+2))+(0.27/N1)*sin(PI*xu*N0/N2)")
    deps = np.finfo(np.float64).eps

    # Initial Guess
    y0 = 2.0*np.ones(N1)

    while np.max(np.abs(y-y0)) > deps:
        L[:,0] = np.ones(N1)
        L[:,1] = y
        Lp  = np.zeros(N1)

        for k in range(1,N1):
            L[:,k+1] = ((2*k+1)*L[:,k]*y - k*L[:,k-1])/(k+1)

        Lp = N2*(L[:,N0]-L[:,N1]*y)/(1-y**2)

        y0 = y
        y=y0-L[:,N1]/Lp

    z = (a*(1-y)+b*(1+y))/2.0
    w = (b-a)/((1-y**2)*Lp**2)*pow((np.float64(N2)/N1),2)


    z = np.fliplr(z.reshape(1,z.shape[0])).reshape(z.shape[0]) 
    w = np.fliplr(w.reshape(1,w.shape[0])).reshape(w.shape[0]) 

    return (z,w) 
Example 52
Project: lie_learn   Author: AMLab-Amsterdam   File: S2.py    MIT License 4 votes vote down vote up
def quadrature_weights(b, grid_type='Gauss-Legendre'):
    """
    Compute quadrature weights for a given grid-type.
    The function S2.meshgrid generates the points that correspond to the weights generated by this function.

    if convention == 'Gauss-Legendre':
    The quadrature formula is exact for polynomials up to degree M less than or equal to 2b + 1,
    so that we can compute exact Fourier coefficients for f a polynomial of degree at most b.

    if convention == 'Clenshaw-Curtis':
    The quadrature formula is exact for polynomials up to degree M less than or equal to 2b,
    so that we can compute exact Fourier coefficients for f a polynomial of degree at most b.

    :param b: the grid resolution. See S2.meshgrid
    :param grid_type:
    :return:
    """
    if grid_type == 'Clenshaw-Curtis':
        # There is a faster fft based method to compute these weights
        # see "Fast evaluation of quadrature formulae on the sphere"
        # W = np.empty((2 * b + 2, 2 * b + 1))
        # for j in range(2 * b + 1):
        #    eps_j_2b = 0.5 if j == 0 or j == 2 * b else 1.
        #    for k in range(2 * b + 2):  # Doesn't seem to depend on k..
        #        W[k, j] = (4 * np.pi * eps_j_2b) / (b * (2 * b + 2))
        #        sum = 0.
        #        for l in range(b + 1):
        #            eps_l_b = 0.5 if l == 0 or l == b else 1.
        #            sum += eps_l_b / (1 - 4 * l ** 2) * np.cos(j * l * np.pi / b)
        #        W[k, j] *= sum
        w = _clenshaw_curtis_weights(n=2 * b)
        W = np.empty((2 * b + 1, 2 * b + 2))
        W[:] = w[:, None]
    elif grid_type == 'Gauss-Legendre':
        # We found this formula in:
        # "A Fast Algorithm for Spherical Grid Rotations and its Application to Singular Quadrature"
        # eq. 10
        _, w = leggauss(b + 1)
        W = w[:, None] * (2 * np.pi / (2 * b + 2) * np.ones(2 * b + 2)[None, :])
    elif grid_type == 'SOFT':
        print("WARNING: SOFT quadrature weights don't work yet")
        k = np.arange(0, b)
        w = np.array([(2. / b) * np.sin(np.pi * (2. * j + 1.) / (4. * b)) *
                      (np.sum((1. / (2 * k + 1))
                              * np.sin((2 * j + 1) * (2 * k + 1)
                                       * np.pi / (4. * b))))
                      for j in range(2 * b)])
        W = w[:, None] * np.ones(2 * b)[None, :]
    else:
        raise ValueError('Unknown grid_type:' + str(grid_type))

    return W