Arithmetic/Complex: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|C}}: whoops, it is in the C99 standard after all as a primitive type; add some refs.)
(Forth w/FSL's complex.seq)
Line 98: Line 98:
// negation
// negation
z = -x ; writefln(z) ; // => -1+-1i</d>
z = -x ; writefln(z) ; // => -1+-1i</d>

=={{header|Forth}}==
{{libheader|Forth Scientific Library}}
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 fixed-point implementations suitable for microcontrollers and DSPs.

include complex.seq
: ZNEGATE 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.

=={{header|Java}}==
=={{header|Java}}==
<java>public class Complex{
<java>public class Complex{

Revision as of 16:28, 11 March 2008

Task
Arithmetic/Complex
You are encouraged to solve this task according to the task description, using any language you may know.

A complex number is a number which can be written as "a + b*i" (sometimes shown as "b + a*i") where a and b are real numbers and i is the square root of -1. Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part", where the imaginary part is the number to be multiplied by i.

Show addition, multiplication, negation, and inversion of complex numbers in separate functions (subtraction and division operations can be made with pairs of these operations).

Some languages have complex number libraries available. If your language does, show the operations. If your language does not, also show the definition of this type (print functions are not necessary).

Ada

<ada>with Ada.Numerics.Generic_Complex_Types;

procedure Complex_Operations is

  -- Ada provides a pre-defined generic package for complex types
  -- That package contains definitions for composition,
  -- negation, addition, subtraction, multiplication, division,
  -- conjugation, exponentiation, and absolute value, as well as
  -- basic comparison operations.
  -- Ada provides a second pre-defined package for sin, cos, tan, cot,
  -- arcsin, arccos, arctan, arccot, and the hyperbolic versions of 
  -- those trigonometric functions.
  
  -- The package Ada.Numerics.Generic_Complex_Types requires definition
  -- with the real type to be used in the complex type definition.
  
  package Complex_Type is new Ada.Numerics.Generic_Complex_Types(Long_Float);
  use Complex_Type;
  
  A : Complex := Compose_From_Cartesian(Re => 1.0, Im => 1.0);
  B : Complex := Compose_From_Cartesian(Re => 3.14159, Im => 1.25);
  C : Complex;
 

begin

  -- Addition
  C := A + B;
  -- Multiplication
  C := A * B;
  -- Inversion
  C := 1.0 / A;
  -- Negation
  C := -A;

end Complex_Operations;</ada>

C

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++.) [1] [2] <c>

  1. include <complex.h>

void complex_operations() {

 double complex a = 1.0 + 1.0I;
 double complex b = 3.14159 + 1.25I;
 double complex c;
 // addition
 c = a + b;
 // multiplication
 c = a * b;
 // inversion
 c = 1.0 / a;
 // negation
 c = -a;

} </c>

C++

<cpp>

  1. include <complex>

using std::complex;

void complex_operations() {

 complex<double> a(1.0, 1.0);
 complex<double> b(3.14159, 1.25);
 complex<double> c;
 // addition
 c = a + b;
 // multiplication
 c = a * b;
 // inversion
 c = 1.0 / a;
 // negation
 c = -a;

} </cpp>

D

Complex number is a D built-in type. <d>auto x = 1F+1i ; // auto type to cfloat auto y = 3.14159+1.2i ; // cdouble creal z ;

// addition z = x + y ; writefln(z) ; // => 4.14159+2.2i // multiplication z = x * y ; writefln(z) ; // => 1.94159+4.34159i // inversion z = 1.0 / x ; writefln(z) ; // => 0.5+-0.5i // negation z = -x ; writefln(z) ; // => -1+-1i</d>

Forth

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 fixed-point implementations suitable for microcontrollers and DSPs.

include complex.seq

: ZNEGATE  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.

Java

<java>public class Complex{

  public final double real;
  public final double imag;
  public Complex(){this(0,0)}//default values to 0...force of habit
  public Complex(double r, double i){real = r; imag = i;}
  public Complex add(Complex b){
     return new Complex(this.real + b.real, this.imag + b.imag);
  }
  public Complex mult(Complex b){
     //FOIL of (a+bi)(c+di) with i*i = -1
     return new Complex(this.real * b.real - this.imag * b.imag, this.real * b.imag + this.imag * b.real);
  }
  public Complex invert(){
     //1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
     double denom = real * real + imag * imag;
     return new Complex(real/denom,-imag/denom);
  }
  public Complex neg(){
     return new Complex(-real, -imag);
  }

}</java>