SymPy Basic Flashcards
How do you import Sympy and create a symbolic variable (x)?
```python
import sympy
x = sympy.Symbol(‘x’)
~~~
How do you define an expression (x^2 + 2x + 1) in Sympy using the symbol (x)?
```python
expr = x**2 + 2*x + 1
~~~
How do you simplify the expression ((x^2 + 2x + 1) / (x + 1)) using sympy.simplify
?
```python
import sympy
x = sympy.Symbol(‘x’)
expr = (x**2 + 2*x + 1) / (x + 1)
simpl_expr = sympy.simplify(expr)
# Output: x + 1
~~~
How do you factor the polynomial (x^3 - 6x^2 + 11x - 6)?
```python
p = x3 - 6x**2 + 11x - 6
factor_p = sympy.factor(p)
# (x - 1)(x - 2)(x - 3)
~~~
How do you expand ((x + 1)^3) into a polynomial form using sympy.expand
?
```python
expr = (x + 1)3
expanded = sympy.expand(expr)
# x^3 + 3x^2 + 3x + 1
~~~
How do you collect like terms of (x) in the expression (x^3 + 2x*x^2 + x)?
```python
expr = x3 + 2xx2 + x
collected = sympy.collect(sympy.expand(expr), x)
# x^3 + 2x^3 + x -> 3x^3 + x
~~~
How do you substitute (x = 2) into the expression (x^2 + x + 1)?
```python
expr = x**2 + x + 1
sub_expr = expr.subs(x, 2)
# 2^2 + 2 + 1 = 7
~~~
How do you solve the equation (x^2 - 4 = 0) for (x) using sympy.solve
?
```python
solution = sympy.solve(sympy.Eq(x**2, 4), x)
# [ -2, 2 ]
~~~
How do you solve the system of linear equations: 2x + y = 5, x - 3y = -4 for (x, y)?
```python
x, y = sympy.symbols(‘x y’)
sol = sympy.solve([2x + y - 5, x - 3y + 4], [x, y])
# {x: 2, y: 1}
~~~
How do you compute the derivative of (x^3 + 2x^2) with respect to (x)?
```python
expr = x3 + 2x**2
deriv_expr = sympy.diff(expr, x)
# 3x^2 + 4*x
~~~
How do you compute the partial derivative of (f(x,y) = x^2 y^3) with respect to (y)?
```python
x, y = sympy.symbols(‘x y’)
f = x2 * y3
partial_y = sympy.diff(f, y)
# 3x^2y^2
~~~
How do you compute the indefinite integral of (x^2) with respect to (x)?
```python
integrand = x**2
indef_int = sympy.integrate(integrand, (x))
# x^3/3
~~~
How do you compute the definite integral of (x^2) from (x=0) to (x=2)?
```python
def_int = sympy.integrate(x**2, (x, 0, 2))
# 8/3
~~~
How do you calculate the limit of (rac{sin(x)}{x}) as (x) approaches 0?
```python
limit_expr = sympy.limit(sympy.sin(x)/x, x, 0)
# 1
~~~
How do you find the series expansion of (e^x) around (x=0) up to (x^4)?
```python
series_expr = sympy.series(sympy.exp(x), (x, 0, 5))
# 1 + x + x^2/2 + x^3/6 + x^4/24 + O(x^5)
~~~
How do you compute the first 4 terms of the series expansion of (sin(x)) around (x = 0)?
```python
series_sin = sympy.series(sympy.sin(x), (x, 0, 5))
# x - x^3/6 + x^5/120 - …
~~~
How do you rewrite (sin(x)) in exponential form using sympy.rewrite(sympy.exp)
?
```python
rewritten = sympy.sin(x).rewrite(sympy.exp)
# (exp(Ix) - exp(-Ix))/(2*I)
~~~
How do you rewrite (log(x, 2)) as a fraction of natural logs using rewrite(sympy.log)
?
```python
expr = sympy.log(x, 2)
rewritten_log = expr.rewrite(sympy.log)
# log(x)/log(2)
~~~
How do you evaluate the expression (sqrt{2}) as a floating-point number using evalf()
?
```python
val = sympy.sqrt(2).evalf()
# 1.414213562…
~~~
How do you approximate (pi) to 10 decimal places using sympy.nsimplify
or .evalf(10)
?
```python
approx_pi = sympy.pi.evalf(10)
# 3.141592654
~~~
How do you create a 2x2 symbolic matrix (M = egin{pmatrix} x & 1 \ 2 & y end{pmatrix})?
```python
M = sympy.Matrix([[x, 1],
[2, sympy.Symbol(‘y’)]])
~~~
How do you multiply two symbolic matrices A
and B
of compatible shapes using A*B
?
```python
A = sympy.Matrix([[1, 2], [3, 4]])
B = sympy.Matrix([[x, 0], [0, y]])
product = A * B
~~~
How do you compute the determinant of the 2x2 matrix (egin{pmatrix} x & 2 \ 3 & y end{pmatrix})?
```python
mat = sympy.Matrix([[x, 2],
[3, y]])
det_mat = mat.det()
# x*y - 6
~~~