def mat_pow(M, N): """Returns matrix M in N-th power using Chebyshev polinomials of second kind M – Unimodular matrix N – power return = M^N """ import numpy as np from scipy.special import eval_chebyu p = (M[0,0] + M[1,1])/2 U_m1 = eval_chebyu(N-1, p) U_m2 = eval_chebyu(N-2, p) a = M[0,0]*U_m1 - U_m2 b = M[0,1]*U_m1 c = M[1,0]*U_m1 d = M[1,1]*U_m1 - U_m2 X = np.matrix([[a, b], [c, d]]) return X
def test_chebyshev(): allTrue = True for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Scaler(order, x) if np.round(valuePy, DECIMALS_ROUND) != np.round( valueCpp, DECIMALS_ROUND): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Array(order, cArray) if not np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Scaler(order, x) if np.round(valuePy, DECIMALS_ROUND) != np.round( valueCpp, DECIMALS_ROUND): allTrue = False assert allTrue allTrue = True for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Array(order, cArray) if not np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)): allTrue = False assert allTrue
def gradchebyquad(x): """ Evaluation of the gradient function of chebyquad """ chq = chebyquad_fcn(x) UM = 4. / len(x) * array([[(i+1) * eval_chebyu(i, 2. * xj - 1.) for xj in x] for i in xrange(len(x) - 1)]) return dot(chq[1:].reshape((1, -1)), UM).reshape((-1, ))
def test_chebyshev(): if NumCpp.NO_USE_BOOST: return for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Scaler(order, x) assert np.round(valuePy, DECIMALS_ROUND) == np.round(valueCpp, DECIMALS_ROUND) for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyt(order, x) valueCpp = NumCpp.chebyshev_t_Array(order, cArray) assert np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND)) for order in range(ORDER_MAX): x = np.random.rand(1).item() valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Scaler(order, x) assert np.round(valuePy, DECIMALS_ROUND) == np.round(valueCpp, DECIMALS_ROUND) for order in range(ORDER_MAX): shapeInput = np.random.randint(10, 100, [ 2, ], dtype=np.uint32) shape = NumCpp.Shape(*shapeInput) cArray = NumCpp.NdArray(shape) x = np.random.rand(*shapeInput) cArray.setArray(x) valuePy = sp.eval_chebyu(order, x) valueCpp = NumCpp.chebyshev_u_Array(order, cArray) assert np.array_equal(np.round(valuePy, DECIMALS_ROUND), np.round(valueCpp, DECIMALS_ROUND))
def _eval_numpy_(self, n, x): """ Evaluate ``self`` using numpy. EXAMPLES:: sage: import numpy sage: z = numpy.array([1,2]) sage: z2 = numpy.array([[1,2],[1,2]]) sage: z3 = numpy.array([1,2,3.]) sage: chebyshev_U(1,z) array([ 2., 4.]) sage: chebyshev_U(1,z2) array([[ 2., 4.], [ 2., 4.]]) sage: chebyshev_U(1,z3) array([ 2., 4., 6.]) sage: chebyshev_U(z,0.1) array([ 0.2 , -0.96]) """ from scipy.special import eval_chebyu return eval_chebyu(n, x)