// 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.") } }
// 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} }
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 }
// 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 }
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) }
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 }
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) }
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 }
// 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 }
//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 }
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 }
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)) }
// 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) }
/* 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) }
func main() { defer fmt.Printf("Hello World!\n\n") var ( a bool MaxInt uint64 = 1<<64 - 1 z complex128 = cmplx.Sqrt(-5 + 12i) ) const f = "\n%T(%v)\n" fmt.Printf(f, a, a) fmt.Printf(f, MaxInt, MaxInt) fmt.Printf(f, z, z) }
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 }
func main() { f := 3.2e5 display(f) x := -7.3 - 8.9i display(x) y := complex64(-18.3 + 8.9i) display(y) z := complex(f, 13.2) display(z) display(real(y)) display(imag(z)) display(cmplx.Conj(z)) display(cmplx.Inf()) display(cmplx.NaN()) display(cmplx.Sqrt(z)) }
func solve(numbers []float64) (complex128, complex128) { var a, b, c complex128 a = complex(numbers[0], 0) b = complex(numbers[1], 0) c = complex(numbers[2], 0) sqrt := (cmplx.Sqrt((b * b) - (4 * a * c))) x1 := (-b + sqrt) / (2 * a) x2 := (-b - sqrt) / (2 * a) return x1, x2 }
func main() { var x complex128 = complex(1, 2) // 1+2i var y complex128 = complex(3, 4) // 3+4i fmt.Println(x * y) // "(-5+10i)" fmt.Println(real(x * y)) // "-5" fmt.Println(imag(x * y)) // "10" fmt.Println(1i * 1i) // "(-1+0i)", i² = -1 x1 := 1 + 2i y1 := 3 + 4i fmt.Println(x1 * y1) // "(-5+10i)" fmt.Println(cmplx.Sqrt(-1)) // "(0+1i)" // This is kind-of strange to me robert := 1 + 10i + 2 + 10i fmt.Println(robert) }
// builtinSqrt implements the sqrt procedure. func builtinSqrt(name string, args []interface{}) (val interface{}, err LispError) { switch num := args[0].(type) { case Integer: in := num.ToInteger() if in < 0 { val = NewComplex(complex(0, float64(isqrt(in)))) } else { val = isqrt(in) } case Float: val = NewFloat(math.Sqrt(num.ToFloat())) case Complex: val = NewComplex(cmplx.Sqrt(num.ToComplex())) case Rational: val = NewFloat(math.Sqrt(num.toFloat())) default: err = NewLispError(EARGUMENT, "sqrt requires a numeric argument") } return }
func main() { //declares by var var c, python, java bool //with initializers var m, n, o = 1, false, "test" //short declarations a, b := 2, "nothing" var ( Boolean = false MaxInt uint64 = 1<<64 - 1 cmplex = cmplx.Sqrt(-5 + 21i) ) fmt.Println(x, y, z, c, python, java) fmt.Println(m, n, o) fmt.Println(a, b) const ( f = "%T(%v)\n" ) fmt.Printf(f, Boolean, Boolean) fmt.Printf(f, MaxInt, MaxInt) fmt.Printf(f, cmplex, cmplex) }
func main() { rand.Seed(42) // Shouldn't this throw an error at compile time? //fmt.Println(rand.Intn(0)) fmt.Println(rand.Intn(1000)) fmt.Printf("Now you have %g problems.\n", math.Nextafter(2, 3)) fmt.Println(math.Pi) fmt.Println(add(123, 456)) a, b := swap("Hello", "world") fmt.Println(a, b) fmt.Println(split(20)) i, j := 1, x+2 var c, python, java = true, false, "no!" fmt.Println(i, j, c, python, java) var ( ToBe bool = false //MaxInt uint64 = -1 // errors for overflowing?? MaxInt uint64 = 1<<64 - 1 // but 1<<64 - 1 doesn't... z complex128 = cmplx.Sqrt(-5 + 12i) ) const f = "%T(%v)\n" fmt.Printf(f, ToBe, ToBe) fmt.Printf("%T(0x%x)\n", MaxInt, MaxInt) fmt.Printf(f, z, z) // This is kind of odd fmt.Println(needInt(Small)) //fmt.Println(needInt(Big)) fmt.Println(needFloat(Small)) fmt.Println(needFloat(Big)) }
func solve(equation Quadratic) Solutions { var solutions Solutions if EqualFloat(equation.a, 0) && EqualFloat(equation.b, 0) { solutions = append(solutions, Solution(cmplx.Inf())) return solutions } if EqualFloat(equation.a, 0) { solutions = append(solutions, Solution(complex(equation.c/equation.b, 0))) return solutions } a := complex(equation.a, 0) b := complex(equation.b, 0) c := complex(equation.c, 0) delta := cmplx.Sqrt(b*b - 4*a*c) x1 := (-b - delta) / 2 / a x2 := (-b + delta) / 2 / a solutions = append(solutions, Solution(x1)) if !EqualComplex(x1, x2) { solutions = append(solutions, Solution(x2)) } return solutions }
import ( "fmt" "github.com/roackb2/hi/utils" "io" "math" "math/cmplx" "math/rand" "net/http" "runtime" "time" ) var ( fb, twitter, yo bool = true, true, false MaxInt uint64 = 1<<64 - 1 z complex128 = cmplx.Sqrt(-5 + 12i) i8 int8 i16 int16 i32 int32 i64 int64 u8 uint8 u16 uint16 u32 uint32 u64 uint64 uptr uintptr bt byte ru rune f32 float32 f64 float64 c64 complex64 = 5 - 2i c128 complex128
"strconv" "sync" "time" ) type ComplexFunc func(complex128) complex128 var Funcs []ComplexFunc = []ComplexFunc{ func(z complex128) complex128 { return z*z - 0.61803398875 }, func(z complex128) complex128 { return z*z + complex(0, 1) }, func(z complex128) complex128 { return z*z + complex(-0.835, -0.2321) }, func(z complex128) complex128 { return z*z + complex(0.45, 0.1428) }, func(z complex128) complex128 { return z*z*z + 0.400 }, func(z complex128) complex128 { return cmplx.Exp(z*z*z) - 0.621 }, func(z complex128) complex128 { return (z*z+z)/cmplx.Log(z) + complex(0.268, 0.060) }, func(z complex128) complex128 { return cmplx.Sqrt(cmplx.Sinh(z*z)) + complex(0.065, 0.122) }, } func RunJulia() { t := time.Now() for n, fn := range Funcs { err := CreatePng("picture-"+strconv.Itoa(n)+".png", fn, 1024) if err != nil { log.Fatal(err) } } fmt.Println("Time passed: ", time.Since(t).Seconds()) } // CreatePng creates a PNG picture file with a Julia image of size n x n.
*/ package main // section for imports import ( "fmt" "math/cmplx" ) // define vars -- note we can make our own section for vars! var ( fa, fb, fc = true, false, true // auto type = bool s, x, y, z = "test", 1, 2, 3 // can be different types xx, yy, zz = 1 + 2i, 2 + 3i, 4 + 5i // complex uses i, not j like Python qq complex128 = cmplx.Sqrt(1 + 1i) ) // print them using %T for type of the value, and %v for value in default format func main() { fmt.Printf("go_vars: Simple types and their values using %%T(%%v)\n") const mypi = 3.1415 const f = "%T(%v)\n" fmt.Printf(f, mypi, mypi) fmt.Printf(f, fa, fa) fmt.Printf(f, s, s) fmt.Printf(f, x, x) fmt.Printf(f, xx, xx) fmt.Printf(f, qq, qq)
func sqrt(z complex128) color.Color { v := cmplx.Sqrt(z) blue := uint8(real(v)*128) + 127 red := uint8(imag(v)*128) + 127 return color.YCbCr{128, blue, red} }
package main import ( "fmt" "math/cmplx" "strconv" ) type Maps struct { m, b float32 } var ( ToBe bool = false MaxInt uint64 = 4 >> 1 z complex128 = cmplx.Sqrt(4) ) func main() { /*q := make(map[string]Maps) fmt.Println(q) q["num"] = Maps{23.12, 43.34} q["count"] = Maps{45.23, 657.34} delete(q, "num") a, b := q["count"] fmt.Println(a, "bb", b) */ const f = "%T(%v)\n"
type ComplexFunc func(complex128) complex128 var Funcs []ComplexFunc = []ComplexFunc{ func(z complex128) complex128 { return z*z - 0.61803398875 }, func(z complex128) complex128 { return z*z + complex(0, 1) }, func(z complex128) complex128 { return z*z + complex(-0.835, -0.2321) }, func(z complex128) complex128 { return z*z + complex(0.45, 0.1428) }, func(z complex128) complex128 { return z*z*z + 0.400 }, func(z complex128) complex128 { return cmplx.Exp(z*z*z) - 0.621 }, func(z complex128) complex128 { return (z*z+z)/cmplx.Log(z) + complex(0.268, 0.060) }, func(z complex128) complex128 { return cmplx.Sqrt(cmplx.Sinh(z*z)) + complex(0.065, 0.122) }, } //This makes the program use all the cores in the processor func init() { numcpu := runtime.NumCPU() runtime.GOMAXPROCS(numcpu) // Try to use all available CPUs. } func main() { wg := new(sync.WaitGroup) before := time.Now() for n, fn := range Funcs { wg.Add(1)
func main() { var z complex128 = cmplx.Sqrt(-5 + 12i) fmt.Println(pow(3, 2, 10)) fmt.Println(pow(3, 3, 20)) fmt.Printf("%T(%v)\n", z, z) }