From cf17d06fe91f4a274404f037b0bd3f78a94399fd Mon Sep 17 00:00:00 2001 From: Paul Bottin Date: Mon, 6 Apr 2015 14:50:54 +0200 Subject: [PATCH] Update cmplx.js fixed typo (mulInline) fixed norm calculation added converting from/to point objects --- lib/cmplx.js | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/cmplx.js b/lib/cmplx.js index 6d24a8f..17a07ef 100644 --- a/lib/cmplx.js +++ b/lib/cmplx.js @@ -1,7 +1,7 @@ /** * @fileOverview This file provides JavaScript classes related to complex numbers. * @author Paul Bottin - * @version 0.0.3 + * @version 0.0.4 */ var util = require('util'); @@ -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 @@ -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); }; /** @@ -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); }; /** @@ -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()); }; /** @@ -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); }; /** @@ -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 @@ -704,4 +738,3 @@ Arith.Polar = Polar; Polar.Polar = Polar; module.exports = Cmplx; -