edd : dd("complex_number : calculate ( ) " ) .
%% Task: Complex Arithmetic
  Author :  
 
-module(complex_number).
-export([calculate/0, inverse/1]).
 
-record(complex, {real, img}).
 
calculate() ->
    A = #complex{real=1, img=3},
    B = #complex{real=5, img=2},
 
    Sum = add (A, B),
    %print (Sum),
 
    Product = multiply (A, B),
    %print (Product),
 
    Negation = negation (A),
    %print (Negation),
 
    Inversion = inverse (A),
    %print (Inversion),
 
    Conjugate = conjugate (A),
    %print (Conjugate).
    
    {Sum, Product, Negation, Inversion, Conjugate}.
 
add (A, B) ->
    RealPart = A#complex.real + B#complex.real,
    ImgPart = A#complex.img + B#complex.img,
    #complex{real=RealPart, img=ImgPart}.
 
multiply (A, B) ->
    RealPart = (A#complex.real * B#complex.real) - (A#complex.img * B#complex.img),
    ImgPart = (A#complex.real * B#complex.img) + (B#complex.real * A#complex.img),
    #complex{real=RealPart, img=ImgPart}.
 
negation (A) ->
    #complex{real=-A#complex.real, img=-A#complex.img}.
 
inverse (A) ->
    C = conjugate (A),
    %Mod = (A#complex.real * A#complex.real) + (A#complex.img * A#complex.img), %RIGHT
    Mod = (A#complex.real * A#complex.img) + (A#complex.img * A#complex.img), %WRONG
    RealPart = C#complex.real / Mod,
    ImgPart = C#complex.img / Mod,
    #complex{real=RealPart, img=ImgPart}.
 
conjugate (A) ->
    RealPart = A#complex.real,
    ImgPart = -A#complex.img,
    #complex{real=RealPart, img=ImgPart}.
 
print (A) ->
    if A#complex.img < 0 ->
        io:format("Ans = ~p~pi~n", [A#complex.real, A#complex.img]);
       true ->
        io:format("Ans = ~p+~pi~n", [A#complex.real, A#complex.img])
    end. 

 Task: Complex Arithmetic
print (Sum),
print (Product),
print (Negation),
print (Inversion),
print (Conjugate).
Mod = (A#complex.real * A#complex.real) + (A#complex.img * A#complex.img), %RIGHT
WRONG
edd : dd("complex_number : calculate ( ) " ) .
  Author :  
 
-module(complex_number).
-export([calculate/0, inverse/1]).
 
-record(complex, {real, img}).
 
calculate() ->
    A = #complex{real=1, img=3},
    B = #complex{real=5, img=2},
 
    Sum = add (A, B),
 
    Product = multiply (A, B),
 
    Negation = negation (A),
 
    Inversion = inverse (A),
 
    Conjugate = conjugate (A),
    
    {Sum, Product, Negation, Inversion, Conjugate}.
 
add (A, B) ->
    RealPart = A#complex.real + B#complex.real,
    ImgPart = A#complex.img + B#complex.img,
    #complex{real=RealPart, img=ImgPart}.
 
multiply (A, B) ->
    RealPart = (A#complex.real * B#complex.real) - (A#complex.img * B#complex.img),
    ImgPart = (A#complex.real * B#complex.img) + (B#complex.real * A#complex.img),
    #complex{real=RealPart, img=ImgPart}.
 
negation (A) ->
    #complex{real=-A#complex.real, img=-A#complex.img}.
 
inverse (A) ->
    C = conjugate (A),
    RealPart = C#complex.real / Mod,
    ImgPart = C#complex.img / Mod,
    #complex{real=RealPart, img=ImgPart}.
 
conjugate (A) ->
    RealPart = A#complex.real,
    ImgPart = -A#complex.img,
    #complex{real=RealPart, img=ImgPart}.
 
print (A) ->
    if A#complex.img < 0 ->
        io:format("Ans = ~p~pi~n", [A#complex.real, A#complex.img]);
       true ->
        io:format("Ans = ~p+~pi~n", [A#complex.real, A#complex.img])
    end. 
