Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions lib/cmplx.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @fileOverview This file provides JavaScript classes related to complex numbers.
* @author <a href="mailto:paulbottin+git@gmail.com">Paul Bottin</a>
* @version 0.0.3
* @version 0.0.4
*/

var util = require('util');
Expand Down Expand Up @@ -40,6 +40,16 @@ Cmplx.prototype.toPolar = function () {
return Polar(this.radius, this.angle);
};

/**
* Explicitly convert this complex number into a point object.
* @name Cmplx#toPoint
* @function
* @returns {Object}
*/
Cmplx.prototype.toPoint = function () {
return { x: this.real, y: this.imaginary };
};

/**
* Converts an angle given in degrees into radians.
* @name Cmplx.toRad
Expand Down Expand Up @@ -352,7 +362,8 @@ Arith.conjInline = function (lhs) {
* @returns {Arith}
*/
Arith.norm = function (lhs) {
return Arith.mul(lhs, Arith.conj(lhs));
var r = lhs.radius;
return Arith(lhs.real/r, lhs.imaginary/r);
};

/**
Expand All @@ -362,7 +373,20 @@ Arith.norm = function (lhs) {
* @returns {Arith}
*/
Arith.normInline = function (lhs) {
return Arith.mulInline(lhs, Arith.conj(lhs));
var r = lhs.radius;
lhs.real /= r;
lhs.imaginary /= r;
return lhs;
};

/**
* @name Arith.fromPoint
* @function
* @param {Object} point
* @returns {Arith}
*/
Arith.fromPoint = function (point) {
return Arith(point.x, point.y);
};

/**
Expand Down Expand Up @@ -455,7 +479,7 @@ Polar.prototype.mul = function (rhs) {
* @returns {Polar}
*/
Polar.prototype.mulInline = function (rhs) {
return Polar.mulIinine(this, rhs.toPolar());
return Polar.mulInline(this, rhs.toPolar());
};

/**
Expand Down Expand Up @@ -649,7 +673,7 @@ Polar.conjInline = function (lhs) {
* @returns {Polar}
*/
Polar.norm = function (lhs) {
return Polar.mul(lhs, Polar.conj(lhs));
return Polar(1, lhs.angle);
};

/**
Expand All @@ -659,10 +683,20 @@ Polar.norm = function (lhs) {
* @returns {Polar}
*/
Polar.normInline = function (lhs) {
Polar.mulInline(lhs, Polar.conj(lhs));
lhs.radius = 1;
return lhs;
};

/**
* @name Polar.fromPoint
* @function
* @param {Object} point
* @returns {Polar}
*/
Polar.fromPoint = function (point) {
return Arith(point.x, point.y).toPolar();
};

/**
* @name Cmplx.ZERO
* @field
Expand Down Expand Up @@ -704,4 +738,3 @@ Arith.Polar = Polar;
Polar.Polar = Polar;

module.exports = Cmplx;