Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View 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

View 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

View 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

View 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

View file

@ -0,0 +1,3 @@
(defrecord complex
real
img)

View 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)))

View file

@ -0,0 +1,3 @@
(defun conj
(((match-complex real r img i))
(new r (* -1 i))))

View 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)))))

View 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")))

View file

@ -0,0 +1,10 @@
import complex
var a: TComplex = (1.0,1.0)
var b: TComplex = (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))

View file

@ -0,0 +1,26 @@
Number 100 Class newPriority: Complex(re, im)
Complex method: re @re ;
Complex method: im @im ;
Complex method: initialize := im := re ;
Complex method: << '(' <<c @re << ',' <<c @im << ')' <<c ;
Integer method: asComplex self 0 Complex new ;
Float method: asComplex self 0 Complex new ;
Complex new(0, 1) Constant new: I
Complex method: ==(c) c re @re == c im @im == and ;
Complex method: norm @re sq @im sq + sqrt ;
Complex method: conj Complex new(@re, @im neg) ;
Complex method: +(c) Complex new(c re @re +, c im @im +) ;
Complex method: -(c) Complex new(c re @re -, c im @im -) ;
Complex method: *(c) Complex new(c re @re * c im @im * -, c re @im * @re c im * + ) ;
Complex method: inv
| n |
@re sq @im sq + asFloat ->n
Complex new(@re n /, @im neg n / ) ;
Complex method: /(c) c self inv * ;

View file

@ -0,0 +1,4 @@
2 3.2 I * + .cr
Complex new(2, 3) 1.2 + .cr
Complex new(2, 3) 1.2 * .cr
2 Complex new(2, 3) / .cr

View file

@ -0,0 +1,59 @@
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 sq_add(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 sq_uminus(a)
end function
function scomplex(complex a)
sequence s = ""
atom ar, ai
{ar, ai} = a
if ar!=0 then
s = sprintf("%g",ar)
end if
if ai!=0 then
if ai=1 then
s &= "+i"
elsif ai=-1 then
s &= "-i"
else
s &= sprintf("%+gi",ai)
end if
end if
if length(s)=0 then
return "0"
end if
return s
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))})

View file

@ -0,0 +1,13 @@
var a = 1:1; # Complex(1, 1)
var b = 3.14159:1.25; # Complex(3.14159, 1.25)
[ a + b, # addition
a * b, # multiplication
-a, # negation
1 / a, # multiplicative inverse
~a, # complex conjugate
a.abs, # abs
a.sqrt, # sqrt
b.re, # real
b.im, # imaginary
].each { |c| say c }

View file

@ -0,0 +1,45 @@
public struct Complex {
public let real : Double
public let imaginary : Double
public init(real inReal:Double, imaginary inImaginary:Double) {
real = inReal
imaginary = inImaginary
}
public static var i : Complex = Complex(real:0, imaginary: 1)
public static var zero : Complex = Complex(real: 0, imaginary: 0)
public var negate : Complex {
return Complex(real: -real, imaginary: -imaginary)
}
public var invert : Complex {
let d = (real*real + imaginary*imaginary)
return Complex(real: real/d, imaginary: -imaginary/d)
}
public var conjugate : Complex {
return Complex(real: real, imaginary: -imaginary)
}
}
public func + (left: Complex, right: Complex) -> Complex {
return Complex(real: left.real+right.real, imaginary: left.imaginary+right.imaginary)
}
public func * (left: Complex, right: Complex) -> Complex {
return Complex(real: left.real*right.real - left.imaginary*right.imaginary,
imaginary: left.real*right.imaginary+left.imaginary*right.real)
}
public prefix func - (right:Complex) -> Complex {
return right.negate
}
// Checking equality is almost necessary for a struct of this type to be useful
extension Complex : Equatable {}
public func == (left:Complex, right:Complex) -> Bool {
return left.real == right.real && left.imaginary == right.imaginary
}

View file

@ -0,0 +1,24 @@
extension Complex : CustomStringConvertible {
public var description : String {
guard real != 0 || imaginary != 0 else { return "0" }
let rs : String = real != 0 ? "\(real)" : ""
let iS : String
let sign : String
let iSpace = real != 0 ? " " : ""
switch imaginary {
case let i where i < 0:
sign = "-"
iS = i == -1 ? "i" : "\(-i)i"
case let i where i > 0:
sign = real != 0 ? "+" : ""
iS = i == 1 ? "i" : "\(i)i"
default:
sign = ""
iS = ""
}
return "\(rs)\(iSpace)\(sign)\(iSpace)\(iS)"
}
}

View file

@ -0,0 +1,10 @@
public func - (left:Complex, right:Complex) -> Complex {
return left + -right
}
public func / (divident:Complex, divisor:Complex) -> Complex {
let rc = divisor.conjugate
let num = divident * rc
let den = divisor * rc
return Complex(real: num.real/den.real, imaginary: num.imaginary/den.real)
}

View file

@ -0,0 +1,30 @@
@class Complex {
&[r i] @: {
^r || r 0
^i || i 0
^m +@sq^r @sq^i
}
add &o @new Complex[+ ^r o.r + ^i o.i]
mul &o @new Complex[-* ^r o.r * ^i o.i +* ^r o.i * ^i o.r]
neg &^ @new Complex[@-^r @-^i]
inv &^ @new Complex[/ ^r ^m / @-^i ^m]
toString &^?{
=^i 0 "{^r}"
=^r 0 "{^i}i"
>^i 0 "{^r} + {^i}i"
"{^r} - {@-^i}i"
}
}
@vars {
a @new Complex[5 3]
b @new Complex[4 3N]
}
@each &x !console.log x [
"({a}) + ({b}) = {!a.add b}"
"({a}) * ({b}) = {!a.mul b}"
"-1 * ({b}) = {b.neg.}"
"({a}) - ({b}) = {!a.add b.neg.}"
"1 / ({b}) = {b.inv.}"
"({!a.mul b}) / ({b}) = {`!.mul b.inv. !a.mul b}"
]

View file

@ -0,0 +1,62 @@
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 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] )

View 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]"