Skip to content

Commit 02f917c

Browse files
Manual review of builtin functions: complex-memoryview
1 parent 30d2cf8 commit 02f917c

1 file changed

Lines changed: 3 additions & 119 deletions

File tree

docs/builtins/complex_func.md

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The `complex()` function creates complex numbers from numeric values or strings.
99
| From two numbers | O(1) | O(1) | real + imaginary |
1010
| From complex | O(1) | O(1) | Copy or identity |
1111
| From string | O(n) | O(1) | n = string length |
12-
| From int/float | O(1) | O(1) | Direct conversion |
12+
| From int/float | O(1) | O(1) | Direct conversion for floats and small ints; O(n) for arbitrary precision ints |
1313

1414
## Basic Usage
1515

@@ -36,7 +36,7 @@ c = complex(0) # 0j
3636
```python
3737
# O(1)
3838
original = complex(3, 4)
39-
c = complex(original) # (3+4j) - creates copy
39+
c = complex(original) # (3+4j) - returns the same object
4040
```
4141

4242
### From String
@@ -104,58 +104,6 @@ c = 3 + 4j # Direct syntax (no function call)
104104
c = complex("3+4j") # O(5)
105105
```
106106

107-
### Engineering Applications
108-
109-
```python
110-
# O(1) - electrical impedance
111-
resistance = 100 # Ohms
112-
reactance = 50 # Ohms
113-
114-
impedance = complex(resistance, reactance) # (100+50j)
115-
116-
# Magnitude - O(1)
117-
z = abs(impedance) # sqrt(100^2 + 50^2) ≈ 111.8
118-
119-
# Phase angle - O(1)
120-
import math
121-
phase = math.atan2(impedance.imag, impedance.real)
122-
```
123-
124-
### Complex Arithmetic
125-
126-
```python
127-
# O(1) - all operations
128-
c1 = complex(1, 2)
129-
c2 = complex(3, 4)
130-
131-
# Basic operations
132-
sum_val = c1 + c2 # (4+6j)
133-
diff = c1 - c2 # (-2-2j)
134-
prod = c1 * c2 # (-5+10j)
135-
quot = c1 / c2 # (0.44+0.08j)
136-
137-
# Power
138-
power = c1 ** 2 # (-3+4j)
139-
```
140-
141-
### Polar to Rectangular Conversion
142-
143-
```python
144-
# O(1) - convert between forms
145-
import cmath
146-
147-
# Magnitude and phase
148-
magnitude = 5
149-
phase = 0.927 # radians
150-
151-
# Rectangular form
152-
c = magnitude * cmath.exp(1j * phase) # (3+4j)
153-
154-
# Back to polar
155-
mag = abs(c) # 5
156-
phi = cmath.phase(c) # 0.927
157-
```
158-
159107
## Performance Patterns
160108

161109
### vs Tuple Representation
@@ -196,70 +144,6 @@ c = complex("3+4j") # O(5)
196144
# For user input, use complex()
197145
```
198146

199-
## Practical Examples
200-
201-
### Signal Processing
202-
203-
```python
204-
# O(n) - process complex signals
205-
import cmath
206-
207-
def frequency_response(frequencies):
208-
responses = []
209-
for freq in frequencies:
210-
# Complex impedance at frequency
211-
impedance = complex(100, 2 * 3.14159 * freq * 0.001)
212-
response = abs(impedance) # O(1)
213-
responses.append(response)
214-
return responses
215-
216-
freqs = [100, 1000, 10000]
217-
responses = frequency_response(freqs) # O(n)
218-
```
219-
220-
### Quantum Mechanics
221-
222-
```python
223-
# O(1) - quantum amplitude
224-
amplitude = complex(0.707, -0.707) # 1/sqrt(2) * (1 - i)
225-
226-
# Magnitude (probability)
227-
probability = abs(amplitude) ** 2 # 0.5
228-
229-
# Phase
230-
import cmath
231-
phase = cmath.phase(amplitude) # -π/4
232-
```
233-
234-
### Electrical Impedance
235-
236-
```python
237-
# O(1) - impedance calculation
238-
impedance = complex(50, 75) # 50 + 75j Ohms
239-
240-
# Admittance (reciprocal)
241-
admittance = 1 / impedance # O(1)
242-
243-
# Power calculation
244-
voltage = complex(230, 0)
245-
current = voltage / impedance # O(1)
246-
power = voltage * current.conjugate() # O(1)
247-
```
248-
249-
### Fourier Analysis
250-
251-
```python
252-
# O(n) - create complex exponentials
253-
import cmath
254-
255-
def fourier_basis(n, k):
256-
# e^(-2πijk/n)
257-
factor = -2j * 3.14159 * k / n
258-
return cmath.exp(factor) # O(1)
259-
260-
basis = fourier_basis(8, 2) # Complex basis function
261-
```
262-
263147
## Edge Cases
264148

265149
### Zero Complex
@@ -295,7 +179,7 @@ c = complex(3, 4) # (3+4j)
295179
conj = c.conjugate() # (3-4j)
296180

297181
# Useful in calculations
298-
magnitude_sq = c * c.conjugate() # 9 + 16 = 25
182+
magnitude_sq = (c * c.conjugate()).real # 9 + 16 = 25.0
299183
```
300184

301185
### From String Errors

0 commit comments

Comments
 (0)