diff --git a/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/eigenvalue_of_Kronecker_product_of_Pauli_matrices.py b/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/eigenvalue_of_Kronecker_product_of_Pauli_matrices.py index 7b392b1..a62150e 100755 --- a/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/eigenvalue_of_Kronecker_product_of_Pauli_matrices.py +++ b/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/eigenvalue_of_Kronecker_product_of_Pauli_matrices.py @@ -1,3 +1,8 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/12731 +""" + import numpy as np import guan diff --git a/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/test2_eigenvalue_of_Kronecker_product_of_Pauli_matrices.py b/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/test2_eigenvalue_of_Kronecker_product_of_Pauli_matrices.py index 35d2618..7e882f4 100755 --- a/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/test2_eigenvalue_of_Kronecker_product_of_Pauli_matrices.py +++ b/academic_codes/2021.05.21_eigenvalue_of_Kronecker_product_of_Pauli_matrices/test2_eigenvalue_of_Kronecker_product_of_Pauli_matrices.py @@ -1,3 +1,8 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/12731 +""" + import numpy as np import guan diff --git a/language_learning/2021.06.24_sympy_example/sympy_example.py b/language_learning/2021.06.24_sympy_example/sympy_example.py new file mode 100644 index 0000000..61b7356 --- /dev/null +++ b/language_learning/2021.06.24_sympy_example/sympy_example.py @@ -0,0 +1,85 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/14684 +""" + +import sympy + + +# 定义符号 +print() +print('定义符号:') +x, y, z = sympy.symbols('x y z') # 使用sympy.symbols +print(x) +print(y+1) +print(z**2) +print() + + + + +# 替换(Substitution) +print('变量替换:') +expression_1 = x**2+1 +value_1 = expression_1.subs(x, 3) # 使用.subs()方法 +print(value_1) +print() + + + + +# 字符串转成符号表达式 +print('字符串转成符号表达式:') +expression_string = 'x**3+1' +print(expression_string) +expression_2 = sympy.sympify(expression_string) # 使用sympy.sympify() +print(expression_2) +value_2 = expression_2.subs(x, 2) +print(value_2) +print() + + + + +# 简化表达式 +print('简化表达式:') +expression_3 = sympy.simplify(sympy.sin(x)**2 + sympy.cos(x)**2) # 使用sympy.simplify() +print(expression_3) +print() + + + + +# 符号矩阵 +print('符号矩阵:') +a, b = sympy.symbols('a b') +matrix = sympy.Matrix([[1, a], [0, b]]) # 使用sympy.Matrix() +print(matrix) +print() + + + + +# 符号矩阵的特征值和特征向量 +print('符号矩阵的特征值和特征向量:') +eigenvalue = matrix.eigenvals() # 使用.eigenvals()方法 +eigenvector = matrix.eigenvects() # 使用.eigenvects()方法 +print('特征值\n', eigenvalue) +print('特征向量\n', eigenvector) + +print() +P, D = matrix.diagonalize() # 使用.diagonalize()方法 +print('特征值\n', D) +print('特征向量\n', P) +print('特征值\n', D.subs(a, -4).subs(b, 2)) +print('特征向量\n', P.subs(a, -4).subs(b, 2)) +print() + +# 和numpy对比 +print('和numpy对比:') +import numpy as np +matrix = np.array([[1, -4], [0, 2]]) +eigenvalue, eigenvector = np.linalg.eig(matrix) +print('特征值\n', eigenvalue) +print('特征向量\n', eigenvector) +print() \ No newline at end of file