/* * complex.c implements some complex arithmetic * */ #include #include #include "complex.h" struct cpoint cplus(struct cpoint z1, struct cpoint z2) { struct cpoint sum = z1; sum.x += z2.x; sum.y += z2.y; return sum; } struct cpoint ctimes(struct cpoint z1, struct cpoint z2) { struct cpoint product = z1; product.x *= z2.x; product.x -= z1.y * z2.y; product.y = z1.x * z2.y + z1.y * z2.x; return product; } double mag(struct cpoint z) { return sqrt(z.x * z.x + z.y * z.y); } struct cpoint conj(struct cpoint z) { z.y *= -1; return z; } struct cpoint cdiv(struct cpoint z1, struct cpoint z2) { struct cpoint quotient = z1; double magsquared = z2.x * z2.x + z2.y * z2.y; assert(magsquared != 0); /* or crash informatively */ quotient = ctimes(z1, conj(z2)); quotient.x /= magsquared; quotient.y /= magsquared; return quotient; }