-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathcomplex_numbers.imag_complex.m
73 lines (58 loc) · 2.44 KB
/
complex_numbers.imag_complex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-1998, 2001, 2004-2006 The University of Melbourne.
% Copyright (C) 2015, 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%-----------------------------------------------------------------------------%
%
% File: imag_complex.m.
% Main author: fjh.
% Stability: medium.
%
% This module provides binary operators on (imag, complex).
%
% See also: complex.m, imag.m, complex_imag.m.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module complex_numbers.imag_complex.
:- interface.
:- import_module complex_numbers.complex.
:- import_module complex_numbers.imag.
%-----------------------------------------------------------------------------%
% Addition.
%
:- func imag + complex = complex.
:- mode in + in = uo is det.
% Subtraction.
%
:- func imag - complex = complex.
:- mode in - in = uo is det.
% Multiplication.
%
:- func imag * complex = complex.
:- mode in * in = uo is det.
% Division.
%
:- func imag / complex = complex.
:- mode in / in = uo is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module float.
%-----------------------------------------------------------------------------%
im(XI) + cmplx(YR, YI) = cmplx(0.0 + YR, XI + YI).
im(XI) - cmplx(YR, YI) = cmplx(0.0 - YR, XI - YI).
im(XI) * cmplx(YR, YI) = cmplx(-XI * YI, XI * YR).
im(XI) / cmplx(YR, YI) = cmplx((XI * YI) / Div, (XI * YR) / Div) :-
Div = (YR * YR + YI * YI).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Division of imag / complex formula obtained by simplifying this one:
% cmplx(Xr, Xi) / cmplx(Yr, Yi) =
% cmplx((Xr * Yr + Xi * Yi) / Div, (Xi * Yr - Xr * Yi) / Div) :-
% Div = (Yr * Yr + Yi * Yi).
%-----------------------------------------------------------------------------%
:- end_module complex_numbers.imag_complex.
%-----------------------------------------------------------------------------%