1

I need to iterate over the complex refractive index = n + ik

I made two floats.Span() filled with evenly spaced numbers, containing every n and k that I need to iterate over. How do I "mix" these two values now so I can make a for loop over every possible combination?

I need something like:

0.1+0.1i, 0.1+0.2i, 0.1+0.2i, (...) 0.2+0.1i, 0.2+0.2i, (...)

And if it is not a slice, how do I iterate over it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Aramus
  • 47
  • 8

1 Answers1

0

I need to iterate over the complex refractive index = n + ik. I made two floats.Span(). how do I iterate over them? How do I "mix" these two values?


package floats

import "gonum.org/v1/gonum/floats"

func Span

func Span(dst []float64, l, u float64) []float64

Span returns a set of N equally spaced points between l and u, where N is equal to the length of the destination. The first element of the destination is l, the final element of the destination is u.

Panics if len(dst) < 2.

Span also returns the mutated slice dst, so that it can be used in range expressions, like:

for i, x := range Span(dst, l, u) { ... }

The floats.Span documentation suggests using for wiith a range clause.


The Go Programming Language Specification

Manipulating complex numbers

Three functions assemble and disassemble complex numbers. The built-in function complex constructs a complex value from a floating-point real and imaginary part, while real and imag extract the real and imaginary parts of a complex value.

complex(realPart, imaginaryPart floatT) complexT
real(complexT) floatT
imag(complexT) floatT

The Go programming language documentation explains complex numbers in Go.


For example,

package main

import (
    "fmt"

    "gonum.org/v1/gonum/floats"
)

func main() {
    loN, hiN := 1.0, 4.0
    dstN := make([]float64, int((hiN-loN)+1))
    loK, hiK := 5.0, 8.0
    dstK := make([]float64, int((hiK-loK)+1))
    for _, n := range floats.Span(dstN, loN, hiN) {
        for _, k := range floats.Span(dstK, loK, hiK) {
            c := complex(n, k)
            fmt.Println(n, k, c)
        }
    }
}

Output:

1 5 (1+5i)
1 6 (1+6i)
1 7 (1+7i)
1 8 (1+8i)
2 5 (2+5i)
2 6 (2+6i)
2 7 (2+7i)
2 8 (2+8i)
3 5 (3+5i)
3 6 (3+6i)
3 7 (3+7i)
3 8 (3+8i)
4 5 (4+5i)
4 6 (4+6i)
4 7 (4+7i)
4 8 (4+8i)
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • But i need to "mix" my two Spans first. I got one for the real part n and one for the imaginary part k. I need to make a new "matrix, slice, array, whatever" with both linked as complex number – Aramus Jul 20 '18 at 11:23