Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/Arithmetic-Complex/Erlang/arithmetic-complex-1.erl
Normal file
58
Task/Arithmetic-Complex/Erlang/arithmetic-complex-1.erl
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
%% Task: Complex Arithmetic
|
||||
%% Author: Abhay Jain
|
||||
|
||||
-module(complex_number).
|
||||
-export([calculate/0]).
|
||||
|
||||
-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).
|
||||
|
||||
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),
|
||||
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.
|
||||
5
Task/Arithmetic-Complex/Erlang/arithmetic-complex-2.erl
Normal file
5
Task/Arithmetic-Complex/Erlang/arithmetic-complex-2.erl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Ans = 6+5i
|
||||
Ans = -1+17i
|
||||
Ans = -1-3i
|
||||
Ans = 0.1-0.3i
|
||||
Ans = 1-3i
|
||||
Loading…
Add table
Add a link
Reference in a new issue