Created
April 17, 2019 15:42
-
-
Save jenncross/2e79f3f8fd2dc73815f9ec81e4a7b690 to your computer and use it in GitHub Desktop.
SymPy Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sympy as sym | |
import math | |
def title(s): | |
print("\n") | |
print("*******") | |
print(s) | |
title("Square Root of 9") | |
print("Math: " + str(math.sqrt(9))) | |
print("Symbolic: " + str(sym.sqrt(9))) | |
title("Square Root of 3") | |
print("Math: " + str(math.sqrt(3))) | |
print("Symbolic: " + str(sym.sqrt(3))) | |
title("Square Root of 3") | |
print("Math: " + str(math.sqrt(3))) | |
print("Symbolic: " + str(sym.sqrt(3))) | |
title("Square Root of 8") | |
print("Math: " + str(math.sqrt(8))) | |
print("Symbolic: " + str(sym.sqrt(8))) | |
title("The expression: x + 2 * y") | |
x, y = sym.symbols('x y') | |
expression = x + 2 * y | |
print("Symbolic: " + str(expression)) | |
title("The expression: x + 2 * y - x") | |
expr2 = expression - x | |
print("Symbolic: " + str(expr2)) | |
title("The expression: (x + 2 * y) * x") | |
expr2 = expression * x | |
print("Symbolic: " + str(expr2)) | |
expanded_expr = sym.expand(expr2) | |
print("Symbolic - expanded: " + str(expanded_expr)) | |
print("Symbolic - expanded then factored: " + str(sym.factor(expanded_expr))) | |
title("The expression: x**2 + 4*x*y + 4*y**2") | |
expr2 = x**2 + 4*x*y + 4*y**2 | |
print("Symbolic: " + str(expr2)) | |
expanded_expr = sym.expand(expr2) | |
print("Symbolic - expanded: " + str(expanded_expr)) | |
print("Symbolic - expanded then factored: " + str(sym.factor(expanded_expr))) | |
title("solve for x, where 0 = x**2 + 4*x*y + 4*y**2") | |
print("Symbolic x = " + str(sym.solve(expr2, x))) | |
title("df/dx") | |
print("Symbolic: " + str(sym.diff(expr2, x))) | |
title("df/dy") | |
print("Symbolic: " + str(sym.diff(expr2, y))) | |
title("f = exp(x) + sin(x)") | |
expr2 = sym.exp(x)+ sym.sin(x) | |
print("Symbolic (df/dx): " + str(sym.diff(expr2, x))) | |
print("Symbolic (df/dx): " + str(sym.diff(expr2, x))) | |
title("integral of sin(x**2) over x from - infinity to + infinity") | |
print("Symbolic: " + str(sym.integrate(sym.sin(x**2), (x, -sym.oo, sym.oo)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment