def test_curl(): x1, x2, x3 = symbols('x1 x2 x3') X = [x1 * x2**3, x2 - cos(x3), x3**3 - x1] arg = [x1, x2, x3] j = Arraypy(3) k0 = TensorArray(j, (1)) k0[0] = x1 * x2**3 k0[1] = x2 - cos(x3) k0[2] = x3**3 - x1 k1 = Arraypy([1, 3, 1]).to_tensor(1) k1[1] = x1 * x2**3 k1[2] = x2 - cos(x3) k1[3] = x3**3 - x1 v0 = TensorArray(j, (1)) v0[0] = x1 v0[1] = x2 v0[2] = x3 v1 = Arraypy([1, 3, 1]).to_tensor(1) v1[1] = x1 v1[2] = x2 v1[3] = x3 s0 = TensorArray(j, (1)) s0[0] = -sin(x3) s0[1] = 1 s0[2] = -3 * x1 * x2**2 s1 = Arraypy([1, 3, 1]).to_tensor(1) s1[1] = -sin(x3) s1[2] = 1 s1[3] = -3 * x1 * x2**2 assert curl(X, arg) == [-sin(x3), 1, -3 * x1 * x2**2] assert isinstance(curl(X, arg), list) assert curl(X, arg, 'a') == list2arraypy([-sin(x3), 1, -3 * x1 * x2**2]) assert isinstance(curl(X, arg, 'a'), Arraypy) assert curl(X, arg, 't') == s0 assert isinstance(curl(X, arg, 't'), TensorArray) assert curl(X, arg, 't').type_pq == (1, 0) assert curl(k0, arg) == s0 assert isinstance(curl(k0, arg), TensorArray) assert curl(X, arg, 't').type_pq == (1, 0) assert curl(k0, arg, 'a') == list2arraypy([-sin(x3), 1, -3 * x1 * x2**2]) assert isinstance(curl(k0, arg, 'a'), Arraypy) assert curl(k0, arg, 't') == s0 assert isinstance(curl(k0, arg, 't'), TensorArray) assert curl(X, arg, 't').type_pq == (1, 0) assert curl(k1, v1, 't') == s1 assert isinstance(curl(k1, v1, 't'), TensorArray) assert curl(k1, v1, 't').type_pq == (1, 0)
def test_df_varlist(): x1, x2, x3 = symbols('x1 x2 x3') f = x1**2 * x2 + sin(x2 * x3 - x2) var_list = [x1, x2, x3] assert df(f, var_list) == [ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ] assert isinstance(df(f, var_list), list) assert df(f, var_list, 'l') == [ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ] assert isinstance(df(f, var_list, 'l'), list) assert df(f, var_list, 'a') == list2arraypy([ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ]) assert isinstance(df(f, var_list, 'a'), Arraypy) assert df(f, var_list, 't') == list2tensor([ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ]) assert isinstance(df(f, var_list, 't'), TensorArray) assert df(f, var_list, 't').type_pq == (0, 1)
def test_df_var_tnsr0(): x1, x2, x3 = symbols('x1 x2 x3') f = x1**2 * x2 + sin(x2 * x3 - x2) var_tnsr0 = TensorArray(Arraypy(3), (1)) var_tnsr0[0] = x1 var_tnsr0[1] = x2 var_tnsr0[2] = x3 assert df(f, var_tnsr0) == [ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ] assert isinstance(df(f, var_tnsr0), list) assert df(f, var_tnsr0, 'l') == [ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ] assert isinstance(df(f, var_tnsr0, 'l'), list) assert df(f, var_tnsr0, 'a') == list2arraypy([ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ]) assert isinstance(df(f, var_tnsr0, 'a'), Arraypy) assert df(f, var_tnsr0, 't') == list2tensor([ 2 * x1 * x2, x1**2 + (x3 - 1) * cos(x2 * x3 - x2), x2 * cos(x2 * x3 - x2) ]) assert isinstance(df(f, var_tnsr0, 't'), TensorArray) assert df(f, var_tnsr0, 't').type_pq == (0, 1)
def test_calculation(): # Arraypy list_of_ones = [1 for i in range(9)] list_of_nines = [9 for i in range(9)] shape = (3, 3) a = list2arraypy(list_of_ones, shape) b = list2arraypy(list_of_nines, shape) if sys.version_info[0] >= 3: c = a + b for i in c: assert i == 10 c = b - a for i in c: assert i == 8 else: c = a + b idx = c.start_index for i in range(len(c)): assert c[idx] == 10 idx = c.next_index(idx) idx = c.start_index c = b - a for i in range(len(c)): assert c[idx] == 8 idx = c.next_index(idx) # TensorArray x0, x1, y0, y1 = symbols('X[0], X[1], Y[0], Y[1]') tensor1 = TensorArray(Arraypy(2, 'X'), 1) tensor2 = TensorArray(Arraypy(2, 'Y'), -1) assert tensor1.rank == tensor2.rank == 1 res_tensor = tensor_product(tensor1, tensor2) assert len(res_tensor) == 4 assert res_tensor.rank == 2 assert res_tensor[0, 0] == x0 * y0 assert res_tensor[0, 1] == x0 * y1 assert res_tensor[1, 0] == x1 * y0 assert res_tensor[1, 1] == x1 * y1
def test_iterator(): if sys.version_info[0] >= 3: array = list2arraypy([0, 1, 2, 3], (2, 2)) j = 0 for i in array: assert i == j j += 1 array = array.reshape(4) j = 0 for i in array: assert i == j j += 1
def test_equality(): first_list = [1, 2, 3, 4] second_list = [1, 2, 3, 4] third_list = [4, 3, 2, 1] assert first_list == second_list assert first_list != third_list first_arraypy = list2arraypy(first_list, (2, 2)) second_arraypy = list2arraypy(second_list, (2, 2)) third_arraypy = list2arraypy(third_list, (2, 2)) fourth_arraypy = list2arraypy(first_list, 4) assert first_arraypy == second_arraypy second_arraypy[0, 0] = 0 assert first_arraypy != second_arraypy assert first_arraypy != third_arraypy assert first_arraypy != fourth_arraypy first_tensor = list2tensor(first_list, (2, 2), (1, 1)) second_tensor = list2tensor(second_list, (2, 2), (1, 1)) third_tensor = list2tensor(second_list, (2, 2), (-1, 1)) assert first_tensor == second_tensor assert first_tensor != third_tensor
def test_arraypy_converting(): arr_arraypy = list2arraypy([1, 2, 3, 4], (2, 2)) arr_list = arr_arraypy.to_list() assert (isinstance(arr_list, list)) arr_tensor = arr_arraypy.to_tensor((1, -1)) assert (isinstance(arr_tensor, TensorArray)) arr_matrix = arr_arraypy.to_matrix() assert (isinstance(arr_matrix, Matrix)) idx = (0, 0) for i in range(len(arr_arraypy)): assert arr_arraypy[idx] == arr_tensor[idx] == arr_matrix[idx] idx = arr_arraypy.next_index(idx)
def test_converting_functions(): arr_list = [1, 2, 3, 4] arr_matrix = Matrix(((1, 2), (3, 4))) # list arr_arraypy = list2arraypy(arr_list, (2, 2)) assert (isinstance(arr_arraypy, Arraypy)) arr_tensor = list2tensor(arr_list, (2, 2), (-1, -1)) assert (isinstance(arr_tensor, TensorArray)) # Matrix arr_arraypy = matrix2arraypy(arr_matrix) assert (isinstance(arr_arraypy, Arraypy)) arr_tensor = matrix2tensor(arr_matrix, (-1, -1)) assert (isinstance(arr_tensor, TensorArray))
def test_is_symmetric(): a = list2arraypy([0, 3, 5, 3, 0, 0, 5, 0, 0], (3, 3)) assert not is_asymmetric(a) assert is_symmetric(a)
# -*- coding: utf-8 -*- from sympy.tensor.arraypy import Arraypy, TensorArray, list2arraypy, \ list2tensor, matrix2arraypy, matrix2tensor from sympy.tensor.tensor_methods import symmetric, asymmetric, is_symmetric, \ is_asymmetric, change_basis, tensor_product, wedge, raise_index,\ lower_index from sympy import Symbol, symbols, sin from copy import copy arr = list2arraypy(list(range(9)), (3, 3)) # 0 1 2 # 3 4 5 # 6 7 8 brr = list2arraypy([1 for i in range(9)], (3, 3)) # 1 1 1 # 1 1 1 # 1 1 1 def test_symmetric(): # Arraypy sym_arr = symmetric(arr) # 0.0 2.0 4.0 # 2.0 4.0 6.0 # 4.0 6.0 8.0 assert sym_arr[0, 1] == sym_arr[1, 0] assert sym_arr[0, 2] == sym_arr[1, 1] == sym_arr[2, 0] assert sym_arr[2, 1] == sym_arr[1, 2]