Arithmetic/Complex: Difference between revisions

→‎Pascal: no need to reinvent the wheel and define a custom record type since `complex` is already standardized in ISO 10206, besides there were multiple mistakes; move Free Pascal implementation under dedicated section →‎Free Pascal
(→‎{{header|jq}}: + multiply/0)
(→‎Pascal: no need to reinvent the wheel and define a custom record type since `complex` is already standardized in ISO 10206, besides there were multiple mistakes; move Free Pascal implementation under dedicated section →‎Free Pascal)
Line 1,924:
x* = 1-3j
</pre>
 
=={{header|Free Pascal}}==
FreePascal has a complex units. Example of usage:
<lang Pascal>Program ComplexDemo;
 
uses
ucomplex;
 
var
a, b, absum, abprod, aneg, ainv, acong: complex;
 
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
 
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
</lang>
 
=={{header|Frink}}==
Line 2,286 ⟶ 2,318:
Conjugate:1.0 :+ (-2.0)</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
{{improve|Unicon|This could be better implemented as an object i n Unicon. Note, however, that Unicon doesn't allow for operator overloading at the current time.}}
 
=={{header|Icon}} and {{header|Unicon}}==
Icon doesn't provide native support for complex numbers. Support is included in the IPL.
<lang Icon>procedure main()
Line 3,788 ⟶ 3,819:
 
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<lang pascal>program showcomplex(output);
The simple data type <tt>complex</tt> is part of Extended Pascal, ISO standard 10206.
<lang pascal>program complexDemo(output);
 
const
{ I experienced some hiccups with -1.0 using GPC (GNU Pascal Compiler) }
negativeOne = -1.0;
 
type
line = string(80);
complex = record
re,im: real
end;
 
{ as per task requirements wrap arithmetic operations into separate functions }
var
z1,function z2sum(protected x, zry: complex): complex;
begin
sum := x + y
end;
 
function product(protected x, y: complex): complex;
procedure set(var result: complex; re, im: real);
begin
product := x * y
result.re := re;
end;
result.im := im
end;
 
procedurefunction printnegative(aprotected x: complex): complex;
begin
negative := -x
write('(', a.re , ',', a.im, ')')
end;
 
procedurefunction addinverse(varprotected resultx: complex; a, b): complex);
begin
inverse := x ** negativeOne
result.re := a.re + b.re;
end;
result.im := a.im + b.im;
end;
 
{ only this function is not covered by Extended Pascal, ISO 10206 }
procedure neg(var result: complex; a: complex);
function conjugation(protected x: complex): complex;
begin
begin
result.re := -a.re;
conjugation := cmplx(re(x), im(x) * negativeOne)
result.im := -a.im
end;
 
{ --- test suite ------------------------------------------------------------- }
procedure mult(var result: complex; a, b: complex);
function asString(protected x: complex): line;
begin
const
result.re := a.re*b.re - a.im*b.im;
totalWidth = 5;
result.im := a.re*b.im + a.im*b.re
fractionDigits = 2;
end;
var
 
result: line;
procedure inv(var result: complex; a: complex);
begin
var
writeStr(result, '(', re(x):totalWidth:fractionDigits, ', ',
anorm: real;
im(x):totalWidth:fractionDigits, ')');
begin
asString := result
anorm := a.re*a.re + a.im*a.im;
end;
result.re := a.re/anorm;
result.im := -a.im/anorm
end;
 
{ === MAIN =================================================================== }
var
x: complex;
{ for demonstration purposes: how to initialize complex variables }
y: complex value cmplx(1.0, 4.0);
z: complex value polar(exp(1.0), 3.14159265358979);
begin
set(z1,x := cmplx(-3, 42);
set(z2, 5, 6);
writeLn(asString(x), ' + ', asString(y), ' = ', asString(sum(x, y)));
 
writeLn(asString(x), ' * ', asString(z), ' = ', asString(product(x, z)));
neg(zr, z1);
print(zr); { prints (-3,-4) }
writelnwriteLn;
 
writeLn(' −', asString(z), ' = ', asString(negative(z)));
add(zr, z1, z2);
writeLn(' inverse(', asString(z), ') = ', asString(inverse(z)));
print(zr); { prints (8,10) }
writeLn(' conjugation(', asString(y), ') = ', asString(conjugation(y)));
writeln;
 
inv(zr, z1);
print(zr); { prints (0.12,-0.16) }
writeln;
 
mul(zr, z1, z2);
print(zr); { prints (-9,38) }
writeln
end.</lang>
{{out}}
<pre>(-3.00, 2.00) + ( 1.00, 4.00) = (-2.00, 6.00)
(-3.00, 2.00) * (-2.72, 0.00) = ( 8.15, -5.44)
 
−(-2.72, 0.00) = ( 2.72, -0.00)
FreePascal has a complex units. Example of usage:
inverse((-2.72, 0.00)) = (-0.37, -0.00)
<lang Pascal>Program ComplexDemo;
conjugation(( 1.00, 4.00)) = ( 1.00, -4.00)</pre>
 
The PXSC, Pascal eXtensions for scientific computing, define a standard data type similar to [[#Free Pascal|Free Pascal’s]] <tt>ucomplex</tt> data type.
uses
ucomplex;
 
var
a, b, absum, abprod, aneg, ainv, acong: complex;
 
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
 
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.
</lang>
 
=={{header|Perl}}==
149

edits