Oct 4 08:44 2013 complexRectangularRep.lua Page 1 --[[ Complex Number Rectangular Representation Module H. Conrad Cunningham, Professor Computer and Information Science University of Mississippi Developed for CSci 658, Software Language Engineering, Fall 2013 1234567890123456789012345678901234567890123456789012345678901234567890 2013-09-22: Separated Rectangular Representation Module This module represents complex numbers with rectangular coordinates stored in record-style tables. --]] -- Load complex number utilities module local util = require "complexUtilities" -- local definitions for convenience local square = util.square local show_data = util.show_data -- SCIP Section 2.4.1: Representations for Complex Numbers -- Rectangular representation (character Ben in SICP) -- Uses table { real = real_part, imag = imag_part } local RECTANGULAR = "Rectangular" local function real_part(z) return z.real end local function imag_part(z) return z.imag end local function magnitude(z) return math.sqrt(square(real_part(z)) + square(imag_part(z))) end local function angle(z) return math.atan2(imag_part(z), real_part(z)) end local function make_from_real_imag(x,y) return { real = x, imag = y } end local function make_from_mag_ang(r,a) return { real = r * math.cos(a), imag = r * math.sin(a) } end -- Added function: Convert representation to string local function to_string(z) return RECTANGULAR .. "(" .. tostring(real_part(z)) .. ", " Oct 4 08:44 2013 complexRectangularRep.lua Page 2 .. tostring(imag_part(z)) .. ")" end -- Module Export return { make_from_real_imag = make_from_real_imag, make_from_mag_ang = make_from_mag_ang, real_part = real_part, imag_part = imag_part, magnitude = magnitude, angle = angle, to_string = to_string }