tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
35
Task/Quaternion-type/Haskell/quaternion-type-1.hs
Normal file
35
Task/Quaternion-type/Haskell/quaternion-type-1.hs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import Control.Monad
|
||||
import Control.Arrow
|
||||
import Data.List
|
||||
|
||||
data Quaternion = Q Double Double Double Double
|
||||
deriving (Show, Ord, Eq)
|
||||
|
||||
realQ :: Quaternion -> Double
|
||||
realQ (Q r _ _ _) = r
|
||||
|
||||
imagQ :: Quaternion -> [Double]
|
||||
imagQ (Q _ i j k) = [i, j, k]
|
||||
|
||||
quaternionFromScalar s = Q s 0 0 0
|
||||
|
||||
listFromQ (Q a b c d) = [a,b,c,d]
|
||||
quaternionFromList [a, b, c, d] = Q a b c d
|
||||
|
||||
addQ, subQ, mulQ :: Quaternion -> Quaternion -> Quaternion
|
||||
addQ (Q a b c d) (Q p q r s) = Q (a+p) (b+q) (c+r) (d+s)
|
||||
|
||||
subQ (Q a b c d) (Q p q r s) = Q (a-p) (b-q) (c-r) (d-s)
|
||||
|
||||
mulQ (Q a b c d) (Q p q r s) =
|
||||
Q (a*p - b*q - c*r - d*s)
|
||||
(a*q + b*p + c*s - d*r)
|
||||
(a*r - b*s + c*p + d*q)
|
||||
(a*s + b*r - c*q + d*p)
|
||||
|
||||
normQ = sqrt. sum. join (zipWith (*)). listFromQ
|
||||
|
||||
conjQ, negQ :: Quaternion -> Quaternion
|
||||
conjQ (Q a b c d) = Q a (-b) (-c) (-d)
|
||||
|
||||
negQ (Q a b c d) = Q (-a) (-b) (-c) (-d)
|
||||
4
Task/Quaternion-type/Haskell/quaternion-type-2.hs
Normal file
4
Task/Quaternion-type/Haskell/quaternion-type-2.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[q,q1,q2] = map quaternionFromList [[1..4],[2..5],[3..6]]
|
||||
-- a*b == b*a
|
||||
test :: Quaternion -> Quaternion -> Bool
|
||||
test a b = a `mulQ` b == b `mulQ` a
|
||||
43
Task/Quaternion-type/Haskell/quaternion-type-3.hs
Normal file
43
Task/Quaternion-type/Haskell/quaternion-type-3.hs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
class Quaternion(a, b, c, d)
|
||||
|
||||
method norm ()
|
||||
return sqrt (a*a + b*b + c*c + d*d)
|
||||
end
|
||||
|
||||
method negative ()
|
||||
return Quaternion(-a, -b, -c, -d)
|
||||
end
|
||||
|
||||
method conjugate ()
|
||||
return Quaternion(a, -b, -c, -d)
|
||||
end
|
||||
|
||||
method add (n)
|
||||
if type(n) == "Quaternion__state"
|
||||
then return Quaternion(a+n.a, b+n.b, c+n.c, d+n.d)
|
||||
else return Quaternion(a+n, b, c, d)
|
||||
end
|
||||
|
||||
method multiply (n)
|
||||
if type(n) == "Quaternion__state"
|
||||
then return Quaternion(a*n.a - b*n.b - c*n.c - d*n.d,
|
||||
a*n.b + b*n.a + c*n.d - d*n.c,
|
||||
a*n.c - b*n.d + c*n.a + d*n.b,
|
||||
a*n.d + b*n.c - c*n.b + d*n.a)
|
||||
else return Quaternion(a*n, b*n, c*n, d*n)
|
||||
end
|
||||
|
||||
method sign (n)
|
||||
return if n >= 0 then "+" else "-"
|
||||
end
|
||||
|
||||
method string ()
|
||||
return ("" || a || sign(b) || abs(b) || "i" || sign(c) || abs(c) || "j" || sign(d) || abs(d) || "k");
|
||||
end
|
||||
|
||||
initially(a, b, c, d)
|
||||
self.a := if /a then 0 else a
|
||||
self.b := if /b then 0 else b
|
||||
self.c := if /c then 0 else c
|
||||
self.d := if /d then 0 else d
|
||||
end
|
||||
16
Task/Quaternion-type/Haskell/quaternion-type-4.hs
Normal file
16
Task/Quaternion-type/Haskell/quaternion-type-4.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
procedure main ()
|
||||
q := Quaternion (1,2,3,4)
|
||||
q1 := Quaternion (2,3,4,5)
|
||||
q2 := Quaternion (3,4,5,6)
|
||||
r := 7
|
||||
|
||||
write ("The norm of " || q.string() || " is " || q.norm ())
|
||||
write ("The negative of " || q.string() || " is " || q.negative().string ())
|
||||
write ("The conjugate of " || q.string() || " is " || q.conjugate().string ())
|
||||
write ("Sum of " || q.string() || " and " || r || " is " || q.add(r).string ())
|
||||
write ("Sum of " || q.string() || " and " || q1.string() || " is " || q.add(q1).string ())
|
||||
write ("Product of " || q.string() || " and " || r || " is " || q.multiply(r).string ())
|
||||
write ("Product of " || q.string() || " and " || q1.string() || " is " || q.multiply(q1).string ())
|
||||
write ("q1*q2 = " || q1.multiply(q2).string ())
|
||||
write ("q2*q1 = " || q2.multiply(q1).string ())
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue