Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write the following functions in C 1. double complex_dot(complex, complex); comp

ID: 3753850 • Letter: W

Question

write the following functions in C

1. double complex_dot(complex, complex);

complex_dot() will return the dot product (aka the scalar product or inner product) of the complex arguments. This is the sum of the products of the corresponding terms. E.g. the dot product of (2, 3) and (4, 5) is 23.

2. complex complex_inv(complex);

complex_inv() will return the reciprocal of the argument. I.e., 1 / argument. The complex reciprocal is determined by dividing each of its components by the square of the magnitude of the complex number and negating the imaginary part. The magnitude (aka absolute value or modulus) of a complex number is determined by taking the square root of the sum of the square of each term. E.g. the reciprocal of (1, 2) is (.2, -.4) and the reciprocal of (2, 4) is (.1, -.2)

3. complex complex_mul(complex, complex);

complex_mul() will return the complex product of the two complex arguments. Given two complex numbers: x1 + y1*i and x2 + y2*i, the complex product is given by: (x1*x2 – y1*y2) + (x1*y2 + y1*x2)*i So, for example, multiplying (2, 4) by (3, 5) would result in: (-14, 22).

Explanation / Answer

#include <stdio.h>
#include <complex.h>

double complex_dot(complex c1, complex c2){
    double result = creal(c1) * creal(c2) + cimag(c1) * cimag(c2);
    return result;
}

complex complex_inv(complex c){
    double denominator = creal(c) * creal(c) + cimag(c) * cimag(c);
    double real = creal(c) / denominator;
    double imaginary = cimag(c) / denominator;
    complex result = real - imaginary * I;
    return result;
}

complex complex_mul(complex c1, complex c2){
    double real = creal(c1) * creal(c2) - cimag(c1) * cimag(c2);
    double imaginary = creal(c1) * cimag(c2) + cimag(c1) * creal(c2);
    complex result = real + imaginary * I;
    return result;
}