Jump to content

Arithmetic/Complex: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|J}}: Add lang tags)
m (Fixed lang tags.)
Line 51:
-- Conjugation
C := Conjugate (C);
end Complex_Operations;</lang>
</lang>
 
=={{header|ALGOL 68}}==
<lang algol68>main:(
FORMAT compl fmt = $g(-7,5)"&perp;"g(-7,5)$;
PROC compl operations = VOID: (
LONG COMPL a = 1.0 &perp; 1.0;
LONG COMPL b = 3.14159 &perp; 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
)</lang>
)
Output:
<lang algol68>a=1.00000&perp;100000⊥1.00000
b=3.14159&perp;114159⊥1.20000
a+b=4.14159&perp;214159⊥2.20000
a*b=1.94159&perp;494159⊥4.34159
1/c=0.50000&perp;50000⊥-.50000
-a=-1.0000&perp;0000⊥-1.0000</lang>
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276431.html#276431 forum]
<lang AutoHotkey>Cset(C,1,1)
Cset(C,1,1)
MsgBox % Cstr(C) ; 1 + i*1
Cneg(C,C)
Line 197 ⟶ 195:
{{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]
<lang c>#include <complex.h>
#include <complex.h>
 
void cprint(double complex c)
Line 225 ⟶ 222:
c = -a;
printf("\n-a="); cprint(c); printf("\n");
}</lang>
}
</lang>
 
{{works with|C89}}
Line 280 ⟶ 276:
printf("\n1/a="); put(inv(a));
printf("\n-a="); put(neg(a)); printf("\n");
}</lang>
}
</lang>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <iostream>
#include <complex>
using std::complex;
Line 301 ⟶ 295:
// negation
std::cout << -a << std::endl;
}</lang>
}
</lang>
 
=={{header|C sharp|C#}}==
Line 515 ⟶ 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:
 
<lang lisp>> (sqrt -1)
#C(0.0 1.0)
 
> (expt #c(0 1) 2)
-1</lang>
-1
 
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.
 
<lang lisp>> (complex 64 (/ 3 4))
#C(64 3/4)
 
> (realpart #c(5 5))
5
 
> (imagpart (complex 0 pi))
3.141592653589793d0</lang>
 
=={{header|D}}==
Line 568 ⟶ 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.
 
<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}}==
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> 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
<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}}==
Line 789 ⟶ 782:
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:
 
<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}}==
Line 815 ⟶ 808:
<tt>complex</tt> (and <tt>dcomplex</tt> for double-precision) is a built-in data type in IDL:
 
<lang idl>x=complex(1,1)
<pre>
x=complex(1,1)
y=complex(!pi,1.2)
print,x+y
Line 825 ⟶ 817:
( -1.00000, -1.00000)
print,1/x
( 0.500000, -0.500000)</lang>
</pre>
 
=={{header|J}}==
Line 839 ⟶ 830:
0.5j_0.5
-x
_1j_1</lang>
</lang>
 
=={{header|Java}}==
Line 885 ⟶ 875:
=={{header|JavaScript}}==
 
<lang javascript>function Complex ( r, i ){
function Complex ( r, i ){
this.r = r;
this.i = i;
Line 931 ⟶ 920:
Complex.prototype.getMod = function ( ){
return Math.sqrt( Math.pow(this.r,2) , Math.pow(this.i,2) )
}</lang>
}
 
</lang>
 
=={{header|Maple}}==
Line 939 ⟶ 926:
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:
 
<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
 
<lang maple>x+y;
x*y;
-x;
1/x;</lang>
 
=={{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:
<lang Mathematica>x=1+2I
xy=13+2I4I
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.
However Mathematica goes much further, basically all functions can handle complex numbers to arbitrary precision, including (but not limited to!):
<lang Mathematica>Exp Log
Exp Log
Sin Cos Tan Csc Sec Cot
ArcSin ArcCos ArcTan ArcCsc ArcSec ArcCot
Line 982 ⟶ 966:
Haversine InverseHaversine
Factorial Gamma PolyGamma LogGamma
Erf BarnesG Hyperfactorial Zeta ProductLog RamanujanTauL</lang>
</lang>
and many many more. The documentation states:
 
Line 1,021 ⟶ 1,004:
 
=={{header|Pascal}}==
<lang pascal>program showcomplex(output);
program showcomplex(output);
 
type
Line 1,089 ⟶ 1,071:
print(zr); { prints (-9,38) }
writeln
end.</lang>
</lang>
 
=={{header|Perl}}==
Line 1,115 ⟶ 1,096:
'1 -: 3' is '1 - 3i' in mathematical notation.
 
<lang pop11>lvars a = 1.0 +: 1.0, b = 2.0 +: 5.0 ;
<pre>
lvars a = 1.0 +: 1.0, b = 2.0 +: 5.0 ;
a+b =>
a*b =>
Line 1,134 ⟶ 1,114:
a-a =>
a/b =>
a/a =></lang>
</pre>
 
=={{header|Python}}==
Line 1,188 ⟶ 1,167:
=={{header|Slate}}==
 
<lang slate>[| a b |
[| a b |
a: 1 + 1 i.
b: Pi + 1.2 i.
Line 1,199 ⟶ 1,177:
print: a abs.
print: a negated.
].</lang>
</lang>
 
=={{header|Smalltalk}}==
Line 1,238 ⟶ 1,215:
The choice of examples here is {{trans|Common Lisp}}.
 
<prelang style="font-family: 'TI Uni';"ti89b>■ √(–1) 
■ ^2 —1
■  + 1 1 + 
Line 1,246 ⟶ 1,223:
■ 1/(2) —1 - 
■ real(1 + 2) 1
■ imag(1 + 2) 2</prelang>
 
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).
 
<prelang style="font-family: 'TI Uni';"ti89b>■ (1∠π/4)
√(2)/2 + √(2)/2*</prelang>
 
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.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.