Anonymous user
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>
)▼
Output:
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276431.html#276431 forum]
<lang AutoHotkey>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>
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 <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:
-1</lang>▼
Here are some arithmetic operations on complex numbers:
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
=={{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.
=={{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>
And, although you did not ask, here are demonstrations of some other common complex number operations
<lang fortran>
=={{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.
Output:
=={{header|IDL}}==
Line 815 ⟶ 808:
<tt>complex</tt> (and <tt>dcomplex</tt> for double-precision) is a built-in data type in IDL:
▲ 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>
=={{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 ){
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
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:
Other than that, the task merely asks for
=={{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
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
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>
and many many more. The documentation states:
Line 1,021 ⟶ 1,004:
=={{header|Pascal}}==
<lang pascal>program showcomplex(output);
type
Line 1,089 ⟶ 1,071:
print(zr); { prints (-9,38) }
writeln
end.</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 ;▼
▲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>
=={{header|Python}}==
Line 1,188 ⟶ 1,167:
=={{header|Slate}}==
<lang slate>[| a b |
a: 1 + 1 i.
b: Pi + 1.2 i.
Line 1,199 ⟶ 1,177:
print: a abs.
print: a negated.
].</lang>
=={{header|Smalltalk}}==
Line 1,238 ⟶ 1,215:
The choice of examples here is {{trans|Common Lisp}}.
<
■ ^2 —1
■ + 1 1 +
Line 1,246 ⟶ 1,223:
■ 1/(2) —1 -
■ real(1 + 2) 1
■ imag(1 + 2) 2</
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).
<
√(2)/2 + √(2)/2*</
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.
|