tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
61
Task/Quaternion-type/E/quaternion-type-1.e
Normal file
61
Task/Quaternion-type/E/quaternion-type-1.e
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
interface Quaternion guards QS {}
|
||||
def makeQuaternion(a, b, c, d) {
|
||||
return def quaternion implements QS {
|
||||
|
||||
to __printOn(out) {
|
||||
out.print("(", a, " + ", b, "i + ")
|
||||
out.print(c, "j + ", d, "k)")
|
||||
}
|
||||
|
||||
# Task requirement 1
|
||||
to norm() {
|
||||
return (a**2 + b**2 + c**2 + d**2).sqrt()
|
||||
}
|
||||
|
||||
# Task requirement 2
|
||||
to negate() {
|
||||
return makeQuaternion(-a, -b, -c, -d)
|
||||
}
|
||||
|
||||
# Task requirement 3
|
||||
to conjugate() {
|
||||
return makeQuaternion(a, -b, -c, -d)
|
||||
}
|
||||
|
||||
# Task requirement 4, 5
|
||||
# This implements q + r; r + q is deliberately prohibited by E
|
||||
to add(other :any[Quaternion, int, float64]) {
|
||||
switch (other) {
|
||||
match q :Quaternion {
|
||||
return makeQuaternion(
|
||||
a+q.a(), b+q.b(), c+q.c(), d+q.d())
|
||||
}
|
||||
match real {
|
||||
return makeQuaternion(a+real, b, c, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Task requirement 6, 7
|
||||
# This implements q * r; r * q is deliberately prohibited by E
|
||||
to multiply(other :any[Quaternion, int, float64]) {
|
||||
switch (other) {
|
||||
match q :Quaternion {
|
||||
return makeQuaternion(
|
||||
a*q.a() - b*q.b() - c*q.c() - d*q.d(),
|
||||
a*q.b() + b*q.a() + c*q.d() - d*q.c(),
|
||||
a*q.c() - b*q.d() + c*q.a() + d*q.b(),
|
||||
a*q.d() + b*q.c() - c*q.b() + d*q.a())
|
||||
}
|
||||
match real {
|
||||
return makeQuaternion(real*a, real*b, real*c, real*d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
to a() { return a }
|
||||
to b() { return b }
|
||||
to c() { return c }
|
||||
to d() { return d }
|
||||
}
|
||||
}
|
||||
17
Task/Quaternion-type/E/quaternion-type-2.e
Normal file
17
Task/Quaternion-type/E/quaternion-type-2.e
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
? def q1 := makeQuaternion(2,3,4,5)
|
||||
# value: (2 + 3i + 4j + 5k)
|
||||
|
||||
? def q2 := makeQuaternion(3,4,5,6)
|
||||
# value: (3 + 4i + 5j + 6k)
|
||||
|
||||
? q1+q2
|
||||
# value: (5 + 7i + 9j + 11k)
|
||||
|
||||
? q1*q2
|
||||
# value: (-56 + 16i + 24j + 26k)
|
||||
|
||||
? q2*q1
|
||||
# value: (-56 + 18i + 20j + 28k)
|
||||
|
||||
? q1+(-2)
|
||||
# value: (0 + 3i + 4j + 5k)
|
||||
Loading…
Add table
Add a link
Reference in a new issue