Arithmetic/Complex: Difference between revisions

m
m (Automated syntax highlighting fixup (second round - minor fixes))
(10 intermediate revisions by 6 users not shown)
Line 1,061:
===.NET Complex class===
{{trans|C#}}
<syntaxhighlight lang="cobolcobolfree"> $SET SOURCEFORMAT "FREE"
$SET ILUSING "System"
$SET ILUSING "System.Numerics"
Line 1,077:
 
===Implementation===
<syntaxhighlight lang="cobolcobolfree"> $SET SOURCEFORMAT "FREE"
class-id Prog.
method-id. Main static.
Line 2,495:
 
=={{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.}}
Icon doesn't provide native support for complex numbers. Support is included in the IPL.
Note: see the [[Arithmetic/Complex#Unicon|Unicon]] section below for a Unicon-specific solution.
<syntaxhighlight lang="icon">procedure main()
 
Line 2,898:
{C.div {z1} {z2}} -> (0.6666666666666667 0)
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
fp.cprint = ($z) -> fn.printf(%.3f%+.3fi%n, fn.creal($z), fn.cimag($z))
 
$a = fn.complex(1.5, 3)
$b = fn.complex(1.5, 1.5)
 
fn.print(a =\s)
fp.cprint($a)
 
fn.print(b =\s)
fp.cprint($b)
 
# Addition
fn.print(a + b =\s)
fp.cprint(fn.cadd($a, $b))
 
# Multiplication
fn.print(a * b =\s)
fp.cprint(fn.cmul($a, $b))
 
# Inversion
fn.print(1/a =\s)
fp.cprint(fn.cdiv(fn.complex(1, 0), $a))
 
# Negation
fn.print(-a =\s)
fp.cprint(fn.cinv($a))
 
# Conjugate
fn.print(conj(a) =\s)
fp.cprint(fn.conj($a))
</syntaxhighlight>
{{out}}
<pre>
a = 1.500+3.000i
b = 1.500+1.500i
a + b = 3.000+4.500i
a * b = -2.250+6.750i
1/a = 0.133-0.267i
-a = -1.500-3.000i
conj(a) = 1.500-3.000i
</pre>
 
=={{header|LFE}}==
Line 4,565 ⟶ 4,609:
</syntaxhighlight>
 
=={{header|RPL}}==
{{in}}
<pre>
(1.5,3) 'Z1' STO
(1.5,1.5) 'Z2' STO
Z1 Z2 +
Z1 Z2 -
Z1 Z2 *
Z1 Z2 /
Z1 NEG
Z1 CONJ
Z1 ABS
Z1 RE
Z1 IM
</pre>
{{out}}
<pre>
(3,4.5)
(0,1.5)
(-2.25,6.75)
(1.5,.5)
(-1.5,-3)
(1.5,-3)
63.4349488229
1.5
3
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">
Line 5,125 ⟶ 5,196:
 
Note that the parentheses around ∠ notation are required. It has a related use in vectors: (1∠π/4) is a complex number, [1,∠π/4] is a vector in two dimensions in polar notation, and [(1∠π/4)] is a complex number in a vector.
 
=={{header|Unicon}}==
Takes advantage of Unicon's operator overloading extension and Unicon's Complex class. Negation is not supported by the Complex class.
 
<syntaxhighlight lang="unicon">import math
 
procedure main()
write("c1: ",(c1 := Complex(1.5,3)).toString())
write("c2: ",(c2 := Complex(1.5,1.5)).toString())
write("+: ",(c1+c2).toString())
write("-: ",(c1-c2).toString())
write("*: ",(c1*c2).toString())
write("/: ",(c1/c2).toString())
write("additive inverse: ",c1.addInverse().toString())
write("multiplicative inverse: ",c1.multInverse().toString())
write("conjugate of (4,-3i): ",Complex(4,-3).conjugate().toString())
end</syntaxhighlight>
 
{{out}}
<pre>c1: (1.5,3i)
c2: (1.5,1.5i)
+: (3.0,4.5i)
-: (0.0,1.5i)
*: (-2.25,6.75i)
/: (1.5,0.5i)
additive inverse: (-1.5,-3i)
multiplicative inverse: (0.1333333333333333,-0.2666666666666667i)
conjugate of (4,-3i): (4,3i)</pre>
 
=={{header|UNIX Shell}}==
Line 5,338 ⟶ 5,437:
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math.complex
fn main() {
a := complex.complex(1, 1)
Line 5,403 ⟶ 5,502:
=={{header|Wren}}==
{{libheader|Wren-complex}}
<syntaxhighlight lang="ecmascriptwren">import "./complex" for Complex
 
var x = Complex.new(1, 3)