Arithmetic/Complex: Difference between revisions

Content added Content deleted
(added ruby)
No edit summary
Line 6: Line 6:


=={{header|Ada}}==
=={{header|Ada}}==
<ada>with Ada.Numerics.Generic_Complex_Types;
<lang ada>with Ada.Numerics.Generic_Complex_Types;
with Ada.Text_IO.Complex_IO;
with Ada.Text_IO.Complex_IO;


Line 52: Line 52:
C := Conjugate (C);
C := Conjugate (C);
end Complex_Operations;
end Complex_Operations;
</ada>
</lang>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 92: Line 92:
{{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]
<c>
<lang c>
#include <complex.h>
#include <complex.h>


Line 121: Line 121:
printf("\n-a="); cprint(c); printf("\n");
printf("\n-a="); cprint(c); printf("\n");
}
}
</c>
</lang>


{{works with|C89}}
{{works with|C89}}
User-defined type:
User-defined type:
<c>typedef struct{
<lang c>typedef struct{
double real;
double real;
double imag;
double imag;
Line 176: Line 176:
printf("\n-a="); put(neg(a)); printf("\n");
printf("\n-a="); put(neg(a)); printf("\n");
}
}
</c>
</lang>


=={{header|C++}}==
=={{header|C++}}==
<cpp>
<lang cpp>
#include <iostream>
#include <iostream>
#include <complex>
#include <complex>
Line 197: Line 197:
std::cout << -a << std::endl;
std::cout << -a << std::endl;
}
}
</cpp>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is <code>#C(<var>real</var> <var>imaginary</var>)</code>. 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:


> (sqrt -1)
> (sqrt -1)
Line 226: Line 226:
#C(0 -1/2)
#C(0 -1/2)


Complex numbers can be constructed from real and imaginary parts using the <code>complex</code> function, and taken apart using the <code>realpart</code> and <code>imagpart</code> 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.


> (complex 64 (/ 3 4))
> (complex 64 (/ 3 4))
Line 239: Line 239:
=={{header|D}}==
=={{header|D}}==
Complex number is a D built-in type.
Complex number is a D built-in type.
<d>auto x = 1F+1i ; // auto type to cfloat
<lang d>auto x = 1F+1i ; // auto type to cfloat
auto y = 3.14159+1.2i ; // cdouble
auto y = 3.14159+1.2i ; // cdouble
creal z ;
creal z ;
Line 250: Line 250:
z = 1.0 / x ; writefln(z) ; // => 0.5+-0.5i
z = 1.0 / x ; writefln(z) ; // => 0.5+-0.5i
// negation
// negation
z = -x ; writefln(z) ; // => -1+-1i</d>
z = -x ; writefln(z) ; // => -1+-1i</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 370: Line 370:


=={{header|Java}}==
=={{header|Java}}==
<java>public class Complex{
<lang java>public class Complex{
public final double real;
public final double real;
public final double imag;
public final double imag;
Line 408: Line 408:
System.out.println(a.mult(b));
System.out.println(a.mult(b));
}
}
}</java>
}</lang>


=={{header|Maple}}==
=={{header|Maple}}==
Line 433: Line 433:
=={{header|OCaml}}==
=={{header|OCaml}}==
The "Complex" module provides the functionality of complex numbers.
The "Complex" module provides the functionality of complex numbers.
<ocaml>open Complex
<lang ocaml>open Complex


let print_complex z =
let print_complex z =
Line 444: Line 444:
print_complex (mul a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (inv a);
print_complex (neg a)</ocaml>
print_complex (neg a)</lang>


=={{header|Pascal}}==
=={{header|Pascal}}==
<pascal>
<lang pascal>
program showcomplex(output);
program showcomplex(output);


Line 516: Line 516:
writeln
writeln
end.
end.
</pascal>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
The Math::Complex module provides the functionality of complex numbers.
The Math::Complex module provides the functionality of complex numbers.
<perl>use Math::Complex;
<lang perl>use Math::Complex;


$a = 1 + 1*i;
$a = 1 + 1*i;
Line 528: Line 528:
$c = $a * $b;
$c = $a * $b;
$c = 1 / $a;
$c = 1 / $a;
$c = -$a;</perl>
$c = -$a;</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 565: Line 565:
=={{header|Python}}==
=={{header|Python}}==


<python>a = 1 + 1j
<lang python>a = 1 + 1j
b = 3.14159 + 1.25j
b = 3.14159 + 1.25j


Line 571: Line 571:
c = a * b
c = a * b
c = 1 / a
c = 1 / a
c = -a</python>
c = -a</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==


<ruby>require 'complex'
<lang ruby>require 'complex'


a = Complex(1, 1)
a = Complex(1, 1)
Line 584: Line 584:
c = a * b
c = a * b
c = 1.0 / a
c = 1.0 / a
c = -a</ruby>
c = -a</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
<scheme>(define a (make-rectangular 1 1))
<lang scheme>(define a (make-rectangular 1 1))
(define b (make-rectangular 3.14159 1.25))
(define b (make-rectangular 3.14159 1.25))


Line 593: Line 593:
(define c (* a b))
(define c (* a b))
(define c (/ 1 a))
(define c (/ 1 a))
(define c (- a))</scheme>
(define c (- a))</lang>