Skip to content

Commit 140ebe6

Browse files
authored
complex.nim: Use func everywhere (#16294)
1 parent 40255f6 commit 140ebe6

File tree

2 files changed

+61
-60
lines changed

2 files changed

+61
-60
lines changed

lib/pure/complex.nim

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ type
2424
Complex32* = Complex[float32]
2525
## Alias for a pair of 32-bit floats.
2626

27-
proc complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
27+
func complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
2828
result.re = re
2929
result.im = im
3030

31-
proc complex32*(re: float32; im: float32 = 0.0): Complex[float32] =
31+
func complex32*(re: float32; im: float32 = 0.0): Complex[float32] =
3232
result.re = re
3333
result.im = im
3434

35-
proc complex64*(re: float64; im: float64 = 0.0): Complex[float64] =
35+
func complex64*(re: float64; im: float64 = 0.0): Complex[float64] =
3636
result.re = re
3737
result.im = im
3838

@@ -41,71 +41,71 @@ template im*(arg: typedesc[float64]): Complex64 = complex[float64](0, 1)
4141
template im*(arg: float32): Complex32 = complex[float32](0, arg)
4242
template im*(arg: float64): Complex64 = complex[float64](0, arg)
4343

44-
proc abs*[T](z: Complex[T]): T =
44+
func abs*[T](z: Complex[T]): T =
4545
## Returns the distance from (0,0) to ``z``.
4646
result = hypot(z.re, z.im)
4747

48-
proc abs2*[T](z: Complex[T]): T =
48+
func abs2*[T](z: Complex[T]): T =
4949
## Returns the squared distance from (0,0) to ``z``.
5050
result = z.re*z.re + z.im*z.im
5151

52-
proc conjugate*[T](z: Complex[T]): Complex[T] =
52+
func conjugate*[T](z: Complex[T]): Complex[T] =
5353
## Conjugates of complex number ``z``.
5454
result.re = z.re
5555
result.im = -z.im
5656

57-
proc inv*[T](z: Complex[T]): Complex[T] =
57+
func inv*[T](z: Complex[T]): Complex[T] =
5858
## Multiplicatives inverse of complex number ``z``.
5959
conjugate(z) / abs2(z)
6060

61-
proc `==` *[T](x, y: Complex[T]): bool =
61+
func `==` *[T](x, y: Complex[T]): bool =
6262
## Compares two complex numbers ``x`` and ``y`` for equality.
6363
result = x.re == y.re and x.im == y.im
6464

65-
proc `+` *[T](x: T; y: Complex[T]): Complex[T] =
65+
func `+` *[T](x: T; y: Complex[T]): Complex[T] =
6666
## Adds a real number to a complex number.
6767
result.re = x + y.re
6868
result.im = y.im
6969

70-
proc `+` *[T](x: Complex[T]; y: T): Complex[T] =
70+
func `+` *[T](x: Complex[T]; y: T): Complex[T] =
7171
## Adds a complex number to a real number.
7272
result.re = x.re + y
7373
result.im = x.im
7474

75-
proc `+` *[T](x, y: Complex[T]): Complex[T] =
75+
func `+` *[T](x, y: Complex[T]): Complex[T] =
7676
## Adds two complex numbers.
7777
result.re = x.re + y.re
7878
result.im = x.im + y.im
7979

80-
proc `-` *[T](z: Complex[T]): Complex[T] =
80+
func `-` *[T](z: Complex[T]): Complex[T] =
8181
## Unary minus for complex numbers.
8282
result.re = -z.re
8383
result.im = -z.im
8484

85-
proc `-` *[T](x: T; y: Complex[T]): Complex[T] =
85+
func `-` *[T](x: T; y: Complex[T]): Complex[T] =
8686
## Subtracts a complex number from a real number.
8787
x + (-y)
8888

89-
proc `-` *[T](x: Complex[T]; y: T): Complex[T] =
89+
func `-` *[T](x: Complex[T]; y: T): Complex[T] =
9090
## Subtracts a real number from a complex number.
9191
result.re = x.re - y
9292
result.im = x.im
9393

94-
proc `-` *[T](x, y: Complex[T]): Complex[T] =
94+
func `-` *[T](x, y: Complex[T]): Complex[T] =
9595
## Subtracts two complex numbers.
9696
result.re = x.re - y.re
9797
result.im = x.im - y.im
9898

99-
proc `/` *[T](x: Complex[T]; y: T): Complex[T] =
99+
func `/` *[T](x: Complex[T]; y: T): Complex[T] =
100100
## Divides complex number ``x`` by real number ``y``.
101101
result.re = x.re / y
102102
result.im = x.im / y
103103

104-
proc `/` *[T](x: T; y: Complex[T]): Complex[T] =
104+
func `/` *[T](x: T; y: Complex[T]): Complex[T] =
105105
## Divides real number ``x`` by complex number ``y``.
106106
result = x * inv(y)
107107

108-
proc `/` *[T](x, y: Complex[T]): Complex[T] =
108+
func `/` *[T](x, y: Complex[T]): Complex[T] =
109109
## Divides ``x`` by ``y``.
110110
var r, den: T
111111
if abs(y.re) < abs(y.im):
@@ -119,44 +119,44 @@ proc `/` *[T](x, y: Complex[T]): Complex[T] =
119119
result.re = (x.re + r * x.im) / den
120120
result.im = (x.im - r * x.re) / den
121121

122-
proc `*` *[T](x: T; y: Complex[T]): Complex[T] =
122+
func `*` *[T](x: T; y: Complex[T]): Complex[T] =
123123
## Multiplies a real number and a complex number.
124124
result.re = x * y.re
125125
result.im = x * y.im
126126

127-
proc `*` *[T](x: Complex[T]; y: T): Complex[T] =
127+
func `*` *[T](x: Complex[T]; y: T): Complex[T] =
128128
## Multiplies a complex number with a real number.
129129
result.re = x.re * y
130130
result.im = x.im * y
131131

132-
proc `*` *[T](x, y: Complex[T]): Complex[T] =
132+
func `*` *[T](x, y: Complex[T]): Complex[T] =
133133
## Multiplies ``x`` with ``y``.
134134
result.re = x.re * y.re - x.im * y.im
135135
result.im = x.im * y.re + x.re * y.im
136136

137137

138-
proc `+=` *[T](x: var Complex[T]; y: Complex[T]) =
138+
func `+=` *[T](x: var Complex[T]; y: Complex[T]) =
139139
## Adds ``y`` to ``x``.
140140
x.re += y.re
141141
x.im += y.im
142142

143-
proc `-=` *[T](x: var Complex[T]; y: Complex[T]) =
143+
func `-=` *[T](x: var Complex[T]; y: Complex[T]) =
144144
## Subtracts ``y`` from ``x``.
145145
x.re -= y.re
146146
x.im -= y.im
147147

148-
proc `*=` *[T](x: var Complex[T]; y: Complex[T]) =
148+
func `*=` *[T](x: var Complex[T]; y: Complex[T]) =
149149
## Multiplies ``y`` to ``x``.
150150
let im = x.im * y.re + x.re * y.im
151151
x.re = x.re * y.re - x.im * y.im
152152
x.im = im
153153

154-
proc `/=` *[T](x: var Complex[T]; y: Complex[T]) =
154+
func `/=` *[T](x: var Complex[T]; y: Complex[T]) =
155155
## Divides ``x`` by ``y`` in place.
156156
x = x / y
157157

158158

159-
proc sqrt*[T](z: Complex[T]): Complex[T] =
159+
func sqrt*[T](z: Complex[T]): Complex[T] =
160160
## Square root for a complex number ``z``.
161161
var x, y, w, r: T
162162

@@ -179,28 +179,28 @@ proc sqrt*[T](z: Complex[T]): Complex[T] =
179179
result.im = if z.im >= 0.0: w else: -w
180180
result.re = z.im / (result.im + result.im)
181181

182-
proc exp*[T](z: Complex[T]): Complex[T] =
182+
func exp*[T](z: Complex[T]): Complex[T] =
183183
## ``e`` raised to the power ``z``.
184184
var
185185
rho = exp(z.re)
186186
theta = z.im
187187
result.re = rho * cos(theta)
188188
result.im = rho * sin(theta)
189189

190-
proc ln*[T](z: Complex[T]): Complex[T] =
190+
func ln*[T](z: Complex[T]): Complex[T] =
191191
## Returns the natural log of ``z``.
192192
result.re = ln(abs(z))
193193
result.im = arctan2(z.im, z.re)
194194

195-
proc log10*[T](z: Complex[T]): Complex[T] =
195+
func log10*[T](z: Complex[T]): Complex[T] =
196196
## Returns the log base 10 of ``z``.
197197
result = ln(z) / ln(10.0)
198198

199-
proc log2*[T](z: Complex[T]): Complex[T] =
199+
func log2*[T](z: Complex[T]): Complex[T] =
200200
## Returns the log base 2 of ``z``.
201201
result = ln(z) / ln(2.0)
202202

203-
proc pow*[T](x, y: Complex[T]): Complex[T] =
203+
func pow*[T](x, y: Complex[T]): Complex[T] =
204204
## ``x`` raised to the power ``y``.
205205
if x.re == 0.0 and x.im == 0.0:
206206
if y.re == 0.0 and y.im == 0.0:
@@ -222,126 +222,126 @@ proc pow*[T](x, y: Complex[T]): Complex[T] =
222222
result.re = s * cos(r)
223223
result.im = s * sin(r)
224224

225-
proc pow*[T](x: Complex[T]; y: T): Complex[T] =
225+
func pow*[T](x: Complex[T]; y: T): Complex[T] =
226226
## Complex number ``x`` raised to the power ``y``.
227227
pow(x, complex[T](y))
228228

229229

230-
proc sin*[T](z: Complex[T]): Complex[T] =
230+
func sin*[T](z: Complex[T]): Complex[T] =
231231
## Returns the sine of ``z``.
232232
result.re = sin(z.re) * cosh(z.im)
233233
result.im = cos(z.re) * sinh(z.im)
234234

235-
proc arcsin*[T](z: Complex[T]): Complex[T] =
235+
func arcsin*[T](z: Complex[T]): Complex[T] =
236236
## Returns the inverse sine of ``z``.
237237
result = -im(T) * ln(im(T) * z + sqrt(T(1.0) - z*z))
238238

239-
proc cos*[T](z: Complex[T]): Complex[T] =
239+
func cos*[T](z: Complex[T]): Complex[T] =
240240
## Returns the cosine of ``z``.
241241
result.re = cos(z.re) * cosh(z.im)
242242
result.im = -sin(z.re) * sinh(z.im)
243243

244-
proc arccos*[T](z: Complex[T]): Complex[T] =
244+
func arccos*[T](z: Complex[T]): Complex[T] =
245245
## Returns the inverse cosine of ``z``.
246246
result = -im(T) * ln(z + sqrt(z*z - T(1.0)))
247247

248-
proc tan*[T](z: Complex[T]): Complex[T] =
248+
func tan*[T](z: Complex[T]): Complex[T] =
249249
## Returns the tangent of ``z``.
250250
result = sin(z) / cos(z)
251251

252-
proc arctan*[T](z: Complex[T]): Complex[T] =
252+
func arctan*[T](z: Complex[T]): Complex[T] =
253253
## Returns the inverse tangent of ``z``.
254254
result = T(0.5)*im(T) * (ln(T(1.0) - im(T)*z) - ln(T(1.0) + im(T)*z))
255255

256-
proc cot*[T](z: Complex[T]): Complex[T] =
256+
func cot*[T](z: Complex[T]): Complex[T] =
257257
## Returns the cotangent of ``z``.
258258
result = cos(z)/sin(z)
259259

260-
proc arccot*[T](z: Complex[T]): Complex[T] =
260+
func arccot*[T](z: Complex[T]): Complex[T] =
261261
## Returns the inverse cotangent of ``z``.
262262
result = T(0.5)*im(T) * (ln(T(1.0) - im(T)/z) - ln(T(1.0) + im(T)/z))
263263

264-
proc sec*[T](z: Complex[T]): Complex[T] =
264+
func sec*[T](z: Complex[T]): Complex[T] =
265265
## Returns the secant of ``z``.
266266
result = T(1.0) / cos(z)
267267

268-
proc arcsec*[T](z: Complex[T]): Complex[T] =
268+
func arcsec*[T](z: Complex[T]): Complex[T] =
269269
## Returns the inverse secant of ``z``.
270270
result = -im(T) * ln(im(T) * sqrt(1.0 - 1.0/(z*z)) + T(1.0)/z)
271271

272-
proc csc*[T](z: Complex[T]): Complex[T] =
272+
func csc*[T](z: Complex[T]): Complex[T] =
273273
## Returns the cosecant of ``z``.
274274
result = T(1.0) / sin(z)
275275

276-
proc arccsc*[T](z: Complex[T]): Complex[T] =
276+
func arccsc*[T](z: Complex[T]): Complex[T] =
277277
## Returns the inverse cosecant of ``z``.
278278
result = -im(T) * ln(sqrt(T(1.0) - T(1.0)/(z*z)) + im(T)/z)
279279

280-
proc sinh*[T](z: Complex[T]): Complex[T] =
280+
func sinh*[T](z: Complex[T]): Complex[T] =
281281
## Returns the hyperbolic sine of ``z``.
282282
result = T(0.5) * (exp(z) - exp(-z))
283283

284-
proc arcsinh*[T](z: Complex[T]): Complex[T] =
284+
func arcsinh*[T](z: Complex[T]): Complex[T] =
285285
## Returns the inverse hyperbolic sine of ``z``.
286286
result = ln(z + sqrt(z*z + 1.0))
287287

288-
proc cosh*[T](z: Complex[T]): Complex[T] =
288+
func cosh*[T](z: Complex[T]): Complex[T] =
289289
## Returns the hyperbolic cosine of ``z``.
290290
result = T(0.5) * (exp(z) + exp(-z))
291291

292-
proc arccosh*[T](z: Complex[T]): Complex[T] =
292+
func arccosh*[T](z: Complex[T]): Complex[T] =
293293
## Returns the inverse hyperbolic cosine of ``z``.
294294
result = ln(z + sqrt(z*z - T(1.0)))
295295

296-
proc tanh*[T](z: Complex[T]): Complex[T] =
296+
func tanh*[T](z: Complex[T]): Complex[T] =
297297
## Returns the hyperbolic tangent of ``z``.
298298
result = sinh(z) / cosh(z)
299299

300-
proc arctanh*[T](z: Complex[T]): Complex[T] =
300+
func arctanh*[T](z: Complex[T]): Complex[T] =
301301
## Returns the inverse hyperbolic tangent of ``z``.
302302
result = T(0.5) * (ln((T(1.0)+z) / (T(1.0)-z)))
303303

304-
proc sech*[T](z: Complex[T]): Complex[T] =
304+
func sech*[T](z: Complex[T]): Complex[T] =
305305
## Returns the hyperbolic secant of ``z``.
306306
result = T(2.0) / (exp(z) + exp(-z))
307307

308-
proc arcsech*[T](z: Complex[T]): Complex[T] =
308+
func arcsech*[T](z: Complex[T]): Complex[T] =
309309
## Returns the inverse hyperbolic secant of ``z``.
310310
result = ln(1.0/z + sqrt(T(1.0)/z+T(1.0)) * sqrt(T(1.0)/z-T(1.0)))
311311

312-
proc csch*[T](z: Complex[T]): Complex[T] =
312+
func csch*[T](z: Complex[T]): Complex[T] =
313313
## Returns the hyperbolic cosecant of ``z``.
314314
result = T(2.0) / (exp(z) - exp(-z))
315315

316-
proc arccsch*[T](z: Complex[T]): Complex[T] =
316+
func arccsch*[T](z: Complex[T]): Complex[T] =
317317
## Returns the inverse hyperbolic cosecant of ``z``.
318318
result = ln(T(1.0)/z + sqrt(T(1.0)/(z*z) + T(1.0)))
319319

320-
proc coth*[T](z: Complex[T]): Complex[T] =
320+
func coth*[T](z: Complex[T]): Complex[T] =
321321
## Returns the hyperbolic cotangent of ``z``.
322322
result = cosh(z) / sinh(z)
323323

324-
proc arccoth*[T](z: Complex[T]): Complex[T] =
324+
func arccoth*[T](z: Complex[T]): Complex[T] =
325325
## Returns the inverse hyperbolic cotangent of ``z``.
326326
result = T(0.5) * (ln(T(1.0) + T(1.0)/z) - ln(T(1.0) - T(1.0)/z))
327327

328-
proc phase*[T](z: Complex[T]): T =
328+
func phase*[T](z: Complex[T]): T =
329329
## Returns the phase of ``z``.
330330
arctan2(z.im, z.re)
331331

332-
proc polar*[T](z: Complex[T]): tuple[r, phi: T] =
332+
func polar*[T](z: Complex[T]): tuple[r, phi: T] =
333333
## Returns ``z`` in polar coordinates.
334334
(r: abs(z), phi: phase(z))
335335

336-
proc rect*[T](r, phi: T): Complex[T] =
336+
func rect*[T](r, phi: T): Complex[T] =
337337
## Returns the complex number with polar coordinates ``r`` and ``phi``.
338338
##
339339
## | ``result.re = r * cos(phi)``
340340
## | ``result.im = r * sin(phi)``
341341
complex(r * cos(phi), r * sin(phi))
342342

343343

344-
proc `$`*(z: Complex): string =
344+
func `$`*(z: Complex): string =
345345
## Returns ``z``'s string representation as ``"(re, im)"``.
346346
result = "(" & $z.re & ", " & $z.im & ")"
347347

tests/effects/tstrict_funcs.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import tables, streams, parsecsv
66
# We import the below modules to check that they compile with `strictFuncs`.
77
# They are otherwise unused in this file.
88
import
9+
complex,
910
httpcore,
1011
math,
1112
nre,

0 commit comments

Comments
 (0)