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,104 @@
PROGRAM QUATERNION
!$DOUBLE
TYPE QUATERNION=(A,B,C,D)
DIM Q:QUATERNION,Q1:QUATERNION,Q2:QUATERNION
DIM R:QUATERNION,S:QUATERNION,T:QUATERNION
PROCEDURE NORM(T.->NORM)
NORM=SQR(T.A*T.A+T.B*T.B+T.C*T.C+T.D*T.D)
END PROCEDURE
PROCEDURE NEGATIVE(T.->T.)
T.A=-T.A
T.B=-T.B
T.C=-T.C
T.D=-T.D
END PROCEDURE
PROCEDURE CONJUGATE(T.->T.)
T.A=T.A
T.B=-T.B
T.C=-T.C
T.D=-T.D
END PROCEDURE
PROCEDURE ADD_REAL(T.,REAL->T.)
T.A=T.A+REAL
T.B=T.B
T.C=T.C
T.D=T.D
END PROCEDURE
PROCEDURE ADD(T.,S.->T.)
T.A=T.A+S.A
T.B=T.B+S.B
T.C=T.C+S.C
T.D=T.D+S.D
END PROCEDURE
PROCEDURE MULT_REAL(T.,REAL->T.)
T.A=T.A*REAL
T.B=T.B*REAL
T.C=T.C*REAL
T.D=T.D*REAL
END PROCEDURE
PROCEDURE MULT(T.,S.->R.)
R.A=T.A*S.A-T.B*S.B-T.C*S.C-T.D*S.D
R.B=T.A*S.B+T.B*S.A+T.C*S.D-T.D*S.C
R.C=T.A*S.C-T.B*S.D+T.C*S.A+T.D*S.B
R.D=T.A*S.D+T.B*S.C-T.C*S.B+T.D*S.A
END PROCEDURE
PROCEDURE PRINTQ(T.)
PRINT("(";T.A;",";T.B;",";T.C;",";T.D;")")
END PROCEDURE
BEGIN
Q.A=1 Q.B=2 Q.C=3 Q.D=4
Q1.A=2 Q1.B=3 Q1.C=4 Q1.D=5
Q2.A=3 Q2.B=4 Q2.C=5 Q2.D=6
REAL=7
NORM(Q.->NORM)
PRINT("Norm(q)=";NORM)
NEGATIVE(Q.->T.)
PRINT("Negative(q) =";)
PRINTQ(T.)
CONJUGATE(Q.->T.)
PRINT("Conjugate(q) =";)
PRINTQ(T.)
ADD_REAL(Q.,REAL->T.)
PRINT("q + real =";)
PRINTQ(T.)
! addition is commutative
ADD(Q1.,Q2.->T.)
PRINT("q1 + q2 =";)
PRINTQ(T.)
ADD(Q2.,Q1.->T.)
PRINT("q2 + q1 = ";)
PRINTQ(T.)
MULT_REAL(Q.,REAL->T.)
PRINT("q * real =";)
PRINTQ(T.)
! multiplication is not commutative
MULT(Q1.,Q2.->R.)
PRINT("q1 * q2=";)
PRINTQ(R.)
MULT(Q2.,Q1.->R.)
PRINT("q2 * q1=";)
PRINTQ(R.)
END PROGRAM

View file

@ -0,0 +1,102 @@
#import <Foundation/Foundation.h>
interface Quaternion : Number
// Properties -- note that this is an immutable class.
double real, i, j, k {readonly}
end
implementation Quaternion
initWithReal: double, i: double, j: double, k: double, return instancetype
self = super.init
if self
_real = real; _i = i; _j = j; _k = k
return self
+new: double real, ..., return instancetype
va_list args
va_start(args, real)
object := Quaternion.alloc.initWithReal: real,
i: va_arg(args, double),
j: va_arg(args, double),
k: va_arg(args, double)
va_end(args)
return object
descriptionWithLocale: id, return String = String.stringWithFormat:
'(%.1f, %.1f, %.1f, %.1f)', self.real, self.i, self.j, self.k
norm, return double =
sqrt(self.real * self.real +
self.i * self.i + self.j * self.j + self.k * self.k)
negative, return Quaternion =
Quaternion.new: -self.real, -self.i, -self.j, -self.k
conjugate, return Quaternion =
Quaternion.new: self.real, -self.i, -self.j, -self.k
// Overload "+" operator (left operand is Quaternion)
plus: Number operand, return Quaternion
real := self.real, i = self.i, j = self.j, k = self.k
if operand.isKindOfClass: Quaternion.class
q := (Quaternion)operand
real += q.real; i += q.i; j += q.j; k += q.k
else
real += (double)operand
return Quaternion.new: real, i, j, k
// Overload "*" operator (left operand is Quaternion)
multipliedBy: Number operand, return Quaternion
real := self.real, i = self.i, j = self.j, k = self.k
if operand.isKindOfClass: Quaternion.class
q := (Quaternion)operand
real = self.real * q.real - self.i* q.i - self.j * q.j - self.k * q.k
i = self.real * q.i + self.i * q.real + self.j * q.k - self.k * q.j
j = self.real * q.j - self.i * q.k + self.j * q.real + self.k * q.i
k = self.real * q.k + self.i * q.j - self.j * q.i + self.k * q.real
else
real *= (double)operand
i *= (double)operand; j *= (double)operand; k *= (double)operand
return Quaternion.new: real, i, j, k
end
implementation Number (QuaternionOperators)
// Overload "+" operator (left operand is Number)
plus: Quaternion operand, return Quaternion
real := (double)self + operand.real
return Quaternion.new: real, operand.i, operand.j, operand.k
// Overload "*" operator (left operand is Number)
multipliedBy: Quaternion operand, return Quaternion
r := (double)self
return Quaternion.new: r * operand.real, r * operand.i,
r * operand.j, r * operand.k
end
int main()
autoreleasepool
q := Quaternion.new: 1.0, 2.0, 3.0, 4.0
q1 := Quaternion.new: 2.0, 3.0, 4.0, 5.0
q2 := Quaternion.new: 3.0, 4.0, 5.0, 6.0
Log( 'q = %@', q )
Log( 'q1 = %@', q1 )
Log( 'q2 = %@\n\n', q2 )
Log( 'q norm = %.3f', q.norm )
Log( 'q negative = %@', q.negative )
Log( 'q conjugate = %@', q.conjugate )
Log( '7 + q = %@', 7.0 + q )
Log( 'q + 7 = %@', q + 7.0 )
Log( 'q1 + q2 = %@', q1 + q2 )
Log( '7 * q = %@', 7 * q)
Log( 'q * 7 = %@', q * 7.0 )
Log( 'q1 * q2 = %@', q1 * q2 )
Log( 'q2 * q1 = %@', q2 * q1 )
return 0

View file

@ -0,0 +1,24 @@
160 Number Class newPriority: Quaternion(a, b, c, d)
Quaternion method: _a @a ;
Quaternion method: _b @b ;
Quaternion method: _c @c ;
Quaternion method: _d @d ;
Quaternion method: initialize := d := c := b := a ;
Quaternion method: << '(' <<c @a << ',' <<c @b << ',' <<c @c << ',' <<c @d << ')' <<c ;
Integer method: asQuaternion self 0 0 0 Quaternion new ;
Float method: asQuaternion self 0 0 0 Quaternion new ;
Quaternion method: ==(q) q _a @a == q _b @b == and q _c @c == and q _d @d == and ;
Quaternion method: norm @a sq @b sq + @c sq + @d sq + sqrt ;
Quaternion method: conj @a @b neg @c neg @d neg Quaternion new ;
Quaternion method: +(q) Quaternion new(q _a @a +, q _b @b +, q _c @c +, q _d @d +) ;
Quaternion method: -(q) Quaternion new(q _a @a -, q _b @b -, q _c @c -, q _d @d -) ;
Quaternion method: *(q)
Quaternion new(q _a @a * q _b @b * - q _c @c * - q _d @d * -,
q _a @b * q _b @a * + q _c @d * + q _d @c * -,
q _a @c * q _b @d * - q _c @a * + q _d @b * +,
q _a @d * q _b @c * + q _c @b * - q _d @a * + ) ;

View file

@ -0,0 +1,20 @@
: test
| q q1 q2 r |
Quaternion new(1, 2, 3, 4) ->q
Quaternion new(2, 3, 4, 5) ->q1
Quaternion new(3, 4, 5, 6) ->q2
7.0 -> r
System.Out "q = " << q << cr
System.Out "q1 = " << q1 << cr
System.Out "q2 = " << q2 << cr
System.Out "norm q = " << q norm << cr
System.Out "neg q = " << q neg << cr
System.Out "conj q = " << q conj << cr
System.Out "q +r = " << q r + << cr
System.Out "q1 + q2 = " << q1 q2 + << cr
System.Out "q * r = " << q r * << cr
System.Out "q1 * q2 = " << q1 q2 * << cr
q1 q2 * q2 q1 * == ifFalse: [ "q1q2 and q2q1 are different quaternions" println ] ;

View file

@ -0,0 +1,54 @@
function norm(sequence q)
return sqrt(sum(sq_power(q,2)))
end function
function conj(sequence q)
q[2..4] = sq_uminus(q[2..4])
return q
end function
function add(object q1, object q2)
if atom(q1)!=atom(q2) then
if atom(q1) then
q1 = {q1,0,0,0}
else
q2 = {q2,0,0,0}
end if
end if
return sq_add(q1,q2)
end function
function mul(object q1, object q2)
if sequence(q1) and sequence(q2) then
return { q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3] - q1[4]*q2[4],
q1[1]*q2[2] + q1[2]*q2[1] + q1[3]*q2[4] - q1[4]*q2[3],
q1[1]*q2[3] - q1[2]*q2[4] + q1[3]*q2[1] + q1[4]*q2[2],
q1[1]*q2[4] + q1[2]*q2[3] - q1[3]*q2[2] + q1[4]*q2[1] }
else
return sq_mul(q1,q2)
end if
end function
function quats(sequence q)
return sprintf("%g + %gi + %gj + %gk",q)
end function
constant
q = {1, 2, 3, 4},
q1 = {2, 3, 4, 5},
q2 = {3, 4, 5, 6},
r = 7
printf(1, "q = %s\n", {quats(q)})
printf(1, "r = %g\n", r)
printf(1, "norm(q) = %g\n", norm(q))
printf(1, "-q = %s\n", {quats(-q)})
printf(1, "conj(q) = %s\n", {quats(conj(q))})
printf(1, "q + r = %s\n", {quats(add(q,r))})
printf(1, "q * r = %s\n", {quats(mul(q,r))})
printf(1, "q1 = %s\n", {quats(q1)})
printf(1, "q2 = %s\n", {quats(q2)})
printf(1, "q1 + q2 = %s\n", {quats(add(q1,q2))})
printf(1, "q2 + q1 = %s\n", {quats(add(q2,q1))})
printf(1, "q1 * q2 = %s\n", {quats(mul(q1,q2))})
printf(1, "q2 * q1 = %s\n", {quats(mul(q2,q1))})

View file

@ -0,0 +1,39 @@
class Quaternion(r, i, j, k) {
func qu(*r) { Quaternion(r...) }
method to_s { "#{r} + #{i}i + #{j}j + #{k}k" }
method reals { [r, i, j, k] }
method conj { qu(r, -i, -j, -k) }
method norm { self.reals.map { _*_ }.sum.sqrt }
method ==(Quaternion b) { self.reals == b.reals }
method +(Number b) { qu(b+r, i, j, k) }
method +(Quaternion b) { qu((self.reals ~Z+ b.reals)...) }
method neg { qu(self.reals.map{ .neg }...) }
method *(Number b) { qu((self.reals»*»b)...) }
method *(Quaternion b) {
var (r,i,j,k) = b.reals...
qu(sum(self.reals ~Z* [r, -i, -j, -k]),
sum(self.reals ~Z* [i, r, k, -j]),
sum(self.reals ~Z* [j, -k, r, i]),
sum(self.reals ~Z* [k, j, -i, r]))
}
}
var q = Quaternion(1, 2, 3, 4)
var q1 = Quaternion(2, 3, 4, 5)
var q2 = Quaternion(3, 4, 5, 6)
var r = 7
say "1) q norm = #{q.norm}"
say "2) -q = #{-q}"
say "3) q conj = #{q.conj}"
say "4) q + r = #{q + r}"
say "5) q1 + q2 = #{q1 + q2}"
say "6) q * r = #{q * r}"
say "7) q1 * q2 = #{q1 * q2}"
say "8) q1q2 #{ q1*q2 == q2*q1 ? '==' : '!=' } q2q1"

View file

@ -0,0 +1,92 @@
def Quaternion(q0;q1;q2;q3): { "q0": q0, "q1": q1, "q2": q2, "q3": q3, "type": "Quaternion" };
# promotion of a real number to a quaternion
def Quaternion(r): if (r|type) == "number" then Quaternion(r;0;0;0) else r end;
# thoroughly recursive pretty-print
def pp:
def signage: if . >= 0 then "+ \(.)" else "- \(-.)" end;
if type == "object" then
if .type == "Quaternion" then
"\(.q0) \(.q1|signage)i \(.q2|signage)j \(.q3|signage)k"
else with_entries( {key, "value" : (.value|pp)} )
end
elif type == "array" then map(pp)
else .
end ;
def real(z): Quaternion(z).q0;
# Note: imag(z) returns the "i" component only,
# reflecting the embedding of the complex numbers within the quaternions:
def imag(z): Quaternion(z).q1;
def conj(z): Quaternion(z) | Quaternion(.q0; -(.q1); -(.q2); -(.q3));
def abs2(z): Quaternion(z) | .q0 * .q0 + .q1*.q1 + .q2*.q2 + .q3*.q3;
def abs(z): abs2(z) | sqrt;
def negate(z): Quaternion(z) | Quaternion(-.q0; -.q1; -.q2; -.q3);
# z + w
def plus(z; w):
def plusq(z;w): Quaternion(z.q0 + w.q0; z.q1 + w.q1;
z.q2 + w.q2; z.q3 + w.q3);
plusq( Quaternion(z); Quaternion(w) );
# z - w
def minus(z; w):
def minusq(z;w): Quaternion(z.q0 - w.q0; z.q1 - w.q1;
z.q2 - w.q2; z.q3 - w.q3);
minusq( Quaternion(z); Quaternion(w) );
# *
def times(z; w):
def timesq(z; w):
Quaternion(z.q0*w.q0 - z.q1*w.q1 - z.q2*w.q2 - z.q3*w.q3;
z.q0*w.q1 + z.q1*w.q0 + z.q2*w.q3 - z.q3*w.q2;
z.q0*w.q2 - z.q1*w.q3 + z.q2*w.q0 + z.q3*w.q1;
z.q0*w.q3 + z.q1*w.q2 - z.q2*w.q1 + z.q3*w.q0);
timesq( Quaternion(z); Quaternion(w) );
# (z/w)
def div(z; w):
if (w|type) == "number" then Quaternion(z.q0/w; z.q1/w; z.q2/w; z.q3/w)
else times(z; inv(w))
end;
def inv(z): div(conj(z); abs2(z));
# Example usage and output:
def say(msg; e): "\(msg) => \(e|pp)";
def demo:
say( "Quaternion(1;0;0;0)"; Quaternion(1;0;0;0)),
(Quaternion (1; 2; 3; 4) as $q
| Quaternion(2; 3; 4; 5) as $q1
| Quaternion(3; 4; 5; 6) as $q2
| 7 as $r
| say( "abs($q)"; abs($q) ), # norm
say( "negate($q)"; negate($q) ),
say( "conj($q)"; conj($q) ),
"",
say( "plus($r; $q)"; plus($r; $q)),
say( "plus($q; $r)"; plus($q; $r)),
"",
say( "plus($q1; $q2 )"; plus($q1; $q2)),
"",
say( "times($r;$q)"; times($r;$q)),
say( "times($q;$r)"; times($q;$r)),
"",
say( "times($q1;$q2)"; times($q1;$q2)),
say( "times($q2; $q1)"; times($q2; $q1)),
say( "times($q1; $q2) != times($q2; $q1)";
times($q1; $q2) != times($q2; $q1) )
) ;
demo

View file

@ -0,0 +1,17 @@
# jq -c -n -R -f quaternion.jq
Quaternion(1;0;0;0) => 1 + 0i + 0j + 0k
abs($q) => 5.477225575051661
negate($q) => -1 - 2i - 3j + -4k
conj($q) => 1 - 2i - 3j - 4k
plus($r; $q) => 8 + 2i + 3j + 4k
plus($q; $r) => 8 + 2i + 3j + 4k
plus($q1; $q2 ) => 5 + 7i + 9j + 11k
times($r;$q) => 7 + 14i + 21j + 28k
times($q;$r) => 7 + 14i + 21j + 28k
times($q1;$q2) => -56 + 16i + 24j + 26k
times($q2; $q1) => -56 + 18i + 20j + 28k
times($q1; $q2) != times($q2; $q1) => true