Function for Multiplying Complex Numbers

I am using THREE.Vector2 to represent a complex number and am trying to create a simple routine to multiply two complex numbers together. I have created a function that appears to work, but there must be something that is not so (ahem) complex!

Here is the place my program where I set the variables and call the function:

	let c0 = new THREE.Vector2();
	let c1 = new THREE.Vector2();
	let c = new THREE.Vector2();
	c = mulComplex(c0,c1);

Here is the function:

function mulComplex(c0,c1) {
	let c0r = c0.getComponent(0);
	let c0i = c0.getComponent(1);
	let c1r = c1.getComponent(0);
	let c1i = c1.getComponent(1);
	let cr = c0r * c1r - c0i * c1i;
	let ci = c0r * c1i + c0i * c1r;
	let c = new THREE.Vector2(cr,ci);	
	return c;
}

This seems rather messy. I was hoping for something simpler like:

function mulComplex(c0,c1) {
	let c = new THREE.Vector2();
	let c.x = c0.x * c1.x - c0.y * c1.y;
	let c.y = c0.x * c1.y + c0.y * c1.x;
	return c;
}

Any suggestions?

You are almost done.

function mulComplex(c0,c1) {
	let c = new THREE.Vector2();
	c.x = c0.x * c1.x - c0.y * c1.y;
	c.y = c0.x * c1.y + c0.y * c1.x;
	return c;
}
1 Like