func accumulateSeqence(bailout int, acc *accumulator) { // Find a sequence. var c complex128 for { c = complex(rand.Float64()*4-2, rand.Float64()*4-2) var z complex128 var i int for i < bailout && cmplx.Abs(z) <= 2 { z = z*z + c i++ } if i == bailout { break } } // Accumulate the sequence. var z complex128 var i int for i < bailout && cmplx.Abs(z) < 2 { z = z*z + c i++ acc.increment(z) } }
func logInterpolate(a complex128, b complex128, proportion float64) complex128 { // TODO - precalc arg/norm outside the loop. if cmplx.Abs(a) < cmplx.Abs(b) { return logInterpolate(b, a, 1-proportion) } z := b / a zArg := cmplx.Phase(z) if zArg > 0 { // aArg -> bArg, or aArg -> bArg + 2PI, whichever is closer if zArg > math.Pi { zArg -= 2 * math.Pi } } else { // aArg -> bArg, or aArg -> bArg - 2PI, whichever is closer if zArg < -math.Pi { zArg += 2 * math.Pi } } zLogAbs := math.Log(cmplx.Abs(z)) cArg, cLogAbs := zArg*proportion, zLogAbs*proportion cAbs := math.Exp(cLogAbs) return a * cmplx.Rect(cAbs, cArg) }
func (this *Matrix) TaxicabNorm() float64 { sum := make(chan complex128, 1) this.sumApplyFunction(0, len(this.A), sum, func(a complex128) float64 { return cmplx.Abs(a) }) v := <-sum return cmplx.Abs(v) }
//Pivot func (this *Matrix) Pivot() (*Matrix, *Matrix) { pivot := this.Copy() if this.m == this.n { p := I(this.m) for j := 1; j <= this.m; j++ { max := cmplx.Abs(pivot.GetValue(j, j)) row := j for i := j; i <= pivot.m; i++ { pvalue := pivot.GetValue(i, j) if cmplx.Abs(pvalue) > max { max = cmplx.Abs(pvalue) row = i } } if j != row { tj := this.GetRow(j) trow := this.GetRow(row) pivot.SetRow(j, trow) pivot.SetRow(row, tj) pj := p.GetRow(j) prow := p.GetRow(row) p.SetRow(j, prow) p.SetRow(row, pj) } } return p, pivot } return nil, nil }
// Solve is Gift wrapping algorithm func Solve(ps []complex128) []complex128 { var chList []complex128 a := nearest(ps) for { chList = append(chList, a) b := ps[0] for _, c := range ps[1:] { if a == b { b = c continue } p1, p2 := b-a, c-a v := product(p2, p1) if v > 0 || (v == 0 && cmplx.Abs(p2) > cmplx.Abs(p1)) { b = c } } a = b if a == chList[0] { break } } return chList }
func agm(a, g complex128) complex128 { for cmplx.Abs(a-g) > cmplx.Abs(a)*ε { a, g = (a+g)*.5, cmplx.Rect(math.Sqrt(cmplx.Abs(a)*cmplx.Abs(g)), (cmplx.Phase(a)+cmplx.Phase(g))*.5) } return a }
func eqEpsRel(a, b complex128, eps float64) bool { if a == b { return true } // cmplx.Abs(a - b) / math.Max(cmplx.Abs(a), cmplx.Abs(b)) <= eps return cmplx.Abs(a-b) <= eps*math.Max(cmplx.Abs(a), cmplx.Abs(b)) }
func (this *Matrix) FrobeniusNorm() float64 { sum := make(chan complex128, 1) this.sumApplyFunction(0, len(this.A), sum, func(a complex128) float64 { return cmplx.Abs(a) * cmplx.Abs(a) }) v := <-sum return math.Sqrt(cmplx.Abs(v)) }
func maxidx(row []complex128) int { idx, max := 0, cmplx.Abs(row[0]) for i, v := range row { vAbs := cmplx.Abs(v) if vAbs > max { idx, max = i, vAbs } } return idx }
// Perform iter iterations using the mandelbrot algorithm, and return // the magnitude of the result // Strictly speaking, this isn't actually the Mandelbrot set, as we // return the size of z after iter iterations, rather than the number // of iteration it takes for z to reach a set value. Nevertheless, it // still produces nice pictures! func invMandelbrot(c complex128, iter int) float64 { z := complex(0, 0) for i := 0; i < iter; i++ { z = z*z + c if cmplx.Abs(z) > 1000 { return 1000 } } return cmplx.Abs(z) }
// Iterate as per Mandelbrot algo and returns the magnitude func iterate(c complex128, iter int) float64 { z := complex(0, 0) for i := 1; i < iter; i++ { z = z*z + c if cmplx.Abs(z) > 2000 { return 2000 } } return cmplx.Abs(z) }
// DistanceLS determine the distance between the point and the line segment func DistanceLS(p, a, b complex128) float64 { ab, ba, pa, pb := a-b, b-a, p-a, p-b if Dot(ba, pa) < 0 { return cmplx.Abs(pa) } if Dot(ab, pb) < 0 { return cmplx.Abs(pb) } v, z := Cross(ba, 0, pa, 0) return math.Sqrt(real(v)*real(v)+imag(v)*imag(v)+z*z) / cmplx.Abs(ba) }
func colorOf(row int, col int) uint8 { c := transform(row, col) var z complex128 = c for i := 0; i < MAX_ITERS || cmplx.Abs(z) > 2; i += 1 { z = z*z + c } if cmplx.Abs(z) < 2 { return uint8(0) } else { return uint8(255) } }
func Cbrt(x complex128) (z complex128) { z = complex128(1) lastZ := complex128(0) delta := cmplx.Abs(z - lastZ) for delta*100000 > 0.00001*100000 { //saving last z lastZ = z //getting new z z = z - (((z * z * z) - x) / (3 * (z * z))) //figuring out the delta delta = cmplx.Abs(z - lastZ) } return }
// well-formed superpositions must add up to 100%: func (p QuantumSuperposition) IsOk() error { var sum float64 for _, v := range p { if _v, ok := v.([]interface{}); ok { sum += cmplx.Abs(_v[1].(complex128)) } else { sum += cmplx.Abs(v.(complex128)) } } if sum != 1 { return errors.New("The QuantumSuperposition is not Well Formed") } return nil }
func DotProduct(A, B *Matrix) float64 { if A.n != B.n || A.m != B.m { return complex(0, 0) } out := NullMatrixP(A.m, A.n) done := make(chan bool) go TwoVariableFuncionApply(0, len(A.A), A, B, out, done, func(a, b complex128) complex128 { return a * b }) <-done sum := make(chan complex128, 1) out.sumApplyFunction(0, len(out.A), sum, func(a complex128) float64 { return cmplx.Abs(a) }) v := <-sum return cmplx.Abs(v) }
// mandelbrotColor computes a Mandelbrot value and then assigns a color from the // color table. func mandelbrotColor(c complex128, zoom int) color.RGBA { // Scale so we can fit the entire set in one tile when zoomed out. c = c*3.5 - complex(2.5, 1.75) z := complex(0, 0) iter := 0 for ; iter < iterations; iter++ { z = z*z + c r, i := real(z), imag(z) absSquared := r*r + i*i if absSquared >= 4 { // This is the "Continuous (smooth) coloring" described in Wikipedia: // http://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring v := float64(iter) - math.Log2(math.Log(cmplx.Abs(z))/math.Log(4)) // We are scaling the value based on the zoom level so things don't get // too busy as we get further in. v = math.Abs(v) * float64(colorDensity) / math.Max(float64(zoom), 1) minValue = math.Min(float64(v), minValue) maxValue = math.Max(float64(v), maxValue) colorIdx := (int(v) + numColors*zoom/len(colorStops)) % numColors return colors[colorIdx] } } return centerColor }
func mandelbrot(out io.Writer) { const ( xmin, ymin, xmax, ymax = -2, -2, +2, +2 width, height = 1024, 1024 ) mandelbrot := func(z complex128) color.Color { const iterations = 200 const contrast = 15 var v complex128 for n := uint8(0); n < iterations; n++ { v = v*v + z if cmplx.Abs(v) > 2 { return color.Gray{255 - contrast*n} } } return color.Black } img := image.NewRGBA(image.Rect(0, 0, width, height)) for py := 0; py < height; py++ { y := float64(py)/height*(ymax-ymin) + ymin for px := 0; px < width; px++ { x := float64(px)/width*(xmax-xmin) + xmin z := complex(x, y) // Image point (px, py) represents complex value z. img.Set(px, py, mandelbrot(z)) } } png.Encode(out, img) // NOTE: ignoring errors }
func main() { x0 := Cbrt(2) x1 := cmplx.Pow(2, 1.0/3) fmt.Println(x0) fmt.Println(x1) fmt.Println(cmplx.Abs(x0 - x1)) }
// CDF computes the value of the cumulative density function at x. func (w Weibull) CDF(x float64) float64 { if x < 0 { return 0 } else { return 1 - cmplx.Abs(cmplx.Exp(w.LogCDF(x))) } }
func mandelbrot(z complex128) color.Color { const iterations = 200 const contrast = 15 var v complex128 for n := uint8(0); n < iterations; n++ { v = v*v + z if cmplx.Abs(v) > 2 { i := 255 - contrast*n switch { case n%8 == 0: return color.NRGBA{0, 0, 0, i} case n%8 == 1: return color.NRGBA{0, 0, i, i} case n%8 == 2: return color.NRGBA{0, i, 0, i} case n%8 == 3: return color.NRGBA{0, i, i, i} case n%8 == 4: return color.NRGBA{i, 0, 0, i} case n%8 == 5: return color.NRGBA{i, 0, i, i} case n%8 == 6: return color.NRGBA{i, i, 0, i} case n%8 == 7: return color.NRGBA{i, i, i, i} } } } return color.Black }
func (p *SingPert) Escape(z complex128) uint16 { i := uint16(0) for current := z; cmplx.Abs(current) < 3; i++ { current = p.Step(current) } return i }
func TestGoertzel(t *testing.T) { samplerate := 1024 blocksize := 1024 freq := 128 samples := make([]float64, blocksize) w := 2 * math.Pi / float64(samplerate) for i := 0; i < blocksize; i++ { samples[i] = math.Sin(float64(i) * float64(freq) * w) } g := NewGoertzel([]uint64{128, 129}, samplerate, blocksize) g.Feed(samples) m := g.Magnitude() if e := math.Pow(float64(blocksize)/2, 2); !approxEqual(m[0], e, 1e-8) { t.Errorf("Goertzel magnitude = %f. Want %f", m[0], e) } if !approxEqual(float64(m[1]), 0.0, 1e-10) { t.Errorf("Foertzel magnitude = %f. Want 0.0", m[1]) } c := g.Complex() if e, m := math.Sqrt(math.Pow(float64(blocksize)/2, 2)), cmplx.Abs(complex128(c[0])); !approxEqual(m, e, 1e-8) { t.Errorf("Goertzel magnitude = %f. Want %f", m, e) } if e, p := -math.Pi/2, cmplx.Phase(complex128(c[0])); !approxEqual(p, e, 1e-12) { t.Errorf("Goertzel phase = %f. Want %f", p, e) } }
func mandelbrotIter(c complex128) float64 { i := 0 for z := c; cmplx.Abs(z) < 2 && i < maxIter; i++ { z = z*z + c } return float64(maxIter-i) / maxIter }
// f(x) = x^4 - 1 // // z' = z - f(z)/f'(z) // = z - (z^4 - 1) / (4 * z^3) // = z - (z - 1/z^3) / 4 func newton(z complex128) color.Color { var quadrant int if real(z) > 0 { if imag(z) < 0 { quadrant = 1 } else { quadrant = 4 } } else { if imag(z) < 0 { quadrant = 2 } else { quadrant = 3 } } const iterations = 37 const contrast = 7 for i := uint8(0); i < iterations; i++ { z -= (z - 1/(z*z*z)) / 4 if cmplx.Abs(z*z*z*z-1) < 1e-6 { t := contrast * i switch quadrant { case 1: return color.RGBA{0xB0 - t, 0x00, 0x00, 0xFF} case 2: return color.RGBA{0xB0 - t, 0xB0 - t, 0x00, 0xFF} case 3: return color.RGBA{0x00, 0x00, 0xB0 - t, 0xFF} case 4: return color.RGBA{0xB0 - t, 0x00, 0xB0 - t, 0xFF} } } } return color.Black }
func mandelbrot(a complex128) float64 { i := 0 for z := a; cmplx.Abs(z) < 2 && i < maxEsc; i++ { z = z*z + a } return float64(maxEsc-i) / maxEsc }
// Compute the probability of observing a basis state. func (qreg *QReg) StateProb(values ...int) float64 { label := qreg.basisStateLabel(values...) // The probability of observing a state is the square of the magnitude // of the complex amplitude. magnitude := cmplx.Abs(qreg.amplitudes[label]) return magnitude * magnitude }
//QR algorithm for EigenValues func (this *Matrix) EigenValues(Tol complex128) *Matrix { Ai := this.Copy() Error := 1.0 Qi, Ri := Ai.QRDec() Ai = Product(Ri, Qi) xi, _ := Ai.GetDiagonal() for Error > cmplx.Abs(Tol) { Qi, Ri := Ai.QRDec() Ai = Product(Ri, Qi) xi1, _ := Ai.GetDiagonal() diff, _ := Sustract(xi, xi1) Error = diff.FrobeniusNorm() xi = xi1 } Eig := NullMatrixP(this.n, 1) for i := 1; i <= this.n; i++ { Eig.SetValue(i, 1, Ai.GetValue(i, i)) } return Eig }
func mandelbrot(z complex128) color.Color { const iterations = 200 const contrast = 5 var v complex128 for n := uint8(0); n < iterations; n++ { v = v*v + z //fmt.Println(cmplx.Abs(v)) if cmplx.Abs(v) > 2 { //fmt.Println(255 - contrast*n) return color.RGBA{255 - uint8(cmplx.Abs(v))*n, 0 + uint8(cmplx.Abs(v))*n, 128 - uint8(cmplx.Abs(v))*n, 128} } } return color.Black }
func CalculateInputImpedance(brass Brass, minHz, maxHz, deltaHz float64) []float64 { simpleBrass := BrassToSimpleBrass(brass) numFreqs := int(math.Floor(float64((maxHz - minHz) / deltaHz))) impedances := make([]float64, numFreqs) /*for i := 0; i < numFreqs; i++ { radianFrequency := (float64(i)*deltaHz + minHz) * 2 * pi inputImpedance := inputImpedanceAt(simpleBrass, radianFrequency) impedances[i] = float64(cmplx.Abs(inputImpedance)) }*/ // calculate all different frequencies in parallel resultChannels := make([]chan float64, numFreqs) for i := 0; i < numFreqs; i++ { i := i resultChannels[i] = make(chan float64, 1) go func() { radianFrequency := (float64(i)*deltaHz + minHz) * 2 * pi inputImpedance := inputImpedanceAt(simpleBrass, radianFrequency) resultChannels[i] <- float64(cmplx.Abs(inputImpedance)) }() } for i := 0; i < numFreqs; i++ { impedances[i] = <-resultChannels[i] } return impedances }