Arithmetic/Complex: Difference between revisions
Content added Content deleted
m (→{{header|J}}: Add lang tags) |
m (Fixed lang tags.) |
||
Line 51: | Line 51: | ||
-- Conjugation |
-- Conjugation |
||
C := Conjugate (C); |
C := Conjugate (C); |
||
end Complex_Operations; |
end Complex_Operations;</lang> |
||
⚫ | |||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<lang algol68>main:( |
|||
FORMAT compl fmt = $g(-7,5)"⊥"g(-7,5)$; |
|||
PROC compl operations = VOID: ( |
|||
LONG COMPL a = 1.0 ⊥ 1.0; |
|||
LONG COMPL b = 3.14159 ⊥ 1.2; |
|||
LONG COMPL c; |
|||
printf(($x"a="f(compl fmt)l$,a)); |
|||
printf(($x"b="f(compl fmt)l$,b)); |
|||
# addition # |
|||
c := a + b; |
|||
printf(($x"a+b="f(compl fmt)l$,c)); |
|||
# multiplication # |
|||
c := a * b; |
|||
printf(($x"a*b="f(compl fmt)l$,c)); |
|||
# inversion # |
|||
c := 1.0 / a; |
|||
printf(($x"1/c="f(compl fmt)l$,c)); |
|||
# negation # |
|||
c := -a; |
|||
printf(($x"-a="f(compl fmt)l$,c)) |
|||
); |
|||
compl operations |
|||
⚫ | |||
⚫ | |||
Output: |
Output: |
||
<lang algol68>a=1.00000⊥1.00000 |
|||
b=3.14159⊥1.20000 |
|||
a+b=4.14159⊥2.20000 |
|||
a*b=1.94159⊥4.34159 |
|||
1/c=0.50000⊥-.50000 |
|||
-a=-1.0000⊥-1.0000</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276431.html#276431 forum] |
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276431.html#276431 forum] |
||
<lang AutoHotkey> |
<lang AutoHotkey>Cset(C,1,1) |
||
Cset(C,1,1) |
|||
MsgBox % Cstr(C) ; 1 + i*1 |
MsgBox % Cstr(C) ; 1 + i*1 |
||
Cneg(C,C) |
Cneg(C,C) |
||
Line 197: | Line 195: | ||
{{works with|C99}} |
{{works with|C99}} |
||
The more recent [[C99]] standard has built-in complex number primitive types, which can be declared with float, double, or long double precision. To use these types and their associated library functions, you must include the <complex.h> header. (Note: this is a ''different'' header than the <complex> templates that are defined by [[C++]].) [http://www.opengroup.org/onlinepubs/009695399/basedefs/complex.h.html] [http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03complex_types.htm] |
The more recent [[C99]] standard has built-in complex number primitive types, which can be declared with float, double, or long double precision. To use these types and their associated library functions, you must include the <complex.h> header. (Note: this is a ''different'' header than the <complex> templates that are defined by [[C++]].) [http://www.opengroup.org/onlinepubs/009695399/basedefs/complex.h.html] [http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.vacpp7a.doc/language/ref/clrc03complex_types.htm] |
||
<lang c> |
<lang c>#include <complex.h> |
||
#include <complex.h> |
|||
void cprint(double complex c) |
void cprint(double complex c) |
||
Line 225: | Line 222: | ||
c = -a; |
c = -a; |
||
printf("\n-a="); cprint(c); printf("\n"); |
printf("\n-a="); cprint(c); printf("\n"); |
||
⚫ | |||
} |
|||
⚫ | |||
{{works with|C89}} |
{{works with|C89}} |
||
Line 280: | Line 276: | ||
printf("\n1/a="); put(inv(a)); |
printf("\n1/a="); put(inv(a)); |
||
printf("\n-a="); put(neg(a)); printf("\n"); |
printf("\n-a="); put(neg(a)); printf("\n"); |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp> |
<lang cpp>#include <iostream> |
||
#include <iostream> |
|||
#include <complex> |
#include <complex> |
||
using std::complex; |
using std::complex; |
||
Line 301: | Line 295: | ||
// negation |
// negation |
||
std::cout << -a << std::endl; |
std::cout << -a << std::endl; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 515: | Line 508: | ||
Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is <tt>#C(<var>real</var> <var>imaginary</var>)</tt>. The components of a complex number may be integers, ratios, or floating-point. Arithmetic operations automatically return complex (or real) numbers when appropriate: |
Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is <tt>#C(<var>real</var> <var>imaginary</var>)</tt>. The components of a complex number may be integers, ratios, or floating-point. Arithmetic operations automatically return complex (or real) numbers when appropriate: |
||
<lang lisp>> (sqrt -1) |
|||
#C(0.0 1.0) |
|||
> (expt #c(0 1) 2) |
|||
⚫ | |||
-1 |
|||
Here are some arithmetic operations on complex numbers: |
Here are some arithmetic operations on complex numbers: |
||
<lang lisp>> (+ #c(0 1) #c(1 0)) |
|||
#C(1 1) |
|||
> (* #c(1 1) 2) |
|||
#C(2 2) |
|||
> (* #c(1 1) #c(0 2)) |
|||
#C(-2 2) |
|||
> (- #c(1 1)) |
|||
#C(-1 -1) |
|||
> (/ #c(0 2)) |
|||
#C(0 -1/2)</lang> |
|||
Complex numbers can be constructed from real and imaginary parts using the <tt>complex</tt> function, and taken apart using the <tt>realpart</tt> and <tt>imagpart</tt> functions. |
Complex numbers can be constructed from real and imaginary parts using the <tt>complex</tt> function, and taken apart using the <tt>realpart</tt> and <tt>imagpart</tt> functions. |
||
<lang lisp>> (complex 64 (/ 3 4)) |
|||
#C(64 3/4) |
|||
> (realpart #c(5 5)) |
|||
5 |
|||
> (imagpart (complex 0 pi)) |
|||
3.141592653589793d0</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 568: | Line 561: | ||
There is no standard syntax or mechanism for complex numbers. The FSL provides several implementations suitable for different uses. This example uses the existing floating point stack, but other libraries define a separate complex stack and/or a fixed-point implementation suitable for microcontrollers and DSPs. |
There is no standard syntax or mechanism for complex numbers. The FSL provides several implementations suitable for different uses. This example uses the existing floating point stack, but other libraries define a separate complex stack and/or a fixed-point implementation suitable for microcontrollers and DSPs. |
||
<lang forth>include complex.seq |
|||
: ZNEGATE ( r i -- -r -i ) fswap fnegate fswap fnegate ; |
|||
zvariable x |
|||
zvariable y |
|||
1e 1e x z! |
|||
pi 1.2e y z! |
|||
x z@ y z@ z+ z. |
|||
x z@ y z@ z* z. |
|||
1+0i x z@ z/ z. |
|||
x z@ znegate z.</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
In ANSI FORTRAN 66 or later, COMPLEX is a built-in data type with full access to intrinsic arithmetic operations. Putting each native operation in a function is horribly inefficient, so I will simply demonstrate the operations. This example shows usage for Fortran 90 or later: |
In ANSI FORTRAN 66 or later, COMPLEX is a built-in data type with full access to intrinsic arithmetic operations. Putting each native operation in a function is horribly inefficient, so I will simply demonstrate the operations. This example shows usage for Fortran 90 or later: |
||
<lang fortran> |
<lang fortran>program cdemo |
||
complex :: a = (5,3), b = (0.5, 6.0) ! complex initializer |
|||
complex :: absum, abprod, aneg, ainv |
|||
absum = a + b |
|||
abprod = a * b |
|||
aneg = -a |
|||
ainv = 1.0 / a |
|||
end program cdemo</lang> |
|||
And, although you did not ask, here are demonstrations of some other common complex number operations |
And, although you did not ask, here are demonstrations of some other common complex number operations |
||
<lang fortran> |
<lang fortran>program cdemo2 |
||
complex :: a = (5,3), b = (0.5, 6) ! complex initializer |
|||
real, parameter :: pi = 3.141592653589793 ! The constant "pi" |
|||
complex, parameter :: i = (0, 1) ! the imaginary unit "i" (sqrt(-1)) |
|||
complex :: abdiff, abquot, abpow, aconj, p2cart, newc |
|||
real :: areal, aimag, anorm, rho = 10, theta = pi / 3.0, x = 2.3, y = 3.0 |
|||
integer, parameter :: n = 50 |
|||
integer :: j |
|||
complex, dimension(0:n-1) :: unit_circle |
|||
abdiff = a - b |
|||
abquot = a / b |
|||
abpow = a ** b |
|||
areal = real(a) ! Real part |
|||
aimag = imag(a) ! Imaginary part |
|||
newc = cmplx(x,y) ! Creating a complex on the fly from two reals intrinsically |
|||
! (initializer only works in declarations) |
|||
newc = x + y*i ! Creating a complex on the fly from two reals arithmetically |
|||
anorm = abs(a) ! Complex norm (or "modulus" or "absolute value") |
|||
! (use CABS before Fortran 90) |
|||
aconj = conjg(a) ! Complex conjugate (same as real(a) - i*imag(a)) |
|||
p2cart = rho * exp(i * theta) ! Euler's polar complex notation to cartesian complex notation |
|||
! conversion (use CEXP before Fortran 90) |
|||
⚫ | |||
! The following creates an array of N evenly spaced points around the complex unit circle |
|||
! useful for FFT calculations, among other things |
|||
unit_circle = exp(2*i*pi/n * (/ (j, j=0, n-1) /) ) |
|||
end program cdemo2</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 789: | Line 782: | ||
have ''Complex Integer'' for the Gaussian Integers, ''Complex Float'', ''Complex Double'', etc. The operations are just the usual overloaded numeric operations. |
have ''Complex Integer'' for the Gaussian Integers, ''Complex Float'', ''Complex Double'', etc. The operations are just the usual overloaded numeric operations. |
||
<lang haskell>import Data.Complex |
|||
main = do |
|||
let a = 1.0 :+ 2.0 -- complex number 1+2i |
|||
let b = fromInteger 4 -- complex number 4+0i |
|||
putStrLn $ "Add: " ++ show (a + b) |
|||
putStrLn $ "Subtract: " ++ show (a - b) |
|||
putStrLn $ "Multiply: " ++ show (a * b) |
|||
putStrLn $ "Divide: " ++ show (a / b) |
|||
putStrLn $ "Negate: " ++ show (-a) |
|||
putStrLn $ "Inverse: " ++ show (recip a)</lang> |
|||
Output: |
Output: |
||
<lang haskell>*Main> main |
|||
Add: 5.0 :+ 2.0 |
|||
Subtract: (-3.0) :+ 2.0 |
|||
Multiply: 4.0 :+ 8.0 |
|||
Divide: 0.25 :+ 0.5 |
|||
Negate: (-1.0) :+ (-2.0) |
|||
Inverse: 0.2 :+ (-0.4)</lang> |
|||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
Line 815: | Line 808: | ||
<tt>complex</tt> (and <tt>dcomplex</tt> for double-precision) is a built-in data type in IDL: |
<tt>complex</tt> (and <tt>dcomplex</tt> for double-precision) is a built-in data type in IDL: |
||
⚫ | |||
<pre> |
|||
⚫ | |||
y=complex(!pi,1.2) |
y=complex(!pi,1.2) |
||
print,x+y |
print,x+y |
||
Line 825: | Line 817: | ||
( -1.00000, -1.00000) |
( -1.00000, -1.00000) |
||
print,1/x |
print,1/x |
||
( 0.500000, -0.500000) |
( 0.500000, -0.500000)</lang> |
||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 839: | Line 830: | ||
0.5j_0.5 |
0.5j_0.5 |
||
-x |
-x |
||
_1j_1 |
_1j_1</lang> |
||
⚫ | |||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 885: | Line 875: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<lang javascript> |
<lang javascript>function Complex ( r, i ){ |
||
function Complex ( r, i ){ |
|||
this.r = r; |
this.r = r; |
||
this.i = i; |
this.i = i; |
||
Line 931: | Line 920: | ||
Complex.prototype.getMod = function ( ){ |
Complex.prototype.getMod = function ( ){ |
||
return Math.sqrt( Math.pow(this.r,2) , Math.pow(this.i,2) ) |
return Math.sqrt( Math.pow(this.r,2) , Math.pow(this.i,2) ) |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Line 939: | Line 926: | ||
Maple has "I" (the square root of -1) built-in. Thus: |
Maple has "I" (the square root of -1) built-in. Thus: |
||
<lang maple>x := 1+I; |
|||
y := Pi+I*1.2;</lang> |
|||
By itself, it will perform mathematical operations symbolically, i.e. it will not try to perform computational evaluation unless specifically asked to do so. Thus: |
By itself, it will perform mathematical operations symbolically, i.e. it will not try to perform computational evaluation unless specifically asked to do so. Thus: |
||
<lang maple>x*y; |
|||
==> (1 + I) (Pi + 1.2 I) |
|||
simplify(x*y); |
|||
==> 1.941592654 + 4.341592654 I</lang> |
|||
Other than that, the task merely asks for |
Other than that, the task merely asks for |
||
<lang maple>x+y; |
|||
x*y; |
|||
-x; |
|||
1/x;</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Mathematica has fully implemented support for complex numbers throughout the software. Addition, subtraction, division, multiplications and powering need no further syntax than for real numbers: |
Mathematica has fully implemented support for complex numbers throughout the software. Addition, subtraction, division, multiplications and powering need no further syntax than for real numbers: |
||
<lang Mathematica> |
<lang Mathematica>x=1+2I |
||
y=3+4I |
|||
y=3+4I |
|||
x+y => 4 + 6 I |
|||
x-y => -2 - 2 I |
|||
y x => -5 + 10 I |
|||
y/x => 11/5 - (2 I)/5 |
|||
x^3 => -11 - 2 I |
|||
y^4 => -527 - 336 I |
|||
x^y => (1 + 2 I)^(3 + 4 I) |
|||
N[x^y] => 0.12901 + 0.0339241 I</lang> |
|||
</lang> |
|||
Powering to a complex power can in general not be written shorter, so Mathematica leaves it unevaluated if the numbers are exact. An approximation can be acquired using the function N. |
Powering to a complex power can in general not be written shorter, so Mathematica leaves it unevaluated if the numbers are exact. An approximation can be acquired using the function N. |
||
However Mathematica goes much further, basically all functions can handle complex numbers to arbitrary precision, including (but not limited to!): |
However Mathematica goes much further, basically all functions can handle complex numbers to arbitrary precision, including (but not limited to!): |
||
<lang Mathematica> |
<lang Mathematica>Exp Log |
||
Exp Log |
|||
Sin Cos Tan Csc Sec Cot |
Sin Cos Tan Csc Sec Cot |
||
ArcSin ArcCos ArcTan ArcCsc ArcSec ArcCot |
ArcSin ArcCos ArcTan ArcCsc ArcSec ArcCot |
||
Line 982: | Line 966: | ||
Haversine InverseHaversine |
Haversine InverseHaversine |
||
Factorial Gamma PolyGamma LogGamma |
Factorial Gamma PolyGamma LogGamma |
||
Erf BarnesG Hyperfactorial Zeta ProductLog RamanujanTauL |
Erf BarnesG Hyperfactorial Zeta ProductLog RamanujanTauL</lang> |
||
</lang> |
|||
and many many more. The documentation states: |
and many many more. The documentation states: |
||
Line 1,021: | Line 1,004: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<lang pascal> |
<lang pascal>program showcomplex(output); |
||
program showcomplex(output); |
|||
type |
type |
||
Line 1,089: | Line 1,071: | ||
print(zr); { prints (-9,38) } |
print(zr); { prints (-9,38) } |
||
writeln |
writeln |
||
end. |
end.</lang> |
||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 1,115: | Line 1,096: | ||
'1 -: 3' is '1 - 3i' in mathematical notation. |
'1 -: 3' is '1 - 3i' in mathematical notation. |
||
⚫ | |||
<pre> |
|||
⚫ | |||
a+b => |
a+b => |
||
a*b => |
a*b => |
||
Line 1,134: | Line 1,114: | ||
a-a => |
a-a => |
||
a/b => |
a/b => |
||
a/a => |
a/a =></lang> |
||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,188: | Line 1,167: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
<lang slate> |
<lang slate>[| a b | |
||
[| a b | |
|||
a: 1 + 1 i. |
a: 1 + 1 i. |
||
b: Pi + 1.2 i. |
b: Pi + 1.2 i. |
||
Line 1,199: | Line 1,177: | ||
print: a abs. |
print: a abs. |
||
print: a negated. |
print: a negated. |
||
]. |
].</lang> |
||
</lang> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 1,238: | Line 1,215: | ||
The choice of examples here is {{trans|Common Lisp}}. |
The choice of examples here is {{trans|Common Lisp}}. |
||
< |
<lang ti89b>■ √(–1) |
||
■ ^2 —1 |
■ ^2 —1 |
||
■ + 1 1 + |
■ + 1 1 + |
||
Line 1,246: | Line 1,223: | ||
■ 1/(2) —1 - |
■ 1/(2) —1 - |
||
■ real(1 + 2) 1 |
■ real(1 + 2) 1 |
||
■ imag(1 + 2) 2</ |
■ imag(1 + 2) 2</lang> |
||
Complex numbers can also be entered and displayed in polar form. (This example shows input in polar form while the complex display mode is rectangular and the angle mode is radians). |
Complex numbers can also be entered and displayed in polar form. (This example shows input in polar form while the complex display mode is rectangular and the angle mode is radians). |
||
< |
<lang ti89b>■ (1∠π/4) |
||
√(2)/2 + √(2)/2*</ |
√(2)/2 + √(2)/2*</lang> |
||
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. |
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. |