func Cbrt(x complex128) complex128 { z := 1.0 + 0i for i := 0; i < 50; i++ { z = z - (cmplx.Pow(z, 3)-x)/(3.0*cmplx.Pow(z, 2)) } return z }
func cbrt(x complex128) complex128 { z := x for i := 0; i < 10; i++ { z -= (cmplx.Pow(z, 3) - x) / (3 * cmplx.Pow(z, 2)) } return z }
func Cbrt(x complex128) complex128 { z := complex128(1) for i := 0; i < 10; i++ { z = z - ((cmplx.Pow(z, 3) - x) / (3 * cmplx.Pow(z, 2))) } return z }
func Cbrt(x complex128) complex128 { var z complex128 = 1.0 for i := 0; i < 10; i++ { z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) } return z }
func Cbrt(x complex128) complex128 { z := x for i := 0; i < 10; i += 1 { z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) } return z }
func main() { x := Cbrt(2) fmt.Println(x) fmt.Println(cmplx.Pow(x, 3)) x = Cbrt(complex(2, 2)) fmt.Println(x) fmt.Println(cmplx.Pow(x, 3)) }
/* * 뉴턴의 방법 참고 * https://en.wikipedia.org/wiki/Newton%27s_method * http://ntalbs.github.io/2014/07/25/newtons-method/ * http://nosyu.pe.kr/1169 * f(x) = x^3 - X 에서 시작. * xn+1 = xn - (xn^3 - X) / 3xn^2 * 초기값은 1로 * 실제 2의 3제곱근은 1.259921049894873 * 5의 3제곱근은 1.709975946676697 * */ func Cbrt(x complex128) complex128 { var result complex128 = 1 for i := 0; i < 10; i++ { result = result - ((cmplx.Pow(result, 3) - x) / (3 * cmplx.Pow(result, 2))) } return result }
func Cbrt(x complex128) complex128 { z := complex128(1) fmt.Println(z) for i := 0; i < 28; i++ { z = z - (cmplx.Pow(z, 3)-x)/(complex128(3)*cmplx.Pow(z, 3)) } return z }
func MyCbrt(x complex128) complex128 { z, prev_z := x, x+1 eps := math.Pow(10, -10) for float64(cmplx.Abs(z-prev_z))-eps > 0 { prev_z = z z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 3)) } return z }
func Cbrt(x complex128) complex128 { z := complex128(1) for i := 0; i < 5; i++ { // How do we calculate how many iterations we need for it not to change the output? z -= (cmplx.Pow(z, complex128(3)) - x) / (3 * cmplx.Pow(z, complex128(2))) } return z }
func Cbrt(x complex128) complex128 { z := x for { pz := z z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) if cmplx.Abs(pz-z) < 1e-31 { return pz } } }
func cbrtByNewtonMethod(x complex128) complex128 { z0 := x / 2 for { z1 := z0 - (cmplx.Pow(z0, 3)-x)/(3*cmplx.Pow(z0, 2)) if cmplx.Abs(z1-z0) < 1e-15 { return z1 } z0 = z1 } }
func Cbrt(x complex128) complex128 { y := complex128(0) z := complex128(1) for y != z { y = z z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) } return z }
func Cbrt(x complex128) complex128 { z := complex128(3) for i := 0; i < 100; i++ { temp := z z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) temp = (z - temp) temp = temp * temp } return z }
func Cbrt(x complex128) complex128 { z := complex128(1) for { y := z z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) if cmplx.Abs(y-z) < 0.01 { break } } return z }
func Cbrt(x complex128) complex128 { z := complex128(1) for { z3 := cmplx.Pow(z, 3) if real(cmplx.Pow(z3-x, 2)) < 1e-5 { break } z = z - (z3-x)/(3*cmplx.Pow(z, 2)) } return z }
func Cbrt(x complex128) complex128 { z := complex128(1) for i := 0; i < 10; i++ { znew := z - ((cmplx.Pow(z, 3) - x) / (3 * cmplx.Pow(z, 2))) z = znew fmt.Println("iterartion ", i, " : ", z) } return z }
func Cbrt(x complex128) complex128 { z := x last := complex128(0) var i int for i = 0; i < 10 && last != z; i++ { last = z z = z - ((cmplx.Pow(z, 3) - x) / (3 * cmplx.Pow(z, 2))) //fmt.Printf("last=%v, z=%v\n", last, z) } return z }
func Cbrt(x complex128) complex128 { var res complex128 = 1.0 for delta := 1.0; delta > .000001; { old_res := res res3 := cmplx.Pow(res, 3) res2 := cmplx.Pow(res, 2) res = res - (res3-x)/(3*res2) delta = cmplx.Abs(res - old_res) } return res }
func Cbrt(x complex128) complex128 { z := complex128(1) avg := complex128(0) for { z = z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z, 2)) if cmplx.Abs(z-avg) < 1e-15 { break } avg = z } return z }
func main() { x0 := Cbrt(2) x1 := cmplx.Pow(2, 1.0/3) fmt.Println(x0) fmt.Println(x1) fmt.Println(cmplx.Abs(x0 - x1)) }
func Cbrt(x complex128) complex128 { z := complex128(2) for i := 1; i < 11; i++ { z = z - (cmplx.Pow(z, 3)-x)/(3*(z*z)) } return z }
func (self *ComplexV) Pow(x float64) *ComplexV { r := Zeros(len(*self)) for k, a := range *self { (*r)[k] = cmplx.Pow(a, complex(x, 0.)) } return r }
func init_MIXED_IC() { put(SUM, INTEGER, COMPLEX, c_(c_ir_(c_ir_c_(func(lr float64, rc complex128) (ret complex128) { return complex(lr, 0) + rc })))) put(DIFF, INTEGER, COMPLEX, c_(c_ir_(c_ir_c_(func(lr float64, rc complex128) (ret complex128) { return complex(lr, 0) - rc })))) put(MULT, INTEGER, COMPLEX, c_(c_ir_(c_ir_c_(func(lr float64, rc complex128) (ret complex128) { return complex(lr, 0) * rc })))) put(QUOT, INTEGER, COMPLEX, c_(c_ir_(c_ir_c_(func(lr float64, rc complex128) (ret complex128) { return complex(lr, 0) / rc })))) put(POW, INTEGER, COMPLEX, c_(c_ir_(c_ir_c_(func(lr float64, rc complex128) (ret complex128) { return cmplx.Pow(complex(lr, 0), rc) })))) put(EQ, INTEGER, COMPLEX, b_(b_ir_(b_ir_c_(func(lr float64, rc complex128) bool { return complex(lr, 0) == rc })))) put(NEQ, INTEGER, COMPLEX, b_(b_ir_(b_ir_c_(func(lr float64, rc complex128) bool { return complex(lr, 0) != rc })))) }
func init_MIXED_CI() { put(SUM, COMPLEX, INTEGER, c_(c_c_(c_c_ir_(func(lc complex128, rr float64) (ret complex128) { return lc + complex(rr, 0) })))) put(DIFF, COMPLEX, INTEGER, c_(c_c_(c_c_ir_(func(lc complex128, rr float64) (ret complex128) { return lc - complex(rr, 0) })))) put(MULT, COMPLEX, INTEGER, c_(c_c_(c_c_ir_(func(lc complex128, rr float64) (ret complex128) { return lc * complex(rr, 0) })))) put(QUOT, COMPLEX, INTEGER, c_(c_c_(c_c_ir_(func(lc complex128, rr float64) (ret complex128) { return lc / complex(rr, 0) })))) put(POW, COMPLEX, INTEGER, c_(c_c_(c_c_ir_(func(lc complex128, rr float64) (ret complex128) { return cmplx.Pow(lc, complex(rr, 0)) })))) put(EQ, COMPLEX, INTEGER, b_(b_c_(b_c_ir_(func(lc complex128, rr float64) bool { return complex(rr, 0) == lc })))) put(NEQ, COMPLEX, INTEGER, b_(b_c_(b_c_ir_(func(lc complex128, rr float64) bool { return complex(rr, 0) == lc })))) }
func main() { cr1 := cmplx.Pow(2.0+3i, 1.0/3.0) cr2 := Cbrt(2 + 3i) fmt.Println("cmplx.Pow: ", cr1) fmt.Println("Cbrt: ", cr2) fmt.Println("Diff: ", cr2-cr1) }
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 }
// #48 Advanced Exercise: Complex cube roots func TestCbrt(t *testing.T) { for i := 1; i < 10; i++ { ret, ans := Cbrt(complex(float64(i), 0)), cmplx.Pow(complex(float64(i), 0), 1.0/3.0) if cmplx.Abs(ret-ans) > 0.001 { t.Errorf("Cbrt(%d) = %f, want %f.", i, ret, ans) } } }
// FreqZ return frequency response given filter coefficients. // The name of this function is followed by Matlab. func FreqZ(b, a []float64, N int) []complex128 { H := make([]complex128, N) for n := 0; n < N; n++ { z := cmplx.Exp(complex(0.0, -2.0*math.Pi*float64(n)/float64(N))) numerator, denominator := complex(0, 0), complex(0, 0) for i := range b { numerator += complex(b[len(b)-1-i], 0.0) * cmplx.Pow(z, complex(float64(i), 0)) } for i := range a { denominator += complex(a[len(a)-1-i], 0.0) * cmplx.Pow(z, complex(float64(i), 0)) } H[n] = numerator / denominator } return H }
func toInfinity(cNum complex128, iterations int) bool { z := cNum for j := 0; j < iterations; j++ { z = cmplx.Pow(z, 2.0) + cNum if cmplx.Abs(z) > 4 { return true } } return false }