diff --git a/src/go/types/testdata/linalg.go2 b/src/go/types/testdata/linalg.go2 new file mode 100644 index 0000000000..2d63dd86f9 --- /dev/null +++ b/src/go/types/testdata/linalg.go2 @@ -0,0 +1,84 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// TODO(gri) add this file to the test suite once it type-checks. + +package linalg + +import "math" + +// Numeric is a contract that matches any numeric type. +// It would likely be in a contracts package in the standard library. +contract Numeric(T) { + T int; T int8; T int16; T int32; T int64 + T uint; T uint8; T uint16; T uint32; T uint64; T uintptr + T float32; T float64 + T complex64; T complex128 +} + +func DotProduct(type T Numeric)(s1, s2 []T) T { + if len(s1) != len(s2) { + panic("DotProduct: slices of unequal length") + } + var r T + for i := range s1 { + r += s1[i] * s2[i] + } + return r +} + +// NumericAbs matches numeric types with an Abs method. +contract NumericAbs(T) { + Numeric(T) + T Abs() T +} + +// AbsDifference computes the absolute value of the difference of +// a and b, where the absolute value is determined by the Abs method. +func AbsDifference(type T NumericAbs)(a, b T) T { + d := a - b + return d.Abs() +} + +// OrderedNumeric matches numeric types that support the < operator. +contract OrderedNumeric(T) { + T int; T int8; T int16; T int32; T int64 + T uint; T uint8; T uint16; T uint32; T uint64; T uintptr + T float32; T float64 +} + +// Complex matches the two complex types, which do not have a < operator. +contract Complex(T) { + T complex64; T complex128 +} + +// OrderedAbs is a helper type that defines an Abs method for +// ordered numeric types. +type OrderedAbs(type T OrderedNumeric) T + +func (a OrderedAbs(T)) Abs() T { + if a < 0 { + return -a + } + return a +} + +// ComplexAbs is a helper type that defines an Abs method for +// complex types. +type ComplexAbs(type T Complex) T + +func (a ComplexAbs(T)) Abs() T { + r := float64(real(a)) + i := float64(imag(a)) + d := math.Sqrt(r * r + i * i) + return T(complex(d, 0)) +} + +func OrderedAbsDifference(type T OrderedNumeric)(a, b T) T { + return T(AbsDifference(OrderedAbs(T)(a), OrderedAbs(T)(b))) +} + +func ComplexAbsDifference(type T Complex)(a, b T) T { + return T(AbsDifference(ComplexAbs(T)(a), ComplexAbs(T)(b))) +} diff --git a/src/go/types/testdata/map.go2 b/src/go/types/testdata/map.go2 index 20bff12b0b..b92cec7dff 100644 --- a/src/go/types/testdata/map.go2 +++ b/src/go/types/testdata/map.go2 @@ -1,3 +1,7 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + // Package orderedmap provides an ordered map, implemented as a binary tree. package orderedmap diff --git a/src/go/types/testdata/map2.go2 b/src/go/types/testdata/map2.go2 index 96981d7c3a..6947ad774d 100644 --- a/src/go/types/testdata/map2.go2 +++ b/src/go/types/testdata/map2.go2 @@ -1,3 +1,7 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + // This file is like map.go2, but instead if importing chans, it contains // the necessary functionality at the end of the file. diff --git a/src/go/types/testdata/slices.go2 b/src/go/types/testdata/slices.go2 index c8f8b9df0c..9cb92f65a7 100644 --- a/src/go/types/testdata/slices.go2 +++ b/src/go/types/testdata/slices.go2 @@ -1,3 +1,7 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + // Package slices implements various slice algorithms. package slices diff --git a/src/go/types/testdata/tmp.go2 b/src/go/types/testdata/tmp.go2 index 07eca63bcf..7e27825eaa 100644 --- a/src/go/types/testdata/tmp.go2 +++ b/src/go/types/testdata/tmp.go2 @@ -1,3 +1,7 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package p type Pair(type K) struct {