Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Arithmetic-Complex/00-META.yaml
Normal file
3
Task/Arithmetic-Complex/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Arithmetic/Complex
|
||||
note: Arithmetic operations
|
||||
25
Task/Arithmetic-Complex/00-TASK.txt
Normal file
25
Task/Arithmetic-Complex/00-TASK.txt
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
A '''[[wp:Complex number|complex number]]''' is a number which can be written as:
|
||||
<big><math>a + b \times i</math></big>
|
||||
(sometimes shown as:
|
||||
<big><math>b + a \times i</math></big>
|
||||
where <big><math>a</math></big> and <big><math>b</math></big> are real numbers, and [[wp:Imaginary_unit|<big><math>i</math></big>]] is <big>√{{overline| -1 }}</big>
|
||||
|
||||
|
||||
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 <big><math>i</math></big>.
|
||||
|
||||
|
||||
;Task:
|
||||
* Show addition, multiplication, negation, and inversion of complex numbers in separate functions. (Subtraction and division operations can be made with pairs of these operations.)
|
||||
* Print the results for each operation tested.
|
||||
* ''Optional:'' Show complex conjugation.
|
||||
|
||||
<br>
|
||||
By definition, the [[wp:complex conjugate|complex conjugate]] of
|
||||
<big><math>a + bi</math></big>
|
||||
is
|
||||
<big><math>a - bi</math></big>
|
||||
|
||||
<br>
|
||||
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.
|
||||
<br><br>
|
||||
|
||||
12
Task/Arithmetic-Complex/11l/arithmetic-complex.11l
Normal file
12
Task/Arithmetic-Complex/11l/arithmetic-complex.11l
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
V z1 = 1.5 + 3i
|
||||
V z2 = 1.5 + 1.5i
|
||||
print(z1 + z2)
|
||||
print(z1 - z2)
|
||||
print(z1 * z2)
|
||||
print(z1 / z2)
|
||||
print(-z1)
|
||||
print(conjugate(z1))
|
||||
print(abs(z1))
|
||||
print(z1 ^ z2)
|
||||
print(z1.real)
|
||||
print(z1.imag)
|
||||
27
Task/Arithmetic-Complex/ALGOL-68/arithmetic-complex.alg
Normal file
27
Task/Arithmetic-Complex/ALGOL-68/arithmetic-complex.alg
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
main:(
|
||||
FORMAT compl fmt = $g(-7,5)"⊥"g(-7,5)$;
|
||||
|
||||
PROC compl operations = VOID: (
|
||||
LONG COMPL a = 1.0 ⊥ 1.0;
|
||||
LONG COMPL b = 3.14159 ⊥ 1.2;
|
||||
|
||||
LONG COMPL c;
|
||||
|
||||
printf(($x"a="f(compl fmt)l$,a));
|
||||
printf(($x"b="f(compl fmt)l$,b));
|
||||
|
||||
# addition #
|
||||
c := a + b;
|
||||
printf(($x"a+b="f(compl fmt)l$,c));
|
||||
# multiplication #
|
||||
c := a * b;
|
||||
printf(($x"a*b="f(compl fmt)l$,c));
|
||||
# inversion #
|
||||
c := 1.0 / a;
|
||||
printf(($x"1/c="f(compl fmt)l$,c));
|
||||
# negation #
|
||||
c := -a;
|
||||
printf(($x"-a="f(compl fmt)l$,c))
|
||||
);
|
||||
compl operations
|
||||
)
|
||||
25
Task/Arithmetic-Complex/ALGOL-W/arithmetic-complex.alg
Normal file
25
Task/Arithmetic-Complex/ALGOL-W/arithmetic-complex.alg
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
begin
|
||||
% show some complex arithmetic %
|
||||
% returns c + d, using the builtin complex + operator %
|
||||
complex procedure cAdd ( complex value c, d ) ; c + d;
|
||||
% returns c * d, using the builtin complex * operator %
|
||||
complex procedure cMul ( complex value c, d ) ; c * d;
|
||||
% returns the negation of c, using the builtin complex unary - operator %
|
||||
complex procedure cNeg ( complex value c ) ; - c;
|
||||
% returns the inverse of c, using the builtin complex / operatror %
|
||||
complex procedure cInv ( complex value c ) ; 1 / c;
|
||||
% returns the conjugate of c %
|
||||
complex procedure cConj ( complex value c ) ; realpart( c ) - imag( imagpart( c ) );
|
||||
complex c, d;
|
||||
c := 1 + 2i;
|
||||
d := 3 + 4i;
|
||||
% set I/O format for real aand complex numbers %
|
||||
r_format := "A"; s_w := 0; r_w := 6; r_d := 2;
|
||||
write( "c : ", c );
|
||||
write( "d : ", d );
|
||||
write( "c + d : ", cAdd( c, d ) );
|
||||
write( "c * d : ", cMul( c, d ) );
|
||||
write( "-c : ", cNeg( c ) );
|
||||
write( "1/c : ", cInv( c ) );
|
||||
write( "conj c : ", cConj( c ) )
|
||||
end.
|
||||
10
Task/Arithmetic-Complex/APL/arithmetic-complex.apl
Normal file
10
Task/Arithmetic-Complex/APL/arithmetic-complex.apl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
x←1j1 ⍝assignment
|
||||
y←5.25j1.5
|
||||
x+y ⍝addition
|
||||
6.25J2.5
|
||||
x×y ⍝multiplication
|
||||
3.75J6.75
|
||||
⌹x ⍝inversion
|
||||
0.5j_0.5
|
||||
-x ⍝negation
|
||||
¯1J¯1
|
||||
56
Task/Arithmetic-Complex/AWK/arithmetic-complex.awk
Normal file
56
Task/Arithmetic-Complex/AWK/arithmetic-complex.awk
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# simulate a struct using associative arrays
|
||||
function complex(arr, re, im) {
|
||||
arr["re"] = re
|
||||
arr["im"] = im
|
||||
}
|
||||
|
||||
function re(cmplx) {
|
||||
return cmplx["re"]
|
||||
}
|
||||
|
||||
function im(cmplx) {
|
||||
return cmplx["im"]
|
||||
}
|
||||
|
||||
function printComplex(cmplx) {
|
||||
print re(cmplx), im(cmplx)
|
||||
}
|
||||
|
||||
function abs2(cmplx) {
|
||||
return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx)
|
||||
}
|
||||
|
||||
function abs(cmplx) {
|
||||
return sqrt(abs2(cmplx))
|
||||
}
|
||||
|
||||
function add(res, cmplx1, cmplx2) {
|
||||
complex(res, re(cmplx1) + re(cmplx2), im(cmplx1) + im(cmplx2))
|
||||
}
|
||||
|
||||
function mult(res, cmplx1, cmplx2) {
|
||||
complex(res, re(cmplx1) * re(cmplx2) - im(cmplx1) * im(cmplx2), re(cmplx1) * im(cmplx2) + im(cmplx1) * re(cmplx2))
|
||||
}
|
||||
|
||||
function scale(res, cmplx, scalar) {
|
||||
complex(res, re(cmplx) * scalar, im(cmplx) * scalar)
|
||||
}
|
||||
|
||||
function negate(res, cmplx) {
|
||||
scale(res, cmplx, -1)
|
||||
}
|
||||
|
||||
function conjugate(res, cmplx) {
|
||||
complex(res, re(cmplx), -im(cmplx))
|
||||
}
|
||||
|
||||
function invert(res, cmplx) {
|
||||
conjugate(res, cmplx)
|
||||
scale(res, res, 1 / abs(cmplx))
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
complex(i, 0, 1)
|
||||
mult(i, i, i)
|
||||
printComplex(i)
|
||||
}
|
||||
135
Task/Arithmetic-Complex/Action-/arithmetic-complex.action
Normal file
135
Task/Arithmetic-Complex/Action-/arithmetic-complex.action
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
|
||||
|
||||
DEFINE R_="+0"
|
||||
DEFINE I_="+6"
|
||||
TYPE Complex=[CARD cr1,cr2,cr3,ci1,ci2,ci3]
|
||||
|
||||
BYTE FUNC Positive(REAL POINTER x)
|
||||
BYTE ARRAY tmp
|
||||
|
||||
tmp=x
|
||||
IF (tmp(0)&$80)=$00 THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
PROC PrintComplex(Complex POINTER x)
|
||||
PrintR(x R_)
|
||||
IF Positive(x I_) THEN
|
||||
Put('+)
|
||||
FI
|
||||
PrintR(x I_) Put('i)
|
||||
RETURN
|
||||
|
||||
PROC PrintComplexXYZ(Complex POINTER x,y,z CHAR ARRAY s)
|
||||
Print("(") PrintComplex(x)
|
||||
Print(") ") Print(s)
|
||||
Print(" (") PrintComplex(y)
|
||||
Print(") = ") PrintComplex(z)
|
||||
PutE()
|
||||
RETURN
|
||||
|
||||
PROC PrintComplexXY(Complex POINTER x,y CHAR ARRAY s)
|
||||
Print(s)
|
||||
Print("(") PrintComplex(x)
|
||||
Print(") = ") PrintComplex(y)
|
||||
PutE()
|
||||
RETURN
|
||||
|
||||
PROC ComplexAdd(Complex POINTER x,y,res)
|
||||
RealAdd(x R_,y R_,res R_) ;res.r=x.r+y.r
|
||||
RealAdd(x I_,y I_,res I_) ;res.i=x.i+y.i
|
||||
RETURN
|
||||
|
||||
PROC ComplexSub(Complex POINTER x,y,res)
|
||||
RealSub(x R_,y R_,res R_) ;res.r=x.r-y.r
|
||||
RealSub(x I_,y I_,res I_) ;res.i=x.i-y.i
|
||||
RETURN
|
||||
|
||||
PROC ComplexMult(Complex POINTER x,y,res)
|
||||
REAL tmp1,tmp2
|
||||
|
||||
RealMult(x R_,y R_,tmp1) ;tmp1=x.r*y.r
|
||||
RealMult(x I_,y I_,tmp2) ;tmp2=x.i*y.i
|
||||
RealSub(tmp1,tmp2,res R_) ;res.r=x.r*y.r-x.i*y.i
|
||||
|
||||
RealMult(x R_,y I_,tmp1) ;tmp1=x.r*y.i
|
||||
RealMult(x I_,y R_,tmp2) ;tmp2=x.i*y.r
|
||||
RealAdd(tmp1,tmp2,res I_) ;res.i=x.r*y.i+x.i*y.r
|
||||
RETURN
|
||||
|
||||
PROC ComplexDiv(Complex POINTER x,y,res)
|
||||
REAL tmp1,tmp2,tmp3,tmp4
|
||||
|
||||
RealMult(x R_,y R_,tmp1) ;tmp1=x.r*y.r
|
||||
RealMult(x I_,y I_,tmp2) ;tmp2=x.i*y.i
|
||||
RealAdd(tmp1,tmp2,tmp3) ;tmp3=x.r*y.r+x.i*y.i
|
||||
RealMult(y R_,y R_,tmp1) ;tmp1=y.r^2
|
||||
RealMult(y I_,y I_,tmp2) ;tmp2=y.i^2
|
||||
RealAdd(tmp1,tmp2,tmp4) ;tmp4=y.r^2+y.i^2
|
||||
RealDiv(tmp3,tmp4,res R_) ;res.r=(x.r*y.r+x.i*y.i)/(y.r^2+y.i^2)
|
||||
|
||||
RealMult(x I_,y R_,tmp1) ;tmp1=x.i*y.r
|
||||
RealMult(x R_,y I_,tmp2) ;tmp2=x.r*y.i
|
||||
RealSub(tmp1,tmp2,tmp3) ;tmp3=x.i*y.r-x.r*y.i
|
||||
RealDiv(tmp3,tmp4,res I_) ;res.i=(x.i*y.r-x.r*y.i)/(y.r^2+y.i^2)
|
||||
RETURN
|
||||
|
||||
PROC ComplexNeg(Complex POINTER x,res)
|
||||
REAL neg
|
||||
|
||||
ValR("-1",neg) ;neg=-1
|
||||
RealMult(x R_,neg,res R_) ;res.r=-x.r
|
||||
RealMult(x I_,neg,res I_) ;res.r=-x.r
|
||||
RETURN
|
||||
|
||||
PROC ComplexInv(Complex POINTER x,res)
|
||||
REAL tmp1,tmp2,tmp3
|
||||
|
||||
RealMult(x R_,x R_,tmp1) ;tmp1=x.r^2
|
||||
RealMult(x I_,x I_,tmp2) ;tmp2=x.i^2
|
||||
RealAdd(tmp1,tmp2,tmp3) ;tmp3=x.r^2+x.i^2
|
||||
RealDiv(x R_,tmp3,res R_) ;res.r=x.r/(x.r^2+x.i^2)
|
||||
|
||||
ValR("-1",tmp1) ;tmp1=-1
|
||||
RealMult(x I_,tmp1,tmp2) ;tmp2=-x.i
|
||||
RealDiv(tmp2,tmp3,res I_) ;res.i=-x.i/(x.r^2+x.i^2)
|
||||
RETURN
|
||||
|
||||
PROC ComplexConj(Complex POINTER x,res)
|
||||
REAL neg
|
||||
|
||||
ValR("-1",neg) ;neg=-1
|
||||
RealAssign(x R_,res R_) ;res.r=x.r
|
||||
RealMult(x I_,neg,res I_) ;res.i=-x.i
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Complex x,y,res
|
||||
|
||||
IntToReal(5,x R_) IntToReal(3,x I_)
|
||||
IntToReal(4,y R_) ValR("-3",y I_)
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
|
||||
ComplexAdd(x,y,res)
|
||||
PrintComplexXYZ(x,y,res,"+")
|
||||
|
||||
ComplexSub(x,y,res)
|
||||
PrintComplexXYZ(x,y,res,"-")
|
||||
|
||||
ComplexMult(x,y,res)
|
||||
PrintComplexXYZ(x,y,res,"*")
|
||||
|
||||
ComplexDiv(x,y,res)
|
||||
PrintComplexXYZ(x,y,res,"/")
|
||||
|
||||
ComplexNeg(y,res)
|
||||
PrintComplexXY(y,res," -")
|
||||
|
||||
ComplexInv(y,res)
|
||||
PrintComplexXY(y,res," 1 / ")
|
||||
|
||||
ComplexConj(y,res)
|
||||
PrintComplexXY(y,res," conj")
|
||||
RETURN
|
||||
47
Task/Arithmetic-Complex/Ada/arithmetic-complex.ada
Normal file
47
Task/Arithmetic-Complex/Ada/arithmetic-complex.ada
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
with Ada.Numerics.Generic_Complex_Types;
|
||||
with Ada.Text_IO.Complex_IO;
|
||||
|
||||
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_Types is new Ada.Numerics.Generic_Complex_Types (Long_Float);
|
||||
use Complex_Types;
|
||||
package Complex_IO is new Ada.Text_IO.Complex_IO (Complex_Types);
|
||||
use Complex_IO;
|
||||
use Ada.Text_IO;
|
||||
|
||||
A : Complex := Compose_From_Cartesian (Re => 1.0, Im => 1.0);
|
||||
B : Complex := Compose_From_Polar (Modulus => 1.0, Argument => 3.14159);
|
||||
C : Complex;
|
||||
|
||||
begin
|
||||
-- Addition
|
||||
C := A + B;
|
||||
Put("A + B = "); Put(C);
|
||||
New_Line;
|
||||
-- Multiplication
|
||||
C := A * B;
|
||||
Put("A * B = "); Put(C);
|
||||
New_Line;
|
||||
-- Inversion
|
||||
C := 1.0 / A;
|
||||
Put("1.0 / A = "); Put(C);
|
||||
New_Line;
|
||||
-- Negation
|
||||
C := -A;
|
||||
Put("-A = "); Put(C);
|
||||
New_Line;
|
||||
-- Conjugation
|
||||
Put("Conjugate(-A) = ");
|
||||
C := Conjugate (C); Put(C);
|
||||
end Complex_Operations;
|
||||
11
Task/Arithmetic-Complex/Arturo/arithmetic-complex.arturo
Normal file
11
Task/Arithmetic-Complex/Arturo/arithmetic-complex.arturo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
a: to :complex [1 1]
|
||||
b: to :complex @[pi 1.2]
|
||||
|
||||
print ["a:" a]
|
||||
print ["b:" b]
|
||||
|
||||
print ["a + b:" a + b]
|
||||
print ["a * b:" a * b]
|
||||
print ["1 / a:" 1 / a]
|
||||
print ["neg a:" neg a]
|
||||
print ["conj a:" conj a]
|
||||
47
Task/Arithmetic-Complex/AutoHotkey/arithmetic-complex.ahk
Normal file
47
Task/Arithmetic-Complex/AutoHotkey/arithmetic-complex.ahk
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
Cset(C,1,1)
|
||||
MsgBox % Cstr(C) ; 1 + i*1
|
||||
Cneg(C,C)
|
||||
MsgBox % Cstr(C) ; -1 - i*1
|
||||
Cadd(C,C,C)
|
||||
MsgBox % Cstr(C) ; -2 - i*2
|
||||
Cinv(D,C)
|
||||
MsgBox % Cstr(D) ; -0.25 + 0.25*i
|
||||
Cmul(C,C,D)
|
||||
MsgBox % Cstr(C) ; 1 + i*0
|
||||
|
||||
Cset(ByRef C, re, im) {
|
||||
VarSetCapacity(C,16)
|
||||
NumPut(re,C,0,"double")
|
||||
NumPut(im,C,8,"double")
|
||||
}
|
||||
Cre(ByRef C) {
|
||||
Return NumGet(C,0,"double")
|
||||
}
|
||||
Cim(ByRef C) {
|
||||
Return NumGet(C,8,"double")
|
||||
}
|
||||
Cstr(ByRef C) {
|
||||
Return Cre(C) ((i:=Cim(C))<0 ? " - i*" . -i : " + i*" . i)
|
||||
}
|
||||
Cadd(ByRef C, ByRef A, ByRef B) {
|
||||
VarSetCapacity(C,16)
|
||||
NumPut(Cre(A)+Cre(B),C,0,"double")
|
||||
NumPut(Cim(A)+Cim(B),C,8,"double")
|
||||
}
|
||||
Cmul(ByRef C, ByRef A, ByRef B) {
|
||||
VarSetCapacity(C,16)
|
||||
t := Cre(A)*Cim(B)+Cim(A)*Cre(B)
|
||||
NumPut(Cre(A)*Cre(B)-Cim(A)*Cim(B),C,0,"double")
|
||||
NumPut(t,C,8,"double") ; A or B can be C!
|
||||
}
|
||||
Cneg(ByRef C, ByRef A) {
|
||||
VarSetCapacity(C,16)
|
||||
NumPut(-Cre(A),C,0,"double")
|
||||
NumPut(-Cim(A),C,8,"double")
|
||||
}
|
||||
Cinv(ByRef C, ByRef A) {
|
||||
VarSetCapacity(C,16)
|
||||
d := Cre(A)**2 + Cim(A)**2
|
||||
NumPut( Cre(A)/d,C,0,"double")
|
||||
NumPut(-Cim(A)/d,C,8,"double")
|
||||
}
|
||||
76
Task/Arithmetic-Complex/BASIC/arithmetic-complex.basic
Normal file
76
Task/Arithmetic-Complex/BASIC/arithmetic-complex.basic
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
TYPE complex
|
||||
real AS DOUBLE
|
||||
imag AS DOUBLE
|
||||
END TYPE
|
||||
|
||||
DECLARE SUB suma (a AS complex, b AS complex, c AS complex)
|
||||
DECLARE SUB rest (a AS complex, b AS complex, c AS complex)
|
||||
DECLARE SUB mult (a AS complex, b AS complex, c AS complex)
|
||||
DECLARE SUB divi (a AS complex, b AS complex, c AS complex)
|
||||
DECLARE SUB neg (a AS complex, b AS complex)
|
||||
DECLARE SUB inv (a AS complex, b AS complex)
|
||||
DECLARE SUB conj (a AS complex, b AS complex)
|
||||
|
||||
CLS
|
||||
DIM x AS complex
|
||||
DIM y AS complex
|
||||
DIM z AS complex
|
||||
x.real = 1
|
||||
x.imag = 1
|
||||
y.real = 2
|
||||
y.imag = 2
|
||||
|
||||
PRINT "Siendo x = "; x.real; "+"; x.imag; "i"
|
||||
PRINT " e y = "; y.real; "+"; y.imag; "i"
|
||||
PRINT
|
||||
CALL suma(x, y, z)
|
||||
PRINT "x + y = "; z.real; "+"; z.imag; "i"
|
||||
CALL rest(x, y, z)
|
||||
PRINT "x - y = "; z.real; "+"; z.imag; "i"
|
||||
CALL mult(x, y, z)
|
||||
PRINT "x * y = "; z.real; "+"; z.imag; "i"
|
||||
CALL divi(x, y, z)
|
||||
PRINT "x / y = "; z.real; "+"; z.imag; "i"
|
||||
CALL neg(x, z)
|
||||
PRINT " -x = "; z.real; "+"; z.imag; "i"
|
||||
CALL inv(x, z)
|
||||
PRINT "1 / x = "; z.real; "+"; z.imag; "i"
|
||||
CALL conj(x, z)
|
||||
PRINT " x* = "; z.real; "+"; z.imag; "i"
|
||||
END
|
||||
|
||||
SUB suma (a AS complex, b AS complex, c AS complex)
|
||||
c.real = a.real + b.real
|
||||
c.imag = a.imag + b.imag
|
||||
END SUB
|
||||
|
||||
SUB inv (a AS complex, b AS complex)
|
||||
denom = a.real ^ 2 + a.imag ^ 2
|
||||
b.real = a.real / denom
|
||||
b.imag = -a.imag / denom
|
||||
END SUB
|
||||
|
||||
SUB mult (a AS complex, b AS complex, c AS complex)
|
||||
c.real = a.real * b.real - a.imag * b.imag
|
||||
c.imag = a.real * b.imag + a.imag * b.real
|
||||
END SUB
|
||||
|
||||
SUB neg (a AS complex, b AS complex)
|
||||
b.real = -a.real
|
||||
b.imag = -a.imag
|
||||
END SUB
|
||||
|
||||
SUB conj (a AS complex, b AS complex)
|
||||
b.real = a.real
|
||||
b.imag = -a.imag
|
||||
END SUB
|
||||
|
||||
SUB divi (a AS complex, b AS complex, c AS complex)
|
||||
c.real = ((a.real * b.real + b.imag * a.imag) / (b.real ^ 2 + b.imag ^ 2))
|
||||
c.imag = ((a.imag * b.real - a.real * b.imag) / (b.real ^ 2 + b.imag ^ 2))
|
||||
END SUB
|
||||
|
||||
SUB rest (a AS complex, b AS complex, c AS complex)
|
||||
c.real = a.real - b.real
|
||||
c.imag = a.imag - b.imag
|
||||
END SUB
|
||||
40
Task/Arithmetic-Complex/BBC-BASIC/arithmetic-complex.basic
Normal file
40
Task/Arithmetic-Complex/BBC-BASIC/arithmetic-complex.basic
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
DIM Complex{r, i}
|
||||
|
||||
DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0
|
||||
DIM b{} = Complex{} : b.r = PI# : b.i = 1.2
|
||||
DIM o{} = Complex{}
|
||||
|
||||
PROCcomplexadd(o{}, a{}, b{})
|
||||
PRINT "Result of addition is " FNcomplexshow(o{})
|
||||
PROCcomplexmul(o{}, a{}, b{})
|
||||
PRINT "Result of multiplication is " ; FNcomplexshow(o{})
|
||||
PROCcomplexneg(o{}, a{})
|
||||
PRINT "Result of negation is " ; FNcomplexshow(o{})
|
||||
PROCcomplexinv(o{}, a{})
|
||||
PRINT "Result of inversion is " ; FNcomplexshow(o{})
|
||||
END
|
||||
|
||||
DEF PROCcomplexadd(dst{}, one{}, two{})
|
||||
dst.r = one.r + two.r
|
||||
dst.i = one.i + two.i
|
||||
ENDPROC
|
||||
|
||||
DEF PROCcomplexmul(dst{}, one{}, two{})
|
||||
dst.r = one.r*two.r - one.i*two.i
|
||||
dst.i = one.i*two.r + one.r*two.i
|
||||
ENDPROC
|
||||
|
||||
DEF PROCcomplexneg(dst{}, src{})
|
||||
dst.r = -src.r
|
||||
dst.i = -src.i
|
||||
ENDPROC
|
||||
|
||||
DEF PROCcomplexinv(dst{}, src{})
|
||||
LOCAL denom : denom = src.r^2 + src.i^ 2
|
||||
dst.r = src.r / denom
|
||||
dst.i = -src.i / denom
|
||||
ENDPROC
|
||||
|
||||
DEF FNcomplexshow(src{})
|
||||
IF src.i >= 0 THEN = STR$(src.r) + " + " +STR$(src.i) + "i"
|
||||
= STR$(src.r) + " - " + STR$(-src.i) + "i"
|
||||
28
Task/Arithmetic-Complex/Bracmat/arithmetic-complex.bracmat
Normal file
28
Task/Arithmetic-Complex/Bracmat/arithmetic-complex.bracmat
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
(add=a b.!arg:(?a,?b)&!a+!b)
|
||||
& ( multiply
|
||||
= a b.!arg:(?a,?b)&1+!a*!b+-1
|
||||
)
|
||||
& (negate=.1+-1*!arg+-1)
|
||||
& ( conjugate
|
||||
= a b
|
||||
. !arg:i&-i
|
||||
| !arg:-i&i
|
||||
| !arg:?a_?b&(conjugate$!a)_(conjugate$!b)
|
||||
| !arg
|
||||
)
|
||||
& ( invert
|
||||
= conjugated
|
||||
. conjugate$!arg:?conjugated
|
||||
& multiply$(!arg,!conjugated)^-1*!conjugated
|
||||
)
|
||||
& out$("(a+i*b)+(a+i*b) =" add$(a+i*b,a+i*b))
|
||||
& out$("(a+i*b)+(a+-i*b) =" add$(a+i*b,a+-i*b))
|
||||
& out$("(a+i*b)*(a+i*b) =" multiply$(a+i*b,a+i*b))
|
||||
& out$("(a+i*b)*(a+-i*b) =" multiply$(a+i*b,a+-i*b))
|
||||
& out$("-1*(a+i*b) =" negate$(a+i*b))
|
||||
& out$("-1*(a+-i*b) =" negate$(a+-i*b))
|
||||
& out$("sin$x = " sin$x)
|
||||
& out$("conjugate sin$x =" conjugate$(sin$x))
|
||||
& out
|
||||
$ ("sin$x minus conjugate sin$x =" sin$x+negate$(conjugate$(sin$x)))
|
||||
& done;
|
||||
19
Task/Arithmetic-Complex/C++/arithmetic-complex.cpp
Normal file
19
Task/Arithmetic-Complex/C++/arithmetic-complex.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <iostream>
|
||||
#include <complex>
|
||||
using std::complex;
|
||||
|
||||
void complex_operations() {
|
||||
complex<double> a(1.0, 1.0);
|
||||
complex<double> b(3.14159, 1.25);
|
||||
|
||||
// addition
|
||||
std::cout << a + b << std::endl;
|
||||
// multiplication
|
||||
std::cout << a * b << std::endl;
|
||||
// inversion
|
||||
std::cout << 1.0 / a << std::endl;
|
||||
// negation
|
||||
std::cout << -a << std::endl;
|
||||
// conjugate
|
||||
std::cout << std::conj(a) << std::endl;
|
||||
}
|
||||
17
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-1.cs
Normal file
17
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-1.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
namespace RosettaCode.Arithmetic.Complex
|
||||
{
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static void Main()
|
||||
{
|
||||
var number = Complex.ImaginaryOne;
|
||||
foreach (var result in new[] { number + number, number * number, -number, 1 / number, Complex.Conjugate(number) })
|
||||
{
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
205
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-2.cs
Normal file
205
Task/Arithmetic-Complex/C-sharp/arithmetic-complex-2.cs
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
using System;
|
||||
|
||||
public struct ComplexNumber
|
||||
{
|
||||
public static readonly ComplexNumber i = new ComplexNumber(0.0, 1.0);
|
||||
public static readonly ComplexNumber Zero = new ComplexNumber(0.0, 0.0);
|
||||
|
||||
public double Re;
|
||||
public double Im;
|
||||
|
||||
public ComplexNumber(double re)
|
||||
{
|
||||
this.Re = re;
|
||||
this.Im = 0;
|
||||
}
|
||||
|
||||
public ComplexNumber(double re, double im)
|
||||
{
|
||||
this.Re = re;
|
||||
this.Im = im;
|
||||
}
|
||||
|
||||
public static ComplexNumber operator *(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re * n2.Re - n1.Im * n2.Im,
|
||||
n1.Im * n2.Re + n1.Re * n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator *(double n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1 * n2.Re, n1 * n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator /(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
double n2Norm = n2.Re * n2.Re + n2.Im * n2.Im;
|
||||
return new ComplexNumber((n1.Re * n2.Re + n1.Im * n2.Im) / n2Norm,
|
||||
(n1.Im * n2.Re - n1.Re * n2.Im) / n2Norm);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator /(ComplexNumber n1, double n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re / n2, n1.Im / n2);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator +(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re + n2.Re, n1.Im + n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator -(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return new ComplexNumber(n1.Re - n2.Re, n1.Im - n2.Im);
|
||||
}
|
||||
|
||||
public static ComplexNumber operator -(ComplexNumber n)
|
||||
{
|
||||
return new ComplexNumber(-n.Re, -n.Im);
|
||||
}
|
||||
|
||||
public static implicit operator ComplexNumber(double n)
|
||||
{
|
||||
return new ComplexNumber(n, 0.0);
|
||||
}
|
||||
|
||||
public static explicit operator double(ComplexNumber n)
|
||||
{
|
||||
return n.Re;
|
||||
}
|
||||
|
||||
public static bool operator ==(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return n1.Re == n2.Re && n1.Im == n2.Im;
|
||||
}
|
||||
|
||||
public static bool operator !=(ComplexNumber n1, ComplexNumber n2)
|
||||
{
|
||||
return n1.Re != n2.Re || n1.Im != n2.Im;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this == (ComplexNumber)obj;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Re.GetHashCode() ^ Im.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("{0}+{1}*i", Re, Im);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ComplexMath
|
||||
{
|
||||
public static double Abs(ComplexNumber a)
|
||||
{
|
||||
return Math.Sqrt(Norm(a));
|
||||
}
|
||||
|
||||
public static double Norm(ComplexNumber a)
|
||||
{
|
||||
return a.Re * a.Re + a.Im * a.Im;
|
||||
}
|
||||
|
||||
public static double Arg(ComplexNumber a)
|
||||
{
|
||||
return Math.Atan2(a.Im, a.Re);
|
||||
}
|
||||
|
||||
public static ComplexNumber Inverse(ComplexNumber a)
|
||||
{
|
||||
double norm = Norm(a);
|
||||
return new ComplexNumber(a.Re / norm, -a.Im / norm);
|
||||
}
|
||||
|
||||
public static ComplexNumber Conjugate(ComplexNumber a)
|
||||
{
|
||||
return new ComplexNumber(a.Re, -a.Im);
|
||||
|
||||
}
|
||||
|
||||
public static ComplexNumber Exp(ComplexNumber a)
|
||||
{
|
||||
double e = Math.Exp(a.Re);
|
||||
return new ComplexNumber(e * Math.Cos(a.Im), e * Math.Sin(a.Im));
|
||||
}
|
||||
|
||||
public static ComplexNumber Log(ComplexNumber a)
|
||||
{
|
||||
|
||||
return new ComplexNumber(0.5 * Math.Log(Norm(a)), Arg(a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Power(ComplexNumber a, ComplexNumber power)
|
||||
{
|
||||
return Exp(power * Log(a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Power(ComplexNumber a, int power)
|
||||
{
|
||||
bool inverse = false;
|
||||
if (power < 0)
|
||||
{
|
||||
inverse = true; power = -power;
|
||||
}
|
||||
|
||||
ComplexNumber result = 1.0;
|
||||
ComplexNumber multiplier = a;
|
||||
while (power > 0)
|
||||
{
|
||||
if ((power & 1) != 0) result *= multiplier;
|
||||
multiplier *= multiplier;
|
||||
power >>= 1;
|
||||
}
|
||||
|
||||
if (inverse)
|
||||
return Inverse(result);
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ComplexNumber Sqrt(ComplexNumber a)
|
||||
{
|
||||
return Exp(0.5 * Log(a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Sin(ComplexNumber a)
|
||||
{
|
||||
return Sinh(ComplexNumber.i * a) / ComplexNumber.i;
|
||||
}
|
||||
|
||||
public static ComplexNumber Cos(ComplexNumber a)
|
||||
{
|
||||
return Cosh(ComplexNumber.i * a);
|
||||
}
|
||||
|
||||
public static ComplexNumber Sinh(ComplexNumber a)
|
||||
{
|
||||
return 0.5 * (Exp(a) - Exp(-a));
|
||||
}
|
||||
|
||||
public static ComplexNumber Cosh(ComplexNumber a)
|
||||
{
|
||||
return 0.5 * (Exp(a) + Exp(-a));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// usage
|
||||
ComplexNumber i = 2;
|
||||
ComplexNumber j = new ComplexNumber(1, -2);
|
||||
Console.WriteLine(i * j);
|
||||
Console.WriteLine(ComplexMath.Power(j, 2));
|
||||
Console.WriteLine((double)ComplexMath.Sin(i) + " vs " + Math.Sin(2));
|
||||
Console.WriteLine(ComplexMath.Power(j, 0) == 1.0);
|
||||
}
|
||||
}
|
||||
32
Task/Arithmetic-Complex/C/arithmetic-complex-1.c
Normal file
32
Task/Arithmetic-Complex/C/arithmetic-complex-1.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <complex.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void cprint(double complex c)
|
||||
{
|
||||
printf("%f%+fI", creal(c), cimag(c));
|
||||
}
|
||||
void complex_operations() {
|
||||
double complex a = 1.0 + 1.0I;
|
||||
double complex b = 3.14159 + 1.2I;
|
||||
|
||||
double complex c;
|
||||
|
||||
printf("\na="); cprint(a);
|
||||
printf("\nb="); cprint(b);
|
||||
|
||||
// addition
|
||||
c = a + b;
|
||||
printf("\na+b="); cprint(c);
|
||||
// multiplication
|
||||
c = a * b;
|
||||
printf("\na*b="); cprint(c);
|
||||
// inversion
|
||||
c = 1.0 / a;
|
||||
printf("\n1/c="); cprint(c);
|
||||
// negation
|
||||
c = -a;
|
||||
printf("\n-a="); cprint(c);
|
||||
// conjugate
|
||||
c = conj(a);
|
||||
printf("\nconj a="); cprint(c); printf("\n");
|
||||
}
|
||||
61
Task/Arithmetic-Complex/C/arithmetic-complex-2.c
Normal file
61
Task/Arithmetic-Complex/C/arithmetic-complex-2.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
typedef struct{
|
||||
double real;
|
||||
double imag;
|
||||
} Complex;
|
||||
|
||||
Complex add(Complex a, Complex b){
|
||||
Complex ans;
|
||||
ans.real = a.real + b.real;
|
||||
ans.imag = a.imag + b.imag;
|
||||
return ans;
|
||||
}
|
||||
|
||||
Complex mult(Complex a, Complex b){
|
||||
Complex ans;
|
||||
ans.real = a.real * b.real - a.imag * b.imag;
|
||||
ans.imag = a.real * b.imag + a.imag * b.real;
|
||||
return ans;
|
||||
}
|
||||
|
||||
/* it's arguable that things could be better handled if either
|
||||
a.real or a.imag is +/-inf, but that's much work */
|
||||
Complex inv(Complex a){
|
||||
Complex ans;
|
||||
double denom = a.real * a.real + a.imag * a.imag;
|
||||
ans.real = a.real / denom;
|
||||
ans.imag = -a.imag / denom;
|
||||
return ans;
|
||||
}
|
||||
|
||||
Complex neg(Complex a){
|
||||
Complex ans;
|
||||
ans.real = -a.real;
|
||||
ans.imag = -a.imag;
|
||||
return ans;
|
||||
}
|
||||
|
||||
Complex conj(Complex a){
|
||||
Complex ans;
|
||||
ans.real = a.real;
|
||||
ans.imag = -a.imag;
|
||||
return ans;
|
||||
}
|
||||
|
||||
void put(Complex c)
|
||||
{
|
||||
printf("%lf%+lfI", c.real, c.imag);
|
||||
}
|
||||
|
||||
void complex_ops(void)
|
||||
{
|
||||
Complex a = { 1.0, 1.0 };
|
||||
Complex b = { 3.14159, 1.2 };
|
||||
|
||||
printf("\na="); put(a);
|
||||
printf("\nb="); put(b);
|
||||
printf("\na+b="); put(add(a,b));
|
||||
printf("\na*b="); put(mult(a,b));
|
||||
printf("\n1/a="); put(inv(a));
|
||||
printf("\n-a="); put(neg(a));
|
||||
printf("\nconj a="); put(conj(a)); printf("\n");
|
||||
}
|
||||
14
Task/Arithmetic-Complex/COBOL/arithmetic-complex-1.cobol
Normal file
14
Task/Arithmetic-Complex/COBOL/arithmetic-complex-1.cobol
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
$SET SOURCEFORMAT "FREE"
|
||||
$SET ILUSING "System"
|
||||
$SET ILUSING "System.Numerics"
|
||||
class-id Prog.
|
||||
method-id. Main static.
|
||||
procedure division.
|
||||
declare num as type Complex = type Complex::ImaginaryOne()
|
||||
declare results as type Complex occurs any
|
||||
set content of results to ((num + num), (num * num), (- num), (1 / num), type Complex::Conjugate(num))
|
||||
perform varying result as type Complex thru results
|
||||
display result
|
||||
end-perform
|
||||
end method.
|
||||
end class.
|
||||
98
Task/Arithmetic-Complex/COBOL/arithmetic-complex-2.cobol
Normal file
98
Task/Arithmetic-Complex/COBOL/arithmetic-complex-2.cobol
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
$SET SOURCEFORMAT "FREE"
|
||||
class-id Prog.
|
||||
method-id. Main static.
|
||||
procedure division.
|
||||
declare a as type Complex = new Complex(1, 1)
|
||||
declare b as type Complex = new Complex(3.14159, 1.25)
|
||||
|
||||
display "a = " a
|
||||
display "b = " b
|
||||
display space
|
||||
|
||||
declare result as type Complex = a + b
|
||||
display "a + b = " result
|
||||
move (a - b) to result
|
||||
display "a - b = " result
|
||||
move (a * b) to result
|
||||
display "a * b = " result
|
||||
move (a / b) to result
|
||||
display "a / b = " result
|
||||
move (- b) to result
|
||||
display "-b = " result
|
||||
display space
|
||||
|
||||
display "Inverse of b: " type Complex::Inverse(b)
|
||||
display "Conjugate of b: " type Complex::Conjugate(b)
|
||||
end method.
|
||||
end class.
|
||||
|
||||
class-id Complex.
|
||||
|
||||
01 Real float-long property.
|
||||
01 Imag float-long property.
|
||||
|
||||
method-id new.
|
||||
set Real, Imag to 0
|
||||
end method.
|
||||
|
||||
method-id new.
|
||||
procedure division using value real-val as float-long, imag-val as float-long.
|
||||
set Real to real-val
|
||||
set Imag to imag-val
|
||||
end method.
|
||||
|
||||
method-id Norm static.
|
||||
procedure division using value a as type Complex returning ret as float-long.
|
||||
compute ret = a::Real ** 2 + a::Imag ** 2
|
||||
end method.
|
||||
|
||||
method-id Inverse static.
|
||||
procedure division using value a as type Complex returning ret as type Complex.
|
||||
declare norm as float-long = type Complex::Norm(a)
|
||||
set ret to new Complex(a::Real / norm, (0 - a::Imag) / norm)
|
||||
end method.
|
||||
|
||||
method-id Conjugate static.
|
||||
procedure division using value a as type Complex returning c as type Complex.
|
||||
set c to new Complex(a::Real, 0 - a::Imag)
|
||||
end method.
|
||||
|
||||
method-id ToString override.
|
||||
procedure division returning str as string.
|
||||
set str to type String::Format("{0}{1:+#0;-#}i", Real, Imag)
|
||||
end method.
|
||||
|
||||
operator-id + .
|
||||
procedure division using value a as type Complex, b as type Complex
|
||||
returning c as type Complex.
|
||||
set c to new Complex(a::Real + b::Real, a::Imag + b::Imag)
|
||||
end operator.
|
||||
|
||||
operator-id - .
|
||||
procedure division using value a as type Complex, b as type Complex
|
||||
returning c as type Complex.
|
||||
set c to new Complex(a::Real - b::Real, a::Imag - b::Imag)
|
||||
end operator.
|
||||
|
||||
operator-id * .
|
||||
procedure division using value a as type Complex, b as type Complex
|
||||
returning c as type Complex.
|
||||
set c to new Complex(a::Real * b::Real - a::Imag * b::Imag,
|
||||
a::Real * b::Imag + a::Imag * b::Real)
|
||||
end operator.
|
||||
|
||||
operator-id / .
|
||||
procedure division using value a as type Complex, b as type Complex
|
||||
returning c as type Complex.
|
||||
set c to new Complex()
|
||||
declare b-norm as float-long = type Complex::Norm(b)
|
||||
compute c::Real = (a::Real * b::Real + a::Imag * b::Imag) / b-norm
|
||||
compute c::Imag = (a::Imag * b::Real - a::Real * b::Imag) / b-norm
|
||||
end operator.
|
||||
|
||||
operator-id - .
|
||||
procedure division using value a as type Complex returning ret as type Complex.
|
||||
set ret to new Complex(- a::Real, 0 - a::Imag)
|
||||
end operator.
|
||||
|
||||
end class.
|
||||
37
Task/Arithmetic-Complex/Clojure/arithmetic-complex.clj
Normal file
37
Task/Arithmetic-Complex/Clojure/arithmetic-complex.clj
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
(ns rosettacode.arithmetic.cmplx
|
||||
(:require [clojure.algo.generic.arithmetic :as ga])
|
||||
(:import [java.lang Number]))
|
||||
|
||||
(defrecord Complex [^Number r ^Number i]
|
||||
Object
|
||||
(toString [{:keys [r i]}]
|
||||
(apply str
|
||||
(cond
|
||||
(zero? r) [(if (= i 1) "" i) "i"]
|
||||
(zero? i) [r]
|
||||
:else [r (if (neg? i) "-" "+") i "i"]))))
|
||||
|
||||
(defmethod ga/+ [Complex Complex]
|
||||
[x y] (map->Complex (merge-with + x y)))
|
||||
|
||||
(defmethod ga/+ [Complex Number] ; reals become y + 0i
|
||||
[{:keys [r i]} y] (->Complex (+ r y) i))
|
||||
|
||||
(defmethod ga/- Complex
|
||||
[x] (->> x vals (map -) (apply ->Complex)))
|
||||
|
||||
(defmethod ga/* [Complex Complex]
|
||||
[x y] (map->Complex (merge-with * x y)))
|
||||
|
||||
(defmethod ga/* [Complex Number]
|
||||
[{:keys [r i]} y] (->Complex (* r y) (* i y)))
|
||||
|
||||
(ga/defmethod* ga / Complex
|
||||
[x] (->> x vals (map /) (apply ->Complex)))
|
||||
|
||||
(defn conj [^Complex {:keys [r i]}]
|
||||
(->Complex r (- i)))
|
||||
|
||||
(defn inv [^Complex {:keys [r i]}]
|
||||
(let [m (+ (* r r) (* i i))]
|
||||
(->Complex (/ r m) (- (/ i m)))))
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
# create an immutable Complex type
|
||||
class Complex
|
||||
constructor: (@r=0, @i=0) ->
|
||||
@magnitude = @r*@r + @i*@i
|
||||
|
||||
plus: (c2) ->
|
||||
new Complex(
|
||||
@r + c2.r,
|
||||
@i + c2.i
|
||||
)
|
||||
|
||||
times: (c2) ->
|
||||
new Complex(
|
||||
@r*c2.r - @i*c2.i,
|
||||
@r*c2.i + @i*c2.r
|
||||
)
|
||||
|
||||
negation: ->
|
||||
new Complex(
|
||||
-1 * @r,
|
||||
-1 * @i
|
||||
)
|
||||
|
||||
inverse: ->
|
||||
throw Error "no inverse" if @magnitude is 0
|
||||
new Complex(
|
||||
@r / @magnitude,
|
||||
-1 * @i / @magnitude
|
||||
)
|
||||
|
||||
toString: ->
|
||||
return "#{@r}" if @i == 0
|
||||
return "#{@i}i" if @r == 0
|
||||
if @i > 0
|
||||
"#{@r} + #{@i}i"
|
||||
else
|
||||
"#{@r} - #{-1 * @i}i"
|
||||
|
||||
# test
|
||||
do ->
|
||||
a = new Complex(5, 3)
|
||||
b = new Complex(4, -3)
|
||||
|
||||
sum = a.plus b
|
||||
console.log "(#{a}) + (#{b}) = #{sum}"
|
||||
|
||||
product = a.times b
|
||||
console.log "(#{a}) * (#{b}) = #{product}"
|
||||
|
||||
negation = b.negation()
|
||||
console.log "-1 * (#{b}) = #{negation}"
|
||||
|
||||
diff = a.plus negation
|
||||
console.log "(#{a}) - (#{b}) = #{diff}"
|
||||
|
||||
inverse = b.inverse()
|
||||
console.log "1 / (#{b}) = #{inverse}"
|
||||
|
||||
quotient = product.times inverse
|
||||
console.log "(#{product}) / (#{b}) = #{quotient}"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
> (sqrt -1)
|
||||
#C(0.0 1.0)
|
||||
|
||||
> (expt #c(0 1) 2)
|
||||
-1
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
> (+ #c(0 1) #c(1 0))
|
||||
#C(1 1)
|
||||
|
||||
> (* #c(1 1) 2)
|
||||
#C(2 2)
|
||||
|
||||
> (* #c(1 1) #c(0 2))
|
||||
#C(-2 2)
|
||||
|
||||
> (- #c(1 1))
|
||||
#C(-1 -1)
|
||||
|
||||
> (/ #c(0 2))
|
||||
#C(0 -1/2)
|
||||
|
||||
> (conjugate #c(1 1))
|
||||
#C(1 -1)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
> (complex 64 (/ 3 4))
|
||||
#C(64 3/4)
|
||||
|
||||
> (realpart #c(5 5))
|
||||
5
|
||||
|
||||
> (imagpart (complex 0 pi))
|
||||
3.141592653589793d0
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
MODULE Complex;
|
||||
IMPORT StdLog;
|
||||
TYPE
|
||||
Complex* = POINTER TO ComplexDesc;
|
||||
ComplexDesc = RECORD
|
||||
r-,i-: REAL;
|
||||
END;
|
||||
|
||||
VAR
|
||||
r,x,y: Complex;
|
||||
|
||||
PROCEDURE New(x,y: REAL): Complex;
|
||||
VAR
|
||||
r: Complex;
|
||||
BEGIN
|
||||
NEW(r);r.r := x;r.i := y;
|
||||
RETURN r
|
||||
END New;
|
||||
|
||||
PROCEDURE (x: Complex) Add*(y: Complex): Complex,NEW;
|
||||
BEGIN
|
||||
RETURN New(x.r + y.r,x.i + y.i)
|
||||
END Add;
|
||||
|
||||
PROCEDURE ( x: Complex) Sub*( y: Complex): Complex, NEW;
|
||||
BEGIN
|
||||
RETURN New(x.r - y.r,x.i - y.i)
|
||||
END Sub;
|
||||
|
||||
PROCEDURE ( x: Complex) Mul*( y: Complex): Complex, NEW;
|
||||
BEGIN
|
||||
RETURN New(x.r*y.r - x.i*y.i,x.r*y.i + x.i*y.r)
|
||||
END Mul;
|
||||
|
||||
PROCEDURE ( x: Complex) Div*( y: Complex): Complex, NEW;
|
||||
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 (x: Complex) Rec*(): Complex,NEW;
|
||||
VAR
|
||||
d: REAL;
|
||||
BEGIN
|
||||
d := x.r * x.r + x.i * x.i;
|
||||
RETURN New(x.r/d,(-1.0 * x.i)/d);
|
||||
END Rec;
|
||||
|
||||
(* Conjugate *)
|
||||
PROCEDURE (x: Complex) Con*(): Complex,NEW;
|
||||
BEGIN
|
||||
RETURN New(x.r, (-1.0) * x.i);
|
||||
END Con;
|
||||
|
||||
PROCEDURE (x: Complex) Out(),NEW;
|
||||
BEGIN
|
||||
StdLog.String("Complex(");
|
||||
StdLog.Real(x.r);StdLog.String(',');StdLog.Real(x.i);
|
||||
StdLog.String("i );")
|
||||
END Out;
|
||||
|
||||
PROCEDURE Do*;
|
||||
BEGIN
|
||||
x := New(1.5,3);
|
||||
y := New(1.0,1.0);
|
||||
|
||||
StdLog.String("x: ");x.Out();StdLog.Ln;
|
||||
StdLog.String("y: ");y.Out();StdLog.Ln;
|
||||
r := x.Add(y);
|
||||
StdLog.String("x + y: ");r.Out();StdLog.Ln;
|
||||
r := x.Sub(y);
|
||||
StdLog.String("x - y: ");r.Out();StdLog.Ln;
|
||||
r := x.Mul(y);
|
||||
StdLog.String("x * y: ");r.Out();StdLog.Ln;
|
||||
r := x.Div(y);
|
||||
StdLog.String("x / y: ");r.Out();StdLog.Ln;
|
||||
r := y.Rec();
|
||||
StdLog.String("1 / y: ");r.Out();StdLog.Ln;
|
||||
r := x.Con();
|
||||
StdLog.String("x': ");r.Out();StdLog.Ln;
|
||||
END Do;
|
||||
|
||||
END Complex.
|
||||
11
Task/Arithmetic-Complex/D/arithmetic-complex.d
Normal file
11
Task/Arithmetic-Complex/D/arithmetic-complex.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std.stdio, std.complex;
|
||||
|
||||
void main() {
|
||||
auto x = complex(1, 1); // complex of doubles on default
|
||||
auto y = complex(3.14159, 1.2);
|
||||
|
||||
writeln(x + y); // addition
|
||||
writeln(x * y); // multiplication
|
||||
writeln(1.0 / x); // inversion
|
||||
writeln(-x); // negation
|
||||
}
|
||||
50
Task/Arithmetic-Complex/Dart/arithmetic-complex.dart
Normal file
50
Task/Arithmetic-Complex/Dart/arithmetic-complex.dart
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
class complex {
|
||||
|
||||
num real=0;
|
||||
num imag=0;
|
||||
|
||||
complex(num r,num i){
|
||||
this.real=r;
|
||||
this.imag=i;
|
||||
}
|
||||
|
||||
|
||||
complex add(complex b){
|
||||
return new complex(this.real + b.real, this.imag + b.imag);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
complex inv(){
|
||||
//1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
|
||||
num denom = real * real + imag * imag;
|
||||
double r =real/denom;
|
||||
double i= -imag/denom;
|
||||
return new complex( r,-i);
|
||||
}
|
||||
|
||||
complex neg(){
|
||||
return new complex(-real, -imag);
|
||||
}
|
||||
|
||||
complex conj(){
|
||||
return new complex(real, -imag);
|
||||
}
|
||||
|
||||
|
||||
String toString(){
|
||||
return this.real.toString()+' + '+ this.imag.toString()+'*i';
|
||||
}
|
||||
}
|
||||
void main() {
|
||||
var cl= new complex(1,2);
|
||||
var cl2= new complex(3,-1);
|
||||
print(cl.toString());
|
||||
print(cl2.toString());
|
||||
print(cl.inv().toString());
|
||||
print(cl2.mult(cl).toString());
|
||||
|
||||
}
|
||||
27
Task/Arithmetic-Complex/Delphi/arithmetic-complex.delphi
Normal file
27
Task/Arithmetic-Complex/Delphi/arithmetic-complex.delphi
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
program Arithmetic_Complex;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.VarCmplx;
|
||||
|
||||
var
|
||||
a, b: Variant;
|
||||
|
||||
begin
|
||||
a := VarComplexCreate(5, 3);
|
||||
b := VarComplexCreate(0.5, 6.0);
|
||||
|
||||
writeln(format('(%s) + (%s) = %s',[a,b, a+b]));
|
||||
|
||||
writeln(format('(%s) * (%s) = %s',[a,b, a*b]));
|
||||
|
||||
writeln(format('-(%s) = %s',[a,- a]));
|
||||
|
||||
writeln(format('1/(%s) = %s',[a,1/a]));
|
||||
|
||||
writeln(format('conj(%s) = %s',[a,VarComplexConjugate(a)]));
|
||||
|
||||
Readln;
|
||||
end.
|
||||
48
Task/Arithmetic-Complex/ERRE/arithmetic-complex.erre
Normal file
48
Task/Arithmetic-Complex/ERRE/arithmetic-complex.erre
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
PROGRAM COMPLEX_ARITH
|
||||
|
||||
TYPE COMPLEX=(REAL#,IMAG#)
|
||||
|
||||
DIM X:COMPLEX,Y:COMPLEX,Z:COMPLEX
|
||||
|
||||
!
|
||||
! complex arithmetic routines
|
||||
!
|
||||
DIM A:COMPLEX,B:COMPLEX,C:COMPLEX
|
||||
|
||||
PROCEDURE ADD(A.,B.->C.)
|
||||
C.REAL#=A.REAL#+B.REAL#
|
||||
C.IMAG#=A.IMAG#+B.IMAG#
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE INV(A.->B.)
|
||||
LOCAL DENOM#
|
||||
DENOM#=A.REAL#^2+A.IMAG#^2
|
||||
B.REAL#=A.REAL#/DENOM#
|
||||
B.IMAG#=-A.IMAG#/DENOM#
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE MULT(A.,B.->C.)
|
||||
C.REAL#=A.REAL#*B.REAL#-A.IMAG#*B.IMAG#
|
||||
C.IMAG#=A.REAL#*B.IMAG#+A.IMAG#*B.REAL#
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE NEG(A.->B.)
|
||||
B.REAL#=-A.REAL#
|
||||
B.IMAG#=-A.IMAG#
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
PRINT(CHR$(12);) !CLS
|
||||
X.REAL#=1
|
||||
X.IMAG#=1
|
||||
Y.REAL#=2
|
||||
Y.IMAG#=2
|
||||
ADD(X.,Y.->Z.)
|
||||
PRINT(Z.REAL#;" + ";Z.IMAG#;"i")
|
||||
MULT(X.,Y.->Z.)
|
||||
PRINT(Z.REAL#;" + ";Z.IMAG#;"i")
|
||||
INV(X.->Z.)
|
||||
PRINT(Z.REAL#;" + ";Z.IMAG#;"i")
|
||||
NEG(X.->Z.)
|
||||
PRINT(Z.REAL#;" + ";Z.IMAG#;"i")
|
||||
END PROGRAM
|
||||
10
Task/Arithmetic-Complex/EchoLisp/arithmetic-complex.l
Normal file
10
Task/Arithmetic-Complex/EchoLisp/arithmetic-complex.l
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(define a 42+666i) → a
|
||||
(define b 1+i) → b
|
||||
(- a) → -42-666i ; negate
|
||||
(+ a b) → 43+667i ; add
|
||||
(* a b) → -624+708i ; multiply
|
||||
(/ b) → 0.5-0.5i ; invert
|
||||
(conjugate b) → 1-i
|
||||
(angle b) → 0.7853981633974483 ; = PI/4
|
||||
(magnitude b) → 1.4142135623730951 ; = sqrt(2)
|
||||
(exp (* I PI)) → -1+0i ; Euler = e^(I*PI) = -1
|
||||
85
Task/Arithmetic-Complex/Elixir/arithmetic-complex.elixir
Normal file
85
Task/Arithmetic-Complex/Elixir/arithmetic-complex.elixir
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
defmodule Complex do
|
||||
import Kernel, except: [abs: 1, div: 2]
|
||||
|
||||
defstruct real: 0, imag: 0
|
||||
|
||||
def new(real, imag) do
|
||||
%__MODULE__{real: real, imag: imag}
|
||||
end
|
||||
|
||||
def add(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
new(a.real + b.real, a.imag + b.imag)
|
||||
end
|
||||
|
||||
def sub(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
new(a.real - b.real, a.imag - b.imag)
|
||||
end
|
||||
|
||||
def mul(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
new(a.real*b.real - a.imag*b.imag, a.imag*b.real + a.real*b.imag)
|
||||
end
|
||||
|
||||
def div(a, b) do
|
||||
{a, b} = convert(a, b)
|
||||
divisor = abs2(b)
|
||||
new((a.real*b.real + a.imag*b.imag) / divisor,
|
||||
(a.imag*b.real - a.real*b.imag) / divisor)
|
||||
end
|
||||
|
||||
def neg(a) do
|
||||
a = convert(a)
|
||||
new(-a.real, -a.imag)
|
||||
end
|
||||
|
||||
def inv(a) do
|
||||
a = convert(a)
|
||||
divisor = abs2(a)
|
||||
new(a.real / divisor, -a.imag / divisor)
|
||||
end
|
||||
|
||||
def conj(a) do
|
||||
a = convert(a)
|
||||
new(a.real, -a.imag)
|
||||
end
|
||||
|
||||
def abs(a) do
|
||||
:math.sqrt(abs2(a))
|
||||
end
|
||||
|
||||
defp abs2(a) do
|
||||
a = convert(a)
|
||||
a.real*a.real + a.imag*a.imag
|
||||
end
|
||||
|
||||
defp convert(a) when is_number(a), do: new(a, 0)
|
||||
defp convert(%__MODULE__{} = a), do: a
|
||||
|
||||
defp convert(a, b), do: {convert(a), convert(b)}
|
||||
|
||||
def task do
|
||||
a = new(1, 3)
|
||||
b = new(5, 2)
|
||||
IO.puts "a = #{a}"
|
||||
IO.puts "b = #{b}"
|
||||
IO.puts "add(a,b): #{add(a, b)}"
|
||||
IO.puts "sub(a,b): #{sub(a, b)}"
|
||||
IO.puts "mul(a,b): #{mul(a, b)}"
|
||||
IO.puts "div(a,b): #{div(a, b)}"
|
||||
IO.puts "div(b,a): #{div(b, a)}"
|
||||
IO.puts "neg(a) : #{neg(a)}"
|
||||
IO.puts "inv(a) : #{inv(a)}"
|
||||
IO.puts "conj(a) : #{conj(a)}"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Complex do
|
||||
def to_string(%Complex{real: real, imag: imag}) do
|
||||
if imag >= 0, do: "#{real}+#{imag}j",
|
||||
else: "#{real}#{imag}j"
|
||||
end
|
||||
end
|
||||
|
||||
Complex.task
|
||||
58
Task/Arithmetic-Complex/Erlang/arithmetic-complex-1.erl
Normal file
58
Task/Arithmetic-Complex/Erlang/arithmetic-complex-1.erl
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
%% Task: Complex Arithmetic
|
||||
%% Author: Abhay Jain
|
||||
|
||||
-module(complex_number).
|
||||
-export([calculate/0]).
|
||||
|
||||
-record(complex, {real, img}).
|
||||
|
||||
calculate() ->
|
||||
A = #complex{real=1, img=3},
|
||||
B = #complex{real=5, img=2},
|
||||
|
||||
Sum = add (A, B),
|
||||
print (Sum),
|
||||
|
||||
Product = multiply (A, B),
|
||||
print (Product),
|
||||
|
||||
Negation = negation (A),
|
||||
print (Negation),
|
||||
|
||||
Inversion = inverse (A),
|
||||
print (Inversion),
|
||||
|
||||
Conjugate = conjugate (A),
|
||||
print (Conjugate).
|
||||
|
||||
add (A, B) ->
|
||||
RealPart = A#complex.real + B#complex.real,
|
||||
ImgPart = A#complex.img + B#complex.img,
|
||||
#complex{real=RealPart, img=ImgPart}.
|
||||
|
||||
multiply (A, B) ->
|
||||
RealPart = (A#complex.real * B#complex.real) - (A#complex.img * B#complex.img),
|
||||
ImgPart = (A#complex.real * B#complex.img) + (B#complex.real * A#complex.img),
|
||||
#complex{real=RealPart, img=ImgPart}.
|
||||
|
||||
negation (A) ->
|
||||
#complex{real=-A#complex.real, img=-A#complex.img}.
|
||||
|
||||
inverse (A) ->
|
||||
C = conjugate (A),
|
||||
Mod = (A#complex.real * A#complex.real) + (A#complex.img * A#complex.img),
|
||||
RealPart = C#complex.real / Mod,
|
||||
ImgPart = C#complex.img / Mod,
|
||||
#complex{real=RealPart, img=ImgPart}.
|
||||
|
||||
conjugate (A) ->
|
||||
RealPart = A#complex.real,
|
||||
ImgPart = -A#complex.img,
|
||||
#complex{real=RealPart, img=ImgPart}.
|
||||
|
||||
print (A) ->
|
||||
if A#complex.img < 0 ->
|
||||
io:format("Ans = ~p~pi~n", [A#complex.real, A#complex.img]);
|
||||
true ->
|
||||
io:format("Ans = ~p+~pi~n", [A#complex.real, A#complex.img])
|
||||
end.
|
||||
5
Task/Arithmetic-Complex/Erlang/arithmetic-complex-2.erl
Normal file
5
Task/Arithmetic-Complex/Erlang/arithmetic-complex-2.erl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Ans = 6+5i
|
||||
Ans = -1+17i
|
||||
Ans = -1-3i
|
||||
Ans = 0.1-0.3i
|
||||
Ans = 1-3i
|
||||
13
Task/Arithmetic-Complex/Euler/arithmetic-complex.euler
Normal file
13
Task/Arithmetic-Complex/Euler/arithmetic-complex.euler
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
>a=1+4i; b=5-3i;
|
||||
>a+b
|
||||
6+1i
|
||||
>a-b
|
||||
-4+7i
|
||||
>a*b
|
||||
17+17i
|
||||
>a/b
|
||||
-0.205882352941+0.676470588235i
|
||||
>fraction a/b
|
||||
-7/34+23/34i
|
||||
>conj(a)
|
||||
1-4i
|
||||
58
Task/Arithmetic-Complex/Euphoria/arithmetic-complex.euphoria
Normal file
58
Task/Arithmetic-Complex/Euphoria/arithmetic-complex.euphoria
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
constant REAL = 1, IMAG = 2
|
||||
type complex(sequence s)
|
||||
return length(s) = 2 and atom(s[REAL]) and atom(s[IMAG])
|
||||
end type
|
||||
|
||||
function add(complex a, complex b)
|
||||
return a + b
|
||||
end function
|
||||
|
||||
function mult(complex a, complex b)
|
||||
return {a[REAL] * b[REAL] - a[IMAG] * b[IMAG],
|
||||
a[REAL] * b[IMAG] + a[IMAG] * b[REAL]}
|
||||
end function
|
||||
|
||||
function inv(complex a)
|
||||
atom denom
|
||||
denom = a[REAL] * a[REAL] + a[IMAG] * a[IMAG]
|
||||
return {a[REAL] / denom, -a[IMAG] / denom}
|
||||
end function
|
||||
|
||||
function neg(complex a)
|
||||
return -a
|
||||
end function
|
||||
|
||||
function scomplex(complex a)
|
||||
sequence s
|
||||
if a[REAL] != 0 then
|
||||
s = sprintf("%g",a)
|
||||
else
|
||||
s = {}
|
||||
end if
|
||||
|
||||
if a[IMAG] != 0 then
|
||||
if a[IMAG] = 1 then
|
||||
s &= "+i"
|
||||
elsif a[IMAG] = -1 then
|
||||
s &= "-i"
|
||||
else
|
||||
s &= sprintf("%+gi",a[IMAG])
|
||||
end if
|
||||
end if
|
||||
|
||||
if length(s) = 0 then
|
||||
return "0"
|
||||
else
|
||||
return s
|
||||
end if
|
||||
end function
|
||||
|
||||
complex a, b
|
||||
a = { 1.0, 1.0 }
|
||||
b = { 3.14159, 1.2 }
|
||||
printf(1,"a = %s\n",{scomplex(a)})
|
||||
printf(1,"b = %s\n",{scomplex(b)})
|
||||
printf(1,"a+b = %s\n",{scomplex(add(a,b))})
|
||||
printf(1,"a*b = %s\n",{scomplex(mult(a,b))})
|
||||
printf(1,"1/a = %s\n",{scomplex(inv(a))})
|
||||
printf(1,"-a = %s\n",{scomplex(neg(a))})
|
||||
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-1.excel
Normal file
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-1.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=IMSUM(A1;B1)
|
||||
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-2.excel
Normal file
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-2.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=IMPRODUCT(A1;B1)
|
||||
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-3.excel
Normal file
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-3.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=IMSUB(0;D1)
|
||||
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-4.excel
Normal file
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-4.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=IMDIV(1;E28)
|
||||
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-5.excel
Normal file
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-5.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
=IMCONJUGATE(C28)
|
||||
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-6.excel
Normal file
1
Task/Arithmetic-Complex/Excel/arithmetic-complex-6.excel
Normal file
|
|
@ -0,0 +1 @@
|
|||
1+2i 3+5i 4+7i -7+11i 7-11i 0,0411764705882353+0,0647058823529412i 4-7i
|
||||
45
Task/Arithmetic-Complex/F-Sharp/arithmetic-complex.fs
Normal file
45
Task/Arithmetic-Complex/F-Sharp/arithmetic-complex.fs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
> open Microsoft.FSharp.Math;;
|
||||
|
||||
> let a = complex 1.0 1.0;;
|
||||
val a : complex = 1r+1i
|
||||
|
||||
> let b = complex 3.14159 1.25;;
|
||||
val b : complex = 3.14159r+1.25i
|
||||
|
||||
> a + b;;
|
||||
val it : Complex = 4.14159r+2.25i {Conjugate = 4.14159r-2.25i;
|
||||
ImaginaryPart = 2.25;
|
||||
Magnitude = 4.713307515;
|
||||
Phase = 0.497661247;
|
||||
RealPart = 4.14159;
|
||||
i = 2.25;
|
||||
r = 4.14159;}
|
||||
|
||||
> a * b;;
|
||||
val it : Complex = 1.89159r+4.39159i {Conjugate = 1.89159r-4.39159i;
|
||||
ImaginaryPart = 4.39159;
|
||||
Magnitude = 4.781649868;
|
||||
Phase = 1.164082262;
|
||||
RealPart = 1.89159;
|
||||
i = 4.39159;
|
||||
r = 1.89159;}
|
||||
|
||||
> a / b;;
|
||||
val it : Complex =
|
||||
0.384145932435901r+0.165463215905043i
|
||||
{Conjugate = 0.384145932435901r-0.165463215905043i;
|
||||
ImaginaryPart = 0.1654632159;
|
||||
Magnitude = 0.418265673;
|
||||
Phase = 0.4067140652;
|
||||
RealPart = 0.3841459324;
|
||||
i = 0.1654632159;
|
||||
r = 0.3841459324;}
|
||||
|
||||
> -a;;
|
||||
val it : complex = -1r-1i {Conjugate = -1r+1i;
|
||||
ImaginaryPart = -1.0;
|
||||
Magnitude = 1.414213562;
|
||||
Phase = -2.35619449;
|
||||
RealPart = -1.0;
|
||||
i = -1.0;
|
||||
r = -1.0;}
|
||||
18
Task/Arithmetic-Complex/Factor/arithmetic-complex.factor
Normal file
18
Task/Arithmetic-Complex/Factor/arithmetic-complex.factor
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
USING: combinators kernel math math.functions prettyprint ;
|
||||
|
||||
C{ 1 2 } C{ 0.9 -2.78 } {
|
||||
[ + . ] ! addition
|
||||
[ - . ] ! subtraction
|
||||
[ * . ] ! multiplication
|
||||
[ / . ] ! division
|
||||
[ ^ . ] ! power
|
||||
} 2cleave
|
||||
|
||||
C{ 1 2 } {
|
||||
[ neg . ] ! negation
|
||||
[ recip . ] ! multiplicative inverse
|
||||
[ conjugate . ] ! complex conjugate
|
||||
[ sin . ] ! sine
|
||||
[ log . ] ! natural logarithm
|
||||
[ sqrt . ] ! square root
|
||||
} cleave
|
||||
13
Task/Arithmetic-Complex/Forth/arithmetic-complex.fth
Normal file
13
Task/Arithmetic-Complex/Forth/arithmetic-complex.fth
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
S" fsl-util.fs" REQUIRED
|
||||
S" complex.fs" REQUIRED
|
||||
|
||||
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.
|
||||
1e 0e zconstant 1+0i
|
||||
1+0i x z@ z/ z.
|
||||
x z@ znegate z.
|
||||
9
Task/Arithmetic-Complex/Fortran/arithmetic-complex-1.f
Normal file
9
Task/Arithmetic-Complex/Fortran/arithmetic-complex-1.f
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
program cdemo
|
||||
complex :: a = (5,3), b = (0.5, 6.0) ! complex initializer
|
||||
complex :: absum, abprod, aneg, ainv
|
||||
|
||||
absum = a + b
|
||||
abprod = a * b
|
||||
aneg = -a
|
||||
ainv = 1.0 / a
|
||||
end program cdemo
|
||||
28
Task/Arithmetic-Complex/Fortran/arithmetic-complex-2.f
Normal file
28
Task/Arithmetic-Complex/Fortran/arithmetic-complex-2.f
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
program cdemo2
|
||||
complex :: a = (5,3), b = (0.5, 6) ! complex initializer
|
||||
real, parameter :: pi = 3.141592653589793 ! The constant "pi"
|
||||
complex, parameter :: i = (0, 1) ! the imaginary unit "i" (sqrt(-1))
|
||||
complex :: abdiff, abquot, abpow, aconj, p2cart, newc
|
||||
real :: areal, aimag, anorm, rho = 10, theta = pi / 3.0, x = 2.3, y = 3.0
|
||||
integer, parameter :: n = 50
|
||||
integer :: j
|
||||
complex, dimension(0:n-1) :: unit_circle
|
||||
|
||||
abdiff = a - b
|
||||
abquot = a / b
|
||||
abpow = a ** b
|
||||
areal = real(a) ! Real part
|
||||
aimag = imag(a) ! Imaginary part. Function imag(a) is possibly not recognised. Use aimag(a) if so.
|
||||
newc = cmplx(x,y) ! Creating a complex on the fly from two reals intrinsically
|
||||
! (initializer only works in declarations)
|
||||
newc = x + y*i ! Creating a complex on the fly from two reals arithmetically
|
||||
anorm = abs(a) ! Complex norm (or "modulus" or "absolute value")
|
||||
! (use CABS before Fortran 90)
|
||||
aconj = conjg(a) ! Complex conjugate (same as real(a) - i*imag(a))
|
||||
p2cart = rho * exp(i * theta) ! Euler's polar complex notation to cartesian complex notation
|
||||
! conversion (use CEXP before Fortran 90)
|
||||
|
||||
! The following creates an array of N evenly spaced points around the complex unit circle
|
||||
! useful for FFT calculations, among other things
|
||||
unit_circle = exp(2*i*pi/n * (/ (j, j=0, n-1) /) )
|
||||
end program cdemo2
|
||||
28
Task/Arithmetic-Complex/Free-Pascal/arithmetic-complex.pas
Normal file
28
Task/Arithmetic-Complex/Free-Pascal/arithmetic-complex.pas
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
Program ComplexDemo;
|
||||
|
||||
uses
|
||||
ucomplex;
|
||||
|
||||
var
|
||||
a, b, absum, abprod, aneg, ainv, acong: complex;
|
||||
|
||||
function complex(const re, im: real): ucomplex.complex; overload;
|
||||
begin
|
||||
complex.re := re;
|
||||
complex.im := im;
|
||||
end;
|
||||
|
||||
begin
|
||||
a := complex(5, 3);
|
||||
b := complex(0.5, 6.0);
|
||||
absum := a + b;
|
||||
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
|
||||
abprod := a * b;
|
||||
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
|
||||
aneg := -a;
|
||||
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
|
||||
ainv := 1.0 / a;
|
||||
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
|
||||
acong := cong(a);
|
||||
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
|
||||
end.
|
||||
65
Task/Arithmetic-Complex/FreeBASIC/arithmetic-complex.basic
Normal file
65
Task/Arithmetic-Complex/FreeBASIC/arithmetic-complex.basic
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type Complex
|
||||
As Double real, imag
|
||||
Declare Constructor(real As Double, imag As Double)
|
||||
Declare Function invert() As Complex
|
||||
Declare Function conjugate() As Complex
|
||||
Declare Operator cast() As String
|
||||
End Type
|
||||
|
||||
Constructor Complex(real As Double, imag As Double)
|
||||
This.real = real
|
||||
This.imag = imag
|
||||
End Constructor
|
||||
|
||||
Function Complex.invert() As Complex
|
||||
Dim denom As Double = real * real + imag * imag
|
||||
Return Complex(real / denom, -imag / denom)
|
||||
End Function
|
||||
|
||||
Function Complex.conjugate() As Complex
|
||||
Return Complex(real, -imag)
|
||||
End Function
|
||||
|
||||
Operator Complex.Cast() As String
|
||||
If imag >= 0 Then
|
||||
Return Str(real) + "+" + Str(imag) + "j"
|
||||
End If
|
||||
Return Str(real) + Str(imag) + "j"
|
||||
End Operator
|
||||
|
||||
Operator - (c As Complex) As Complex
|
||||
Return Complex(-c.real, -c.imag)
|
||||
End Operator
|
||||
|
||||
Operator + (c1 As Complex, c2 As Complex) As Complex
|
||||
Return Complex(c1.real + c2.real, c1.imag + c2.imag)
|
||||
End Operator
|
||||
|
||||
Operator - (c1 As Complex, c2 As Complex) As Complex
|
||||
Return c1 + (-c2)
|
||||
End Operator
|
||||
|
||||
Operator * (c1 As Complex, c2 As Complex) As Complex
|
||||
Return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c2.real * c1.imag)
|
||||
End Operator
|
||||
|
||||
Operator / (c1 As Complex, c2 As Complex) As Complex
|
||||
Return c1 * c2.invert
|
||||
End Operator
|
||||
|
||||
Var x = Complex(1, 3)
|
||||
Var y = Complex(5, 2)
|
||||
Print "x = "; x
|
||||
Print "y = "; y
|
||||
Print "x + y = "; x + y
|
||||
Print "x - y = "; x - y
|
||||
Print "x * y = "; x * y
|
||||
Print "x / y = "; x / y
|
||||
Print "-x = "; -x
|
||||
Print "1 / x = "; x.invert
|
||||
Print "x* = "; x.conjugate
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
13
Task/Arithmetic-Complex/Frink/arithmetic-complex.frink
Normal file
13
Task/Arithmetic-Complex/Frink/arithmetic-complex.frink
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
add[x,y] := x + y
|
||||
multiply[x,y] := x * y
|
||||
negate[x] := -x
|
||||
invert[x] := 1/x // Could also use inv[x] or recip[x]
|
||||
conjugate[x] := Re[x] - Im[x] i
|
||||
|
||||
a = 3 + 2.5i
|
||||
b = 7.3 - 10i
|
||||
println["$a + $b = " + add[a,b]]
|
||||
println["$a * $b = " + multiply[a,b]]
|
||||
println["-$a = " + negate[a]]
|
||||
println["1/$a = " + invert[a]]
|
||||
println["conjugate[$a] = " + conjugate[a]]
|
||||
27
Task/Arithmetic-Complex/Futhark/arithmetic-complex.futhark
Normal file
27
Task/Arithmetic-Complex/Futhark/arithmetic-complex.futhark
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
type complex = (f64,f64)
|
||||
|
||||
fun complexAdd((a,b): complex) ((c,d): complex): complex =
|
||||
(a + c,
|
||||
b + d)
|
||||
|
||||
fun complexMult((a,b): complex) ((c,d): complex): complex =
|
||||
(a*c - b * d,
|
||||
a*d + b * c)
|
||||
|
||||
fun complexInv((r,i): complex): complex =
|
||||
let denom = r*r + i * i
|
||||
in (r / denom,
|
||||
-i / denom)
|
||||
|
||||
fun complexNeg((r,i): complex): complex =
|
||||
(-r, -i)
|
||||
|
||||
fun complexConj((r,i): complex): complex =
|
||||
(r, -i)
|
||||
|
||||
fun main (o: int) (a: complex) (b: complex): complex =
|
||||
if o == 0 then complexAdd a b
|
||||
else if o == 1 then complexMult a b
|
||||
else if o == 2 then complexInv a
|
||||
else if o == 3 then complexNeg a
|
||||
else complexConj a
|
||||
19
Task/Arithmetic-Complex/GAP/arithmetic-complex.gap
Normal file
19
Task/Arithmetic-Complex/GAP/arithmetic-complex.gap
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# GAP knows gaussian integers, gaussian rationals (i.e. Q[i]), and cyclotomic fields. Here are some examples.
|
||||
# E(n) is an nth primitive root of 1
|
||||
i := Sqrt(-1);
|
||||
# E(4)
|
||||
(3 + 2*i)*(5 - 7*i);
|
||||
# 29-11*E(4)
|
||||
1/i;
|
||||
# -E(4)
|
||||
Sqrt(-3);
|
||||
# E(3)-E(3)^2
|
||||
|
||||
i in GaussianIntegers;
|
||||
# true
|
||||
i/2 in GaussianIntegers;
|
||||
# false
|
||||
i/2 in GaussianRationals;
|
||||
# true
|
||||
Sqrt(-3) in Cyclotomics;
|
||||
# true
|
||||
18
Task/Arithmetic-Complex/Go/arithmetic-complex.go
Normal file
18
Task/Arithmetic-Complex/Go/arithmetic-complex.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/cmplx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a := 1 + 1i
|
||||
b := 3.14159 + 1.25i
|
||||
fmt.Println("a: ", a)
|
||||
fmt.Println("b: ", b)
|
||||
fmt.Println("a + b: ", a+b)
|
||||
fmt.Println("a * b: ", a*b)
|
||||
fmt.Println("-a: ", -a)
|
||||
fmt.Println("1 / a: ", 1/a)
|
||||
fmt.Println("a̅: ", cmplx.Conj(a))
|
||||
}
|
||||
101
Task/Arithmetic-Complex/Groovy/arithmetic-complex-1.groovy
Normal file
101
Task/Arithmetic-Complex/Groovy/arithmetic-complex-1.groovy
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
class Complex {
|
||||
final Number real, imag
|
||||
|
||||
static final Complex i = [0,1] as Complex
|
||||
|
||||
Complex(Number r, Number i = 0) { (real, imag) = [r, i] }
|
||||
|
||||
Complex(Map that) { (real, imag) = [that.real ?: 0, that.imag ?: 0] }
|
||||
|
||||
Complex plus (Complex c) { [real + c.real, imag + c.imag] as Complex }
|
||||
Complex plus (Number n) { [real + n, imag] as Complex }
|
||||
|
||||
Complex minus (Complex c) { [real - c.real, imag - c.imag] as Complex }
|
||||
Complex minus (Number n) { [real - n, imag] as Complex }
|
||||
|
||||
Complex multiply (Complex c) { [real*c.real - imag*c.imag , imag*c.real + real*c.imag] as Complex }
|
||||
Complex multiply (Number n) { [real*n , imag*n] as Complex }
|
||||
|
||||
Complex div (Complex c) { this * c.recip() }
|
||||
Complex div (Number n) { this * (1/n) }
|
||||
|
||||
Complex negative () { [-real, -imag] as Complex }
|
||||
|
||||
/** the complex conjugate of this complex number. Overloads the bitwise complement (~) operator. */
|
||||
Complex bitwiseNegate () { [real, -imag] as Complex }
|
||||
|
||||
/** the magnitude of this complex number. */
|
||||
// could also use Math.sqrt( (this * (~this)).real )
|
||||
Number getAbs() { Math.sqrt( real*real + imag*imag ) }
|
||||
/** the magnitude of this complex number. */
|
||||
Number abs() { this.abs }
|
||||
|
||||
/** the reciprocal of this complex number. */
|
||||
Complex getRecip() { (~this) / (ρ**2) }
|
||||
/** the reciprocal of this complex number. */
|
||||
Complex recip() { this.recip }
|
||||
|
||||
/** derived polar angle θ (theta) for polar form. Normalized to 0 ≤ θ < 2π. */
|
||||
Number getTheta() {
|
||||
def θ = Math.atan2(imag,real)
|
||||
θ = θ < 0 ? θ + 2 * Math.PI : θ
|
||||
}
|
||||
/** derived polar angle θ (theta) for polar form. Normalized to 0 ≤ θ < 2π. */
|
||||
Number getΘ() { this.theta } // this is greek uppercase theta
|
||||
|
||||
/** derived polar magnitude ρ (rho) for polar form. */
|
||||
Number getRho() { this.abs }
|
||||
/** derived polar magnitude ρ (rho) for polar form. */
|
||||
Number getΡ() { this.abs } // this is greek uppercase rho, not roman P
|
||||
|
||||
/** Runs Euler's polar-to-Cartesian complex conversion,
|
||||
* converting [ρ, θ] inputs into a [real, imag]-based complex number */
|
||||
static Complex fromPolar(Number ρ, Number θ) {
|
||||
[ρ * Math.cos(θ), ρ * Math.sin(θ)] as Complex
|
||||
}
|
||||
|
||||
/** Creates new complex with same magnitude ρ, but different angle θ */
|
||||
Complex withTheta(Number θ) { fromPolar(this.rho, θ) }
|
||||
/** Creates new complex with same magnitude ρ, but different angle θ */
|
||||
Complex withΘ(Number θ) { fromPolar(this.rho, θ) }
|
||||
|
||||
/** Creates new complex with same angle θ, but different magnitude ρ */
|
||||
Complex withRho(Number ρ) { fromPolar(ρ, this.θ) }
|
||||
/** Creates new complex with same angle θ, but different magnitude ρ */
|
||||
Complex withΡ(Number ρ) { fromPolar(ρ, this.θ) } // this is greek uppercase rho, not roman P
|
||||
|
||||
static Complex exp(Complex c) { fromPolar(Math.exp(c.real), c.imag) }
|
||||
|
||||
static Complex log(Complex c) { [Math.log(c.rho), c.theta] as Complex }
|
||||
|
||||
Complex power(Complex c) {
|
||||
def zero = [0] as Complex
|
||||
(this == zero && c != zero) \
|
||||
? zero \
|
||||
: c == 1 \
|
||||
? this \
|
||||
: exp( log(this) * c )
|
||||
}
|
||||
|
||||
Complex power(Number n) { this ** ([n, 0] as Complex) }
|
||||
|
||||
boolean equals(that) {
|
||||
that != null && (that instanceof Complex \
|
||||
? [this.real, this.imag] == [that.real, that.imag] \
|
||||
: that instanceof Number && [this.real, this.imag] == [that, 0])
|
||||
}
|
||||
|
||||
int hashCode() { [real, imag].hashCode() }
|
||||
|
||||
String toString() {
|
||||
def realPart = "${real}"
|
||||
def imagPart = imag.abs() == 1 ? "i" : "${imag.abs()}i"
|
||||
real == 0 && imag == 0 \
|
||||
? "0" \
|
||||
: real == 0 \
|
||||
? (imag > 0 ? '' : "-") + imagPart \
|
||||
: imag == 0 \
|
||||
? realPart \
|
||||
: realPart + (imag > 0 ? " + " : " - ") + imagPart
|
||||
}
|
||||
}
|
||||
17
Task/Arithmetic-Complex/Groovy/arithmetic-complex-2.groovy
Normal file
17
Task/Arithmetic-Complex/Groovy/arithmetic-complex-2.groovy
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import org.codehaus.groovy.runtime.DefaultGroovyMethods
|
||||
|
||||
class ComplexCategory {
|
||||
static Complex getI (Number a) { [0, a] as Complex }
|
||||
|
||||
static Complex plus (Number a, Complex b) { b + a }
|
||||
static Complex minus (Number a, Complex b) { -b + a }
|
||||
static Complex multiply (Number a, Complex b) { b * a }
|
||||
static Complex div (Number a, Complex b) { ([a] as Complex) / b }
|
||||
static Complex power (Number a, Complex b) { ([a] as Complex) ** b }
|
||||
|
||||
static <N extends Number,T> T asType (N a, Class<T> type) {
|
||||
type == Complex \
|
||||
? [a as Number] as Complex
|
||||
: DefaultGroovyMethods.asType(a, type)
|
||||
}
|
||||
}
|
||||
64
Task/Arithmetic-Complex/Groovy/arithmetic-complex-3.groovy
Normal file
64
Task/Arithmetic-Complex/Groovy/arithmetic-complex-3.groovy
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import static Complex.*
|
||||
|
||||
Number.metaClass.mixin ComplexCategory
|
||||
Integer.metaClass.mixin ComplexCategory
|
||||
|
||||
def ε = 0.000000001 // tolerance (epsilon): acceptable "wrongness" to account for rounding error
|
||||
|
||||
println 'Demo 1: functionality as requested'
|
||||
def a = [5,3] as Complex
|
||||
def a1 = [real:5, imag:3] as Complex
|
||||
def a2 = 5 + 3.i
|
||||
def a3 = 5 + 3*i
|
||||
assert a == a1 && a == a2 && a == a3
|
||||
println 'a == ' + a
|
||||
def b = [0.5,6] as Complex
|
||||
println 'b == ' + b
|
||||
|
||||
println "a + b == (${a}) + (${b}) == " + (a + b)
|
||||
println "a * b == (${a}) * (${b}) == " + (a * b)
|
||||
assert a + (-a) == 0
|
||||
println "-a == -(${a}) == " + (-a)
|
||||
assert (a * a.recip - 1).abs < ε
|
||||
println "1/a == (${a}).recip == " + (a.recip)
|
||||
println "a * 1/a == " + (a * a.recip)
|
||||
println()
|
||||
|
||||
println 'Demo 2: other functionality not requested, but important for completeness'
|
||||
def c = 10
|
||||
def d = 10 as Complex
|
||||
assert d instanceof Complex && c instanceof Number && d == c
|
||||
assert a + c == c + a
|
||||
println "a + 10 == 10 + a == " + (c + a)
|
||||
assert c - a == -(a - c)
|
||||
println "10 - a == -(a - 10) == " + (c - a)
|
||||
println "a - b == (${a}) - (${b}) == " + (a - b)
|
||||
assert c * a == a * c
|
||||
println "10 * a == a * 10 == " + (c * a)
|
||||
assert (c / a - (a / c).recip).abs < ε
|
||||
println "10 / a == 1 / (a / 10) == " + (c / a)
|
||||
println "a / b == (${a}) / (${b}) == " + (a / b)
|
||||
assert (a ** 2 - a * a).abs < ε
|
||||
println "a ** 2 == a * a == " + (a ** 2)
|
||||
println "0.9 ** b == " + (0.9 ** b)
|
||||
println "a ** b == (${a}) ** (${b}) == " + (a ** b)
|
||||
println 'a.real == ' + a.real
|
||||
println 'a.imag == ' + a.imag
|
||||
println '|a| == ' + a.abs
|
||||
println 'a.rho == ' + a.rho
|
||||
println 'a.ρ == ' + a.ρ
|
||||
println 'a.theta == ' + a.theta
|
||||
println 'a.θ == ' + a.θ
|
||||
println '~a (conjugate) == ' + ~a
|
||||
|
||||
def ρ = 10
|
||||
def π = Math.PI
|
||||
def n = 3
|
||||
def θ = π / n
|
||||
|
||||
def fromPolar1 = fromPolar(ρ, θ) // direct polar-to-cartesian conversion
|
||||
def fromPolar2 = exp(θ.i) * ρ // Euler's equation
|
||||
println "ρ*cos(θ) + i*ρ*sin(θ) == ${ρ}*cos(π/${n}) + i*${ρ}*sin(π/${n})"
|
||||
println " == 10*0.5 + i*10*√(3/4) == " + fromPolar1
|
||||
println "ρ*exp(i*θ) == ${ρ}*exp(i*π/${n}) == " + fromPolar2
|
||||
assert (fromPolar1 - fromPolar2).abs < ε
|
||||
23
Task/Arithmetic-Complex/Hare/arithmetic-complex.hare
Normal file
23
Task/Arithmetic-Complex/Hare/arithmetic-complex.hare
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use fmt;
|
||||
use math::complex::{c128,addc128,mulc128,divc128,negc128,conjc128};
|
||||
|
||||
export fn main() void = {
|
||||
let x: c128 = (1.0, 1.0);
|
||||
let y: c128 = (3.14159265, 1.2);
|
||||
|
||||
// addition
|
||||
let (re, im) = addc128(x, y);
|
||||
fmt::printfln("{} + {}i", re, im)!;
|
||||
// multiplication
|
||||
let (re, im) = mulc128(x, y);
|
||||
fmt::printfln("{} + {}i", re, im)!;
|
||||
// inversion
|
||||
let (re, im) = divc128((1.0, 0.0), x);
|
||||
fmt::printfln("{} + {}i", re, im)!;
|
||||
// negation
|
||||
let (re, im) = negc128(x);
|
||||
fmt::printfln("{} + {}i", re, im)!;
|
||||
// conjugate
|
||||
let (re, im) = conjc128(x);
|
||||
fmt::printfln("{} + {}i", re, im)!;
|
||||
};
|
||||
14
Task/Arithmetic-Complex/Haskell/arithmetic-complex.hs
Normal file
14
Task/Arithmetic-Complex/Haskell/arithmetic-complex.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Data.Complex
|
||||
|
||||
main = do
|
||||
let a = 1.0 :+ 2.0 -- complex number 1+2i
|
||||
let b = 4 -- complex number 4+0i
|
||||
-- 'b' is inferred to be complex because it's used in
|
||||
-- arithmetic with 'a' below.
|
||||
putStrLn $ "Add: " ++ show (a + b)
|
||||
putStrLn $ "Subtract: " ++ show (a - b)
|
||||
putStrLn $ "Multiply: " ++ show (a * b)
|
||||
putStrLn $ "Divide: " ++ show (a / b)
|
||||
putStrLn $ "Negate: " ++ show (-a)
|
||||
putStrLn $ "Inverse: " ++ show (recip a)
|
||||
putStrLn $ "Conjugate:" ++ show (conjugate a)
|
||||
10
Task/Arithmetic-Complex/IDL/arithmetic-complex.idl
Normal file
10
Task/Arithmetic-Complex/IDL/arithmetic-complex.idl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
x=complex(1,1)
|
||||
y=complex(!pi,1.2)
|
||||
print,x+y
|
||||
( 4.14159, 2.20000)
|
||||
print,x*y
|
||||
( 1.94159, 4.34159)
|
||||
print,-x
|
||||
( -1.00000, -1.00000)
|
||||
print,1/x
|
||||
( 0.500000, -0.500000)
|
||||
22
Task/Arithmetic-Complex/Icon/arithmetic-complex-1.icon
Normal file
22
Task/Arithmetic-Complex/Icon/arithmetic-complex-1.icon
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
procedure main()
|
||||
|
||||
SetupComplex()
|
||||
a := complex(1,2)
|
||||
b := complex(3,4)
|
||||
|
||||
c := complex(&pi,1.5)
|
||||
d := complex(1)
|
||||
e := complex(,1)
|
||||
|
||||
every v := !"abcde" do write(v," := ",cpxstr(variable(v)))
|
||||
|
||||
write("a+b := ", cpxstr(cpxadd(a,b)))
|
||||
write("a-b := ", cpxstr(cpxsub(a,b)))
|
||||
write("a*b := ", cpxstr(cpxmul(a,b)))
|
||||
write("a/b := ", cpxstr(cpxdiv(a,b)))
|
||||
write("neg(a) := ", cpxstr(cpxneg(a)))
|
||||
write("inv(a) := ", cpxstr(cpxinv(a)))
|
||||
write("conj(a) := ", cpxstr(cpxconj(a)))
|
||||
write("abs(a) := ", cpxabs(a))
|
||||
write("neg(1) := ", cpxstr(cpxneg(1)))
|
||||
end
|
||||
27
Task/Arithmetic-Complex/Icon/arithmetic-complex-2.icon
Normal file
27
Task/Arithmetic-Complex/Icon/arithmetic-complex-2.icon
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
link complex # for complex number support
|
||||
|
||||
procedure SetupComplex() #: used to setup safe complex
|
||||
COMPLEX() # replace complex record constructor
|
||||
SetupComplex := 1 # never call here again
|
||||
return
|
||||
end
|
||||
|
||||
procedure COMPLEX(rpart,ipart) #: new safe record constructor and coercion
|
||||
initial complex :=: COMPLEX # get in front of record constructor
|
||||
return if /ipart & (type(rpart) == "complex")
|
||||
then rpart # already complex
|
||||
else COMPLEX( real(\rpart | 0.0), real(\ipart|0) ) # create a new complex number
|
||||
end
|
||||
|
||||
procedure cpxneg(z) #: negate z
|
||||
z := complex(z) # coerce
|
||||
return complex( -z.rpart, -z.ipart)
|
||||
end
|
||||
|
||||
procedure cpxinv(z) #: inverse of z
|
||||
local denom
|
||||
z := complex(z) # coerce
|
||||
|
||||
denom := z.rpart ^ 2 + z.ipart ^ 2
|
||||
return complex(z.rpart / denom, z.ipart / denom)
|
||||
end
|
||||
12
Task/Arithmetic-Complex/J/arithmetic-complex.j
Normal file
12
Task/Arithmetic-Complex/J/arithmetic-complex.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
x=: 1j1
|
||||
y=: 3.14159j1.2
|
||||
x+y NB. addition
|
||||
4.14159j2.2
|
||||
x*y NB. multiplication
|
||||
1.94159j4.34159
|
||||
%x NB. inversion
|
||||
0.5j_0.5
|
||||
-x NB. negation
|
||||
_1j_1
|
||||
+x NB. (complex) conjugation
|
||||
1j_1
|
||||
52
Task/Arithmetic-Complex/Java/arithmetic-complex.java
Normal file
52
Task/Arithmetic-Complex/Java/arithmetic-complex.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
public class Complex {
|
||||
public final double real;
|
||||
public final double imag;
|
||||
|
||||
public Complex() {
|
||||
this(0, 0);
|
||||
}
|
||||
|
||||
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 inv() {
|
||||
// 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);
|
||||
}
|
||||
|
||||
public Complex conj() {
|
||||
return new Complex(real, -imag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return real + " + " + imag + " * i";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Complex a = new Complex(Math.PI, -5); //just some numbers
|
||||
Complex b = new Complex(-1, 2.5);
|
||||
System.out.println(a.neg());
|
||||
System.out.println(a.add(b));
|
||||
System.out.println(a.inv());
|
||||
System.out.println(a.mult(b));
|
||||
System.out.println(a.conj());
|
||||
}
|
||||
}
|
||||
55
Task/Arithmetic-Complex/JavaScript/arithmetic-complex.js
Normal file
55
Task/Arithmetic-Complex/JavaScript/arithmetic-complex.js
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
function Complex(r, i) {
|
||||
this.r = r;
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
Complex.add = function() {
|
||||
var num = arguments[0];
|
||||
|
||||
for(var i = 1, ilim = arguments.length; i < ilim; i += 1){
|
||||
num.r += arguments[i].r;
|
||||
num.i += arguments[i].i;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
Complex.multiply = function() {
|
||||
var num = arguments[0];
|
||||
|
||||
for(var i = 1, ilim = arguments.length; i < ilim; i += 1){
|
||||
num.r = (num.r * arguments[i].r) - (num.i * arguments[i].i);
|
||||
num.i = (num.i * arguments[i].r) - (num.r * arguments[i].i);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
Complex.negate = function (z) {
|
||||
return new Complex(-1*z.r, -1*z.i);
|
||||
}
|
||||
|
||||
Complex.invert = function(z) {
|
||||
var denom = Math.pow(z.r,2) + Math.pow(z.i,2);
|
||||
return new Complex(z.r/denom, -1*z.i/denom);
|
||||
}
|
||||
|
||||
Complex.conjugate = function(z) {
|
||||
return new Complex(z.r, -1*z.i);
|
||||
}
|
||||
|
||||
// BONUSES!
|
||||
|
||||
|
||||
Complex.prototype.toString = function() {
|
||||
return this.r === 0 && this.i === 0
|
||||
? "0"
|
||||
: (this.r !== 0 ? this.r : "")
|
||||
+ ((this.r !== 0 || this.i < 0) && this.i !== 0
|
||||
? (this.i > 0 ? "+" : "-")
|
||||
: "" ) + ( this.i !== 0 ? Math.abs(this.i) + "i" : "" );
|
||||
}
|
||||
|
||||
Complex.prototype.getMod = function() {
|
||||
return Math.sqrt( Math.pow(this.r,2) , Math.pow(this.i,2) )
|
||||
}
|
||||
64
Task/Arithmetic-Complex/Jq/arithmetic-complex-1.jq
Normal file
64
Task/Arithmetic-Complex/Jq/arithmetic-complex-1.jq
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
def real(z): if (z|type) == "number" then z else z[0] end;
|
||||
|
||||
def imag(z): if (z|type) == "number" then 0 else z[1] end;
|
||||
|
||||
def plus(x; y):
|
||||
if (x|type) == "number" then
|
||||
if (y|type) == "number" then [ x+y, 0 ]
|
||||
else [ x + y[0], y[1]]
|
||||
end
|
||||
elif (y|type) == "number" then plus(y;x)
|
||||
else [ x[0] + y[0], x[1] + y[1] ]
|
||||
end;
|
||||
|
||||
def multiply(x; y):
|
||||
if (x|type) == "number" then
|
||||
if (y|type) == "number" then [ x*y, 0 ]
|
||||
else [x * y[0], x * y[1]]
|
||||
end
|
||||
elif (y|type) == "number" then multiply(y;x)
|
||||
else [ x[0] * y[0] - x[1] * y[1],
|
||||
x[0] * y[1] + x[1] * y[0]]
|
||||
end;
|
||||
|
||||
def multiply: reduce .[] as $x (1; multiply(.; $x));
|
||||
|
||||
def negate(x): multiply(-1; x);
|
||||
|
||||
def minus(x; y): plus(x; multiply(-1; y));
|
||||
|
||||
def conjugate(z):
|
||||
if (z|type) == "number" then [z, 0]
|
||||
else [z[0], -(z[1]) ]
|
||||
end;
|
||||
|
||||
def invert(z):
|
||||
if (z|type) == "number" then [1/z, 0]
|
||||
else
|
||||
( (z[0] * z[0]) + (z[1] * z[1]) ) as $d
|
||||
# use "0 + ." to convert -0 back to 0
|
||||
| [ z[0]/$d, (0 + -(z[1]) / $d)]
|
||||
end;
|
||||
|
||||
def divide(x;y): multiply(x; invert(y));
|
||||
|
||||
def exp(z):
|
||||
def expi(x): [ (x|cos), (x|sin) ];
|
||||
if (z|type) == "number" then z|exp
|
||||
elif z[0] == 0 then expi(z[1]) # for efficiency
|
||||
else multiply( (z[0]|exp); expi(z[1]) )
|
||||
end ;
|
||||
|
||||
def test(x;y):
|
||||
"x = \( x )",
|
||||
"y = \( y )",
|
||||
"x+y: \( plus(x;y))",
|
||||
"x*y: \( multiply(x;y))",
|
||||
"-x: \( negate(x))",
|
||||
"1/x: \( invert(x))",
|
||||
"conj(x): \( conjugate(x))",
|
||||
"(x/y)*y: \( multiply( divide(x;y) ; y) )",
|
||||
"e^iπ: \( exp( [0, 4 * (1|atan) ] ) )"
|
||||
;
|
||||
|
||||
test( [1,1]; [0,1] )
|
||||
10
Task/Arithmetic-Complex/Jq/arithmetic-complex-2.jq
Normal file
10
Task/Arithmetic-Complex/Jq/arithmetic-complex-2.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
$ jq -n -f complex.jq
|
||||
"x = [1,1]"
|
||||
"y = [0,1]"
|
||||
"x+y: [1,2]"
|
||||
"x*y: [-1,1]"
|
||||
"-x: [-1,-1]"
|
||||
"1/x: [0.5,-0.5]"
|
||||
"conj(x): [1,-1]"
|
||||
"(x/y)*y: [1,1]"
|
||||
"e^iπ: [-1,1.2246467991473532e-16]"
|
||||
22
Task/Arithmetic-Complex/Julia/arithmetic-complex.julia
Normal file
22
Task/Arithmetic-Complex/Julia/arithmetic-complex.julia
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
julia> z1 = 1.5 + 3im
|
||||
julia> z2 = 1.5 + 1.5im
|
||||
julia> z1 + z2
|
||||
3.0 + 4.5im
|
||||
julia> z1 - z2
|
||||
0.0 + 1.5im
|
||||
julia> z1 * z2
|
||||
-2.25 + 6.75im
|
||||
julia> z1 / z2
|
||||
1.5 + 0.5im
|
||||
julia> - z1
|
||||
-1.5 - 3.0im
|
||||
julia> conj(z1), z1' # two ways to conjugate
|
||||
(1.5 - 3.0im,1.5 - 3.0im)
|
||||
julia> abs(z1)
|
||||
3.3541019662496847
|
||||
julia> z1^z2
|
||||
-1.102482955327779 - 0.38306415117199305im
|
||||
julia> real(z1)
|
||||
1.5
|
||||
julia> imag(z1)
|
||||
3.0
|
||||
39
Task/Arithmetic-Complex/Kotlin/arithmetic-complex.kotlin
Normal file
39
Task/Arithmetic-Complex/Kotlin/arithmetic-complex.kotlin
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
class Complex(private val real: Double, private val imag: Double) {
|
||||
operator fun plus(other: Complex) = Complex(real + other.real, imag + other.imag)
|
||||
|
||||
operator fun times(other: Complex) = Complex(
|
||||
real * other.real - imag * other.imag,
|
||||
real * other.imag + imag * other.real
|
||||
)
|
||||
|
||||
fun inv(): Complex {
|
||||
val denom = real * real + imag * imag
|
||||
return Complex(real / denom, -imag / denom)
|
||||
}
|
||||
|
||||
operator fun unaryMinus() = Complex(-real, -imag)
|
||||
|
||||
operator fun minus(other: Complex) = this + (-other)
|
||||
|
||||
operator fun div(other: Complex) = this * other.inv()
|
||||
|
||||
fun conj() = Complex(real, -imag)
|
||||
|
||||
override fun toString() =
|
||||
if (imag >= 0.0) "$real + ${imag}i"
|
||||
else "$real - ${-imag}i"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val x = Complex(1.0, 3.0)
|
||||
val y = Complex(5.0, 2.0)
|
||||
println("x = $x")
|
||||
println("y = $y")
|
||||
println("x + y = ${x + y}")
|
||||
println("x - y = ${x - y}")
|
||||
println("x * y = ${x * y}")
|
||||
println("x / y = ${x / y}")
|
||||
println("-x = ${-x}")
|
||||
println("1 / x = ${x.inv()}")
|
||||
println("x* = ${x.conj()}")
|
||||
}
|
||||
3
Task/Arithmetic-Complex/LFE/arithmetic-complex-1.lfe
Normal file
3
Task/Arithmetic-Complex/LFE/arithmetic-complex-1.lfe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(defrecord complex
|
||||
real
|
||||
img)
|
||||
17
Task/Arithmetic-Complex/LFE/arithmetic-complex-2.lfe
Normal file
17
Task/Arithmetic-Complex/LFE/arithmetic-complex-2.lfe
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defun add
|
||||
(((match-complex real r1 img i1)
|
||||
(match-complex real r2 img i2))
|
||||
(new (+ r1 r2) (+ i1 i2))))
|
||||
|
||||
(defun mult
|
||||
(((match-complex real r1 img i1)
|
||||
(match-complex real r2 img i2))
|
||||
(new (- (* r1 r2) (* i1 i2))
|
||||
(+ (* r1 i2) (* r2 i1)))))
|
||||
|
||||
(defun neg
|
||||
(((match-complex real r img i))
|
||||
(new (* -1 r) (* -1 i))))
|
||||
|
||||
(defun inv (cmplx)
|
||||
(div (conj cmplx) (modulus cmplx)))
|
||||
3
Task/Arithmetic-Complex/LFE/arithmetic-complex-3.lfe
Normal file
3
Task/Arithmetic-Complex/LFE/arithmetic-complex-3.lfe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(defun conj
|
||||
(((match-complex real r img i))
|
||||
(new r (* -1 i))))
|
||||
11
Task/Arithmetic-Complex/LFE/arithmetic-complex-4.lfe
Normal file
11
Task/Arithmetic-Complex/LFE/arithmetic-complex-4.lfe
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defun new (r i)
|
||||
(make-complex real r img i))
|
||||
|
||||
(defun modulus (cmplx)
|
||||
(mult cmplx (conj cmplx)))
|
||||
|
||||
(defun div (c1 c2)
|
||||
(let* ((denom (complex-real (modulus c2)))
|
||||
(c3 (mult c1 (conj c2))))
|
||||
(new (/ (complex-real c3) denom)
|
||||
(/ (complex-img c3) denom)))))
|
||||
11
Task/Arithmetic-Complex/LFE/arithmetic-complex-5.lfe
Normal file
11
Task/Arithmetic-Complex/LFE/arithmetic-complex-5.lfe
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defun ->str
|
||||
(((match-complex real r img i)) (when (>= i 0))
|
||||
(->str r i "+"))
|
||||
(((match-complex real r img i))
|
||||
(->str r i "")))
|
||||
|
||||
(defun ->str (r i pos)
|
||||
(io_lib:format "~p ~s~pi" `(,r ,pos ,i)))
|
||||
|
||||
(defun print (cmplx)
|
||||
(io:format (++ (->str cmplx) "~n")))
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
{require lib_complex}
|
||||
|
||||
{def z1 {C.new 1 1}}
|
||||
-> z1 = (1 1)
|
||||
|
||||
{C.x {z1}} -> 1
|
||||
{C.y {z1}} -> 1
|
||||
{C.mod {z1}} -> 1.4142135623730951
|
||||
{C.arg {z1}} -> 0.7853981633974483 // 45°
|
||||
{C.conj {z1}} -> (1 -1)
|
||||
{C.negat {z1}} -> (-1 -1)
|
||||
{C.invert {z1}} -> (0.5 -0.4999999999999999)
|
||||
{C.sqrt {z1}} -> (1.0986841134678098 0.45508986056222733)
|
||||
{C.exp {z1}} -> (1.4686939399158851 2.2873552871788423)
|
||||
{C.log {z1}} -> (0.3465735902799727 0.7853981633974483)
|
||||
|
||||
{def z2 {C.new 1.5 1.5}}
|
||||
-> z2 = (1.5 1.5)
|
||||
|
||||
{C.add {z1} {z2}} -> (2.5 2.5)
|
||||
{C.sub {z1} {z2}} -> (-0.5 -0.5)
|
||||
{C.mul {z1} {z2}} -> (0 3)
|
||||
{C.div {z1} {z2}} -> (0.6666666666666667 0)
|
||||
30
Task/Arithmetic-Complex/Lang/arithmetic-complex.lang
Normal file
30
Task/Arithmetic-Complex/Lang/arithmetic-complex.lang
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
fp.cprint = ($z) -> fn.printf(%.3f%+.3fi%n, fn.creal($z), fn.cimag($z))
|
||||
|
||||
$a = fn.complex(1.5, 3)
|
||||
$b = fn.complex(1.5, 1.5)
|
||||
|
||||
fn.print(a =\s)
|
||||
fp.cprint($a)
|
||||
|
||||
fn.print(b =\s)
|
||||
fp.cprint($b)
|
||||
|
||||
# Addition
|
||||
fn.print(a + b =\s)
|
||||
fp.cprint(fn.cadd($a, $b))
|
||||
|
||||
# Multiplication
|
||||
fn.print(a * b =\s)
|
||||
fp.cprint(fn.cmul($a, $b))
|
||||
|
||||
# Inversion
|
||||
fn.print(1/a =\s)
|
||||
fp.cprint(fn.cdiv(fn.complex(1, 0), $a))
|
||||
|
||||
# Negation
|
||||
fn.print(-a =\s)
|
||||
fp.cprint(fn.cinv($a))
|
||||
|
||||
# Conjugate
|
||||
fn.print(conj(a) =\s)
|
||||
fp.cprint(fn.conj($a))
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
mainwin 50 10
|
||||
|
||||
print " Adding"
|
||||
call cprint cadd$( complex$( 1, 1), complex$( 3.14159265, 1.2))
|
||||
print " Multiplying"
|
||||
call cprint cmulti$( complex$( 1, 1), complex$( 3.14159265, 1.2))
|
||||
print " Inverting"
|
||||
call cprint cinv$( complex$( 1, 1))
|
||||
print " Negating"
|
||||
call cprint cneg$( complex$( 1, 1))
|
||||
|
||||
end
|
||||
|
||||
sub cprint cx$
|
||||
print "( "; word$( cx$, 1); " + i *"; word$( cx$, 2); ")"
|
||||
end sub
|
||||
|
||||
function complex$( a , bj )
|
||||
''complex number string-object constructor
|
||||
complex$ = str$( a ) ; " " ; str$( bj )
|
||||
end function
|
||||
|
||||
function cadd$( a$ , b$ )
|
||||
ar = val( word$( a$ , 1 ) )
|
||||
ai = val( word$( a$ , 2 ) )
|
||||
br = val( word$( b$ , 1 ) )
|
||||
bi = val( word$( b$ , 2 ) )
|
||||
cadd$ = complex$( ar + br , ai + bi )
|
||||
end function
|
||||
|
||||
function cmulti$( a$ , b$ )
|
||||
ar = val( word$( a$ , 1 ) )
|
||||
ai = val( word$( a$ , 2 ) )
|
||||
br = val( word$( b$ , 1 ) )
|
||||
bi = val( word$( b$ , 2 ) )
|
||||
cmulti$ = complex$( ar * br - ai * bi _
|
||||
, ar * bi + ai * br )
|
||||
end function
|
||||
|
||||
function cneg$( a$)
|
||||
ar = val( word$( a$ , 1 ) )
|
||||
ai = val( word$( a$ , 2 ) )
|
||||
cneg$ =complex$( 0 -ar, 0 -ai)
|
||||
end function
|
||||
|
||||
function cinv$( a$)
|
||||
ar = val( word$( a$ , 1 ) )
|
||||
ai = val( word$( a$ , 2 ) )
|
||||
D =ar^2 +ai^2
|
||||
cinv$ =complex$( ar /D , 0 -ai /D )
|
||||
end function
|
||||
31
Task/Arithmetic-Complex/Lua/arithmetic-complex.lua
Normal file
31
Task/Arithmetic-Complex/Lua/arithmetic-complex.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
--defines addition, subtraction, negation, multiplication, division, conjugation, norms, and a conversion to strgs.
|
||||
complex = setmetatable({
|
||||
__add = function(u, v) return complex(u.real + v.real, u.imag + v.imag) end,
|
||||
__sub = function(u, v) return complex(u.real - v.real, u.imag - v.imag) end,
|
||||
__mul = function(u, v) return complex(u.real * v.real - u.imag * v.imag, u.real * v.imag + u.imag * v.real) end,
|
||||
__div = function(u, v) return u * complex(v.real / v.norm, -v.imag / v.norm) end,
|
||||
__unm = function(u) return complex(-u.real, -u.imag) end,
|
||||
__concat = function(u, v)
|
||||
if type(u) == "table" then return u.real .. " + " .. u.imag .. "i" .. v
|
||||
elseif type(u) == "string" or type(u) == "number" then return u .. v.real .. " + " .. v.imag .. "i"
|
||||
end end,
|
||||
__index = function(u, index)
|
||||
local operations = {
|
||||
norm = function(u) return u.real ^ 2 + u.imag ^ 2 end,
|
||||
conj = function(u) return complex(u.real, -u.imag) end,
|
||||
}
|
||||
return operations[index] and operations[index](u)
|
||||
end,
|
||||
__newindex = function() error() end
|
||||
}, {
|
||||
__call = function(z, realpart, imagpart) return setmetatable({real = realpart, imag = imagpart}, complex) end
|
||||
} )
|
||||
|
||||
local i, j = complex(2, 3), complex(1, 1)
|
||||
|
||||
print(i .. " + " .. j .. " = " .. (i+j))
|
||||
print(i .. " - " .. j .. " = " .. (i-j))
|
||||
print(i .. " * " .. j .. " = " .. (i*j))
|
||||
print(i .. " / " .. j .. " = " .. (i/j))
|
||||
print("|" .. i .. "| = " .. math.sqrt(i.norm))
|
||||
print(i .. "* = " .. i.conj)
|
||||
59
Task/Arithmetic-Complex/MATLAB/arithmetic-complex.m
Normal file
59
Task/Arithmetic-Complex/MATLAB/arithmetic-complex.m
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
>> a = 1+i
|
||||
|
||||
a =
|
||||
|
||||
1.000000000000000 + 1.000000000000000i
|
||||
|
||||
>> b = 3+7i
|
||||
|
||||
b =
|
||||
|
||||
3.000000000000000 + 7.000000000000000i
|
||||
|
||||
>> a+b
|
||||
|
||||
ans =
|
||||
|
||||
4.000000000000000 + 8.000000000000000i
|
||||
|
||||
>> a-b
|
||||
|
||||
ans =
|
||||
|
||||
-2.000000000000000 - 6.000000000000000i
|
||||
|
||||
>> a*b
|
||||
|
||||
ans =
|
||||
|
||||
-4.000000000000000 +10.000000000000000i
|
||||
|
||||
>> a/b
|
||||
|
||||
ans =
|
||||
|
||||
0.172413793103448 - 0.068965517241379i
|
||||
|
||||
>> -a
|
||||
|
||||
ans =
|
||||
|
||||
-1.000000000000000 - 1.000000000000000i
|
||||
|
||||
>> a'
|
||||
|
||||
ans =
|
||||
|
||||
1.000000000000000 - 1.000000000000000i
|
||||
|
||||
>> a^b
|
||||
|
||||
ans =
|
||||
|
||||
0.000808197112874 - 0.011556516327187i
|
||||
|
||||
>> norm(a)
|
||||
|
||||
ans =
|
||||
|
||||
1.414213562373095
|
||||
2
Task/Arithmetic-Complex/Maple/arithmetic-complex-1.maple
Normal file
2
Task/Arithmetic-Complex/Maple/arithmetic-complex-1.maple
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
x := 1+I;
|
||||
y := Pi+I*1.2;
|
||||
4
Task/Arithmetic-Complex/Maple/arithmetic-complex-2.maple
Normal file
4
Task/Arithmetic-Complex/Maple/arithmetic-complex-2.maple
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
x*y;
|
||||
==> (1 + I) (Pi + 1.2 I)
|
||||
simplify(x*y);
|
||||
==> 1.941592654 + 4.341592654 I
|
||||
4
Task/Arithmetic-Complex/Maple/arithmetic-complex-3.maple
Normal file
4
Task/Arithmetic-Complex/Maple/arithmetic-complex-3.maple
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
x+y;
|
||||
x*y;
|
||||
-x;
|
||||
1/x;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
x=1+2I
|
||||
y=3+4I
|
||||
|
||||
x+y => 4 + 6 I
|
||||
x-y => -2 - 2 I
|
||||
y x => -5 + 10 I
|
||||
y/x => 11/5 - (2 I)/5
|
||||
x^3 => -11 - 2 I
|
||||
y^4 => -527 - 336 I
|
||||
x^y => (1 + 2 I)^(3 + 4 I)
|
||||
N[x^y] => 0.12901 + 0.0339241 I
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Exp Log
|
||||
Sin Cos Tan Csc Sec Cot
|
||||
ArcSin ArcCos ArcTan ArcCsc ArcSec ArcCot
|
||||
Sinh Cosh Tanh Csch Sech Coth
|
||||
ArcSinh ArcCosh ArcTanh ArcCsch ArcSech ArcCoth
|
||||
Sinc
|
||||
Haversine InverseHaversine
|
||||
Factorial Gamma PolyGamma LogGamma
|
||||
Erf BarnesG Hyperfactorial Zeta ProductLog RamanujanTauL
|
||||
44
Task/Arithmetic-Complex/Maxima/arithmetic-complex.maxima
Normal file
44
Task/Arithmetic-Complex/Maxima/arithmetic-complex.maxima
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
z1: 5 + 2 * %i;
|
||||
2*%i+5
|
||||
|
||||
z2: 3 - 7 * %i;
|
||||
3-7*%i
|
||||
|
||||
carg(z1);
|
||||
atan(2/5)
|
||||
|
||||
cabs(z1);
|
||||
sqrt(29)
|
||||
|
||||
rectform(z1 * z2);
|
||||
29-29*%i
|
||||
|
||||
polarform(z1);
|
||||
sqrt(29)*%e^(%i*atan(2/5))
|
||||
|
||||
conjugate(z1);
|
||||
5-2*%i
|
||||
|
||||
z1 + z2;
|
||||
8-5*%i
|
||||
|
||||
z1 - z2;
|
||||
9*%i+2
|
||||
|
||||
z1 * z2;
|
||||
(3-7*%i)*(2*%i+5)
|
||||
|
||||
z1 * z2, rectform;
|
||||
29-29*%i
|
||||
|
||||
z1 / z2;
|
||||
(2*%i+5)/(3-7*%i)
|
||||
|
||||
z1 / z2, rectform;
|
||||
(41*%i)/58+1/58
|
||||
|
||||
realpart(z1);
|
||||
5
|
||||
|
||||
imagpart(z1);
|
||||
2
|
||||
66
Task/Arithmetic-Complex/Modula-2/arithmetic-complex.mod2
Normal file
66
Task/Arithmetic-Complex/Modula-2/arithmetic-complex.mod2
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
MODULE complex;
|
||||
|
||||
IMPORT InOut;
|
||||
|
||||
TYPE Complex = RECORD R, Im : REAL END;
|
||||
|
||||
VAR z : ARRAY [0..3] OF Complex;
|
||||
|
||||
PROCEDURE ShowComplex (str : ARRAY OF CHAR; p : Complex);
|
||||
|
||||
BEGIN
|
||||
InOut.WriteString (str); InOut.WriteString (" = ");
|
||||
InOut.WriteReal (p.R, 6, 2);
|
||||
IF p.Im >= 0.0 THEN InOut.WriteString (" + ") ELSE InOut.WriteString (" - ") END;
|
||||
InOut.WriteReal (ABS (p.Im), 6, 2); InOut.WriteString (" i ");
|
||||
InOut.WriteLn; InOut.WriteBf
|
||||
END ShowComplex;
|
||||
|
||||
PROCEDURE AddComplex (x1, x2 : Complex; VAR x3 : Complex);
|
||||
|
||||
BEGIN
|
||||
x3.R := x1.R + x2.R;
|
||||
x3.Im := x1.Im + x2.Im
|
||||
END AddComplex;
|
||||
|
||||
PROCEDURE SubComplex (x1, x2 : Complex; VAR x3 : Complex);
|
||||
|
||||
BEGIN
|
||||
x3.R := x1.R - x2.R;
|
||||
x3.Im := x1.Im - x2.Im
|
||||
END SubComplex;
|
||||
|
||||
PROCEDURE MulComplex (x1, x2 : Complex; VAR x3 : Complex);
|
||||
|
||||
BEGIN
|
||||
x3.R := x1.R * x2.R - x1.Im * x2.Im;
|
||||
x3.Im := x1.R * x2.Im + x1.Im * x2.R
|
||||
END MulComplex;
|
||||
|
||||
PROCEDURE InvComplex (x1 : Complex; VAR x2 : Complex);
|
||||
|
||||
BEGIN
|
||||
x2.R := x1.R / (x1.R * x1.R + x1.Im * x1.Im);
|
||||
x2.Im := -1.0 * x1.Im / (x1.R * x1.R + x1.Im * x1.Im)
|
||||
END InvComplex;
|
||||
|
||||
PROCEDURE NegComplex (x1 : Complex; VAR x2 : Complex);
|
||||
|
||||
BEGIN
|
||||
x2.R := - x1.R; x2.Im := - x1.Im
|
||||
END NegComplex;
|
||||
|
||||
BEGIN
|
||||
InOut.WriteString ("Enter two complex numbers : ");
|
||||
InOut.WriteBf;
|
||||
InOut.ReadReal (z[0].R); InOut.ReadReal (z[0].Im);
|
||||
InOut.ReadReal (z[1].R); InOut.ReadReal (z[1].Im);
|
||||
ShowComplex ("z1", z[0]); ShowComplex ("z2", z[1]);
|
||||
InOut.WriteLn;
|
||||
AddComplex (z[0], z[1], z[2]); ShowComplex ("z1 + z2", z[2]);
|
||||
SubComplex (z[0], z[1], z[2]); ShowComplex ("z1 - z2", z[2]);
|
||||
MulComplex (z[0], z[1], z[2]); ShowComplex ("z1 * z2", z[2]);
|
||||
InvComplex (z[0], z[2]); ShowComplex ("1 / z1", z[2]);
|
||||
NegComplex (z[0], z[2]); ShowComplex (" - z1", z[2]);
|
||||
InOut.WriteLn
|
||||
END complex.
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
import math
|
||||
|
||||
class Complex
|
||||
declare real
|
||||
declare imag
|
||||
|
||||
def Complex()
|
||||
real = 0.0
|
||||
imag = 0.0
|
||||
end
|
||||
|
||||
def Complex(r, i)
|
||||
real = double(r)
|
||||
imag = double(i)
|
||||
end
|
||||
|
||||
def operator-(b)
|
||||
return new(Complex, this.real - b.real, this.imag - b.imag)
|
||||
end
|
||||
|
||||
def operator+(b)
|
||||
return new(Complex, this.real + b.real, this.imag + b.imag)
|
||||
end
|
||||
|
||||
def operator*(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)
|
||||
end
|
||||
|
||||
def inv()
|
||||
// 1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
|
||||
denom = this.real * this.real + this.imag * this.imag
|
||||
return new(Complex, real/denom, -imag/denom)
|
||||
end
|
||||
|
||||
def neg()
|
||||
return new(Complex, -this.real, -this.imag)
|
||||
end
|
||||
|
||||
def conj()
|
||||
return new(Complex, this.real, -this.imag)
|
||||
end
|
||||
|
||||
def toString()
|
||||
return this.real + " + " + this.imag + " * i"
|
||||
end
|
||||
end
|
||||
|
||||
a = new(Complex, math.pi, -5)
|
||||
b = new(Complex, -1, 2.5)
|
||||
println a.neg()
|
||||
println a + b
|
||||
println a.inv()
|
||||
println a * b
|
||||
println a.conj()
|
||||
26
Task/Arithmetic-Complex/Nemerle/arithmetic-complex.nemerle
Normal file
26
Task/Arithmetic-Complex/Nemerle/arithmetic-complex.nemerle
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using System.Numerics;
|
||||
using System.Numerics.Complex;
|
||||
|
||||
module RCComplex
|
||||
{
|
||||
PrettyPrint(this c : Complex) : string
|
||||
{
|
||||
mutable sign = '+';
|
||||
when (c.Imaginary < 0) sign = '-';
|
||||
$"$(c.Real) $sign $(Math.Abs(c.Imaginary))i"
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def complex1 = Complex(1.0, 1.0);
|
||||
def complex2 = Complex(3.14159, 1.2);
|
||||
|
||||
WriteLine(Add(complex1, complex2).PrettyPrint());
|
||||
WriteLine(Multiply(complex1, complex2).PrettyPrint());
|
||||
WriteLine(Negate(complex2).PrettyPrint());
|
||||
WriteLine(Reciprocal(complex2).PrettyPrint());
|
||||
WriteLine(Conjugate(complex2).PrettyPrint());
|
||||
}
|
||||
}
|
||||
10
Task/Arithmetic-Complex/Nim/arithmetic-complex.nim
Normal file
10
Task/Arithmetic-Complex/Nim/arithmetic-complex.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import complex
|
||||
var a: Complex = (1.0,1.0)
|
||||
var b: Complex = (3.1415,1.2)
|
||||
|
||||
echo("a : " & $a)
|
||||
echo("b : " & $b)
|
||||
echo("a + b: " & $(a + b))
|
||||
echo("a * b: " & $(a * b))
|
||||
echo("1/a : " & $(1/a))
|
||||
echo("-a : " & $(-a))
|
||||
13
Task/Arithmetic-Complex/OCaml/arithmetic-complex-1.ocaml
Normal file
13
Task/Arithmetic-Complex/OCaml/arithmetic-complex-1.ocaml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
open Complex
|
||||
|
||||
let print_complex z =
|
||||
Printf.printf "%f + %f i\n" z.re z.im
|
||||
|
||||
let () =
|
||||
let a = { re = 1.0; im = 1.0 }
|
||||
and b = { re = 3.14159; im = 1.25 } in
|
||||
print_complex (add a b);
|
||||
print_complex (mul a b);
|
||||
print_complex (inv a);
|
||||
print_complex (neg a);
|
||||
print_complex (conj a)
|
||||
14
Task/Arithmetic-Complex/OCaml/arithmetic-complex-2.ocaml
Normal file
14
Task/Arithmetic-Complex/OCaml/arithmetic-complex-2.ocaml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
let () =
|
||||
Complex.(
|
||||
let print txt z = Printf.printf "%s = %s\n" txt (to_string z) in
|
||||
let a = 1 + I
|
||||
and b = 3 + 7I in
|
||||
print "a + b" (a + b);
|
||||
print "a - b" (a - b);
|
||||
print "a * b" (a * b);
|
||||
print "a / b" (a / b);
|
||||
print "-a" (- a);
|
||||
print "conj a" (conj a);
|
||||
print "a^b" (a**b);
|
||||
Printf.printf "norm a = %g\n" (float(abs a));
|
||||
)
|
||||
85
Task/Arithmetic-Complex/Oberon-2/arithmetic-complex.oberon
Normal file
85
Task/Arithmetic-Complex/Oberon-2/arithmetic-complex.oberon
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
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.
|
||||
14
Task/Arithmetic-Complex/Octave/arithmetic-complex.octave
Normal file
14
Task/Arithmetic-Complex/Octave/arithmetic-complex.octave
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
z1 = 1.5 + 3i;
|
||||
z2 = 1.5 + 1.5i;
|
||||
disp(z1 + z2); % 3.0 + 4.5i
|
||||
disp(z1 - z2); % 0.0 + 1.5i
|
||||
disp(z1 * z2); % -2.25 + 6.75i
|
||||
disp(z1 / z2); % 1.5 + 0.5i
|
||||
disp(-z1); % -1.5 - 3i
|
||||
disp(z1'); % 1.5 - 3i
|
||||
disp(abs(z1)); % 3.3541 = sqrt(z1*z1')
|
||||
disp(z1 ^ z2); % -1.10248 - 0.38306i
|
||||
disp( exp(z1) ); % -4.43684 + 0.63246i
|
||||
disp( imag(z1) ); % 3
|
||||
disp( real(z2) ); % 1.5
|
||||
%...
|
||||
39
Task/Arithmetic-Complex/Oforth/arithmetic-complex-1.fth
Normal file
39
Task/Arithmetic-Complex/Oforth/arithmetic-complex-1.fth
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Object Class new: Complex(re, im)
|
||||
|
||||
Complex method: re @re ;
|
||||
Complex method: im @im ;
|
||||
|
||||
Complex method: initialize := im := re ;
|
||||
Complex method: << '(' <<c @re << ',' <<c @im << ')' <<c ;
|
||||
|
||||
0 1 Complex new const: I
|
||||
|
||||
Complex method: ==(c -- b )
|
||||
c re @re == c im @im == and ;
|
||||
|
||||
Complex method: norm -- f
|
||||
@re sq @im sq + sqrt ;
|
||||
|
||||
Complex method: conj -- c
|
||||
@re @im neg Complex new ;
|
||||
|
||||
Complex method: +(c -- d )
|
||||
c re @re + c im @im + Complex new ;
|
||||
|
||||
Complex method: -(c -- d )
|
||||
c re @re - c im @im - Complex new ;
|
||||
|
||||
Complex method: *(c -- d)
|
||||
c re @re * c im @im * - c re @im * @re c im * + Complex new ;
|
||||
|
||||
Complex method: inv
|
||||
| n |
|
||||
@re sq @im sq + >float ->n
|
||||
@re n / @im neg n / Complex new
|
||||
;
|
||||
|
||||
Complex method: /( c -- d )
|
||||
c self inv * ;
|
||||
|
||||
Integer method: >complex self 0 Complex new ;
|
||||
Float method: >complex self 0 Complex new ;
|
||||
4
Task/Arithmetic-Complex/Oforth/arithmetic-complex-2.fth
Normal file
4
Task/Arithmetic-Complex/Oforth/arithmetic-complex-2.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
3.2 >complex I * 2 >complex + .cr
|
||||
2 3 Complex new 1.2 >complex + .cr
|
||||
2 3 Complex new 1.2 >complex * .cr
|
||||
2 >complex 2 3 Complex new / .cr
|
||||
23
Task/Arithmetic-Complex/Ol/arithmetic-complex.ol
Normal file
23
Task/Arithmetic-Complex/Ol/arithmetic-complex.ol
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(define A 0+1i) ; manually entered numbers
|
||||
(define B 1+0i)
|
||||
|
||||
(print (+ A B))
|
||||
; <== 1+i
|
||||
|
||||
(print (- A B))
|
||||
; <== -1+i
|
||||
|
||||
(print (* A B))
|
||||
; <== 0+i
|
||||
|
||||
(print (/ A B))
|
||||
; <== 0+i
|
||||
|
||||
|
||||
(define C (complex 2/7 -3)) ; functional way
|
||||
|
||||
(print "real part of " C " is " (car C))
|
||||
; <== real part of 2/7-3i is 2/7
|
||||
|
||||
(print "imaginary part of " C " is " (cdr C))
|
||||
; <== imaginary part of 2/7-3i is -3
|
||||
137
Task/Arithmetic-Complex/OoRexx/arithmetic-complex.rexx
Normal file
137
Task/Arithmetic-Complex/OoRexx/arithmetic-complex.rexx
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
c1 = .complex~new(1, 2)
|
||||
c2 = .complex~new(3, 4)
|
||||
r = 7
|
||||
|
||||
say "c1 =" c1
|
||||
say "c2 =" c2
|
||||
say "r =" r
|
||||
say "-c1 =" (-c1)
|
||||
say "c1 + r =" c1 + r
|
||||
say "c1 + c2 =" c1 + c2
|
||||
say "c1 - r =" c1 - r
|
||||
say "c1 - c2 =" c1 - c2
|
||||
say "c1 * r =" c1 * r
|
||||
say "c1 * c2 =" c1 * c2
|
||||
say "inv(c1) =" c1~inv
|
||||
say "conj(c1) =" c1~conjugate
|
||||
say "c1 / r =" c1 / r
|
||||
say "c1 / c2 =" c1 / c2
|
||||
say "c1 == c1 =" (c1 == c1)
|
||||
say "c1 == c2 =" (c1 == c2)
|
||||
|
||||
|
||||
::class complex
|
||||
::method init
|
||||
expose r i
|
||||
use strict arg r, i = 0
|
||||
|
||||
-- complex instances are immutable, so these are
|
||||
-- read only attributes
|
||||
::attribute r GET
|
||||
::attribute i GET
|
||||
|
||||
::method negative
|
||||
expose r i
|
||||
return self~class~new(-r, -i)
|
||||
|
||||
::method add
|
||||
expose r i
|
||||
use strict arg other
|
||||
if other~isa(.complex) then
|
||||
return self~class~new(r + other~r, i + other~i)
|
||||
else return self~class~new(r + other, i)
|
||||
|
||||
::method subtract
|
||||
expose r i
|
||||
use strict arg other
|
||||
if other~isa(.complex) then
|
||||
return self~class~new(r - other~r, i - other~i)
|
||||
else return self~class~new(r - other, i)
|
||||
|
||||
::method times
|
||||
expose r i
|
||||
use strict arg other
|
||||
if other~isa(.complex) then
|
||||
return self~class~new(r * other~r - i * other~i, r * other~i + i * other~r)
|
||||
else return self~class~new(r * other, i * other)
|
||||
|
||||
::method inv
|
||||
expose r i
|
||||
denom = r * r + i * i
|
||||
return self~class~new(r/denom,-i/denom)
|
||||
|
||||
::method conjugate
|
||||
expose r i
|
||||
return self~class~new(r, -i)
|
||||
|
||||
::method divide
|
||||
use strict arg other
|
||||
-- this is easier if everything is a complex number
|
||||
if \other~isA(.complex) then other = .complex~new(other)
|
||||
-- division is multiplication with the inversion
|
||||
return self * other~inv
|
||||
|
||||
::method "=="
|
||||
expose r i
|
||||
use strict arg other
|
||||
|
||||
if \other~isa(.complex) then return .false
|
||||
-- Note: these are numeric comparisons, so we're using the "="
|
||||
-- method so those are handled correctly
|
||||
return r = other~r & i = other~i
|
||||
|
||||
::method "\=="
|
||||
use strict arg other
|
||||
return \self~"\=="(other)
|
||||
|
||||
::method "="
|
||||
-- this is equivalent of "=="
|
||||
forward message("==")
|
||||
|
||||
::method "\="
|
||||
-- this is equivalent of "\=="
|
||||
forward message("\==")
|
||||
|
||||
::method "<>"
|
||||
-- this is equivalent of "\=="
|
||||
forward message("\==")
|
||||
|
||||
::method "><"
|
||||
-- this is equivalent of "\=="
|
||||
forward message("\==")
|
||||
|
||||
-- some operator overrides -- these only work if the left-hand-side of the
|
||||
-- subexpression is a quaternion
|
||||
::method "*"
|
||||
forward message("TIMES")
|
||||
|
||||
::method "/"
|
||||
forward message("DIVIDE")
|
||||
|
||||
::method "-"
|
||||
-- need to check if this is a prefix minus or a subtract
|
||||
if arg() == 0 then
|
||||
forward message("NEGATIVE")
|
||||
else
|
||||
forward message("SUBTRACT")
|
||||
|
||||
::method "+"
|
||||
-- need to check if this is a prefix plus or an addition
|
||||
if arg() == 0 then
|
||||
return self -- we can return this copy since it is immutable
|
||||
else
|
||||
forward message("ADD")
|
||||
|
||||
::method string
|
||||
expose r i
|
||||
return r self~formatnumber(i)"i"
|
||||
|
||||
::method formatnumber private
|
||||
use arg value
|
||||
if value > 0 then return "+" value
|
||||
else return "-" value~abs
|
||||
|
||||
-- override hashcode for collection class hash uses
|
||||
::method hashCode
|
||||
expose r i
|
||||
return r~hashcode~bitxor(i~hashcode)
|
||||
132
Task/Arithmetic-Complex/OxygenBasic/arithmetic-complex.basic
Normal file
132
Task/Arithmetic-Complex/OxygenBasic/arithmetic-complex.basic
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
'COMPLEX OPERATIONS
|
||||
'=================
|
||||
|
||||
type tcomplex double x,y
|
||||
|
||||
class Complex
|
||||
'============
|
||||
|
||||
has tcomplex
|
||||
static sys i,pp
|
||||
static tcomplex accum[32]
|
||||
|
||||
def operands
|
||||
tcomplex*a,*b
|
||||
@a=@accum+i
|
||||
if pp then
|
||||
@b=@a+sizeof accum
|
||||
pp=0
|
||||
else
|
||||
@b=@this
|
||||
end if
|
||||
end def
|
||||
|
||||
method "load"()
|
||||
operands
|
||||
a.x=b.x
|
||||
a.y=b.y
|
||||
end method
|
||||
|
||||
method "push"()
|
||||
i+=sizeof accum
|
||||
end method
|
||||
|
||||
method "pop"()
|
||||
pp=1
|
||||
i-=sizeof accum
|
||||
end method
|
||||
|
||||
method "="()
|
||||
operands
|
||||
b.x=a.x
|
||||
b.y=a.y
|
||||
end method
|
||||
|
||||
method "+"()
|
||||
operands
|
||||
a.x+=b.x
|
||||
a.y+=b.y
|
||||
end method
|
||||
|
||||
method "-"()
|
||||
operands
|
||||
a.x-=b.x
|
||||
a.y-=b.y
|
||||
end method
|
||||
|
||||
method "*"()
|
||||
operands
|
||||
double d
|
||||
d=a.x
|
||||
a.x = a.x * b.x - a.y * b.y
|
||||
a.y = a.y * b.x + d * b.y
|
||||
end method
|
||||
|
||||
method "/"()
|
||||
operands
|
||||
double d,v
|
||||
v=1/(b.x * b.x + b.y * b.y)
|
||||
d=a.x
|
||||
a.x = (a.x * b.x + a.y * b.y) * v
|
||||
a.y = (a.y * b.x - d * b.y) * v
|
||||
end method
|
||||
|
||||
method power(double n)
|
||||
operands
|
||||
'Using DeMoivre theorem
|
||||
double r,an,mg
|
||||
r = hypot(b.x,b.y)
|
||||
mg = r^n
|
||||
if b.x=0 then
|
||||
ay=.5*pi
|
||||
if b.y<0 then ay=-ay
|
||||
else
|
||||
an = atan(b.y,b.x)
|
||||
end if
|
||||
an *= n
|
||||
a.x = mg * cos(an)
|
||||
a.y = mg * sin(an)
|
||||
end method
|
||||
|
||||
method show() as string
|
||||
return str(x,14) ", " str(y,14)
|
||||
end method
|
||||
|
||||
end class
|
||||
|
||||
'#recordof complexop
|
||||
|
||||
'====
|
||||
'TEST
|
||||
'====
|
||||
|
||||
complex z1,z2,z3,z4,z5
|
||||
|
||||
'ENTER VALUES
|
||||
|
||||
z1 <= 0, 0
|
||||
z2 <= 2, 1
|
||||
z3 <= -2, 1
|
||||
z4 <= 2, 4
|
||||
z5 <= 1, 1
|
||||
|
||||
'EVALUATE COMPLEX EXPRESSIONS
|
||||
|
||||
z1 = z2 * z3
|
||||
print "Z1 = "+z1.show 'RESULT -5.0, 0
|
||||
|
||||
z1 = z3+(z2.power(2))
|
||||
print "Z1 = "+z1.show 'RESULT 1.0, 5.0
|
||||
|
||||
|
||||
z1 = z5/z4
|
||||
print "Z1 = "+z1.show 'RESULT 0.3, 0.1
|
||||
|
||||
z1 = z5/z1
|
||||
print "Z1 = "+z1.show 'RESULT 2.0, 4.0
|
||||
|
||||
z1 = z2/z4
|
||||
print "Z1 = "+z1.show 'RESULT -0.4, -0.3
|
||||
|
||||
z1 = z1*z4
|
||||
print "Z1 = "+z1.show 'RESULT 2.0, 1.0
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
add(a,b)=a+b;
|
||||
mult(a,b)=a*b;
|
||||
neg(a)=-a;
|
||||
inv(a)=1/a;
|
||||
21
Task/Arithmetic-Complex/PL-I/arithmetic-complex.pli
Normal file
21
Task/Arithmetic-Complex/PL-I/arithmetic-complex.pli
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* PL/I complex numbers may be integer or floating-point. */
|
||||
/* In this example, the variables are floating-pint. */
|
||||
/* For integer variables, change 'float' to 'fixed binary' */
|
||||
|
||||
declare (a, b) complex float;
|
||||
a = 2+5i;
|
||||
b = 7-6i;
|
||||
|
||||
put skip list (a+b);
|
||||
put skip list (a - b);
|
||||
put skip list (a*b);
|
||||
put skip list (a/b);
|
||||
put skip list (a**b);
|
||||
put skip list (1/a);
|
||||
put skip list (conjg(a)); /* gives the conjugate of 'a'. */
|
||||
|
||||
/* Functions exist for extracting the real and imaginary parts */
|
||||
/* of a complex number. */
|
||||
|
||||
/* As well, trigonometric functions may be used with complex */
|
||||
/* numbers, such as SIN, COS, TAN, ATAN, and so on. */
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue