本文整理汇总了Golang中math/cmplx.Sqrt函数的典型用法代码示例。如果您正苦于以下问题:Golang Sqrt函数的具体用法?Golang Sqrt怎么用?Golang Sqrt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Sqrt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestQRegBProb
// Test that the correct values are computed for the probability of observing
// a given bit.
func TestQRegBProb(t *testing.T) {
qreg := NewQReg(2)
qreg.amplitudes = []complex128{
cmplx.Sqrt(0.1), // |00>
cmplx.Sqrt(0.2), // |01>
cmplx.Sqrt(0.3), // |10>
cmplx.Sqrt(0.4), // |10>
}
// |00> and |01>
if !verifyProb(qreg.BProb(0)[0], 0.3) {
t.Errorf("Bad probability for |0?>, expected 0.3.")
}
// |10> and |11>
if !verifyProb(qreg.BProb(0)[1], 0.7) {
t.Errorf("Bad probability for |1?>, expected 0.7.")
}
// |00> and |10>
if !verifyProb(qreg.BProb(1)[0], 0.4) {
t.Errorf("Bad probability for |?0>, expected 0.4.")
}
// |01> and |11>
if !verifyProb(qreg.BProb(1)[1], 0.6) {
t.Errorf("Bad probability for |?1>, expected 0.6.")
}
}
示例2: stageMatrix
// calculates stage transformation matrix for a single cone of an instrument's profile
func stageMatrix(radiusIn, radiusOut, radiusCenter, length, radianFrequency float64) [5]complex128 {
r := radiusCenter
w := radianFrequency
l := length
//u := airDynamicViscosity
//lv_inv := (p * c) / u //u / (p * c)
//lt_inv := (p * c * cP) / lm //lm / (p * c * cP)
//y := cP / cV
rv_inv := 1 / (r * math.Sqrt(w*clv_inv))
rt_inv := 1 / (r * math.Sqrt(w*clt_inv))
//zv := complex(0, w * p) * (1 + complex(2 / rv, 0) * (1 - 1i) - complex(0, 3 / (rv * rv)))
zv := complex(0, w*p) * (1 + complex(2*rv_inv, -2*rv_inv) - complex(0, 3*(rv_inv*rv_inv)))
//yt := complex(0, w / (p * c * c)) * (1 + complex(y - 1, 0) * (complex(math.Sqrt2 / rt, 0) * (1 - 1i) + complex(0, 1 / (rt * rt))))
yt := complex(0, w*pcc_inv) * (1 + complex(y-1, 0)*(complex(math.Sqrt2*rt_inv, -math.Sqrt2*rt_inv)+complex(0, rt_inv*rt_inv)))
g := cmplx.Sqrt(zv * yt)
z := cmplx.Sqrt(zv * complex128Inverse(yt))
gl := g * complex(l, 0)
//cosh_gl := cmplx.Cosh(gl)
//sinh_gl := cmplx.Sinh(gl)
cosh_gl, sinh_gl := cmplxCoshSinh(gl)
a11 := cosh_gl
a12 := z * sinh_gl
a21 := complex128Inverse(z) * sinh_gl
a22 := cosh_gl
return [5]complex128{a11, a12, a21, a22, zv}
}
示例3: solveEquation
func solveEquation(a, b, c float64) (eq equation) {
eq.a = a
eq.b = b
eq.c = c
disc := complex(b*b-4*a*c, 0)
eq.root1 = (complex(-b, 0) + cmplx.Sqrt(disc)) / 2 / complex(a, 0)
eq.root2 = (complex(-b, 0) - cmplx.Sqrt(disc)) / 2 / complex(a, 0)
return eq
}
示例4: Sqrt
// Sqrt calculates pixel values for sqrt fractal.
func Sqrt(r, i float64) (blue, red uint8) {
z := complex(r, i)
v := cmplx.Sqrt(z)
blue = uint8(real(v)*128) + 127
red = uint8(imag(v)*128) + 127
return blue, red
}
示例5: main
func main() {
fmt.Println("Hello, Golang!")
fmt.Println("rand number: ", rand.Intn(10))
fmt.Printf("rand number: %g\n", math.Nextafter(2, 3))
fmt.Println(add(2, 3))
fmt.Println(swap("hello", "world"))
fmt.Println(split(111))
// local variable
var i int = 1000
// short declaration
k := 3
s := "hello"
fmt.Println(i, c, python, java, k, s)
const f = "%T(%v)\n"
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 2i)
)
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
fmt.Printf(f, z, z)
fmt.Printf(f, Pi, Pi)
fmt.Printf(f, s, s)
}
示例6: choleskyDecomp
func (a *matrix) choleskyDecomp() *matrix {
l := like(a)
// Cholesky-Banachiewicz algorithm
for r, rxc0 := 0, 0; r < a.stride; r++ {
// calculate elements along row, up to diagonal
x := rxc0
for c, cxc0 := 0, 0; c < r; c++ {
sum := a.ele[x]
for k := 0; k < c; k++ {
sum -= l.ele[rxc0+k] * cmplx.Conj(l.ele[cxc0+k])
}
l.ele[x] = sum / l.ele[cxc0+c]
x++
cxc0 += a.stride
}
// calcualate diagonal element
sum := a.ele[x]
for k := 0; k < r; k++ {
sum -= l.ele[rxc0+k] * cmplx.Conj(l.ele[rxc0+k])
}
l.ele[x] = cmplx.Sqrt(sum)
rxc0 += a.stride
}
return l
}
示例7: main
func main() {
//there are five basic types in golang
//boolean
var boolean bool = true
//string
var stringVariable string = "SomeString"
//integer with sighned and unsighned types
//int defaults to the word size of your operating system
var integerVariable int32 = 444
//there are two types of float values
var floatVariableOne float32 = 44.0
var floatVariableTwo float64 = 55.5555
//the last type is the complex type defined in the math package
var complexVariable complex128 = cmplx.Sqrt(-5)
fmt.Println(boolean, stringVariable, integerVariable, floatVariableOne, floatVariableTwo, complexVariable)
}
示例8: solve
func solve(floats [3]float64) (complex128, complex128) {
a, b, c := complex(floats[0], 0), complex(floats[1], 0), complex(floats[2], 0)
root := cmplx.Sqrt(cmplx.Pow(b, 2) - (4 * a * c))
x1 := (-b + root) / (2 * a)
x2 := (-b - root) / (2 * a)
return x1, x2
}
示例9: Norm
// implementation of Norm method
func (v *vectorComplex) Norm() complex128 {
var norm complex128
var dotProduct complex128
conjVector := MakeNewConjVector(v)
for i := 0; i < v.Dim(); i++ {
dotProduct += v.Get(i) * conjVector.Get(i)
}
norm = cmplx.Sqrt(dotProduct)
return norm
}
示例10: Solve
//Solve returns the roots of a quadratic equation
//with coefficients a,b,c
func Solve(a, b, c complex128) (xpos, xneg complex128) {
negB := -b
twoA := 2 * a
bSquared := b * b
fourAC := 4 * a * c
discrim := bSquared - fourAC
sq := cmplx.Sqrt(complex128(discrim))
xpos = (negB + sq) / twoA
xneg = (negB - sq) / twoA
return
}
示例11: main
func main() {
// 定义整型变量
// 注意:go 是静态类型的语言
var i, j int = 1, 2
var c, python, java = true, false, "no!"
// 不用指定变量类型的定义,系统会根据初始赋值自己确定类型
k := 3 // 等价于 var k int = 3
fmt.Println(i, j, k, c, python, java)
// 结果:1 2 3 true false no!
// 注意:Println不支持,Printf才支持%式的输出
fmt.Printf("Now you have %g problems.", math.Nextafter(2, 3))
fmt.Printf("%t\n", 1 == 2)
fmt.Printf("255的二进制:%b\n", 255)
fmt.Printf("255的八进制:%o\n", 255)
fmt.Printf("浮点数-Pai:%f\n", math.Pi)
// 定义数组
var a [5]int
fmt.Println("array a:", a)
a[1] = 10
a[3] = 30
fmt.Println("assign:", a)
fmt.Println("len:", len(a))
// 常量:可以是字符型, string, boolean, 或者数字
// 常量不能用 := 语法来声明
const World = "世界"
fmt.Println("这是const常量的例子:hello", World)
// 定义复数
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
const f = "%T(%v)\n"
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
fmt.Printf(f, z, z)
// 数字常量
fmt.Println("数字常量例子:")
fmt.Println(needInt(Small)) // 21
// fmt.Println(needInt(Big))
// constant 1267650600228229401496703205376 overflows int
fmt.Println(needFloat(Small)) // 0.2
fmt.Println(needFloat(Big)) // 1.2676506002282295e+29
}
示例12: typesFunc
func typesFunc() {
//Go 的基本类型有Basic types
//bool
//string
//int int8 int16 int32 int64
//uint uint8 uint16 uint32 uint64 uintptr
//byte uint8 的别名
//rune int32 的别名
// 代表一个Unicode码
//float32 float64
//complex64 complex128
var (
boo bool = false
maxIn uint64 = 1<<64 - 1
comp complex128 = cmplx.Sqrt(12 - 33i)
)
fmt.Println(!boo)
fmt.Println(maxIn)
fmt.Println(comp)
//变量默认为零值,数值为 0, bool 为 false, 字符串为 “”
var i int
var b bool
var f float64
var s string
fmt.Println(i, " ", b, " ", f, " ", s)
fmt.Printf("%v %v %v %q \n", i, b, f, s)
//类型转换
//不同类型变量赋值时需要显式转换
var v1, v2 = 1.732, 3
var r float64 = (math.Sqrt(float64(v2)))
fmt.Println(v1, " = ", r, " ? ", v1 == r)
//const 关键字可以声明常量 常量不能用:=来定义
const Haha = "I am angry !"
//Haha = "excited~"
fmt.Println(Haha)
//数值常量
const (
big = 1 << 100
small = big >> 110
)
fmt.Println("small int ", needInt(small))
fmt.Println("big float ", needFloat(big))
fmt.Println("small float ", needFloat(small))
}
示例13: tour14
// Basic types
func tour14() {
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
const f = "%T(%v)\n"
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
fmt.Printf(f, z, z)
}
示例14: page13_BasicTypes
/*
Go's basic types are:
bool
string
int int8 int16 int 32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
float32 float64
complex64 complex128
*/
func page13_BasicTypes() {
// NOTE: grouping can be used in functions as well!
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
comp complex128 = cmplx.Sqrt(-5 + 12i)
)
const f = "%T(%v)\n"
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
fmt.Printf(f, comp, comp)
}
示例15: MeanNormalize
func (this *TrainingSet) MeanNormalize() *TrainingSet {
variance, sustract, mean := this.Variance()
StandardDeviation := variance.Apply(func(a complex128) complex128 { return cmplx.Sqrt(a) })
for i := 1; i <= sustract.GetMRows(); i++ {
Normalize := Matrix.DotDivision(sustract.GetRow(i), StandardDeviation)
sustract.SetRow(i, Normalize)
}
out := MakeTrainingSet(sustract, this.Y)
out.mean = mean
out.standard_deviation = StandardDeviation
return out
}