mat[i, j] = (1-(-1.)**(i+j-1))*i*j/(i+j-1) return mat def derivative_matrix(deg): '''Matrix of \int_{-1}^{1}(P_i, P_j`) of monomials of degree up to deg.''' n = deg + 1 mat = np.zeros((n, n)) for i in range(n): for j in range(1, n): mat[i, j] = (1 - (-1.)**(i + j))*j/(i + j) return mat # ----------------------------------------------------------------------------- if __name__ == '__main__': from tests import test_M, test_A, test_C from sympy.plotting import plot from sympy import Symbol deg = 5 basis_fs = basis_functions(deg=deg) x = Symbol('x') ps = plot(basis_fs[0], (x, -1, 1), show=False) [ps.append(plot(f, (x, -1, 1), show=False)[0]) for f in basis_fs[1:]] ps.show() assert test_M(basis_fs, mass_matrix(deg)) assert test_A(basis_fs, stiffness_matrix(deg)) assert test_C(basis_fs, derivative_matrix(deg))
return common.stiffness_matrix(deg, basis_functions, unroll) def derivative_matrix(deg, unroll=True): ''' Matrix of \int_{-1}^{1}(L_i, L_j`) from Chebyshev polynomials of first kind. ''' return common.derivative_matrix(deg, basis_functions, unroll) # ----------------------------------------------------------------------------- if __name__ == '__main__': from tests import test_M, test_A, test_C from sympy.plotting import plot from sympy import simplify deg = 5 basis_fs = basis_functions(deg=deg) x = Symbol('x') # Need to check the definition reference = [1, x, 2*x**2-1, 4*x**3-3*x, 8*x**4-8*x**2+1] assert not any(map(simplify, (f-f_ for f, f_ in zip(basis_fs, reference)))) ps = plot(basis_fs[0], (x, -1, 1), show=False) [ps.append(plot(f, (x, -1, 1), show=False)[0]) for f in basis_fs[1:]] # ps.show() assert test_M(basis_fs, mass_matrix(deg, True)) assert test_A(basis_fs, stiffness_matrix(deg, True)) assert test_C(basis_fs, derivative_matrix(deg, True))