Golang : Combine slices of complex numbers and operation example
Extension from a previous tutorial on how to perform calculations with complex numbers. For this tutorial, we will learn how to perform simple operation such as changing the real and imaginary parts of a complex number. In particular on how to use the alphabet i
to modify the imaginary value.
We will also learn how to combine two slices in a proper way with the builtin
append function.
Here you go!
package main
import (
"fmt"
)
var a = complex(2, 3)
var b = complex(4, 5)
var c = complex(6, 7)
func main() {
complexSliceA := []complex128{a, b, c}
complexSliceB := []complex128{b, c, a}
fmt.Println("A : ", complexSliceA)
fmt.Println("B : ", complexSliceB)
// add 2 for the real, but not the imaginary part
fmt.Println("Before : ", complexSliceA[0])
fmt.Println("After : ", complexSliceA[0]+2)
// to change imaginary part, include the letter i
// for example, to add 2 ... use 2i
fmt.Println("Before : ", complexSliceA[0])
fmt.Println("After : ", complexSliceA[0]+2i)
// iterate over a complex slice example
for item, data := range complexSliceA {
fmt.Printf("Item %d , complex number %v \n", item, data)
}
// preserve complexSliceA
temp := complexSliceA
// to combine two slices, use for loop and builtin append function
for index, _ := range complexSliceB {
complexSliceA = append(complexSliceA, complexSliceB[index])
}
// restore back
complexSliceC := complexSliceA
complexSliceA = temp
fmt.Println("C = A + B ", complexSliceC)
}
Output:
A : [(2+3i) (4+5i) (6+7i)]
B : [(4+5i) (6+7i) (2+3i)]
Before : (2+3i)
After : (4+3i)
Before : (2+3i)
After : (2+5i)
Item 0 , complex number (2+3i)
Item 1 , complex number (4+5i)
Item 2 , complex number (6+7i)
C = A + B [(2+3i) (4+5i) (6+7i) (4+5i) (6+7i) (2+3i)]
References:
https://www.socketloop.com/tutorials/golang-append-and-add-item-in-slice
https://socketloop.com/tutorials/golang-calculations-using-complex-numbers-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+17.3k Golang : Login and logout a user after password verification and redirect example
+5k Golang : Generate Interleaved 2 inch by 5 inch barcode
+5.3k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+4.7k Swift : Convert (cast) Float to Int or Int32 value
+21.8k Golang : Repeat a character by multiple of x factor
+9.2k Golang : Qt get screen resolution and display on center example
+10.8k Golang : How to pipe input data to executing child process?
+5k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+27.8k Golang : Change a file last modified date and time
+9.3k Golang : Format strings to SEO friendly URL example
+18.2k Unmarshal/Load CSV record into struct in Go
+33.4k Golang : convert(cast) bytes to string