--[[ Complex Number Rectangular 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 This is a Lua implementation of the Scheme example in Section 2.4 "Multiple Representations for Abstract Data" of SICP: Harold Abelson and Gerald J. Sussman with Julie Sussmann. Structure and Interpretation of Computer Programs, Second Edition, The MIT Press, 1996. Full text online at http://mitpress.mit.edu/sicp/ This package implements implement complex numbers in terms of the four selector functions (real_part, imag_part, magnitude, angle) and two constructor functions (make_from_real_imag, make_from_mag_ang). Whatever concrete representation is used, the constructors and selectors must satisfy the following properties for all complex numbers z: z == make_from_real_imag ( real_part(z), imag_part(z) ) z == make_from_mag_ang ( magnitude(z), angle(z) ) The Lua implementations seek to keep the same function names as in the Scheme examples except: (1) an underscore replaces a dash in a function name, e.g., Scheme "real-part" becomes Lua "real_part". (2) the prefix "is_" replaces the suffice "?" in a query function name, e.g., Scheme "polar?" becomes Lua "is_polar". (3) strings stored in MANIFEST CONSTANTS (just variables that we do not reassign and we give names in all upper case) replace Scheme's quoted symbols (4) the representations of the complex number ordered pairs use Lua record-style tables with two elements. The Scheme "cons", "car", "cdr", and "pair?" calls are replaced by appropriate manipulation of the two-element record-style tables. 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 } -- Load representation module local rr = require "complexRectangularRep" -- local definitions for convenience local make_from_real_imag = rr.make_from_real_imag local make_from_mag_ang = rr.make_from_mag_ang local real_part = rr.real_part local imag_part = rr.imag_part local magnitude = rr.magnitude local angle = rr.angle local to_string = rr.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 }