Arithmetic/Complex: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Java}}: Forgot negation (sorry Waldorf))
No edit summary
Line 1: Line 1:
{{task}}An '''imaginary 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, imaginary 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.
{{task}}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 imaginary numbers in separate functions (subtraction and division operations can be made with pairs of these operations).
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 imaginary 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).
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).


=={{header|Ada}}==
=={{header|Ada}}==
Line 21: Line 21:
-- with the real type to be used in the complex type definition.
-- with the real type to be used in the complex type definition.
package Imaginary_Type is new Ada.Numerics.Generic_Complex_Types(Long_Float);
package Complex_Type is new Ada.Numerics.Generic_Complex_Types(Long_Float);
use Imaginary_Type;
use Complex_Type;
A : Complex := Compose_From_Cartesian(Re => 1.0, Im => 1.0);
A : Complex := Compose_From_Cartesian(Re => 1.0, Im => 1.0);
Line 38: Line 38:


=={{header|Java}}==
=={{header|Java}}==
<java>public class Imag{
<java>public class Complex{
public double real;
public final double real;
public double imag;
public final double imag;


public Imag(){this(0,0)}//default values to 0...force of habit
public Complex(){this(0,0)}//default values to 0...force of habit
public Imag(double r, double i){real = r; imag = i;}
public Complex(double r, double i){real = r; imag = i;}


public static Imag add(Imag a, Imag b){
public Complex add(Complex b){
return new Imag(a.real + b.real, a.imag + b.imag);
return new Complex(this.real + b.real, this.imag + b.imag);
}
}


public static Imag mult(Imag a, Imag b){
public Complex mult(Complex b){
//FOIL of (a+bi)(c+di) with i*i = -1
//FOIL of (a+bi)(c+di) with i*i = -1
return new Imag(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real);
return new Complex(this.real * b.real - this.imag * b.imag, this.real * b.imag + this.imag * b.real);
}
}


public static Imag invert(Imag a){
public Complex invert(){
//1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
//1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
double denom = a.real * a.real - a.imag * a.imag;
double denom = real * real - imag * imag;
return new Imag(a.real/denom, a.imag/denom);
return new Complex(real/denom, imag/denom);
}
}


public static Imag neg(Imag a){
public Complex neg(){
return new Imag(-a.real, -a.imag);
return new Complex(-real, -imag);
}
}
}</java>
}</java>

Revision as of 11:53, 9 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;

end Complex_Operations;</ada>

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>