Exemplo n.º 1
0
    def calcular(self, n, norm=False, k_factor=1):
        # Atencion: Norm = calcular normalizada

        if n % 2 == 1:
            k = int((n - 1) / 2)
            arr = []
            for i in range(k + 1):
                arr.append(get_a(i, n))

            poly = legendre.leg2poly(legendre.legint(legendre.legmul(arr,
                                                                     arr)))
        else:
            k = int((n - 2) / 2)
            arr = []
            for i in range(k + 1):
                arr.append(get_a(i, n))

            leg_b = legendre.legmul(legendre.legmul(arr, arr),
                                    legendre.poly2leg([1, 1]))

            poly = legendre.leg2poly(legendre.legint(leg_b))

        exp = 0
        wn, sn, s, sa = sp.symbols("wn sn sa s")

        for i in range(len(poly)):
            exp += poly[i] * ((2 * (wn**2) - 1)**i)
            exp -= poly[i] * ((-1)**i)

        if n % 2 == 1:
            exp = exp * 1 / (2 * (k + 1)**2)
        else:
            exp = exp * 1 / ((k + 1) * (k + 2))

        exp = 1 / (1 + self.getXi(0, n)**2 * exp)
        exp = exp.subs(wn, sn / 1j)

        roots = algebra.getRoots(exp, sn)
        roots[1] = algebra.filterRealNegativeRoots(roots[1])

        poles = []
        for i in roots[1]:
            poles.append({"value": i})

        exp = algebra.armarPolinomino(poles, [], sn, 1)
        self.tf_normalized = algebra.conseguir_tf(exp, sn, poles)

        if not norm:
            exp = self.plantilla.denormalizarFrecuencias(exp, sa, sn)
            self.getGainPoints()

            factor = (self.k1 - self.k2) * norm / 100 + self.k2

            exp = self.plantilla.denormalizarAmplitud(exp, s, sa, factor)

            self.tf = algebra.conseguir_tf(exp, s, [])

            return self.tf
        else:
            return self.tf_normalized
Exemplo n.º 2
0
 def test_legfromroots(self) :
     res = leg.legfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1,5) :
         roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
         pol = leg.legfromroots(roots)
         res = leg.legval(roots, pol)
         tgt = 0
         assert_(len(pol) == i + 1)
         assert_almost_equal(leg.leg2poly(pol)[-1], 1)
         assert_almost_equal(res, tgt)
Exemplo n.º 3
0
 def test_legfromroots(self) :
     res = leg.legfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1, 5) :
         roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
         pol = leg.legfromroots(roots)
         res = leg.legval(roots, pol)
         tgt = 0
         assert_(len(pol) == i + 1)
         assert_almost_equal(leg.leg2poly(pol)[-1], 1)
         assert_almost_equal(res, tgt)
Exemplo n.º 4
0
def legendre_transpose_mult_slow(v):
    """Naive multiplication P^T v where P is the matrix of coefficients of
    Legendre polynomials.
    Parameters:
        v: (batch_size, n)
    Return:
        P^T v: (batch_size, n)
    """
    n = v.shape[-1]
    # Construct the coefficient matrix P for Legendre polynomials
    P = np.zeros((n, n), dtype=np.float32)
    for i, coef in enumerate(np.eye(n)):
        P[i, :i + 1] = legendre.leg2poly(coef)
    P = torch.tensor(P)
    return v @ P
Exemplo n.º 5
0
 def _setup(self, config):
     torch.manual_seed(config['seed'])
     self.model = HstackDiagProduct(size=config['size'])
     self.optimizer = optim.Adam(self.model.parameters(), lr=config['lr'])
     self.n_steps_per_epoch = config['n_steps_per_epoch']
     size = config['size']
     # Target: Legendre polynomials
     P = np.zeros((size, size), dtype=np.float64)
     for i, coef in enumerate(np.eye(size)):
         P[i, :i + 1] = legendre.leg2poly(coef)
     self.target_matrix = torch.tensor(P)
     self.br_perm = bitreversal_permutation(size)
     self.input = (torch.eye(size)[:, :, None, None] *
                   torch.eye(2)).unsqueeze(-1)
     self.input_permuted = self.input[:, self.br_perm]
Exemplo n.º 6
0
def add_plot(X, lbl, clr, type='o'):
    Peq = Newton_Raphson(grad_E, Jacobian_E, X, 100, 1e-8)
    n = Peq.size
    for i in range(n):
        plt.plot(Peq[i, 0], 0, type, color=clr)

    c = [0] * (n + 2)
    c[n + 1] = 1

    d = L.legder(c)
    P = L.leg2poly(d)

    P = mirror(P)
    Poly = np.poly1d(P)
    x = np.linspace(-1, 1, 100)
    y = Poly(x)
    plt.plot(x, y, label=lbl, color=clr)
Exemplo n.º 7
0
def add_plot(X, lbl, clr, type='o'):
    Peq = pos_equilibrium(X)
    n = Peq.size
    for i in range(n):
        plt.plot(Peq[i, 0], 0, type, color=clr)

    c = [0] * (n + 2)
    c[n + 1] = 1

    d = L.legder(c)
    P = L.leg2poly(d)

    P = mirror(P)
    Poly = np.poly1d(P)
    x = np.linspace(-1, 1, 100)
    y = Poly(x)
    plt.plot(x, y, label=lbl, color=clr)
Exemplo n.º 8
0
def leg2poly(cs):
    from numpy.polynomial.legendre import leg2poly
    return leg2poly(cs)
Exemplo n.º 9
0
 def test_leg2poly(self) :
     for i in range(10) :
         assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i])
Exemplo n.º 10
0
    def test_legint(self) :
        # check exceptions
        assert_raises(ValueError, leg.legint, [0], .5)
        assert_raises(ValueError, leg.legint, [0], -1)
        assert_raises(ValueError, leg.legint, [0], 1, [0,0])

        # test integration of zero polynomial
        for i in range(2, 5):
            k = [0]*(i - 2) + [1]
            res = leg.legint([0], m=i, k=k)
            assert_almost_equal(res, [0, 1])

        # check single integration with integration constant
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [1/scl]
            legpol = leg.poly2leg(pol)
            legint = leg.legint(legpol, m=1, k=[i])
            res = leg.leg2poly(legint)
            assert_almost_equal(trim(res), trim(tgt))

        # check single integration with integration constant and lbnd
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            legpol = leg.poly2leg(pol)
            legint = leg.legint(legpol, m=1, k=[i], lbnd=-1)
            assert_almost_equal(leg.legval(-1, legint), i)

        # check single integration with integration constant and scaling
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [2/scl]
            legpol = leg.poly2leg(pol)
            legint = leg.legint(legpol, m=1, k=[i], scl=2)
            res = leg.leg2poly(legint)
            assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with default k
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1)
                res = leg.legint(pol, m=j)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with defined k
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1, k=[k])
                res = leg.legint(pol, m=j, k=range(j))
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with lbnd
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1)
                res = leg.legint(pol, m=j, k=range(j), lbnd=-1)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with scaling
        for i in range(5) :
            for j in range(2,5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1, k=[k], scl=2)
                res = leg.legint(pol, m=j, k=range(j), scl=2)
                assert_almost_equal(trim(res), trim(tgt))
Exemplo n.º 11
0
 def test_leg2poly(self) :
     for i in range(10) :
         assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i])
Exemplo n.º 12
0
    def test_legint(self) :
        # check exceptions
        assert_raises(ValueError, leg.legint, [0], .5)
        assert_raises(ValueError, leg.legint, [0], -1)
        assert_raises(ValueError, leg.legint, [0], 1, [0, 0])

        # test integration of zero polynomial
        for i in range(2, 5):
            k = [0]*(i - 2) + [1]
            res = leg.legint([0], m=i, k=k)
            assert_almost_equal(res, [0, 1])

        # check single integration with integration constant
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [1/scl]
            legpol = leg.poly2leg(pol)
            legint = leg.legint(legpol, m=1, k=[i])
            res = leg.leg2poly(legint)
            assert_almost_equal(trim(res), trim(tgt))

        # check single integration with integration constant and lbnd
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            legpol = leg.poly2leg(pol)
            legint = leg.legint(legpol, m=1, k=[i], lbnd=-1)
            assert_almost_equal(leg.legval(-1, legint), i)

        # check single integration with integration constant and scaling
        for i in range(5) :
            scl = i + 1
            pol = [0]*i + [1]
            tgt = [i] + [0]*i + [2/scl]
            legpol = leg.poly2leg(pol)
            legint = leg.legint(legpol, m=1, k=[i], scl=2)
            res = leg.leg2poly(legint)
            assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with default k
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1)
                res = leg.legint(pol, m=j)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with defined k
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1, k=[k])
                res = leg.legint(pol, m=j, k=list(range(j)))
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with lbnd
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1)
                res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1)
                assert_almost_equal(trim(res), trim(tgt))

        # check multiple integrations with scaling
        for i in range(5) :
            for j in range(2, 5) :
                pol = [0]*i + [1]
                tgt = pol[:]
                for k in range(j) :
                    tgt = leg.legint(tgt, m=1, k=[k], scl=2)
                res = leg.legint(pol, m=j, k=list(range(j)), scl=2)
                assert_almost_equal(trim(res), trim(tgt))
Exemplo n.º 13
0
def leg2poly(cs) :
    from numpy.polynomial.legendre import leg2poly
    return leg2poly(cs)