Example #1
0
    def test_legvander3d(self) :
        # also tests polyval3d for non-square coefficient array
        x1, x2, x3 = self.x
        c = np.random.random((2, 3, 4))
        van = leg.legvander3d(x1, x2, x3, [1, 2, 3])
        tgt = leg.legval3d(x1, x2, x3, c)
        res = np.dot(van, c.flat)
        assert_almost_equal(res, tgt)

        # check shape
        van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3])
        assert_(van.shape == (1, 5, 24))
Example #2
0
    def test_legvander3d(self) :
        # also tests polyval3d for non-square coefficient array
        x1, x2, x3 = self.x
        c = np.random.random((2, 3, 4))
        van = leg.legvander3d(x1, x2, x3, [1, 2, 3])
        tgt = leg.legval3d(x1, x2, x3, c)
        res = np.dot(van, c.flat)
        assert_almost_equal(res, tgt)

        # check shape
        van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3])
        assert_(van.shape == (1, 5, 24))
Example #3
0
def legForwardTransform(orders, locations, functionVals):
    if len(locations.shape)==1:
        return np.array(leg.legfit(locations, functionVals, orders[0]))
    else:
        if locations.shape[1]==2:
            V=leg.legvander2d(locations[:,0], locations[:,1], orders)
        elif locations.shape[1]==3:
            V=leg.legvander3d(locations[:,0],locations[:,1],locations[:,2], orders)
        elif locations.shape[1]==4:
            V=legvander4d(locations,orders)
        elif locations.shape[1]==5:
            V=legvander5d(locations,orders)
        else:
            raise NotImplementedError # there's a bad startup joke about this being good enough for the paper.
        ret, _, _, _=npl.lstsq(V, functionVals, rcond=None)
        return np.reshape(ret, (np.array(orders)+1).flatten())
Example #4
0
def culledLegForwardTransform(orders, locations, functionVals, threshold=None):
    # inspired by : A Simple Regularization of the Polynomial Interpolation For the Runge Phenomemenon
    if len(locations.shape)==1:
        vandermonde=leg.legvander(locations, orders[0])
    elif locations.shape[1]==2:
        vandermonde=leg.legvander2d(locations[:,0], locations[:,1], orders)
    elif locations.shape[1]==3:
        vandermonde=leg.legvander3d(locations[:,0],locations[:,1],locations[:,2], orders)
    elif locations.shape[1]==4:
        vandermonde=legvander4d(locations,orders)
    elif locations.shape[1]==5:
        vandermonde=legvander5d(locations,orders)
    else:
        raise NotImplementedError # there's a bad startup joke about this being good enough for the paper.

    # preconditioner = np.diag((0.94) ** (2* (np.arange(vandermonde.shape[0]))))
    # vandermonde=np.dot(preconditioner, vandermonde)

    U,S,Vh=np.linalg.svd(vandermonde)
    numTake=0
    filtS=S
    if threshold is None:
        Eps= np.finfo(functionVals.dtype).eps
        Neps = np.prod(cmn.numpyze(orders)) * Eps * S[0] #truncation due to ill-conditioning
        Nt = max(np.argmax(Vh, axis=0)) #"Automatic" determination of threshold due to Runge's phenomenon
        threshold=min(Neps, Nt)
    while numTake<=0:
        filter=S>threshold
        numTake=filter.sum()
        if numTake>0:
            filtU=U[:,:numTake]; filtS=S[:numTake]; filtVh=Vh[:numTake, :]
        else:
            if threshold>1e-13:
                threshold=threshold/2
                warnings.warn('cutting threshold for eigenvalues to '+str(threshold))
            else:
                warnings('seems all eigenvalues are zero (<1e-13), setting to zero and breaking')
                filtS=np.zeros_like(S)
    truncVander=np.dot(filtU,np.dot(np.diag(filtS),filtVh))

    ret, _, _, _=npl.lstsq(truncVander, functionVals, rcond=None)
    return np.reshape(ret, np.array(orders).flatten()+1)