Skip to content

Commit 51cf93e

Browse files
committed
Test complex slices.
1 parent 7004871 commit 51cf93e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

_examples/slices/slices.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package slices
66

7-
import "fmt"
7+
import (
8+
"fmt"
9+
"math/cmplx"
10+
)
811

912
func IntSum(s []int) int {
1013
sum := 0
@@ -28,6 +31,8 @@ type SliceInt16 []int16
2831
type SliceInt32 []int32
2932
type SliceInt64 []int64
3033

34+
type SliceComplex []complex128
35+
3136
type SliceIface []interface{}
3237

3338
type S struct {
@@ -47,3 +52,11 @@ func PrintSSlice(ss []*S) {
4752
func PrintS(s *S) {
4853
fmt.Printf("%v\n", s.Name)
4954
}
55+
56+
func CmplxSqrt(arr SliceComplex) SliceComplex {
57+
res := make([]complex128, len(arr))
58+
for i, el := range arr {
59+
res[i] = cmplx.Sqrt(el)
60+
}
61+
return res
62+
}

_examples/slices/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# license that can be found in the LICENSE file.
44

55
from __future__ import print_function
6+
import math
7+
import random
68
import slices, go
79

810
a = [1,2,3,4]
@@ -35,4 +37,11 @@
3537
slices.PrintS(ss[0])
3638
slices.PrintS(ss[1])
3739

40+
cmplx = slices.SliceComplex([(random.random() + random.random() * 1j) for _ in range(16)])
41+
sqrts = slices.CmplxSqrt(cmplx)
42+
for root, orig in zip(sqrts, cmplx):
43+
root_squared = root * root
44+
assert math.isclose(root_squared.real, orig.real)
45+
assert math.isclose(root_squared.imag, orig.imag)
46+
3847
print("OK")

0 commit comments

Comments
 (0)