--[[ Complex Number Polar Coordinates Module H. Conrad Cunningham, Professor Computer and Information Science University of Mississippi The Complex Number set of modules illustrates how we can use data abstraction to design a module that allows multiple representations of the data to be used simultaneously. Developed for CSci 658, Software Language Engineering, Fall 2013 1234567890123456789012345678901234567890123456789012345678901234567890 2013-09-20: Translated SICP Scheme code to Lua draft 2013-09-21: Prototyped complex number modules 2013-09-22: Added require of utilities and representation modules See the "complexRectangular" module for more extensive comments on the overall package. This module represents complex numbers in polar coordinates using Lua record-style table. --]] -- Load complex number utilities module local util = require "complexUtilities" -- local definitions for convenience local square = util.square local show_data = util.show_data -- SICP Section 2.4.1: Representations for Complex Numbers -- Polar representation (character Alyssa in SICP) -- Uses table { mag = magnitude, ang = angle } -- Load representation module local pr = require "complexPolarRep" -- local definitions for convenience local make_from_real_imag = pr.make_from_real_imag local make_from_mag_ang = pr.make_from_mag_ang local real_part = pr.real_part local imag_part = pr.imag_part local magnitude = pr.magnitude local angle = pr.angle local to_string = pr.to_string -- Arithmetic operations local function add_complex(z1,z2) return make_from_real_imag( real_part(z1) + real_part(z2), imag_part(z1) + imag_part(z2) ) end local function sub_complex(z1,z2) return make_from_real_imag( real_part(z1) - real_part(z2), imag_part(z1) - imag_part(z2) ) end local function mul_complex(z1,z2) return make_from_mag_ang( magnitude(z1) * magnitude(z2), angle(z1) + angle(z2) ) end local function div_complex(z1,z2) return make_from_mag_ang( magnitude(z1) / magnitude(z2), angle(z1) - angle(z2) ) 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, add_complex = add_complex, sub_complex = sub_complex, mul_complex = mul_complex, div_complex = div_complex, to_string = to_string }