Update programming style in extras/complex_numbers.

extras/complex_numbers/*/*.m:
    As above.
This commit is contained in:
Julien Fischer
2022-01-18 16:23:55 +11:00
parent d218e1b573
commit 8c7beeccdc
11 changed files with 134 additions and 120 deletions

View File

@@ -185,4 +185,5 @@ polar_to_complex(Magnitude, Theta) = cmplx(Real, Imag) :-
cis(Theta) = cmplx(cos(Theta), sin(Theta)).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.complex.
%-----------------------------------------------------------------------------%

View File

@@ -2,18 +2,18 @@
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2018 The Mercury team.
% Copyright (C) 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
% File: complex_float.m.
% Main author: fjh.
% Stability: medium.
%
% This module provides binary operators on (complex, float).
%
% See also: complex, float, complex_float.
%
%-----------------------------------------------------------------------------%
:- module complex_numbers.complex_float.
@@ -28,22 +28,22 @@
% Addition.
%
:- func complex + float = complex.
:- mode in + in = uo is det.
:- mode in + in = uo is det.
% Subtraction.
%
:- func complex - float = complex.
:- mode in - in = uo is det.
:- mode in - in = uo is det.
% Multiplication.
%
%
:- func complex * float = complex.
:- mode in * in = uo is det.
:- mode in * in = uo is det.
% Division.
%
:- func complex / float = complex.
:- mode in / in = uo is det.
:- mode in / in = uo is det.
% Exponentiation.
%
@@ -70,4 +70,5 @@ pow(Z0, P) = Z :-
Z = polar_to_complex(L, Th).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.complex_float.
%-----------------------------------------------------------------------------%

View File

@@ -2,18 +2,18 @@
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2018 The Mercury team.
% Copyright (C) 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
% File: complex_imag.m.
% Main author: fjh.
% Stability: medium.
%
% This module provides binary operators on (complex, imag).
%
% See also: complex.m, imag.m, imag_complex.m.
%
%-----------------------------------------------------------------------------%
:- module complex_numbers.complex_imag.
@@ -25,24 +25,24 @@
%-----------------------------------------------------------------------------%
% Addition.
%
%
:- func complex + imag = complex.
:- mode in + in = uo is det.
:- mode in + in = uo is det.
% Subtraction.
%
%
:- func complex - imag = complex.
:- mode in - in = uo is det.
:- mode in - in = uo is det.
% Multiplication.
%
%
:- func complex * imag = complex.
:- mode in * in = uo is det.
:- mode in * in = uo is det.
% Division.
%
:- func complex / imag = complex.
:- mode in / in = uo is det.
:- mode in / in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -64,4 +64,5 @@ cmplx(XR, XI) / im(YI) = cmplx(0.0 + XI / YI, 0.0 - XR / YI).
% Div = (Yr * Yr + Yi * Yi).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.complex_imag.
%-----------------------------------------------------------------------------%

View File

@@ -27,22 +27,22 @@
% Addition.
%
:- func float + complex = complex.
:- mode in + in = uo is det.
:- mode in + in = uo is det.
% Subtraction.
%
:- func float - complex = complex.
:- mode in - in = uo is det.
:- mode in - in = uo is det.
% Multiplication.
%
:- func float * complex = complex.
:- mode in * in = uo is det.
:- mode in * in = uo is det.
% Division.
%
:- func float / complex = complex.
:- mode in / in = uo is det.
:- mode in / in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -68,4 +68,5 @@ XR / cmplx(YR, YI) = cmplx(XR * YR / Div, - XR * YI / Div) :-
% Div = (Yr * Yr + Yi * Yi).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.float_complex.
%-----------------------------------------------------------------------------%

View File

@@ -2,18 +2,18 @@
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2018 The Mercury team.
% Copyright (C) 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
% File: float_imag.m.
% Main author: fjh.
% Stability: medium.
%
% This module provides binary operators on (float, imag).
%
% See also: complex.m, imag.m, float.m, imag_float.m.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -28,24 +28,24 @@
%-----------------------------------------------------------------------------%
% Addition.
%
%
:- func float + imag = complex.
:- mode in + in = uo is det.
:- mode in + in = uo is det.
% Subtraction.
%
:- func float - imag = complex.
:- mode in - in = uo is det.
:- mode in - in = uo is det.
% Multiplication.
%
:- func float * imag = imag.
:- mode in * in = uo is det.
:- mode in * in = uo is det.
% Division.
%
:- func float / imag = imag.
:- mode in / in = uo is det.
:- mode in / in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -58,4 +58,5 @@ XR * im(YI) = im(XR * YI).
XR / im(YI) = im(0.0 - XR / YI).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.float_imag.
%-----------------------------------------------------------------------------%

View File

@@ -2,7 +2,7 @@
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998,2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2015, 2018 The Mercury team.
% Copyright (C) 2015, 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
@@ -93,4 +93,5 @@ im(X) * im(Y) = 0.0 - X * Y.
im(X) / im(Y) = X / Y.
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.imag.
%-----------------------------------------------------------------------------%

View File

@@ -2,7 +2,7 @@
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2015, 2018 The Mercury team.
% Copyright (C) 2015, 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
@@ -28,22 +28,22 @@
% Addition.
%
:- func imag + complex = complex.
:- mode in + in = uo is det.
:- mode in + in = uo is det.
% Subtraction.
%
:- func imag - complex = complex.
:- mode in - in = uo is det.
:- mode in - in = uo is det.
% Multiplication.
%
:- func imag * complex = complex.
:- mode in * in = uo is det.
:- mode in * in = uo is det.
% Division.
%
:- func imag / complex = complex.
:- mode in / in = uo is det.
:- mode in / in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -69,4 +69,5 @@ im(XI) / cmplx(YR, YI) = cmplx((XI * YI) / Div, (XI * YR) / Div) :-
% Div = (Yr * Yr + Yi * Yi).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.imag_complex.
%-----------------------------------------------------------------------------%

View File

@@ -2,7 +2,7 @@
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2015, 2018 The Mercury team.
% Copyright (C) 2015, 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
@@ -30,22 +30,22 @@
% Addition.
%
:- func imag + float = complex.
:- mode in + in = uo is det.
:- mode in + in = uo is det.
% Subtraction.
%
:- func imag - float = complex.
:- mode in - in = uo is det.
:- mode in - in = uo is det.
% Multiplication.
%
:- func imag * float = imag.
:- mode in * in = uo is det.
:- mode in * in = uo is det.
% Division.
%
:- func imag / float = imag.
:- mode in / in = uo is det.
:- mode in / in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -60,4 +60,5 @@ im(XI) * YR = im(XI * YR).
im(XI) / YR = im(XI / YR).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.imag_float.
%-----------------------------------------------------------------------------%

View File

@@ -1,6 +1,6 @@
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2006 The University of Melbourne.
% Copyright (C) 2018 The Mercury team.
% Copyright (C) 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
@@ -17,4 +17,5 @@
:- include_module imag_float.
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.
%-----------------------------------------------------------------------------%

View File

@@ -1,14 +1,14 @@
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
%
% File: fft.m.
% Main author: conway.
% Date: August 1996.
%
% This source file is hereby placed in the public domain.
% -conway (the author).
%
% This module provides a predicate for performing the Fast Fourier Transform
% (fft) on a list of complex numbers. The list must be a power of 2 in length
% (ie 4, 8, 16, ... elements).
@@ -22,7 +22,7 @@
% algorithm for combining the component transforms. This code is not maximally
% efficient, but rather is intended as a clear presentation of the FFT
% algorithm.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -57,18 +57,17 @@ main(!IO) :-
Zero = 0.0 + 0.0 * i,
One = 1.0 + 0.0 * i,
fft([One, Zero, One, Zero], T),
io.write(T, !IO),
io.nl(!IO).
io.write_line(T, !IO).
:- pred fft(list(complex)::in, list(complex)::out) is det.
fft(Ins, Outs) :-
% First put the list into bit-reversed order.
% First put the list into bit-reversed order.
bit_rev(Ins, Shuffle),
list.length(Shuffle, NInt),
int.log2(NInt, R),
N = float(NInt),
% Now recombine the component transforms
% Now recombine the component transforms.
combine(N, 1.0, R, Shuffle, Outs).
:- pred bit_rev(list(T)::in, list(T)::out) is det.
@@ -93,20 +92,20 @@ split([X1, X2|Xs], [X1|Ys], [X2|Zs]) :-
list(complex)::in, list(complex)::out) is det.
combine(N, K, R, Ins, Outs) :-
(
( if
R = 0
->
then
Outs = Ins
;
else
R1 = R - 1,
L = 1 << R1,
% Split the list in half
% Split the list in half.
divide(L, Ins, Fs0, Ss0),
K2 = K * 2.0,
% Now combine the transforms in the respective halves
% Now combine the transforms in the respective halves.
combine(N, K2, R1, Fs0, Fs),
combine(N, K2, R1, Ss0, Ss),
% Now perform the 'butterfly'
% Now perform the 'butterfly'.
xform(Fs, Ss, complex(1.0), w(K, N), Rs0, Rs1),
list.append(Rs0, Rs1, Outs)
).
@@ -128,12 +127,12 @@ xform([Di0 | Dis0], [Dj0 | Djs0], WJN, WKN, [Di | Dis], [Dj | Djs]) :-
:- pred divide(int::in, list(T)::in, list(T)::out, list(T)::out) is det.
divide(R, Ins, Fs, Ss) :-
(
( if
R =< 0
->
then
Fs = [],
Ss = Ins
;
else
(
Ins = [],
error("divide error!")
@@ -150,4 +149,5 @@ w(J, N) = cis(Theta) :-
Theta = J * 2.0 * pi / N.
%-----------------------------------------------------------------------------%
:- end_module fft.
%-----------------------------------------------------------------------------%

View File

@@ -1,3 +1,7 @@
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% A test case for arithmetic on complex, imag, and float.
:- module complex_test.
@@ -23,69 +27,70 @@
%-----------------------------------------------------------------------------%
main -->
print("tests of (complex op complex)"), nl,
{ X = 3.0 + 4.0 * i },
print("X = "), print(X), nl,
print("X + X = "), print(X + X), nl,
print("X - X = "), print(X - X), nl,
print("X * X = "), print(X * X), nl,
print("X / X = "), print(X / X), nl,
{ Y = - 5.0 + 6.0 * i },
print("Y = "), print(Y), nl,
print("Y + Y = "), print(Y + Y), nl,
print("Y - Y = "), print(Y - Y), nl,
print("Y * Y = "), print(Y * Y), nl,
print("Y / Y = "), print(Y / Y), nl,
print("X + Y = "), print(X + Y), nl,
print("X - Y = "), print(X - Y), nl,
print("X * Y = "), print(X * Y), nl,
print("X / Y = "), print(X / Y), nl,
nl,
main(!IO) :-
print_line("tests of (complex op complex)", !IO),
X = 3.0 + 4.0 * i,
print("X = ", !IO), print_line(X, !IO),
print("X + X = ", !IO), print_line(X + X, !IO),
print("X - X = ", !IO), print_line(X - X, !IO),
print("X * X = ", !IO), print_line(X * X, !IO),
print("X / X = ", !IO), print_line(X / X, !IO),
Y = - 5.0 + 6.0 * i,
print("Y = ", !IO), print_line(Y, !IO),
print("Y + Y = ", !IO), print_line(Y + Y, !IO),
print("Y - Y = ", !IO), print_line(Y - Y, !IO),
print("Y * Y = ", !IO), print_line(Y * Y, !IO),
print("Y / Y = ", !IO), print_line(Y / Y, !IO),
print("X + Y = ", !IO), print_line(X + Y, !IO),
print("X - Y = ", !IO), print_line(X - Y, !IO),
print("X * Y = ", !IO), print_line(X * Y, !IO),
print("X / Y = ", !IO), print_line(X / Y, !IO),
nl(!IO),
print("tests of (imag op imag)"), nl,
{ Z = 4.0 * i },
print("Z = "), print(Z), nl,
print("Z + Z = "), print(Z + Z), nl,
print("Z - Z = "), print(Z - Z), nl,
print("Z * Z = "), print(Z * Z), nl,
print("Z / Z = "), print(Z / Z), nl,
nl,
print_line("tests of (imag op imag)", !IO),
Z = 4.0 * i,
print("Z = ", !IO), print_line(Z, !IO),
print("Z + Z = ", !IO), print_line(Z + Z, !IO),
print("Z - Z = ", !IO), print_line(Z - Z, !IO),
print("Z * Z = ", !IO), print_line(Z * Z, !IO),
print("Z / Z = ", !IO), print_line(Z / Z, !IO),
nl(!IO),
print("tests of (float op imag)"), nl,
print("5.0 + Z = "), print(5.0 + Z), nl,
print("5.0 - Z = "), print(5.0 - Z), nl,
print("5.0 * Z = "), print(5.0 * Z), nl,
print("5.0 / Z = "), print(5.0 / Z), nl,
nl,
print_line("tests of (float op imag)", !IO),
print("5.0 + Z = ", !IO), print_line(5.0 + Z, !IO),
print("5.0 - Z = ", !IO), print_line(5.0 - Z, !IO),
print("5.0 * Z = ", !IO), print_line(5.0 * Z, !IO),
print("5.0 / Z = ", !IO), print_line(5.0 / Z, !IO),
nl(!IO),
print("tests of (imag op float)"), nl,
print("Z + 5.0 = "), print(Z + 5.0), nl,
print("Z - 5.0 = "), print(Z - 5.0), nl,
print("Z * 5.0 = "), print(Z * 5.0), nl,
print("Z / 5.0 = "), print(Z / 5.0), nl,
nl,
print_line("tests of (imag op float)", !IO),
print("Z + 5.0 = ", !IO), print_line(Z + 5.0, !IO),
print("Z - 5.0 = ", !IO), print_line(Z - 5.0, !IO),
print("Z * 5.0 = ", !IO), print_line(Z * 5.0, !IO),
print("Z / 5.0 = ", !IO), print_line(Z / 5.0, !IO),
nl(!IO),
print("tests of (complex op imag)"), nl,
print("X + Z = "), print(X + Z), nl,
print("X - Z = "), print(X - Z), nl,
print("X * Z = "), print(X * Z), nl,
print("X / Z = "), print(X / Z), nl,
print("Y + Z = "), print(Y + Z), nl,
print("Y - Z = "), print(Y - Z), nl,
print("Y * Z = "), print(Y * Z), nl,
print("Y / Z = "), print(Y / Z), nl,
nl,
print_line("tests of (complex op imag)", !IO),
print("X + Z = ", !IO), print_line(X + Z, !IO),
print("X - Z = ", !IO), print_line(X - Z, !IO),
print("X * Z = ", !IO), print_line(X * Z, !IO),
print("X / Z = ", !IO), print_line(X / Z, !IO),
print("Y + Z = ", !IO), print_line(Y + Z, !IO),
print("Y - Z = ", !IO), print_line(Y - Z, !IO),
print("Y * Z = ", !IO), print_line(Y * Z, !IO),
print("Y / Z = ", !IO), print_line(Y / Z, !IO),
nl(!IO),
print("tests of (imag op complex)"), nl,
print("Z + X = "), print(Z + X), nl,
print("Z - X = "), print(Z - X), nl,
print("Z * X = "), print(Z * X), nl,
print("Z / X = "), print(Z / X), nl,
print("Z + Y = "), print(Z + Y), nl,
print("Z - Y = "), print(Z - Y), nl,
print("Z * Y = "), print(Z * Y), nl,
print("Z / Y = "), print(Z / Y), nl.
print_line("tests of (imag op complex)", !IO),
print("Z + X = ", !IO), print_line(Z + X, !IO),
print("Z - X = ", !IO), print_line(Z - X, !IO),
print("Z * X = ", !IO), print_line(Z * X, !IO),
print("Z / X = ", !IO), print_line(Z / X, !IO),
print("Z + Y = ", !IO), print_line(Z + Y, !IO),
print("Z - Y = ", !IO), print_line(Z - Y, !IO),
print("Z * Y = ", !IO), print_line(Z * Y, !IO),
print("Z / Y = ", !IO), print_line(Z / Y, !IO).
%-----------------------------------------------------------------------------%
:- end_module complex_test.
%-----------------------------------------------------------------------------%