def smith_normal_form(m, domain=None): ''' Return the Smith Normal Form of a matrix `m` over the ring `domain`. This will only work if the ring is a principal ideal domain. Examples ======== >>> from sympy.polys.solvers import RawMatrix as Matrix >>> from sympy.polys.domains import ZZ >>> from sympy.matrices.normalforms import smith_normal_form >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]]) >>> setattr(m, "ring", ZZ) >>> print(smith_normal_form(m)) Matrix([[1, 0, 0], [0, 10, 0], [0, 0, -30]]) ''' invs = invariant_factors(m, domain=domain) smf = diag(*invs) n = len(invs) if m.rows > n: smf = smf.row_insert(m.rows, zeros(m.rows - n, m.cols)) elif m.cols > n: smf = smf.col_insert(m.cols, zeros(m.rows, m.cols - n)) return smf
def smith_normal_form(m, domain = None): ''' Return the Smith Normal Form of a matrix `m` over the ring `domain`. This will only work if the ring is a principal ideal domain. Examples ======== >>> from sympy.polys.solvers import RawMatrix as Matrix >>> from sympy.polys.domains import ZZ >>> from sympy.matrices.normalforms import smith_normal_form >>> m = Matrix([[12, 6, 4], [3, 9, 6], [2, 16, 14]]) >>> setattr(m, "ring", ZZ) >>> print(smith_normal_form(m)) Matrix([[1, 0, 0], [0, 10, 0], [0, 0, -30]]) ''' invs = invariant_factors(m, domain=domain) smf = diag(*invs) n = len(invs) if m.rows > n: smf = smf.row_insert(m.rows, zeros(m.rows-n, m.cols)) elif m.cols > n: smf = smf.col_insert(m.cols, zeros(m.rows, m.cols-n)) return smf
def get_submatrix(self, matrix): r""" Returns ======= macaulay_submatrix: Matrix The Macaulay denominator matrix. Columns that are non reduced are kept. The row which contains one of the a_{i}s is dropped. a_{i}s are the coefficients of x_i ^ {d_i}. """ reduced, non_reduced = self.get_reduced_nonreduced() # if reduced == [], then det(matrix) should be 1 if reduced == []: return diag([1]) # reduced != [] reduction_set = [v ** self.degrees[i] for i, v in enumerate(self.variables)] ais = list([self.polynomials[i].coeff(reduction_set[i]) for i in range(self.n)]) reduced_matrix = matrix[:, reduced] keep = [] for row in range(reduced_matrix.rows): check = [ai in reduced_matrix[row, :] for ai in ais] if True not in check: keep.append(row) return matrix[keep, non_reduced]
def as_explicit(self): from sympy.matrices.dense import diag return diag(*list(self._vector.as_explicit()))
def test_replace_arrays_partial_derivative(): x, y, z, t = symbols("x y z t") # d(A^i)/d(A_j) = d(g^ik A_k)/d(A_j) = g^ik delta_jk expr = PartialDerivative(A(i), A(-j)) assert expr.get_free_indices() == [i, j] assert expr.get_indices() == [i, j] assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, 1)}, [i, j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, -1)}, [i, j]) == Array([[1, 0], [0, -1]]) assert expr.replace_with_arrays({A(-i): [x, y], L: diag(1, 1)}, [i, j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(-i): [x, y], L: diag(1, -1)}, [i, j]) == Array([[1, 0], [0, -1]]) expr = PartialDerivative(A(i), A(j)) assert expr.get_free_indices() == [i, -j] assert expr.get_indices() == [i, -j] assert expr.replace_with_arrays({A(i): [x, y]}, [i, -j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, 1)}, [i, -j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, -1)}, [i, -j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(-i): [x, y], L: diag(1, 1)}, [i, -j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(-i): [x, y], L: diag(1, -1)}, [i, -j]) == Array([[1, 0], [0, 1]]) expr = PartialDerivative(A(-i), A(-j)) assert expr.get_free_indices() == [-i, j] assert expr.get_indices() == [-i, j] assert expr.replace_with_arrays({A(-i): [x, y]}, [-i, j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(-i): [x, y], L: diag(1, 1)}, [-i, j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(-i): [x, y], L: diag(1, -1)}, [-i, j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, 1)}, [-i, j]) == Array([[1, 0], [0, 1]]) assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, -1)}, [-i, j]) == Array([[1, 0], [0, 1]]) expr = PartialDerivative(A(i), A(i)) assert expr.get_free_indices() == [] assert expr.get_indices() == [L_0, -L_0] assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, 1)}, []) == 2 assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, -1)}, []) == 2 expr = PartialDerivative(A(-i), A(-i)) assert expr.get_free_indices() == [] assert expr.get_indices() == [-L_0, L_0] assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, 1)}, []) == 2 assert expr.replace_with_arrays({A(i): [x, y], L: diag(1, -1)}, []) == 2 expr = PartialDerivative(H(i, j) + H(j, i), A(i)) assert expr.get_indices() == [L_0, j, -L_0] assert expr.get_free_indices() == [j] expr = PartialDerivative(H(i, j) + H(j, i), A(k))*B(-i) assert expr.get_indices() == [L_0, j, -k, -L_0] assert expr.get_free_indices() == [j, -k] expr = PartialDerivative(A(i)*(H(-i, j) + H(j, -i)), A(j)) assert expr.get_indices() == [L_0, -L_0, L_1, -L_1] assert expr.get_free_indices() == [] expr = A(j)*A(-j) + expr assert expr.get_indices() == [L_0, -L_0, L_1, -L_1] assert expr.get_free_indices() == [] expr = A(i)*(B(j)*PartialDerivative(C(-j), D(i)) + C(j)*PartialDerivative(D(-j), B(i))) assert expr.get_indices() == [L_0, L_1, -L_1, -L_0] assert expr.get_free_indices() == [] expr = A(i)*PartialDerivative(C(-j), D(i)) assert expr.get_indices() == [L_0, -j, -L_0] assert expr.get_free_indices() == [-j]
def test_H2(): TP = sympy.diffgeom.TensorProduct R2 = sympy.diffgeom.rn.R2 y = R2.y dy = R2.dy dx = R2.dx g = (TP(dx, dx) + TP(dy, dy)) * y**(-2) automat = twoform_to_matrix(g) mat = diag(y**(-2), y**(-2)) assert mat == automat gamma1 = metric_to_Christoffel_1st(g) assert gamma1[0, 0, 0] == 0 assert gamma1[0, 0, 1] == -y**(-3) assert gamma1[0, 1, 0] == -y**(-3) assert gamma1[0, 1, 1] == 0 assert gamma1[1, 1, 1] == -y**(-3) assert gamma1[1, 1, 0] == 0 assert gamma1[1, 0, 1] == 0 assert gamma1[1, 0, 0] == y**(-3) gamma2 = metric_to_Christoffel_2nd(g) assert gamma2[0, 0, 0] == 0 assert gamma2[0, 0, 1] == -y**(-1) assert gamma2[0, 1, 0] == -y**(-1) assert gamma2[0, 1, 1] == 0 assert gamma2[1, 1, 1] == -y**(-1) assert gamma2[1, 1, 0] == 0 assert gamma2[1, 0, 1] == 0 assert gamma2[1, 0, 0] == y**(-1) Rm = metric_to_Riemann_components(g) assert Rm[0, 0, 0, 0] == 0 assert Rm[0, 0, 0, 1] == 0 assert Rm[0, 0, 1, 0] == 0 assert Rm[0, 0, 1, 1] == 0 assert Rm[0, 1, 0, 0] == 0 assert Rm[0, 1, 0, 1] == -y**(-2) assert Rm[0, 1, 1, 0] == y**(-2) assert Rm[0, 1, 1, 1] == 0 assert Rm[1, 0, 0, 0] == 0 assert Rm[1, 0, 0, 1] == y**(-2) assert Rm[1, 0, 1, 0] == -y**(-2) assert Rm[1, 0, 1, 1] == 0 assert Rm[1, 1, 0, 0] == 0 assert Rm[1, 1, 0, 1] == 0 assert Rm[1, 1, 1, 0] == 0 assert Rm[1, 1, 1, 1] == 0 Ric = metric_to_Ricci_components(g) assert Ric[0, 0] == -y**(-2) assert Ric[0, 1] == 0 assert Ric[1, 0] == 0 assert Ric[0, 0] == -y**(-2) assert Ric == ImmutableDenseNDimArray([-y**(-2), 0, 0, -y**(-2)], (2, 2)) ## scalar curvature is -2 #TODO - it would be nice to have index contraction built-in R = (Ric[0, 0] + Ric[1, 1]) * y**2 assert R == -2 ## Gauss curvature is -1 assert R / 2 == -1