So it sounds like the special case that would be helpful is when the real part of the complex base is 0.
On Thu, Aug 3, 2017, 18:26 Dorival Pedroso <dorival.pedr...@gmail.com> wrote: > Thanks for the advice. > > This is the piece of code where I use that function: > // compute hat(Du) > pf := float64(p) > for j := 0; j < N; j++ { > ikp := ImagPowN(p) * complex(math.Pow(K[j], pf), 0) > DuHat[j] = ikp * S[j] * A[j] > } > > where p is an integer (unbounded), K[j] is a real number from -Inf to +Inf > and S[j] and A[j] are complex (unbounded too) > > By using the ImagPowN(p) function, the code is much faster now... > > Cheers. > D > > > > On Friday, August 4, 2017 at 10:54:14 AM UTC+10, Michael Jones wrote: > >> the complex power function is a difficult (time consuming) general >> computation. >> >> are you saying that you actually have a program that uses boolean >> gaussian integers and needs to do lots of power operations? >> >> if so, i highly recommend that you special case this for your own use. >> >> if this is common then the power code could stop and check for Re(x) in >> {-1,0,1} and Im(x) in {-1,0,1} before undertaking the general computation. >> > On Thu, Aug 3, 2017 at 5:43 PM, Dorival Pedroso <dorival...@gmail.com> >> wrote: >> > Hi, >>> >>> This is an interesting benchmark: >>> >>> Given this function: >>> // ImagPowN computes iⁿ = (√-1)ⁿ >>> // >>> // i¹ = i i² = -1 i³ = -i i⁴ = 1 >>> // i⁵ = i i⁶ = -1 i⁷ = -i i⁸ = 1 >>> // i⁹ = i i¹⁰ = -1 i¹¹ = -i i¹² = 1 >>> // >>> func ImagPowN(n int) complex128 { >>> if n == 0 { >>> return 1 >>> } >>> switch n % 4 { >>> case 1: >>> return 1i >>> case 2: >>> return -1 >>> case 3: >>> return -1i >>> } >>> return 1 >>> } >>> >>> And this benchmark test: >>> var ( >>> imagpownRes complex128 >>> ) >>> >>> func BenchmarkImagPowN(b *testing.B) { >>> var res complex128 >>> for i := 0; i < b.N; i++ { >>> for n := 0; n < 200; n++ { >>> res = ImagPowN(n) >>> } >>> } >>> imagpownRes = res >>> } >>> >>> func BenchmarkImagPowNcmplx(b *testing.B) { >>> var res complex128 >>> for i := 0; i < b.N; i++ { >>> for n := 0; n < 200; n++ { >>> res = cmplx.Pow(1i, complex(float64(n), 0)) >>> } >>> } >>> imagpownRes = res >>> } >>> >>> We get this output ( go test -run=XXX -bench=. ): >>> BenchmarkImagPowN-32 3000000 470 ns/op >>> BenchmarkImagPowNcmplx-32 200000 10050 ns/op >>> >>> A 20x speed up... >>> >>> Cheers. >>> Dorival >>> >> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> >> To unsubscribe from this group and stop receiving emails from it, send an >>> email to golang-nuts...@googlegroups.com. >> >> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> Michael T. Jones >> michae...@gmail.com >> > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.