Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
6
Task/Arithmetic-Complex/0DESCRIPTION
Normal file
6
Task/Arithmetic-Complex/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
A '''[[wp:Complex number|complex number]]''' is a number which can be written as "<math>a + b \times i</math>" (sometimes shown as "<math>b + a \times i</math>") where a and b are real numbers and [[wp:Imaginary_unit|<math>i</math> is the square root of -1]]. Typically, complex numbers are represented as a pair of real numbers called the "imaginary part" and "real part", where the imaginary part is the number to be multiplied by <math>i</math>.
|
||||
|
||||
* 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. By definition, the [[wp:complex conjugate|complex conjugate]] of <math>a + bi</math> is <math>a - bi</math>.
|
||||
|
||||
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.
|
||||
2
Task/Arithmetic-Complex/1META.yaml
Normal file
2
Task/Arithmetic-Complex/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Arithmetic operations
|
||||
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
|
||||
)
|
||||
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
|
||||
46
Task/Arithmetic-Complex/Ada/arithmetic-complex.ada
Normal file
46
Task/Arithmetic-Complex/Ada/arithmetic-complex.ada
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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
|
||||
C := Conjugate (C);
|
||||
end Complex_Operations;
|
||||
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")
|
||||
}
|
||||
46
Task/Arithmetic-Complex/BASIC/arithmetic-complex.bas
Normal file
46
Task/Arithmetic-Complex/BASIC/arithmetic-complex.bas
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
TYPE complex
|
||||
real AS DOUBLE
|
||||
imag AS DOUBLE
|
||||
END TYPE
|
||||
DECLARE SUB add (a AS complex, b AS complex, c AS complex)
|
||||
DECLARE SUB mult (a AS complex, b AS complex, c AS complex)
|
||||
DECLARE SUB inv (a AS complex, b AS complex)
|
||||
DECLARE SUB neg (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
|
||||
CALL add(x, y, z)
|
||||
PRINT z.real; "+"; z.imag; "i"
|
||||
CALL mult(x, y, z)
|
||||
PRINT z.real; "+"; z.imag; "i"
|
||||
CALL inv(x, z)
|
||||
PRINT z.real; "+"; z.imag; "i"
|
||||
CALL neg(x, z)
|
||||
PRINT z.real; "+"; z.imag; "i"
|
||||
|
||||
|
||||
SUB add (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
|
||||
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");
|
||||
}
|
||||
|
|
@ -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,7 @@
|
|||
> coffee complex.coffee
|
||||
(5 + 3i) + (4 - 3i) = 9
|
||||
(5 + 3i) * (4 - 3i) = 29 - 3i
|
||||
-1 * (4 - 3i) = -4 + 3i
|
||||
(5 + 3i) - (4 - 3i) = 1 + 6i
|
||||
1 / (4 - 3i) = 0.16 + 0.12i
|
||||
(29 - 3i) / (4 - 3i) = 5 + 3i
|
||||
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/Forth/arithmetic-complex.fth
Normal file
13
Task/Arithmetic-Complex/Forth/arithmetic-complex.fth
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
include complex.seq
|
||||
|
||||
: ZNEGATE ( r i -- -r -i ) fswap fnegate fswap fnegate ;
|
||||
|
||||
zvariable x
|
||||
zvariable y
|
||||
1e 1e x z!
|
||||
pi 1.2e y z!
|
||||
|
||||
x z@ y z@ z+ z.
|
||||
x z@ y z@ z* z.
|
||||
1+0i x z@ z/ z.
|
||||
x z@ znegate z.
|
||||
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
|
||||
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
|
||||
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))
|
||||
}
|
||||
14
Task/Arithmetic-Complex/Haskell/arithmetic-complex-1.hs
Normal file
14
Task/Arithmetic-Complex/Haskell/arithmetic-complex-1.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)
|
||||
8
Task/Arithmetic-Complex/Haskell/arithmetic-complex-2.hs
Normal file
8
Task/Arithmetic-Complex/Haskell/arithmetic-complex-2.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
*Main> main
|
||||
Add: 5.0 :+ 2.0
|
||||
Subtract: (-3.0) :+ 2.0
|
||||
Multiply: 4.0 :+ 8.0
|
||||
Divide: 0.25 :+ 0.5
|
||||
Negate: (-1.0) :+ (-2.0)
|
||||
Inverse: 0.2 :+ (-0.4)
|
||||
Conjugate:1.0 :+ (-2.0)
|
||||
44
Task/Arithmetic-Complex/Java/arithmetic-complex.java
Normal file
44
Task/Arithmetic-Complex/Java/arithmetic-complex.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
public class Complex{
|
||||
public final double real;
|
||||
public final double imag;
|
||||
|
||||
public Complex(){this(0,0)}//default values to 0...force of habit
|
||||
public Complex(double r, double i){real = r; imag = i;}
|
||||
|
||||
public Complex add(Complex b){
|
||||
return new Complex(this.real + b.real, this.imag + b.imag);
|
||||
}
|
||||
|
||||
public Complex mult(Complex b){
|
||||
//FOIL of (a+bi)(c+di) with i*i = -1
|
||||
return new Complex(this.real * b.real - this.imag * b.imag, this.real * b.imag + this.imag * b.real);
|
||||
}
|
||||
|
||||
public Complex 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);
|
||||
}
|
||||
|
||||
public String toString(){ //override Object's 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) )
|
||||
}
|
||||
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)
|
||||
10
Task/Arithmetic-Complex/Perl/arithmetic-complex.pl
Normal file
10
Task/Arithmetic-Complex/Perl/arithmetic-complex.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use Math::Complex;
|
||||
my $a = 1 + 1*i;
|
||||
my $b = 3.14159 + 1.25*i;
|
||||
|
||||
print "$_\n" foreach
|
||||
$a + $b, # addition
|
||||
$a * $b, # multiplication
|
||||
-$a, # negation
|
||||
1 / $a, # multiplicative inverse
|
||||
~$a; # complex conjugate
|
||||
42
Task/Arithmetic-Complex/PicoLisp/arithmetic-complex.l
Normal file
42
Task/Arithmetic-Complex/PicoLisp/arithmetic-complex.l
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
(load "@lib/math.l")
|
||||
|
||||
(de addComplex (A B)
|
||||
(cons
|
||||
(+ (car A) (car B)) # Real
|
||||
(+ (cdr A) (cdr B)) ) ) # Imag
|
||||
|
||||
(de mulComplex (A B)
|
||||
(cons
|
||||
(-
|
||||
(*/ (car A) (car B) 1.0)
|
||||
(*/ (cdr A) (cdr B) 1.0) )
|
||||
(+
|
||||
(*/ (car A) (cdr B) 1.0)
|
||||
(*/ (cdr A) (car B) 1.0) ) ) )
|
||||
|
||||
(de invComplex (A)
|
||||
(let Denom
|
||||
(+
|
||||
(*/ (car A) (car A) 1.0)
|
||||
(*/ (cdr A) (cdr A) 1.0) )
|
||||
(cons
|
||||
(*/ (car A) 1.0 Denom)
|
||||
(- (*/ (cdr A) 1.0 Denom)) ) ) )
|
||||
|
||||
(de negComplex (A)
|
||||
(cons (- (car A)) (- (cdr A))) )
|
||||
|
||||
(de fmtComplex (A)
|
||||
(pack
|
||||
(round (car A) (dec *Scl))
|
||||
(and (gt0 (cdr A)) "+")
|
||||
(round (cdr A) (dec *Scl))
|
||||
"i" ) )
|
||||
|
||||
(let (A (1.0 . 1.0) B (cons pi 1.2))
|
||||
(prinl "A = " (fmtComplex A))
|
||||
(prinl "B = " (fmtComplex B))
|
||||
(prinl "A+B = " (fmtComplex (addComplex A B)))
|
||||
(prinl "A*B = " (fmtComplex (mulComplex A B)))
|
||||
(prinl "1/A = " (fmtComplex (invComplex A)))
|
||||
(prinl "-A = " (fmtComplex (negComplex A))) )
|
||||
23
Task/Arithmetic-Complex/Python/arithmetic-complex.py
Normal file
23
Task/Arithmetic-Complex/Python/arithmetic-complex.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
>>> z1 = 1.5 + 3j
|
||||
>>> z2 = 1.5 + 1.5j
|
||||
>>> z1 + z2
|
||||
(3+4.5j)
|
||||
>>> z1 - z2
|
||||
1.5j
|
||||
>>> z1 * z2
|
||||
(-2.25+6.75j)
|
||||
>>> z1 / z2
|
||||
(1.5+0.5j)
|
||||
>>> - z1
|
||||
(-1.5-3j)
|
||||
>>> z1.conjugate()
|
||||
(1.5-3j)
|
||||
>>> abs(z1)
|
||||
3.3541019662496847
|
||||
>>> z1 ** z2
|
||||
(-1.1024829553277784-0.38306415117199333j)
|
||||
>>> z1.real
|
||||
1.5
|
||||
>>> z1.imag
|
||||
3.0
|
||||
>>>
|
||||
13
Task/Arithmetic-Complex/R/arithmetic-complex.r
Normal file
13
Task/Arithmetic-Complex/R/arithmetic-complex.r
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
z1 <- 1.5 + 3i
|
||||
z2 <- 1.5 + 1.5i
|
||||
print(z1 + z2) # 3+4.5i
|
||||
print(z1 - z2) # 0+1.5i
|
||||
print(z1 * z2) # -2.25+6.75i
|
||||
print(z1 / z2) # 1.5+0.5i
|
||||
print(-z1) # -1.5-3i
|
||||
print(Conj(z1)) # 1.5-3i
|
||||
print(abs(z1)) # 3.354102
|
||||
print(z1^z2) # -1.102483-0.383064i
|
||||
print(exp(z1)) # -4.436839+0.632456i
|
||||
print(Re(z1)) # 1.5
|
||||
print(Im(z1)) # 3
|
||||
25
Task/Arithmetic-Complex/REXX/arithmetic-complex.rexx
Normal file
25
Task/Arithmetic-Complex/REXX/arithmetic-complex.rexx
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*REXX program to show how to support math functions for complex numbers*/
|
||||
|
||||
x = '(5,3i)' /*this little piggy uses "I" (or "i") ... */
|
||||
y = '( .5, 6j)' /*this little piggy uses "J" (or "j") ... */
|
||||
|
||||
sum = Cadd(x,y); say ' addition: ' x " + " y ' = ' sum
|
||||
dif = Csub(x,y); say ' subtration: ' x " + " y ' = ' dif
|
||||
prod = Cmul(x,y); say 'multiplication: ' x " * " y ' = ' prod
|
||||
quot = Cdiv(x,y); say ' division: ' x " ÷ " y ' = ' quot
|
||||
inv = Cinv(x); say ' inverse: ' x " = " inv
|
||||
cnjX = Ccnj(x); say ' conjugate of: ' x " = " cnjX
|
||||
negX = Cneg(x); say ' negation of: ' x " = " negX
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
|
||||
/*─────────────────────────────────────one─liners───────────────────────*/
|
||||
Ccnj: procedure;arg a ',' b,c ',' d;call Cg;r1=a;r2=-b;return Cr()
|
||||
Cadd: procedure;arg a ',' b,c ',' d;call Cg;r1=a+c;r2=b+d;return Cr()
|
||||
Csub: procedure;arg a ',' b,c ',' d;call Cg;r1=a-c;r2=b-d;return Cr()
|
||||
Cmul: procedure;arg a ',' b,c ',' d;call Cg;r1=a*c-b*d; r2=b*c+a*d;return Cr()
|
||||
Cdiv: procedure;arg a ',' b,c ',' d;call Cg;_=c*c+d*d;r1=(a*c+b*d)/_;r2=(b*c-a*d)/_;return Cr()
|
||||
Cg: a=Cdej(a); b=Cdej(b); c=Cdej(c); d=Cdej(d); return
|
||||
Cr: _='['r1; if r2\=0 then _=_','r2"j"; return _']'
|
||||
Cdej: return word(translate(arg(1),,'{[(JI)]}') 0,1)
|
||||
Cneg: return Cmul(arg(1),-1)
|
||||
Cinv: return Cdiv(1,arg(1))
|
||||
12
Task/Arithmetic-Complex/Racket/arithmetic-complex.rkt
Normal file
12
Task/Arithmetic-Complex/Racket/arithmetic-complex.rkt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#lang racket
|
||||
|
||||
(define a 3+4i)
|
||||
(define b 8+0i)
|
||||
|
||||
(+ a b) ; addition
|
||||
(- a b) ; subtraction
|
||||
(/ a b) ; division
|
||||
(* a b) ; multiplication
|
||||
(- a) ; negation
|
||||
(/ 1 a) ; reciprocal
|
||||
(conjugate a) ; conjugation
|
||||
14
Task/Arithmetic-Complex/Ruby/arithmetic-complex-1.rb
Normal file
14
Task/Arithmetic-Complex/Ruby/arithmetic-complex-1.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
require 'complex' # With Ruby 1.9, this line is optional.
|
||||
|
||||
# Two ways to write complex numbers:
|
||||
a = Complex(1, 1) # 1. call Kernel#Complex
|
||||
i = Complex::I # 2. use Complex::I
|
||||
b = 3.14159 + 1.25 * i
|
||||
|
||||
# Operations:
|
||||
puts a + b # addition
|
||||
puts a * b # multiplication
|
||||
puts -a # negation
|
||||
puts 1.quo a # multiplicative inverse
|
||||
puts a.conjugate # complex conjugate
|
||||
puts a.conj # alias for complex conjugate
|
||||
4
Task/Arithmetic-Complex/Ruby/arithmetic-complex-2.rb
Normal file
4
Task/Arithmetic-Complex/Ruby/arithmetic-complex-2.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Other ways to find the multiplicative inverse:
|
||||
puts 1.quo a # always works
|
||||
puts 1.0 / a # works, but forces floating-point math
|
||||
puts 1 / a # might truncate to integer
|
||||
35
Task/Arithmetic-Complex/Scala/arithmetic-complex-1.scala
Normal file
35
Task/Arithmetic-Complex/Scala/arithmetic-complex-1.scala
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package org.rosettacode
|
||||
|
||||
package object ArithmeticComplex {
|
||||
val i = Complex(0, 1)
|
||||
|
||||
implicit def fromDouble(d: Double) = Complex(d)
|
||||
implicit def fromInt(i: Int) = Complex(i.toDouble)
|
||||
}
|
||||
|
||||
package ArithmeticComplex {
|
||||
case class Complex(real: Double = 0.0, imag: Double = 0.0) {
|
||||
def this(s: String) =
|
||||
this("[\\d.]+(?!i)".r findFirstIn s getOrElse "0" toDouble,
|
||||
"[\\d.]+(?=i)".r findFirstIn s getOrElse "0" toDouble)
|
||||
|
||||
def +(b: Complex) = Complex(real + b.real, imag + b.imag)
|
||||
def -(b: Complex) = Complex(real - b.real, imag - b.imag)
|
||||
def *(b: Complex) = Complex(real * b.real - imag * b.imag, real * b.imag + imag * b.real)
|
||||
def inverse = {
|
||||
val denom = real * real + imag * imag
|
||||
Complex(real / denom, -imag / denom)
|
||||
}
|
||||
def /(b: Complex) = this * b.inverse
|
||||
def unary_- = Complex(-real, -imag)
|
||||
lazy val abs = math.hypot(real, imag)
|
||||
override def toString = real + " + " + imag + "i"
|
||||
|
||||
def i = { require(imag == 0.0); Complex(imag = real) }
|
||||
}
|
||||
|
||||
object Complex {
|
||||
def apply(s: String) = new Complex(s)
|
||||
def fromPolar(rho:Double, theta:Double) = Complex(rho*math.cos(theta), rho*math.sin(theta))
|
||||
}
|
||||
}
|
||||
26
Task/Arithmetic-Complex/Scala/arithmetic-complex-2.scala
Normal file
26
Task/Arithmetic-Complex/Scala/arithmetic-complex-2.scala
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
scala> import org.rosettacode.ArithmeticComplex._
|
||||
import org.rosettacode.ArithmeticComplex._
|
||||
|
||||
scala> 1 + i
|
||||
res0: org.rosettacode.ArithmeticComplex.Complex = 1.0 + 1.0i
|
||||
|
||||
scala> 1 + 2 * i
|
||||
res1: org.rosettacode.ArithmeticComplex.Complex = 1.0 + 2.0i
|
||||
|
||||
scala> 2 + 1.i
|
||||
res2: org.rosettacode.ArithmeticComplex.Complex = 2.0 + 1.0i
|
||||
|
||||
scala> res0 + res1
|
||||
res3: org.rosettacode.ArithmeticComplex.Complex = 2.0 + 3.0i
|
||||
|
||||
scala> res1 * res2
|
||||
res4: org.rosettacode.ArithmeticComplex.Complex = 0.0 + 5.0i
|
||||
|
||||
scala> res2 / res0
|
||||
res5: org.rosettacode.ArithmeticComplex.Complex = 1.5 + -0.5i
|
||||
|
||||
scala> res1.inverse
|
||||
res6: org.rosettacode.ArithmeticComplex.Complex = 0.2 + -0.4i
|
||||
|
||||
scala> -res6
|
||||
res7: org.rosettacode.ArithmeticComplex.Complex = -0.2 + 0.4i
|
||||
7
Task/Arithmetic-Complex/Scheme/arithmetic-complex.ss
Normal file
7
Task/Arithmetic-Complex/Scheme/arithmetic-complex.ss
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(define a 1+i)
|
||||
(define b 3.14159+1.25i)
|
||||
|
||||
(define c (+ a b))
|
||||
(define c (* a b))
|
||||
(define c (/ 1 a))
|
||||
(define c (- a))
|
||||
13
Task/Arithmetic-Complex/Smalltalk/arithmetic-complex.st
Normal file
13
Task/Arithmetic-Complex/Smalltalk/arithmetic-complex.st
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
PackageLoader fileInPackage: 'Complex'.
|
||||
|a b|
|
||||
a := 1 + 1 i.
|
||||
b := 3.14159 + 1.2 i.
|
||||
(a + b) displayNl.
|
||||
(a * b) displayNl.
|
||||
(a / b) displayNl.
|
||||
a reciprocal displayNl.
|
||||
a conjugate displayNl.
|
||||
a abs displayNl.
|
||||
a real displayNl.
|
||||
a imaginary displayNl.
|
||||
a negated displayNl.
|
||||
9
Task/Arithmetic-Complex/Tcl/arithmetic-complex.tcl
Normal file
9
Task/Arithmetic-Complex/Tcl/arithmetic-complex.tcl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package require math::complexnumbers
|
||||
namespace import math::complexnumbers::*
|
||||
|
||||
set a [complex 1 1]
|
||||
set b [complex 3.14159 1.2]
|
||||
puts [tostring [+ $a $b]] ;# ==> 4.14159+2.2i
|
||||
puts [tostring [* $a $b]] ;# ==> 1.94159+4.34159i
|
||||
puts [tostring [pow $a [complex -1 0]]] ;# ==> 0.5-0.4999999999999999i
|
||||
puts [tostring [- $a]] ;# ==> -1.0-i
|
||||
Loading…
Add table
Add a link
Reference in a new issue