SymPy Basic Flashcards

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you import Sympy and create a symbolic variable (x)?

A

```python
import sympy
x = sympy.Symbol(‘x’)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define an expression (x^2 + 2x + 1) in Sympy using the symbol (x)?

A

```python
expr = x**2 + 2*x + 1
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you simplify the expression ((x^2 + 2x + 1) / (x + 1)) using sympy.simplify?

A

```python
import sympy
x = sympy.Symbol(‘x’)
expr = (x**2 + 2*x + 1) / (x + 1)
simpl_expr = sympy.simplify(expr)
# Output: x + 1
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you factor the polynomial (x^3 - 6x^2 + 11x - 6)?

A

```python
p = x3 - 6x**2 + 11x - 6
factor_p = sympy.factor(p)
# (x - 1)(x - 2)(x - 3)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you expand ((x + 1)^3) into a polynomial form using sympy.expand?

A

```python
expr = (x + 1)3
expanded = sympy.expand(expr)
# x^3 + 3
x^2 + 3
x + 1
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you collect like terms of (x) in the expression (x^3 + 2x*x^2 + x)?

A

```python
expr = x3 + 2xx2 + x
collected = sympy.collect(sympy.expand(expr), x)
# x^3 + 2x^3 + x -> 3x^3 + x
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you substitute (x = 2) into the expression (x^2 + x + 1)?

A

```python
expr = x**2 + x + 1
sub_expr = expr.subs(x, 2)
# 2^2 + 2 + 1 = 7
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you solve the equation (x^2 - 4 = 0) for (x) using sympy.solve?

A

```python
solution = sympy.solve(sympy.Eq(x**2, 4), x)
# [ -2, 2 ]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you solve the system of linear equations: 2x + y = 5, x - 3y = -4 for (x, y)?

A

```python
x, y = sympy.symbols(‘x y’)
sol = sympy.solve([2x + y - 5, x - 3y + 4], [x, y])
# {x: 2, y: 1}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you compute the derivative of (x^3 + 2x^2) with respect to (x)?

A

```python
expr = x3 + 2x**2
deriv_expr = sympy.diff(expr, x)
# 3
x^2 + 4*x
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you compute the partial derivative of (f(x,y) = x^2 y^3) with respect to (y)?

A

```python
x, y = sympy.symbols(‘x y’)
f = x2 * y3
partial_y = sympy.diff(f, y)
# 3x^2y^2
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you compute the indefinite integral of (x^2) with respect to (x)?

A

```python
integrand = x**2
indef_int = sympy.integrate(integrand, (x))
# x^3/3
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you compute the definite integral of (x^2) from (x=0) to (x=2)?

A

```python
def_int = sympy.integrate(x**2, (x, 0, 2))
# 8/3
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you calculate the limit of (rac{sin(x)}{x}) as (x) approaches 0?

A

```python
limit_expr = sympy.limit(sympy.sin(x)/x, x, 0)
# 1
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you find the series expansion of (e^x) around (x=0) up to (x^4)?

A

```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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you compute the first 4 terms of the series expansion of (sin(x)) around (x = 0)?

A

```python
series_sin = sympy.series(sympy.sin(x), (x, 0, 5))
# x - x^3/6 + x^5/120 - …
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you rewrite (sin(x)) in exponential form using sympy.rewrite(sympy.exp)?

A

```python
rewritten = sympy.sin(x).rewrite(sympy.exp)
# (exp(Ix) - exp(-Ix))/(2*I)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you rewrite (log(x, 2)) as a fraction of natural logs using rewrite(sympy.log)?

A

```python
expr = sympy.log(x, 2)
rewritten_log = expr.rewrite(sympy.log)
# log(x)/log(2)
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you evaluate the expression (sqrt{2}) as a floating-point number using evalf()?

A

```python
val = sympy.sqrt(2).evalf()
# 1.414213562…
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you approximate (pi) to 10 decimal places using sympy.nsimplify or .evalf(10)?

A

```python
approx_pi = sympy.pi.evalf(10)
# 3.141592654
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you create a 2x2 symbolic matrix (M = egin{pmatrix} x & 1 \ 2 & y end{pmatrix})?

A

```python
M = sympy.Matrix([[x, 1],
[2, sympy.Symbol(‘y’)]])
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you multiply two symbolic matrices A and B of compatible shapes using A*B?

A

```python
A = sympy.Matrix([[1, 2], [3, 4]])
B = sympy.Matrix([[x, 0], [0, y]])
product = A * B
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you compute the determinant of the 2x2 matrix (egin{pmatrix} x & 2 \ 3 & y end{pmatrix})?

A

```python
mat = sympy.Matrix([[x, 2],
[3, y]])
det_mat = mat.det()
# x*y - 6
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you compute the inverse of the matrix (egin{pmatrix} x & 2 \ 3 & y end{pmatrix}), assuming it’s invertible?
```python mat_inv = mat.inv() ```
26
How do you find the eigenvalues of the 2x2 matrix (egin{pmatrix} x & 1 \ 0 & y end{pmatrix})?
```python mat = sympy.Matrix([[x, 1], [0, y]]) eigs = mat.eigenvalues() # [x, y] ```
27
How do you symbolically sum the series ( sum_{k=0}^n k ) using `sympy.summation`?
```python k, n = sympy.symbols('k n', positive=True) sum_expr = sympy.summation(k, (k, 0, n)) # n*(n+1)/2 ```
28
How do you symbolically compute the product (prod_{k=1}^n (k)) using `sympy.product`?
```python prod_expr = sympy.product(k, (k, 1, n)) # factorial(n) ```
29
How do you define a piecewise function (f(x) = egin{cases} x^2, & x<0 \ x+1, & x ge 0 end{cases}) using `sympy.Piecewise`?
```python f = sympy.Piecewise( (x**2, x < 0), (x + 1, True) ) ```
30
How do you solve the inequality (x^2 - 4 < 0) using `sympy.solve` and `sympy.StrictInequality` or `sympy.solve_inequality`?
```python ineq = x**2 - 4 < 0 solution_ineq = sympy.solve(ineq, x) # -2 < x < 2 ```
31
How do you simplify a complicated expression using `sympy.simplify(expr)` in general?
```python expr = (x**3 - x**2 + 2*x) / x simpl_expr = sympy.simplify(expr) # x^2 - x + 2 ```
32
How do you factor the expression ((x^2 - 1)(x^2 + 1) + (x - 1)(x + 1)) then simplify?
```python expr = (x**2 - 1)*(x**2 + 1) + (x - 1)*(x + 1) fact_expr = sympy.factor(sympy.simplify(expr)) ```
33
How do you perform partial fraction decomposition on ( rac{x+1}{x(x-1)})?
```python fraction = (x + 1) / (x*(x - 1)) part_frac = sympy.apart(fraction) # 1/x + 2/(x-1) ```
34
How do you rewrite (ln(x^2)) as (2 ln(x)) using `sympy.logcombine` or direct rewriting?
```python expr = sympy.log(x**2) rewritten = sympy.logcombine(expr) # 2*log(x) ```
35
How do you compute an indefinite integral of (sin^2(x)) using `sympy.integrate`?
```python int_sin2 = sympy.integrate(sympy.sin(x)**2, x) # x/2 - sin(2*x)/4 + C ```
36
How do you solve a polynomial equation (x^3 - 3x + 1 = 0) symbolically?
```python poly_eq = sympy.Eq(x**3 - 3*x + 1, 0) solutions = sympy.solve(poly_eq, x) ```
37
How do you solve a transcendental equation (sin(x) = x) for a real solution, approximately, using `sympy.nsolve`?
```python guess = 0 solution_approx = sympy.nsolve(sympy.sin(x) - x, guess) ```
38
How do you declare a symbol (x) with the assumption that (x) is positive?
```python x = sympy.Symbol('x', positive=True) ```
39
How do you create a symbolic function (f(x) = x^2) using `sympy.Function`?
```python f = sympy.Function('f')(x) # Then define expression: f_expr = x**2 # Or simpler for direct usage, just write x**2 as expression. ```
40
How do you compute the derivative of a defined function (f(x) = x^2 + 2x) symbolically?
```python f_expr = x**2 + 2*x df = sympy.diff(f_expr, x) # 2*x + 2 ```
41
How do you compute the indefinite integral of a function (f(x) = x^2 + 2x)?
```python sympy.integrate(f_expr, x) # x^3/3 + x^2 + C ```
42
How do you evaluate a symbolic expression `expr` at multiple points, say x=1 and x=2, quickly in a Python loop?
```python for val in [1, 2]: print(expr.subs(x, val)) ```
43
How do you define two symbolic variables `a` and `b` with the assumption they are integers?
```python a = sympy.Symbol('a', integer=True) b = sympy.Symbol('b', integer=True) ```
44
How do you use `sympy.solve` to solve for `x` in the equation `a*x + b = 0`, where `a` and `b` are also symbols?
```python a, b, x = sympy.symbols('a b x') solution = sympy.solve(sympy.Eq(a*x + b, 0), x) # [-b/a] ```
45
How do you use `sympy.nsimplify` to simplify an expression numerically, for example \(\sqrt{2}/2\)?
```python expr = sympy.sqrt(2)/2 simpl_num = sympy.nsimplify(expr, [sympy.sqrt(2)]) # 2**(1/2)/2, might remain the same or become sqrt(2)/2 ```
46
How do you evaluate the Gamma function \(\Gamma(x)\) at \(x = 1/2\) symbolically?
```python from sympy import gamma val = gamma(sympy.Rational(1,2)) # sqrt(pi) ```
47
How do you expand the binomial \((x+y)^3\) in Sympy?
```python y = sympy.Symbol('y') expr = (x + y)**3 expanded = sympy.expand(expr) # x^3 + 3*x^2*y + 3*x*y^2 + y^3 ```
48
How do you do a piecewise integral of the piecewise function \(f(x) = x\) for x>=0, and 0 for x<0, from -1 to 2?
```python f = sympy.Piecewise((0, x < 0), (x, True)) # Integrate from x=-1 to x=2 value = sympy.integrate(f, (x, -1, 2)) # = (1/2)*2^2 = 2 ```
49
How do you evaluate the expression \(\sin(x)\cos(x)\) at \(x = \frac{\pi}{4}\)?
```python value = (sympy.sin(x)*sympy.cos(x)).subs(x, sympy.pi/4) # sin(pi/4)*cos(pi/4) = sqrt(2)/2 * sqrt(2)/2 = 1/2 ```
50
How do you compute the Taylor series expansion of \(\ln(1+x)\) around \(x=0\) up to \(x^4\)?
```python series_ln = sympy.series(sympy.log(1+x), (x, 0, 5)) # x - x^2/2 + x^3/3 - x^4/4 + ... ```
51
How do you perform numeric approximation of a symbolic expression `sympy.sqrt(3)` to 5 decimal places?
```python approx_val = sympy.sqrt(3).evalf(5) # 1.7321... ```
52
How do you declare multiple symbolic variables at once, for example `x0, x1, x2`, using `sympy.symbols`?
```python import sympy x0, x1, x2 = sympy.symbols('x0 x1 x2') ```
53
How do you sympify a string "cos(x) + sin(x)**2" into a Sympy expression?
```python expr_str = "cos(x) + sin(x)**2" expr = sympy.sympify(expr_str) ```
54
How do you create a rational number \(\frac{3}{4}\) in Sympy using `sympy.Rational`?
```python frac = sympy.Rational(3, 4) # 3/4 ```
55
How do you compute the second derivative of \(x^3 + 5x - 1\) with respect to \(x\)?
```python x = sympy.Symbol('x') expr = x**3 + 5*x - 1 second_derivative = sympy.diff(expr, (x, 2)) # 6*x ```
56
How do you compute the definite integral of \(\sin^2(x)\) from \(0\) to \(\pi\)?
```python integrand = sympy.sin(x)**2 res = sympy.integrate(integrand, (x, 0, sympy.pi)) # pi/2 ```
57
How do you find the limit of \(\frac{1}{x}\) as \(x \to \infty\)?
```python limit_val = sympy.limit(1/x, x, sympy.oo) # 0 ```
58
How do you find the limit of \(\frac{1}{x}\) as \(x \to -\infty\)?
```python limit_val = sympy.limit(1/x, x, -sympy.oo) # 0 ```
59
How do you solve a linear system given by a matrix? For example, solve: \[ \begin{pmatrix} 1 & 2 & 1 \\ 3 & 1 & 4 \end{pmatrix} \]
```python from sympy.matrices import Matrix x, y = sympy.symbols('x y') # Coefficients in augmented matrix form: mat = Matrix([ [1, 2, 1], [3, 1, 4] ]) sol = sympy.solve_linear_system(mat, x, y) # {x: ..., y: ...} ```
60
How do you create a 3x3 symbolic matrix and compute its transpose?
```python a, b, c = sympy.symbols('a b c') M = sympy.Matrix([[a, b, c], [1, 2, 3], [x, x+1, x+2]]) M_T = M.T ```
61
How do you evaluate the matrix exponential `exp(A)` for a 2x2 symbolic matrix `A` using `A.exp()`?
```python A = sympy.Matrix([[0, 1], [-1, 0]]) A_exp = A.exp() ```
62
How do you rewrite the trigonometric expression \(\sin^2(x)\) using the identity \(\sin^2(x) = \frac{1 - \cos(2x)}{2}\) via `sympy.trigsimp` or `sympy.expand_trig`?
```python expr = sympy.sin(x)**2 trig_simpl = sympy.trigsimp(expr) # 1 - cos(2*x) # or (1 - cos(2*x))/2 depending on the simplification ```
63
How do you find the series expansion of \(\cos(x)\) around \(x=0\) up to \(x^6\) and then remove the big-O term?
```python series_cos = sympy.series(sympy.cos(x), (x, 0, 7)) # cos(x) expansion series_no_O = series_cos.removeO() ```
64
How do you rewrite \(\tan(x)\) in terms of \(\sin\) and \(\cos\) using `rewrite(sympy.sin)`?
```python expr = sympy.tan(x) rewritten = expr.rewrite(sympy.sin) # sin(x)/cos(x) ```
65
How do you evaluate a symbolic derivative at a specific value? For example, \(\frac{d}{dx} x^2\) at \(x=3\)?
```python f = x**2 df = sympy.diff(f, x) # 2*x val_at_3 = df.subs(x, 3) # 6 ```
66
How do you define a lambda function in Python from a Sympy expression \(\sin(x) + x\) using `sympy.lambdify` so you can evaluate it numerically?
```python f_expr = sympy.sin(x) + x f_lam = sympy.lambdify(x, f_expr, 'math') print(f_lam(3.14)) ```
67
How do you compute the greatest common divisor (gcd) of two polynomials, e.g. \(x^2 - 4\) and \(x^3 - 8x\)?
```python p1 = x**2 - 4 p2 = x**3 - 8*x g = sympy.gcd(p1, p2) # x - 2 or x+2, depending on factoring ```
68
How do you compute the least common multiple (lcm) of two polynomials, e.g. \(x - 1\) and \((x-1)(x+1)\)?
```python p1 = x - 1 p2 = (x - 1)*(x + 1) l = sympy.lcm(p1, p2) # (x - 1)*(x + 1) ```
69
How do you convert an expression `expr = x**3 + x**2 - x + 1` to a polynomial object using `sympy.poly(expr, x)`?
```python p = sympy.poly(expr, x) degree_p = p.degree() ```
70
How do you evaluate the indefinite integral of \(\exp(-x^2)\) symbolically with Sympy (knowing it doesn't have an elementary antiderivative)?
```python int_expr = sympy.integrate(sympy.exp(-x**2), (x)) # returns sqrt(pi)*erf(x)/2 ```
71
How do you compute the error function \(\mathrm{erf}(x)\) at a specific point, say \(x=1\), in a symbolic exact form or approximate form?
```python val_exact = sympy.erf(1) val_approx = val_exact.evalf() ```
72
How do you find the roots of a polynomial \(x^3 - 2x + 1\) using `sympy.roots` or `sympy.nroots`?
```python p = x**3 - 2*x + 1 # For exact roots: r_exact = sympy.roots(p, x) # For numeric approximations: r_approx = sympy.nroots(p) ```
73
How do you get all solutions (including complex) of \(x^2 + 1 = 0\) using `sympy.solve`?
```python sol = sympy.solve(sympy.Eq(x**2 + 1, 0), x, dict=True) # [{x: I}, {x: -I}] ```
74
How do you check the assumptions on a symbol, for example if `x = sympy.Symbol('x', real=True, positive=True)`, how to see `x.is_real` or `x.is_positive`?
```python x = sympy.Symbol('x', real=True, positive=True) print(x.is_real) # True print(x.is_positive) # True ```
75
How do you define a function of two variables, \(f(x, y) = x^2 + y^2\), and compute its gradient (partial derivatives w.r.t. x and y)?
```python x, y = sympy.symbols('x y', real=True) f = x**2 + y**2 grad_f = (sympy.diff(f, x), sympy.diff(f, y)) # (2*x, 2*y) ```
76
How do you compute the Hessian matrix of \(f(x, y) = x^2 + 3xy + y^2\)?
```python f = x**2 + 3*x*y + y**2 H = sympy.hessian(f, (x, y)) # Matrix([[2, 3], # [3, 2]]) ```
77
How do you evaluate the indefinite integral \(\int \ln(x), dx\)?
```python int_ln = sympy.integrate(sympy.log(x), (x)) # x*log(x) - x ```
78
How do you expand a trigonometric expression like \(\sin(x + y)\) using `sympy.expand_trig`?
```python expr = sympy.sin(x + y) expanded_trig = sympy.expand_trig(expr) # sin(x)*cos(y) + cos(x)*sin(y) ```
79
How do you simplify a logical expression like `(~A & B) | (~A & ~B)` using `sympy.simplify_logic`?
```python A, B = sympy.symbols('A B') expr_logical = (~A & B) | (~A & ~B) simpl_logical = sympy.simplify_logic(expr_logical) # ~A ```
80
How do you evaluate whether \(\sqrt{2}\) is rational or an integer using Sympy’s `.is_rational` or `.is_integer`?
```python root2 = sympy.sqrt(2) print(root2.is_rational) # None or False print(root2.is_integer) # False ```
81
How do you perform partial fraction decomposition on \(\frac{x}{(x+1)(x+2)}\) using `sympy.apart`?
```python expr = x / ((x+1)*(x+2)) apart_expr = sympy.apart(expr) # (x + 2) - 2 / ((x+1)(x+2)) => 1/(x+1) - 1/(x+2), etc. ```
82
How do you solve the rational inequality \(\frac{x-1}{x+2} > 0\) using Sympy’s `solve_rational_inequalities`?
```python inequalities = [((x-1, x+2), '>')] sol = sympy.solve_rational_inequalities([inequalities]) # (-∞, -2) U (1, ∞) ```
83
How do you force Sympy to treat a numeric constant as exact rational if possible, for example `sympy.nsimplify(1.5)`?
```python expr = sympy.nsimplify(1.5) # 3/2 ```
84
How do you convert a Sympy expression to a lambda function that uses NumPy for fast numeric evaluation (e.g., `(x + 1)**2`)?
```python f_expr = (x + 1)**2 f_np = sympy.lambdify(x, f_expr, 'numpy') # f_np can now take NumPy arrays ```
85
How do you compute the third derivative of \(\sin(x)\) with respect to \(x\)?
```python third_deriv = sympy.diff(sympy.sin(x), (x, 3)) # -cos(x) ```
86
How do you solve a polynomial equation \(x^4 - 1 = 0\) and get the four complex roots in a list?
```python eq = sympy.Eq(x**4 - 1, 0) solutions = sympy.solve(eq, x, dict=False) # [-1, 1, -I, I] ```
87
How do you evaluate the numeric approximation of \(\ln(2)\) with 8 decimal places?
```python val_ln2 = sympy.log(2).evalf(8) # 0.69314718 ```
88
How do you expand a binomial coefficient symbolically, for instance `sympy.binomial(n, 2)`?
```python n = sympy.Symbol('n', positive=True) binom_expr = sympy.binomial(n, 2) expanded_binom = sympy.expand(binom_expr) # n*(n - 1)/2 ```
89
How do you use the `sympy.solve_poly_inequality` to solve \( (x-2)(x+1) \ge 0\)?
```python ine_expr = (x - 2)*(x + 1) solution_poly_ineq = sympy.solve_poly_inequality(sympy.Poly(ine_expr, x) >= 0, x) # (-∞, -1] U [2, ∞) ```
90
How do you compute the numeric approximation of a definite integral, say \(\int_0^1 e^{-x^2}, dx\), using `.evalf()` after symbolic integration?
```python int_expr = sympy.integrate(sympy.exp(-x**2), (x, 0, 1)) approx_val = int_expr.evalf() ```
91
How do you create a symbolic expression for a sum like \(\sum_{k=1}^n k^2\) using `sympy.summation`?
```python k, n = sympy.symbols('k n', positive=True) sum_expr = sympy.summation(k**2, (k, 1, n)) # n*(n+1)*(2*n+1)/6 ```
92
How do you declare a symbolic constant `C1` or `C2` often used for integration constants, from the `sympy.symbols` function?
```python C1, C2 = sympy.symbols('C1 C2', real=True) ```
93
How do you convert a Sympy expression to a string containing LaTeX code using `sympy.latex`?
```python expr = x**2 + sympy.sin(x) latex_code = sympy.latex(expr) # "\\sin{\\left(x \\right)} + x^{2}" ```
94
How do you define a Wild symbol, say `A = sympy.Wild('A')`, and use it to match a pattern in an expression?
```python A = sympy.Wild('A') pattern_expr = x**2 + 2*x + 1 match_result = pattern_expr.match(x**2 + A*x + 1) # {A: 2} if it matches ```
95
How do you factor the expression \(\cos(x)^2 - \sin(x)^2\) using `sympy.trigsimp`?
```python expr = sympy.cos(x)**2 - sympy.sin(x)**2 fact_trig = sympy.trigsimp(expr) # cos(2*x) ```
96
How do you check if a symbolic expression `expr = x*(x + 1)` is a polynomial in `x` by using `sympy.poly(expr, x)` inside a try-except?
```python try: p = sympy.poly(expr, x) print("It is a polynomial of degree", p.degree()) except sympy.PolynomialError: print("Not a polynomial in x") ```
97
How do you solve for y in the equation \(y + \frac{1}{y} = x\) in terms of x, using `sympy.solve`?
```python y = sympy.Symbol('y', complex=True) eq = sympy.Eq(y + 1/y, x) solutions_y = sympy.solve(eq, y) # [x/2 + sqrt(x^2 - 4)/2, x/2 - sqrt(x^2 - 4)/2] ```
98
How do you define a piecewise function \(f(x) = \begin{cases} x & x>2 \\ 2 & x\le2 \end{cases}\) using `sympy.Piecewise` and then evaluate at x=3?
```python f = sympy.Piecewise( (x, x > 2), (2, True) ) val_at_3 = f.subs(x, 3) # 3 ```
99
How do you convert a piecewise function to a “standard” expression if possible, using `sympy.piecewise_fold`?
```python folded_expr = sympy.piecewise_fold(f) # Might combine piecewise parts if there's a known simplification ```
100
How do you compute \(\frac{\partial^2}{\partial x \partial y} [x^2 + xy + y^2]\) (the mixed partial derivative) in Sympy?
```python expr = x**2 + x*y + y**2 dxy = sympy.diff(expr, x, 1, y, 1) # 1 ```
101
How do you simplify a piecewise rational expression, say \(\frac{x^2 - 1}{x - 1}\), for \(x \neq 1\), using `sympy.cancel` or `sympy.simplify`?
```python expr = (x**2 - 1)/(x - 1) canceled_expr = sympy.cancel(expr) # x + 1 ```
102
How do you declare a Sympy symbol (t) with the assumption that it’s real and nonnegative?
```python import sympy t = sympy.Symbol('t', real=True, nonnegative=True) ```
103
How do you symbolically compute the derivative ( rac{d}{dx}igl(cos(3x)igr)) using `sympy.diff`?
```python x = sympy.Symbol('x') expr = sympy.cos(3*x) deriv = sympy.diff(expr, x) # -3*sin(3*x) ```
104
How do you evaluate (int_0^{pi} sin(2x),dx) using `sympy.integrate`?
```python res = sympy.integrate(sympy.sin(2*x), (x, 0, sympy.pi)) # 0 ```
105
How do you expand (log(x*y)) into (log(x) + log(y)) using `sympy.logcombine` or a direct rewrite method?
```python x, y = sympy.symbols('x y', positive=True) expr = sympy.log(x*y) expanded_log = sympy.logcombine(expr) # or expr.rewrite(sympy.log) # log(x) + log(y) ```
106
How do you create a symbol (n) that’s intended only for integer values (e.g. for summations)?
```python n = sympy.Symbol('n', integer=True) ```
107
How do you compute the indefinite integral of ( rac{1}{x}) in Sympy?
```python expr = 1/x int_expr = sympy.integrate(expr, (x)) # log(x) ```
108
How do you declare a symbolic function (f) of one variable (x) using `sympy.Function` and then refer to (f(x))?
```python f = sympy.Function('f') expr = f(x) # expr represents f(x) symbolically ```
109
How do you solve the equation (e^x = 5) for (x) symbolically?
```python eq = sympy.Eq(sympy.exp(x), 5) sol = sympy.solve(eq, x) # [log(5)] ```
110
How do you solve the equation (log(x) = 3) for (x)?
```python eq = sympy.Eq(sympy.log(x), 3) sol = sympy.solve(eq, x) # [exp(3)] ```
111
How do you check the structure of a Sympy expression, for instance verifying if ((x+1)^2) is a `Pow` node or an `Add` node, using `.func`?
```python expr = (x+1)**2 expr_func = expr.func # sympy.core.power.Pow ```
112
How do you symbolically differentiate (sin(x^2)) with respect to (x)?
```python expr = sympy.sin(x**2) dexpr = sympy.diff(expr, x) # 2*x*cos(x^2) ```
113
How do you compute the Laplace transform of (t^2) with respect to (t) using `sympy.laplace_transform`?
```python t, s = sympy.symbols('t s', positive=True) laplace_expr = sympy.laplace_transform(t**2, t, s) # 2/s^3 ```
114
How do you compute the inverse Laplace transform of ( rac{1}{s^2 + 1}) using `sympy.inverse_laplace_transform`?
```python ilap = sympy.inverse_laplace_transform(1/(s**2+1), s, t) # sin(t) ```
115
How do you compute the integral (int e^{-x^2}, dx) in terms of the error function ((mathrm{erf}))?
```python expr = sympy.exp(-x**2) res = sympy.integrate(expr, x) # sqrt(pi)*erf(x)/2 ```
116
How do you create a piecewise expression such that (g(x) = 1) if (x ge 0) and (-1) otherwise?
```python g = sympy.Piecewise( (1, x >= 0), (-1, True) ) ```
117
How do you convert a Sympy expression, say (x^2 + 2x + 1), into a Python function that uses `math` (not NumPy) via `lambdify`?
```python expr = x**2 + 2*x + 1 f_math = sympy.lambdify(x, expr, 'math') val = f_math(2) # 9 ```
118
How do you compute the Maclaurin series (i.e. expansion around 0) of ( an(x)) up to (x^5)?
```python series_tan = sympy.series(sympy.tan(x), (x, 0, 6)) # x + x^3/3 + 2*x^5/15 + ... ```
119
How do you define a function (F(x) = int_0^x sin(t^2), dt) symbolically in Sympy?
```python t = sympy.Symbol('t', real=True) F_expr = sympy.integrate(sympy.sin(t**2), (t, 0, x)) ```
120
How do you expand a polynomial expression with multiple variables, like ((x + y)^3)?
```python y = sympy.Symbol('y') expr = (x + y)**3 expanded = sympy.expand(expr) # x^3 + 3*x^2*y + 3*x*y^2 + y^3 ```
121
How do you factor the expression (x^3 + 3x^2y + 3xy^2 + y^3)?
```python factored = sympy.factor(x**3 + 3*x**2*y + 3*x*y**2 + y**3) # (x + y)^3 ```
122
How do you solve the equation (x^2 + y^2 = 1) for (y) in terms of (x)?
```python eq = sympy.Eq(x**2 + y**2, 1) sols = sympy.solve(eq, y) # [sqrt(1 - x^2), -sqrt(1 - x^2)] ```
123
How do you find the real part of a symbolic expression like (exp(i x)) using `.expand_complex()` or `.as_real_imag()`?
```python z = sympy.exp(sympy.I*x) real_part = sympy.expand_complex(z).as_real_imag()[0] # cos(x) ```
124
How do you find the imaginary part of (exp(i x)) similarly?
```python imag_part = sympy.expand_complex(z).as_real_imag()[1] # sin(x) ```
125
How do you check if the expression (x^2 - y^2) is factorable in the real domain, and then factor it?
```python expr = x**2 - y**2 fac = sympy.factor(expr) # (x - y)*(x + y) ```
126
How do you do polynomial long division of ((x^3 + 2x + 1)) by ((x - 1)) using `sympy.div`?
```python num = x**3 + 2*x + 1 den = x - 1 quotient, remainder = sympy.div(num, den) ```
127
How do you symbolically compute (int 2x , e^{x^2}, dx) using `sympy.integrate` (recognizing a substitution pattern)?
```python expr = 2*x*sympy.exp(x**2) res = sympy.integrate(expr, (x)) # exp(x^2) ```
128
How do you compute the gradient of ((x - y)^2 + (y - z)^2 + (z - x)^2) with respect to (x, y, z)?
```python x, y, z = sympy.symbols('x y z', real=True) f = (x - y)**2 + (y - z)**2 + (z - x)**2 grad_f = (sympy.diff(f, x), sympy.diff(f, y), sympy.diff(f, z)) ```
129
How do you define a sum (sum_{k=1}^n 2^k) in Sympy and then simplify it?
```python k, n = sympy.symbols('k n', positive=True) sum_expr = sympy.summation(2**k, (k, 1, n)) sympy_simpl = sympy.simplify(sum_expr) # 2*(2^n - 1) ```
130
How do you compute the binomial coefficient (inom{n}{k}) symbolically in Sympy?
```python n, k = sympy.symbols('n k', nonnegative=True) bin_coeff = sympy.binomial(n, k) ```
131
How do you check if ((x^2 + y^2)) depends on (x) by using `expr.has(x)`?
```python expr = x**2 + y**2 expr.has(x) # True expr.has(z) # False if z isn't in the expression ```
132
How do you evaluate the real definite integral (int_{-infty}^{infty} e^{-x^2}, dx)?
```python res = sympy.integrate(sympy.exp(-x**2), (x, -sympy.oo, sympy.oo)) # sqrt(pi) ```
133
How do you create a symbolic constant for (pi) in Sympy (though it’s already built-in as `sympy.pi`)?
```python # It's built-in: sympy.pi pi_val = sympy.pi ```
134
How do you compute the indefinite integral of (x cdot ln(x)) with respect to (x)?
```python expr = x*sympy.log(x) res = sympy.integrate(expr, (x)) # (x^2*log(x))/2 - x^2/4 ```
135
How do you expand the expression ((x + y)^4) using `sympy.expand`?
```python expr = (x + y)**4 expanded = sympy.expand(expr) # x^4 + 4*x^3*y + 6*x^2*y^2 + 4*x*y^3 + y^4 ```
136
How do you factor the polynomial ((x + y)^4 - x^4 - y^4)?
```python poly_expr = (x + y)**4 - x**4 - y**4 factored_poly = sympy.factor(poly_expr) ```
137
How do you compute the indefinite integral (int sec^2(x), dx)?
```python expr = sympy.sec(x)**2 int_expr = sympy.integrate(expr, (x)) # tan(x) ```
138
How do you compute the indefinite integral (int csc^2(x), dx)?
```python expr = sympy.csc(x)**2 int_expr = sympy.integrate(expr, (x)) # -cot(x) ```
139
How do you compute the indefinite integral (int sec(x), dx)?
```python expr = sympy.sec(x) int_expr = sympy.integrate(expr, x) # log(sec(x) + tan(x)) or something equivalent ```
140
How do you compute the indefinite integral (int csc(x), dx)?
```python expr = sympy.csc(x) int_expr = sympy.integrate(expr, x) # log(csc(x) - cot(x)) or an equivalent form ```
141
How do you express ( an^2(x)) in terms of (sec^2(x)) using trigonometric simplifications?
```python expr = sympy.tan(x)**2 simpl_expr = sympy.trigsimp(expr) # sec(x)^2 - 1 ```
142
How do you create a symbolic array (non-Matrix) of shape 2x2, e.g. `sympy.Array([[1, x], [y, 2]])`?
```python A = sympy.Array([[1, x], [y, 2]]) ```
143
How do you compute the sum of absolute values `|x| + |y|` symbolically in Sympy?
```python abs_expr = sympy.Abs(x) + sympy.Abs(y) ```
144
How do you evaluate `|x|` at `x=-3`?
```python val = sympy.Abs(x).subs(x, -3) # 3 ```
145
How do you find an expression’s free symbols, for instance in `expr = x*y + z`, using `.free_symbols`?
```python expr = x*y + sympy.Symbol('z') print(expr.free_symbols) # {x, y, z} ```
146
How do you rename a free symbol in an expression, say rename (x) to (u) in (x^2 + y^2)?
```python expr = x**2 + y**2 new_expr = expr.subs(x, u) ```
147
How do you rename a free symbol in an expression, say rename (x) to (u) in (x^2 + y), using `.xreplace({old: new})`?
```python expr = x**2 + y expr_renamed = expr.xreplace({x: sympy.Symbol('u')}) ```
148
How do you convert a Sympy `Matrix` to a nested Python list using `.tolist()`?
```python mat = sympy.Matrix([[1, 2], [3, 4]]) py_list = mat.tolist() # [[1, 2], [3, 4]] ```
149
How do you compute the cofactor matrix (matrix of cofactors) of a 2x2 or 3x3 matrix using `.cofactor_matrix()`?
```python M = sympy.Matrix([[a, b], [c, d]]) cof_mat = M.cofactor_matrix() ```
150
How do you specify that (x) is a complex variable using `sympy.Symbol('x', complex=True)`?
```python x = sympy.Symbol('x', complex=True) ```
151
How do you compute (sum_{k=0}^{infty} x^k) symbolically, recognizing it as a geometric series ((|x|<1)) using `sympy.summation` and `.simplify()`?
```python k = sympy.Symbol('k', nonnegative=True) sum_expr = sympy.summation(x**k, (k, 0, sympy.oo)) geom_simpl = sympy.simplify(sum_expr) # 1/(1 - x), for |x| < 1 ```
152
How do you get the sorted list of all symbols in an expression, for instance in `(x + 2)*(y + 3*z)`?
```python expr = (x + 2)*(y + 3*z) sym_list = sorted(expr.free_symbols, key=lambda s: s.name) # [x, y, z], in alphabetical order ```
153
How do you compute the absolute value of a variable (x) when (x = -3) using `sympy.Abs`?
```python val = sympy.Abs(x).subs(x, -3) # 3 ```
154
How do you create a piecewise function (f(x)) such that (f(x) = x^2) for (x < 2) and (f(x) = 4) for (x ge 2), and then differentiate it?
```python import sympy x = sympy.Symbol('x', real=True) f = sympy.Piecewise( (x**2, x < 2), (4, True) ) df = sympy.diff(f, x) # Derivative is 2*x for x<2, and 0 for x>=2 ```
155
How do you declare two symbolic parameters `a` and `b`, define `expr = a^2 + 2*a*b + b^2`, and factor it?
```python a, b = sympy.symbols('a b', real=True) expr = a**2 + 2*a*b + b**2 factored_expr = sympy.factor(expr) # (a + b)**2 ```
156
How do you compute the residue of the function ( rac{1}{(x - 1)(x - 2)}) at the pole (x=1) using `sympy.residue`?
```python expr = 1 / ((x - 1)*(x - 2)) resid = sympy.residue(expr, x, 1) # 1/(1 - 2) = -1 ```
157
How do you construct a `Lambda` object for the expression (x^2 + 1) so that it can be called like a function in Python?
```python f = sympy.Lambda(x, x**2 + 1) value_at_3 = f(3) # 10 ```
158
How do you compute the polynomial remainder of (x^3 + 2x + 1) when divided by (x - 1) using `sympy.rem`?
```python num = x**3 + 2*x + 1 den = x - 1 remainder = sympy.rem(num, den) # Evaluate or check the symbolic expression for the remainder ```
159
How do you convert a Sympy expression like (x^2 + 1) into a Python lambda function that uses NumPy for fast numeric operations?
```python expr = x**2 + 1 f_numpy = sympy.lambdify(x, expr, 'numpy') import numpy as np arr = np.array([0, 1, 2]) res = f_numpy(arr) # array([1, 2, 5]) ```
160
How do you compute the Taylor (Maclaurin) series expansion of (exp(3x)) around (x=0) up to (x^4)?
```python series_expr = sympy.series(sympy.exp(3*x), (x, 0, 5)) # 1 + 3*x + 9*x^2/2 + 9*x^3/2 + 81*x^4/24 + O(x^5) ```
161
How do you define a symbolic function (g(x,y) = x + 2y), then partially differentiate it with respect to (y)?
```python x, y = sympy.symbols('x y', real=True) g_expr = x + 2*y dg_dy = sympy.diff(g_expr, y) # 2 ```
162
How do you use `sympy.integrate` to compute (int cos(3x), dx)?
```python expr = sympy.cos(3*x) int_expr = sympy.integrate(expr, (x)) # sympy.sin(3*x)/3 ```
163
How do you solve the equation ( an(x) = 1) for (x) in the principal domain using `sympy.solve`?
```python eq = sympy.Eq(sympy.tan(x), 1) solutions = sympy.solve(eq, x, dict=False) # [pi/4 + n*pi], but solve() typically gives a principal solution plus a symbolic periodic term. ```
164
How do you compute the complex conjugate of (exp(i x)) using `sympy.conjugate`?
```python z = sympy.exp(sympy.I*x) z_conjugate = sympy.conjugate(z) # exp(-I*x) ```
165
How do you substitute (x = 3) and (y = 4) simultaneously into the expression (x^2 + y^2)?
```python expr = x**2 + y**2 val = expr.subs({x: 3, y: 4}) # 25 ```
166
How do you force an expression like (sqrt{4}) to be simplified to `2` using `sympy.sqrt` plus a simplification?
```python expr = sympy.sqrt(4) simpl_expr = sympy.simplify(expr) # 2 ```
167
How do you evaluate the indefinite integral of ( rac{1}{1 + x^2}) (which is (arctan(x)))?
```python expr = 1/(1 + x**2) int_expr = sympy.integrate(expr, (x)) # atan(x) ```
168
How do you solve the system: \( \begin{cases} x + y = 10 \ x - y = 2 \end{cases} \) for \(x, y\)?
```python sol = sympy.solve([x + y - 10, x - y - 2], [x, y]) # {x: 6, y: 4} ```
169
How do you compute the Jacobian matrix of \(\vec{F}(x,y) = (x^2 + y, \; x + y^2)\)?
```python F1 = x**2 + y F2 = x + y**2 J = sympy.Matrix([F1, F2]).jacobian([x, y]) # [[2*x, 1 ], # [ 1, 2*y ]] ```
170
How do you compute the inverse Laplace transform of \(\frac{1}{s^2 + 4}\) in terms of `t`?
```python s, t = sympy.symbols('s t', real=True, positive=True) expr = 1/(s**2 + 4) ilap = sympy.inverse_laplace_transform(expr, s, t) # sin(2*t)/2 ```
171
How do you compute the Laplace transform of \(\cos(2t)\) with respect to \(t\)?
```python laplace_expr = sympy.laplace_transform(sympy.cos(2*t), t, s) # s/(s^2 + 4) ```
172
How do you do polynomial long division of \(x^4 + x^2 + 1\) by \(x^2 - 1\) using `sympy.div`?
```python num = x**4 + x**2 + 1 den = x**2 - 1 quotient, remainder = sympy.div(num, den) # quotient and remainder are symbolic polynomials ```
173
How do you integrate \(\sec^3(x)\) with respect to \(x\) using Sympy?
```python expr = sympy.sec(x)**3 int_expr = sympy.integrate(expr, x) # (1/2)*log|sec(x) + tan(x)| + (1/2)*sec(x)*tan(x) ```
174
How do you factor the polynomial \(x^4 - 1\) using `sympy.factor`?
```python p = x**4 - 1 factored = sympy.factor(p) # (x - 1)*(x + 1)*(x^2 + 1) ```
175
How do you use `sympy.radsimp` to simplify a radical expression, for example \(\frac{1}{\sqrt{2}} + \frac{\sqrt{18}}{6}\)?
```python expr = 1/sympy.sqrt(2) + sympy.sqrt(18)/6 rad_simpl = sympy.radsimp(expr) # Typically simplifies the radicals (e.g. sqrt(2)/2 + sqrt(2)/2 = sqrt(2)) ```
176
How do you compute \(\int x \cdot \ln(x), dx\) again, but confirm by differentiating the result?
```python int_expr = sympy.integrate(x*sympy.log(x), x) # (x^2*log(x))/2 - x^2/4 diff_back = sympy.diff(int_expr, x) # x*log(x) ```
177
How do you define a symbolic summation of \(\sum_{k=1}^n \frac{1}{k}\) and see if Sympy can simplify it?
```python k, n = sympy.symbols('k n', positive=True) harm_sum = sympy.summation(1/k, (k, 1, n)) # This is the n-th harmonic number H_n, often returned as sympy.HarmonicNumber(n) ```
178
How do you compute the numeric approximation of \(\zeta(3)\) (the Riemann zeta function at 3) to 6 decimal places in Sympy?
```python val = sympy.zeta(3).evalf(6) # ~ 1.20206 ```
179
How do you find the limit of \(\left(1 + \frac{1}{n}\right)^n\) as \(n \to \infty\)?
```python n = sympy.Symbol('n', positive=True) expr = (1 + 1/n)**n limit_val = sympy.limit(expr, n, sympy.oo) # E ```
180
How do you rewrite \(\cos^3(x)\) using `sympy.trigsimp` or `sympy.expand_trig`?
```python expr = sympy.cos(x)**3 expanded_trig = sympy.expand_trig(expr) # (3*cos(x) + cos(3*x))/4 ```
181
How do you check the derivative of \(\arctan(x)\) using `sympy.diff`, verifying it is \(\frac{1}{1+x^2}\)?
```python d_atan = sympy.diff(sympy.atan(x), x) # 1/(1 + x^2) ```
182
How do you define a function \(f(x) = \sqrt{x^2 + 1}\) and find its derivative?
```python f_expr = sympy.sqrt(x**2 + 1) df = sympy.diff(f_expr, x) # x/sqrt(x^2 + 1) ```
183
How do you create a diagonal matrix with symbolic entries `[x, y, z]` using `sympy.diag`?
```python diag_mat = sympy.diag(x, y, z) # Matrix([[x, 0, 0], # [0, y, 0], # [0, 0, z]]) ```
184
How do you declare that a symbol `n` is positive and an integer, and then see `n.is_integer`, `n.is_positive`?
```python n = sympy.Symbol('n', integer=True, positive=True) print(n.is_integer) # True print(n.is_positive) # True ```
185
How do you compute the derivative with respect to `x` of `x^2 + 3*x*y` treating `y` as a constant?
```python expr = x**2 + 3*x*y d_expr = sympy.diff(expr, x) # 2*x + 3*y ```
186
How do you define `f(x, y)` as `x^2 + y^3` and compute the directional derivative in the direction `(1,1)` at the point `(1,1)`?
```python f = x**2 + y**3 grad_f = (sympy.diff(f, x), sympy.diff(f, y)) # (2*x, 3*y^2) direction = sympy.Matrix([1, 1]) point = {x: 1, y: 1} grad_val = sympy.Matrix([g.subs(point) for g in grad_f]) # [2, 3] dir_deriv = grad_val.dot(direction) # 2 + 3 = 5 ```
187
How do you solve the polynomial inequality \(x^2 - 4 \ge 0\) using `sympy.solve` or `solve_inequality`?
```python ineq = x**2 - 4 >= 0 solution_ineq = sympy.solve(ineq, x) # (-∞, -2] U [2, ∞) ```
188
How do you evaluate the gamma function \(\Gamma(n)\) for a positive integer `n` so that it returns `(n-1)!` in simplified form?
```python n = sympy.Symbol('n', positive=True, integer=True) gamma_expr = sympy.gamma(n) # gamma_expr simplifies to factorial(n-1) or Gamma(n) sym_simpl = sympy.simplify(gamma_expr) ```
189
How do you find the minimal polynomial of \(\sqrt{2} + \sqrt{3}\) over the rationals using `sympy.minpoly`?
```python alpha = sympy.sqrt(2) + sympy.sqrt(3) min_poly = sympy.minpoly(alpha, x) # x^4 - 10*x^2 + 1, for example ```
190
How do you convert the polynomial `p = x^4 - 10*x^2 + 1` to a `sympy.polys.polytools.poly` object and get its degree?
```python p = x**4 - 10*x**2 + 1 poly_obj = sympy.poly(p, x) deg = poly_obj.degree() # 4 ```
191
How do you compute the indefinite integral \(\int \sinh(x), dx\)?
```python expr = sympy.sinh(x) int_expr = sympy.integrate(expr, x) # cosh(x) ```
192
How do you compute the indefinite integral \(\int \cosh(x), dx\)?
```python expr = sympy.cosh(x) int_expr = sympy.integrate(expr, x) # sinh(x) ```
193
How do you rewrite \(\cosh^2(x)\) in terms of \(\cosh(2x)\) using a trigonometric/hyperbolic identity simplification?
```python expr = sympy.cosh(x)**2 rewritten = sympy.trigsimp(expr) # (1 + cosh(2*x))/2 ```
194
How do you compute the real part of \(\exp(x + i,y)\) (i.e., \(e^x \cos y\)) in Sympy?
```python z = sympy.exp(x + sympy.I*y) real_z = sympy.re(z) # exp(x)*cos(y) ```
195
How do you compute the imaginary part of \(\exp(x + i,y)\) (i.e., \(e^x \sin y\)) similarly?
```python imag_z = sympy.im(z) # exp(x)*sin(y) ```
196
How do you find a power series expansion of \(\ln(1 + x)\) at \(x=0\) up to \(x^4\)?
```python series_log = sympy.series(sympy.log(1+x), (x, 0, 5)) # x - x^2/2 + x^3/3 - x^4/4 + O(x^5) ```
197
How do you define a function \(F(z) = z^2 + 1\) where `z` is a complex symbol, and find its derivative w.r.t. `z` in Sympy?
```python z = sympy.Symbol('z', complex=True) F = z**2 + 1 dF = sympy.diff(F, z) # 2*z ```