當前位置: 首頁>>代碼示例>>Golang>>正文


Golang cmplx.Sqrt函數代碼示例

本文整理匯總了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.")
	}
}
開發者ID:hsavit1,項目名稱:quantum-computation,代碼行數:31,代碼來源:qreg_test.go

示例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}
}
開發者ID:acshi,項目名稱:acshi,代碼行數:32,代碼來源:impedanceMath.go

示例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
}
開發者ID:pto,項目名稱:go-book,代碼行數:9,代碼來源:quadratic.go

示例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
}
開發者ID:vespian,項目名稱:go-exercises,代碼行數:8,代碼來源:algos.go

示例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)
}
開發者ID:WuXibin,項目名稱:A-tour-of-Go,代碼行數:30,代碼來源:basic.go

示例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
}
開發者ID:jefferyyuan,項目名稱:RosettaCodeData,代碼行數:25,代碼來源:cholesky-decomposition-2.go

示例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)

}
開發者ID:avinash-pandey,項目名稱:golang-tutorial,代碼行數:25,代碼來源:basicsType.go

示例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
}
開發者ID:calam1,項目名稱:programming_in_go_book,代碼行數:7,代碼來源:quadratic.go

示例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
}
開發者ID:pwplus,項目名稱:GoCalculate,代碼行數:11,代碼來源:vectorComplex.go

示例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
}
開發者ID:asimone0,項目名稱:quadratic,代碼行數:13,代碼來源:quadratic.go

示例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

}
開發者ID:krebons-gavin,項目名稱:Go-Basic,代碼行數:54,代碼來源:002-variables.go

示例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))
}
開發者ID:hyace,項目名稱:LearningGo,代碼行數:53,代碼來源:goguide.go

示例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)
}
開發者ID:siteshen,項目名稱:go-practice,代碼行數:13,代碼來源:tour.go

示例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)
}
開發者ID:xingyuli,項目名稱:godemo,代碼行數:31,代碼來源:go_tour.go

示例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
}
開發者ID:eddytrex,項目名稱:AIgo,代碼行數:15,代碼來源:TrainningSet.go


注:本文中的math/cmplx.Sqrt函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。