How to resolve the algorithm Arithmetic/Complex step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic/Complex step by step in the Oberon-2 programming language

Table of Contents

Problem Statement

A   complex number   is a number which can be written as:

a + b × i

{\displaystyle a+b\times i}

(sometimes shown as:

b + a × i

{\displaystyle b+a\times i}

where

a

{\displaystyle a}

and

b

{\displaystyle b}

are real numbers,   and

i

{\displaystyle i}

is   √ -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

{\displaystyle i}

.

By definition, the   complex conjugate   of

a + b i

{\displaystyle a+bi}

is

a − b i

{\displaystyle a-bi}

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic/Complex step by step in the Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE Complex;
IMPORT Files,Out;
TYPE
        Complex* = POINTER TO ComplexDesc;
        ComplexDesc = RECORD
                r-,i-: REAL;
        END;
       
PROCEDURE (CONST x: Complex) Add*(CONST y: Complex): Complex;
BEGIN
        RETURN New(x.r + y.r,x.i + y.i)
END Add;

PROCEDURE (CONST x: Complex) Sub*(CONST y: Complex): Complex;
BEGIN
        RETURN New(x.r - y.r,x.i - y.i)
END Sub;

PROCEDURE (CONST x: Complex) Mul*(CONST y: Complex): Complex;
BEGIN
        RETURN New(x.r*y.r - x.i*y.i,x.r*y.i + x.i*y.r)
END Mul;

PROCEDURE (CONST x: Complex) Div*(CONST y: Complex): Complex;
VAR
        d: REAL;
BEGIN
        d := y.r * y.r + y.i * y.i;
        RETURN New((x.r*y.r + x.i*y.i)/d,(x.i*y.r - x.r*y.i)/d)
END Div;

(* Reciprocal *)
PROCEDURE (CONST x: Complex) Rec*(): Complex;
VAR
        d: REAL;
BEGIN
        d := x.r * x.r + y.i * y.i;
        RETURN New(x.r/d,(-1.0 * x.i)/d);
END Rec;

(* Conjugate *)
PROCEDURE (x: Complex) Con*(): Complex;
BEGIN
        RETURN New(x.r, (-1.0) * x.i);
END Con;

PROCEDURE (x: Complex) Out(out : Files.File);
BEGIN
        Files.WriteString(out,"(");
        Files.WriteReal(out,x.r);
        Files.WriteString(out,",");
        Files.WriteReal(out,x.i);
        Files.WriteString(out,"i)")
END Out;

PROCEDURE New(x,y: REAL): Complex;
VAR
        r: Complex;
BEGIN
        NEW(r);r.r := x;r.i := y;
        RETURN r
END New;

VAR
        r,x,y: Complex;
BEGIN
        x := New(1.5,3);
        y := New(1.0,1.0);
       
        Out.String("x: ");x.Out(Files.stdout);Out.Ln;
        Out.String("y: ");y.Out(Files.stdout);Out.Ln;        
        r := x.Add(y);
        Out.String("x + y: ");r.Out(Files.stdout);Out.Ln;
        r := x.Sub(y);
        Out.String("x - y: ");r.Out(Files.stdout);Out.Ln;
        r := x.Mul(y);
        Out.String("x * y: ");r.Out(Files.stdout);Out.Ln;
        r := x.Div(y);
        Out.String("x / y: ");r.Out(Files.stdout);Out.Ln;
        r := y.Rec();
        Out.String("1 / y: ");r.Out(Files.stdout);Out.Ln;
        r := x.Con();
        Out.String("x': ");r.Out(Files.stdout);Out.Ln;
       
END Complex.

  

You may also check:How to resolve the algorithm Approximate equality step by step in the Ruby programming language
You may also check:How to resolve the algorithm N'th step by step in the Sidef programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the Perl programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the JavaScript programming language