AI Skill Report Card
Computing Complex Math
Quick Start13 / 15
Pythonimport cmath # Basic operations cmath.sqrt(-1) # 1j cmath.exp(1j * cmath.pi) # -1+1.2246e-16j (Euler's identity) cmath.log(1j) # 1.5707963267948966j # Polar conversion z = 3 + 4j r, phi = cmath.polar(z) # (5.0, 0.9272952180016122) z2 = cmath.rect(r, phi) # (3+4j) reconstructed
Recommendation▾
Add an example showing signal processing use case (mentioned in description but not demonstrated), since that's cited as a trigger scenario
Workflow13 / 15
Progress:
- Identify if input/output involves complex numbers (vs. real-only
mathmodule) - Choose the right function category (conversion, power/log, trig, hyperbolic, classification)
- Handle edge cases (infinities, NaN) if inputs may be non-finite
- Verify results with
isclose()instead of==for float comparisons
Function Categories
Conversions to/from polar coordinates:
Pythoncmath.phase(z) # argument (angle) of z, range (-pi, pi] cmath.polar(z) # returns (r, phi) tuple cmath.rect(r, phi) # builds complex number from polar coords
Power and logarithmic functions:
Pythoncmath.exp(z) # e**z cmath.log(z) # natural log; cmath.log(z, base) for other bases cmath.log10(z) # base-10 log cmath.sqrt(z) # square root, principal branch
Trigonometric functions:
Pythoncmath.sin(z), cmath.cos(z), cmath.tan(z) cmath.asin(z), cmath.acos(z), cmath.atan(z)
Hyperbolic functions:
Pythoncmath.sinh(z), cmath.cosh(z), cmath.tanh(z) cmath.asinh(z), cmath.acosh(z), cmath.atanh(z)
Classification functions:
Pythoncmath.isfinite(z) # True if both real and imag parts finite cmath.isinf(z) # True if either part is infinite cmath.isnan(z) # True if either part is NaN cmath.isclose(a, b, rel_tol=1e-09, abs_tol=0.0) # safe comparison
Constants:
Pythoncmath.pi, cmath.e, cmath.tau, cmath.inf, cmath.infj, cmath.nan, cmath.nanj
Recommendation▾
Include a 'bad output' example, e.g. using math.sqrt on negative input causing ValueError, to explicitly contrast with correct cmath usage
Examples15 / 20
Example 1: Solving a quadratic with negative discriminant
Input: Solve x^2 + 2x + 5 = 0 (discriminant = -16)
Output:
Pythonimport cmath a, b, c = 1, 2, 5 disc = b**2 - 4*a*c x1 = (-b + cmath.sqrt(disc)) / (2*a) x2 = (-b - cmath.sqrt(disc)) / (2*a) # x1 = (-1+2j), x2 = (-1-2j)
Example 2: Converting rectangular to polar and back
Input: z = 1 + 1j
Output:
Pythonr, phi = cmath.polar(z) # (1.4142135623730951, 0.7853981633974483) cmath.rect(r, phi) # (1.0000000000000002+0.9999999999999999j)
Example 3: Checking for special values
Input: z = complex(cmath.inf, 0)
Output: cmath.isinf(z) → True; cmath.isfinite(z) → False
Recommendation▾
Workflow checklist is somewhat generic; tie it more concretely to a decision tree (e.g., 'if input contains negative sqrt -> use cmath.sqrt')
Best Practices
- Use
cmathinstead ofmathwhenever a value could be negative under a square root/log, or whenever inputs are already typed ascomplex. - Always use
cmath.isclose()for equality checks — never compare complex floats with==. cmath.phase(z)returns radians in(-pi, pi]; convert to degrees withmath.degrees()if needed.- Prefer
cmath.rect(r, phi)over manually computingr * (cos(phi) + 1j*sin(phi)). - Remember
cmath.sqrtandcmath.logreturn the principal branch — expect one specific root/value, not all solutions.
Common Pitfalls
- Don't use
math.sqrt()on a value that might be negative — it raisesValueError; usecmath.sqrt()instead. - Don't assume
cmath.phase(0j)is meaningful — phase of zero is0.0but is mathematically undefined; treat with care in downstream logic. - Don't forget that
cmathfunctions always returncomplextype, even when the mathematical result is real (e.g.,cmath.sqrt(4)returns(2+0j), not2.0). - Don't use
==to comparenancomponents —cmath.isnan(z)follows IEEE 754 semantics wherenan != nan.