Arithmetic/Complex: Difference between revisions

m
(Add Lang example)
(8 intermediate revisions by 5 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,871:
x* = 1.0 - 3.0i
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{require lib_complex}
 
{def z1 {C.new 1 1}}
-> z1 = (1 1)
 
{C.x {z1}} -> 1
{C.y {z1}} -> 1
{C.mod {z1}} -> 1.4142135623730951
{C.arg {z1}} -> 0.7853981633974483 // 45°
{C.conj {z1}} -> (1 -1)
{C.negat {z1}} -> (-1 -1)
{C.invert {z1}} -> (0.5 -0.4999999999999999)
{C.sqrt {z1}} -> (1.0986841134678098 0.45508986056222733)
{C.exp {z1}} -> (1.4686939399158851 2.2873552871788423)
{C.log {z1}} -> (0.3465735902799727 0.7853981633974483)
 
{def z2 {C.new 1.5 1.5}}
-> z2 = (1.5 1.5)
 
{C.add {z1} {z2}} -> (2.5 2.5)
{C.sub {z1} {z2}} -> (-0.5 -0.5)
{C.mul {z1} {z2}} -> (0 3)
{C.div {z1} {z2}} -> (0.6666666666666667 0)
</syntaxhighlight>
 
=={{header|Lang}}==
Line 2,915 ⟶ 2,942:
conj(a) = 1.500-3.000i
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{require lib_complex}
 
{def z1 {C.new 1 1}}
-> z1 = (1 1)
 
{C.x {z1}} -> 1
{C.y {z1}} -> 1
{C.mod {z1}} -> 1.4142135623730951
{C.arg {z1}} -> 0.7853981633974483 // 45°
{C.conj {z1}} -> (1 -1)
{C.negat {z1}} -> (-1 -1)
{C.invert {z1}} -> (0.5 -0.4999999999999999)
{C.sqrt {z1}} -> (1.0986841134678098 0.45508986056222733)
{C.exp {z1}} -> (1.4686939399158851 2.2873552871788423)
{C.log {z1}} -> (0.3465735902799727 0.7853981633974483)
 
{def z2 {C.new 1.5 1.5}}
-> z2 = (1.5 1.5)
 
{C.add {z1} {z2}} -> (2.5 2.5)
{C.sub {z1} {z2}} -> (-0.5 -0.5)
{C.mul {z1} {z2}} -> (0 3)
{C.div {z1} {z2}} -> (0.6666666666666667 0)
</syntaxhighlight>
 
=={{header|LFE}}==
Line 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,169 ⟶ 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,447 ⟶ 5,502:
=={{header|Wren}}==
{{libheader|Wren-complex}}
<syntaxhighlight lang="ecmascriptwren">import "./complex" for Complex
 
var x = Complex.new(1, 3)