Pat
Pat

Reputation: 325

Multiplication of complex vectors in Swift

I have two complex vectors:

a = [(0.5 + 2.0i), (-1.0 - 3.0i), (-3.0 + 1.5i)]

b = [(-0.5 - 2.2i), (1.3 + 2.0i), (3.0 - 2.5i)]

In Matlab, c = a.*b

That is, element by element multiplication of the two complex vectors gives me:

c = [(4.15 - 2.1i), (4.7 - 5.9i), (-5.25 + 12.0i)]

Generally, in Swift, I represent real and imaginary parts of the complex vectors in separate arrays.

So,

let aReal = [0.5, -1.0, -3.0]

let bReal = [-0.5, 1.3, 3.0]

let aImag = [2.0, -3.0, 1.5]

let bImag = [-2.2, 2.0, -2.5]

Based on the above multiplication, in Swift, I am looking to get:

// cReal = [4.15, 4.7, -5.25]

// cImag = [-2.1, -5.9, 12.0]

Upvotes: 1

Views: 577

Answers (1)

Rob
Rob

Reputation: 437472

It depends upon what you're doing with these, but I'd define a Complex type with a * operator. For example in Swift 3:

struct Complex<T: FloatingPoint> {
    let real: T
    let imaginary: T

    static func +(lhs: Complex<T>, rhs: Complex<T>) -> Complex<T> {
        return Complex(real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary)
    }

    static func -(lhs: Complex<T>, rhs: Complex<T>) -> Complex<T> {
        return Complex(real: lhs.real - rhs.real, imaginary: lhs.imaginary - rhs.imaginary)
    }

    static func *(lhs: Complex<T>, rhs: Complex<T>) -> Complex<T> {
        return Complex(real: lhs.real * rhs.real - lhs.imaginary * rhs.imaginary,
                       imaginary: lhs.imaginary * rhs.real + lhs.real * rhs.imaginary)
    }
}

// you can print it any way you want, but I'd probably do:

extension Complex: CustomStringConvertible {
    var description: String {
        switch (real, imaginary) {
        case (_, 0):
            return "\(real)"
        case (0, _):
            return "\(imaginary)i"
        case (_, let b) where b < 0:
            return "\(real) - \(abs(imaginary))i"
        default:
            return "\(real) + \(imaginary)i"
        }
    }
}

You can then use zip and map to take two arrays and perform some calculation taking the respective values from the two arrays:

let a = [Complex<Double>(real: 0.5, imaginary: 2.0), Complex<Double>(real: -1.0, imaginary: -3.0), Complex<Double>(real: -3.0, imaginary: 1.5)]
let b = [Complex<Double>(real: -0.5, imaginary: -2.2), Complex<Double>(real: 1.3, imaginary: 2.0), Complex<Double>(real: 3.0, imaginary: -2.5)]

let c = zip(a, b).map { $0 * $1 }

print(c)

And that would say:

[4.15 - 2.1i, 4.7 - 5.9i, -5.25 + 12.0i]

Or, if you really have your separate arrays, convert them to [Complex] first:

let aReal = [0.5, -1.0, -3.0]
let aImag = [2.0, -3.0, 1.5]

let bReal = [-0.5, 1.3, 3.0]
let bImag = [-2.2, 2.0, -2.5]

let a = zip(aReal, aImag).map { Complex<Double>(real: $0, imaginary: $1) }
let b = zip(bReal, bImag).map { Complex<Double>(real: $0, imaginary: $1) }

let c = zip(a, b).map { $0 * $1 }
print(c)

Upvotes: 2

Related Questions