mirror of https://github.com/golang/go.git
go/types: add testdata/linalg.go2 (not yet type-checked)
Also, added missing copyright notices in a few places. Change-Id: Iaa39c10a1821a189cef604d2442e6f7ed7ac68a8
This commit is contained in:
parent
660f7c38d1
commit
34f753b9d1
|
|
@ -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)))
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue