September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/Quaternion-type/00META.yaml
Normal file
1
Task/Quaternion-type/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -1,87 +1,88 @@
|
|||
import system'math.
|
||||
import extensions.
|
||||
import system'math;
|
||||
import extensions;
|
||||
import extensions'text;
|
||||
|
||||
struct Quaternion :: BaseValue
|
||||
struct Quaternion : BaseValue
|
||||
{
|
||||
real rprop A :: a.
|
||||
real rprop B :: b.
|
||||
real rprop C :: c.
|
||||
real rprop D :: d.
|
||||
rprop real A;
|
||||
rprop real B;
|
||||
rprop real C;
|
||||
rprop real D;
|
||||
|
||||
constructor new(object a, object b, object c, object d)
|
||||
<= new(a real, b real, c real, d real).
|
||||
constructor new(a, b, c, d)
|
||||
<= new(cast real(a), cast real(b), cast real(c), cast real(d));
|
||||
|
||||
constructor new(real a, real b, real c, real d)
|
||||
[
|
||||
@a := a.
|
||||
@b := b.
|
||||
@c := c.
|
||||
@d := d.
|
||||
]
|
||||
{
|
||||
A := a;
|
||||
B := b;
|
||||
C := c;
|
||||
D := d
|
||||
}
|
||||
|
||||
stacksafe explicit(Real r)
|
||||
[
|
||||
a := r.
|
||||
b := 0.0r.
|
||||
c := 0.0r.
|
||||
d := 0.0r.
|
||||
]
|
||||
constructor(real r)
|
||||
{
|
||||
A := r;
|
||||
B := 0.0r;
|
||||
C := 0.0r;
|
||||
D := 0.0r
|
||||
}
|
||||
|
||||
real Norm = (a*a + b*b + c*c + d*d) sqrt.
|
||||
real Norm = (A*A + B*B + C*C + D*D).sqrt();
|
||||
|
||||
type<Quaternion> negative = Quaternion new(a negative,b negative,c negative,d negative).
|
||||
Quaternion Negative = Quaternion.new(A.Negative,B.Negative,C.Negative,D.Negative);
|
||||
|
||||
type<Quaternion> Conjugate = Quaternion new(a,b negative,c negative,d negative).
|
||||
Quaternion Conjugate = Quaternion.new(A,B.Negative,C.Negative,D.Negative);
|
||||
|
||||
type<Quaternion> add(Quaternion q)
|
||||
= Quaternion new(a + q A, b + q B, c + q C, d + q D).
|
||||
Quaternion add(Quaternion q)
|
||||
= Quaternion.new(A + q.A, B + q.B, C + q.C, D + q.D);
|
||||
|
||||
type<Quaternion> multiply(Quaternion q)
|
||||
= Quaternion new(
|
||||
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).
|
||||
Quaternion multiply(Quaternion q)
|
||||
= Quaternion.new(
|
||||
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);
|
||||
|
||||
type<Quaternion> add(real r)
|
||||
<= add(Quaternion new(r,0,0,0)).
|
||||
Quaternion add(real r)
|
||||
<= add(Quaternion.new(r,0,0,0));
|
||||
|
||||
type<Quaternion> multiply(real r)
|
||||
<= multiply(Quaternion new(r,0,0,0)).
|
||||
Quaternion multiply(real r)
|
||||
<= multiply(Quaternion.new(r,0,0,0));
|
||||
|
||||
bool equal(Quaternion q)
|
||||
= (a == q A) && (b == q B) && (c == q C) && (d == q D).
|
||||
= (A == q.A) && (B == q.B) && (C == q.C) && (D == q.D);
|
||||
|
||||
literal
|
||||
= String writeFormatted("Q({0}, {1}, {2}, {3})",a,b,c,d).
|
||||
string Printable
|
||||
= new StringWriter().printFormatted("Q({0}, {1}, {2}, {3})",A,B,C,D);
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var q := Quaternion new(1,2,3,4).
|
||||
var q1 := Quaternion new(2,3,4,5).
|
||||
var q2 := Quaternion new(3,4,5,6).
|
||||
real r := 7.
|
||||
public program()
|
||||
{
|
||||
auto q := Quaternion.new(1,2,3,4);
|
||||
auto q1 := Quaternion.new(2,3,4,5);
|
||||
auto q2 := Quaternion.new(3,4,5,6);
|
||||
real r := 7;
|
||||
|
||||
console printLine("q = ", q).
|
||||
console printLine("q1 = ", q1).
|
||||
console printLine("q2 = ", q2).
|
||||
console printLine("r = ", r).
|
||||
console.printLine("q = ", q);
|
||||
console.printLine("q1 = ", q1);
|
||||
console.printLine("q2 = ", q2);
|
||||
console.printLine("r = ", r);
|
||||
|
||||
console printLine("q.Norm() = ", q Norm).
|
||||
console printLine("q1.Norm() = ", q1 Norm).
|
||||
console printLine("q2.Norm() = ", q2 Norm).
|
||||
console.printLine("q.Norm() = ", q.Norm);
|
||||
console.printLine("q1.Norm() = ", q1.Norm);
|
||||
console.printLine("q2.Norm() = ", q2.Norm);
|
||||
|
||||
console printLine("-q = ", q negative).
|
||||
console printLine("q.Conjugate() = ", q Conjugate).
|
||||
console.printLine("-q = ", q.Negative);
|
||||
console.printLine("q.Conjugate() = ", q.Conjugate);
|
||||
|
||||
console printLine("q + r = ", q + r).
|
||||
console printLine("q1 + q2 = ", q1 + q2).
|
||||
console printLine("q2 + q1 = ", q2 + q1).
|
||||
console.printLine("q + r = ", q + r);
|
||||
console.printLine("q1 + q2 = ", q1 + q2);
|
||||
console.printLine("q2 + q1 = ", q2 + q1);
|
||||
|
||||
console printLine("q * r = ", q * r).
|
||||
console printLine("q1 * q2 = ", q1 * q2).
|
||||
console printLine("q2 * q1 = ", q2 * q1).
|
||||
console.printLine("q * r = ", q * r);
|
||||
console.printLine("q1 * q2 = ", q1 * q2);
|
||||
console.printLine("q2 * q1 = ", q2 * q1);
|
||||
|
||||
console printLineFormatted("q1*q2 {0} q2*q1", ((q1 * q2) == (q2 * q1)) iif("==","!=")).
|
||||
].
|
||||
console.printLineFormatted("q1*q2 {0} q2*q1", ((q1 * q2) == (q2 * q1)).iif("==","!="))
|
||||
}
|
||||
|
|
|
|||
77
Task/Quaternion-type/FreeBASIC/quaternion-type.freebasic
Normal file
77
Task/Quaternion-type/FreeBASIC/quaternion-type.freebasic
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
Dim Shared As Integer q(3) = {1, 2, 3, 4}
|
||||
Dim Shared As Integer q1(3) = {2, 3, 4, 5}
|
||||
Dim Shared As Integer q2(3) = {3, 4, 5, 6}
|
||||
Dim Shared As Integer i, r = 7, t(3)
|
||||
|
||||
Function q_norm(q() As Integer) As Double
|
||||
' medida o valor absoluto de un cuaternión
|
||||
Dim As Double a = 0
|
||||
For i = 0 To 3
|
||||
a += q(i)^2
|
||||
Next i
|
||||
Return Sqr(a)
|
||||
End Function
|
||||
|
||||
Sub q_neg(q() As Integer)
|
||||
For i = 0 To 3
|
||||
q(i) *= -1
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub q_conj(q() As Integer)
|
||||
' conjugado de un cuaternión
|
||||
For i = 1 To 3
|
||||
q(i) *= -1
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub q_addreal(q() As Integer, r As Integer)
|
||||
q(0) += r
|
||||
End Sub
|
||||
|
||||
Sub q_add(q() As Integer, r() As Integer)
|
||||
' adición entre cuaternios
|
||||
For i = 0 To 3
|
||||
q(i) += r(i)
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub q_mulreal(q() As Integer, r As Integer)
|
||||
For i = 0 To 3
|
||||
q(i) *= r
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub q_mul(q() As Integer, r() As Integer)
|
||||
' producto entre cuaternios
|
||||
Dim As Integer m(3)
|
||||
m(0) = q(0)*r(0) - q(1)*r(1) - q(2)*r(2) - q(3)*r(3)
|
||||
m(1) = q(0)*r(1) + q(1)*r(0) + q(2)*r(3) - q(3)*r(2)
|
||||
m(2) = q(0)*r(2) - q(1)*r(3) + q(2)*r(0) + q(3)*r(1)
|
||||
m(3) = q(0)*r(3) + q(1)*r(2) - q(2)*r(1) + q(3)*r(0)
|
||||
For i = 0 To 3 : q(i) = m(i) : Next i
|
||||
End Sub
|
||||
|
||||
Function q_show(q() As Integer) As String
|
||||
Dim As String a = "("
|
||||
For i = 0 To 3
|
||||
a += Str(q(i)) + ", "
|
||||
Next i
|
||||
Return Mid(a,1,Len(a)-2) + ")"
|
||||
End Function
|
||||
|
||||
'--- Programa Principal ---
|
||||
Print " q = "; q_show(q())
|
||||
Print "q1 = "; q_show(q1())
|
||||
Print "q2 = "; q_show(q2())
|
||||
Print " r = "; r
|
||||
Print "norm(q) ="; q_norm(q())
|
||||
For i = 0 To 3 : t(i) = q(i) : Next i : q_neg(t()) : Print " neg(q) = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q(i) : Next i : q_conj(t()) : Print "conj(q) = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q(i) : Next i : q_addreal(t(),r) : Print " r + q = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q1(i) : Next i : q_add(t(),q2()) : Print "q1 + q2 = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q2(i) : Next i : q_add(t(),q1()) : Print "q2 + q1 = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q(i) : Next i : q_mulreal(t(),r) : Print " r * q = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q1(i) : Next i : q_mul(t(),q2()) : Print "q1 * q2 = "; q_show(t())
|
||||
For i = 0 To 3 : t(i) = q2(i) : Next i : q_mul(t(),q1()) : Print "q2 * q1 = "; q_show(t())
|
||||
End
|
||||
|
|
@ -1,43 +1,53 @@
|
|||
import Control.Monad (join)
|
||||
|
||||
data Quaternion =
|
||||
Q Double
|
||||
Double
|
||||
Double
|
||||
Double
|
||||
deriving (Show, Ord, Eq)
|
||||
data Quaternion a =
|
||||
Q a a a a
|
||||
deriving (Show, Eq)
|
||||
|
||||
realQ :: Quaternion -> Double
|
||||
realQ :: Quaternion a -> a
|
||||
realQ (Q r _ _ _) = r
|
||||
|
||||
imagQ :: Quaternion -> [Double]
|
||||
imagQ :: Quaternion a -> [a]
|
||||
imagQ (Q _ i j k) = [i, j, k]
|
||||
|
||||
quaternionFromScalar :: Double -> Quaternion
|
||||
quaternionFromScalar :: (Num a) => a -> Quaternion a
|
||||
quaternionFromScalar s = Q s 0 0 0
|
||||
|
||||
listFromQ :: Quaternion -> [Double]
|
||||
listFromQ :: Quaternion a -> [a]
|
||||
listFromQ (Q a b c d) = [a, b, c, d]
|
||||
|
||||
quaternionFromList :: [Double] -> Quaternion
|
||||
quaternionFromList :: [a] -> Quaternion a
|
||||
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)
|
||||
normQ :: (RealFloat a) => Quaternion a -> a
|
||||
normQ = sqrt . sum . join (zipWith (*)) . listFromQ
|
||||
|
||||
subQ (Q a b c d) (Q p q r s) = Q (a - p) (b - q) (c - r) (d - s)
|
||||
conjQ :: (Num a) => Quaternion a -> Quaternion a
|
||||
conjQ (Q a b c d) = Q a (-b) (-c) (-d)
|
||||
|
||||
mulQ (Q a b c d) (Q p q r s) =
|
||||
Q
|
||||
instance (RealFloat a) => Num (Quaternion a) where
|
||||
(Q a b c d) + (Q p q r s) = Q (a + p) (b + q) (c + r) (d + s)
|
||||
(Q a b c d) - (Q p q r s) = Q (a - p) (b - q) (c - r) (d - s)
|
||||
(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)
|
||||
negate (Q a b c d) = Q (-a) (-b) (-c) (-d)
|
||||
abs q = quaternionFromScalar (normQ q)
|
||||
signum (Q 0 0 0 0) = 0
|
||||
signum q@(Q a b c d) = Q (a/n) (b/n) (c/n) (d/n) where n = normQ q
|
||||
fromInteger n = quaternionFromScalar (fromInteger n)
|
||||
|
||||
normQ :: Quaternion -> Double
|
||||
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)
|
||||
main :: IO ()
|
||||
main = do
|
||||
let q, q1, q2 :: Quaternion Double
|
||||
q = Q 1 2 3 4
|
||||
q1 = Q 2 3 4 5
|
||||
q2 = Q 3 4 5 6
|
||||
print $ (Q 0 1 0 0) * (Q 0 0 1 0) * (Q 0 0 0 1) -- i*j*k; prints "Q (-1.0) 0.0 0.0 0.0"
|
||||
print $ q1 * q2 -- prints "Q (-56.0) 16.0 24.0 26.0"
|
||||
print $ q2 * q1 -- prints "Q (-56.0) 18.0 20.0 28.0"
|
||||
print $ q1 * q2 == q2 * q1 -- prints "False"
|
||||
print $ imagQ q -- prints "[2.0,3.0,4.0]"
|
||||
|
|
|
|||
|
|
@ -11,13 +11,3 @@ io.write( "r*q1 = " ); Quaternion.print( r*q1 )
|
|||
io.write( "q1*r = " ); Quaternion.print( q1*r )
|
||||
io.write( "q1*q2 = " ); Quaternion.print( q1*q2 )
|
||||
io.write( "q2*q1 = " ); Quaternion.print( q2*q1 )
|
||||
{{out}}
|
||||
norm(q1) = 5.4772255750517
|
||||
-q1 = -1.000000 -2.000000i -3.000000j -4.000000k
|
||||
conj(q1) = 1.000000 -2.000000i -3.000000j -4.000000k
|
||||
r+q1 = 13.000000 + 2.000000i + 3.000000j + 4.000000k
|
||||
q1+r = 13.000000 + 2.000000i + 3.000000j + 4.000000k
|
||||
r*q1 = 12.000000 + 24.000000i + 36.000000j + 48.000000k
|
||||
q1*r = 12.000000 + 24.000000i + 36.000000j + 48.000000k
|
||||
q1*q2 = -60.000000 + 12.000000i + 30.000000j + 24.000000k
|
||||
q2*q1 = -60.000000 + 20.000000i + 14.000000j + 32.000000k
|
||||
|
|
|
|||
134
Task/Quaternion-type/Swift/quaternion-type.swift
Normal file
134
Task/Quaternion-type/Swift/quaternion-type.swift
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
import Foundation
|
||||
|
||||
struct Quaternion {
|
||||
var a, b, c, d: Double
|
||||
|
||||
static let i = Quaternion(a: 0, b: 1, c: 0, d: 0)
|
||||
static let j = Quaternion(a: 0, b: 0, c: 1, d: 0)
|
||||
static let k = Quaternion(a: 0, b: 0, c: 0, d: 1)
|
||||
}
|
||||
extension Quaternion: Equatable {
|
||||
static func ==(lhs: Quaternion, rhs: Quaternion) -> Bool {
|
||||
return (lhs.a, lhs.b, lhs.c, lhs.d) == (rhs.a, rhs.b, rhs.c, rhs.d)
|
||||
}
|
||||
}
|
||||
extension Quaternion: ExpressibleByIntegerLiteral {
|
||||
init(integerLiteral: Double) {
|
||||
a = integerLiteral
|
||||
b = 0
|
||||
c = 0
|
||||
d = 0
|
||||
}
|
||||
}
|
||||
extension Quaternion: Numeric {
|
||||
var magnitude: Double {
|
||||
return norm
|
||||
}
|
||||
init?<T>(exactly: T) { // stub to satisfy protocol requirements
|
||||
return nil
|
||||
}
|
||||
public static func + (lhs: Quaternion, rhs: Quaternion) -> Quaternion {
|
||||
return Quaternion(
|
||||
a: lhs.a + rhs.a,
|
||||
b: lhs.b + rhs.b,
|
||||
c: lhs.c + rhs.c,
|
||||
d: lhs.d + rhs.d
|
||||
)
|
||||
}
|
||||
public static func - (lhs: Quaternion, rhs: Quaternion) -> Quaternion {
|
||||
return Quaternion(
|
||||
a: lhs.a - rhs.a,
|
||||
b: lhs.b - rhs.b,
|
||||
c: lhs.c - rhs.c,
|
||||
d: lhs.d - rhs.d
|
||||
)
|
||||
}
|
||||
public static func * (lhs: Quaternion, rhs: Quaternion) -> Quaternion {
|
||||
return Quaternion(
|
||||
a: lhs.a*rhs.a - lhs.b*rhs.b - lhs.c*rhs.c - lhs.d*rhs.d,
|
||||
b: lhs.a*rhs.b + lhs.b*rhs.a + lhs.c*rhs.d - lhs.d*rhs.c,
|
||||
c: lhs.a*rhs.c - lhs.b*rhs.d + lhs.c*rhs.a + lhs.d*rhs.b,
|
||||
d: lhs.a*rhs.d + lhs.b*rhs.c - lhs.c*rhs.b + lhs.d*rhs.a
|
||||
)
|
||||
}
|
||||
public static func += (lhs: inout Quaternion, rhs: Quaternion) {
|
||||
lhs = Quaternion(
|
||||
a: lhs.a + rhs.a,
|
||||
b: lhs.b + rhs.b,
|
||||
c: lhs.c + rhs.c,
|
||||
d: lhs.d + rhs.d
|
||||
)
|
||||
}
|
||||
public static func -= (lhs: inout Quaternion, rhs: Quaternion) {
|
||||
lhs = Quaternion(
|
||||
a: lhs.a - rhs.a,
|
||||
b: lhs.b - rhs.b,
|
||||
c: lhs.c - rhs.c,
|
||||
d: lhs.d - rhs.d
|
||||
)
|
||||
}
|
||||
public static func *= (lhs: inout Quaternion, rhs: Quaternion) {
|
||||
lhs = Quaternion(
|
||||
a: lhs.a*rhs.a - lhs.b*rhs.b - lhs.c*rhs.c - lhs.d*rhs.d,
|
||||
b: lhs.a*rhs.b + lhs.b*rhs.a + lhs.c*rhs.d - lhs.d*rhs.c,
|
||||
c: lhs.a*rhs.c - lhs.b*rhs.d + lhs.c*rhs.a + lhs.d*rhs.b,
|
||||
d: lhs.a*rhs.d + lhs.b*rhs.c - lhs.c*rhs.b + lhs.d*rhs.a
|
||||
)
|
||||
}
|
||||
}
|
||||
extension Quaternion: CustomStringConvertible {
|
||||
var description: String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.positivePrefix = "+"
|
||||
let f: (Double) -> String = { formatter.string(from: $0 as NSNumber)! }
|
||||
return [f(a), f(b), "i", f(c), "j", f(d), "k"].joined()
|
||||
}
|
||||
}
|
||||
extension Quaternion {
|
||||
var norm: Double {
|
||||
return sqrt(a*a + b*b + c*c + d*d)
|
||||
}
|
||||
var conjugate: Quaternion {
|
||||
return Quaternion(a: a, b: -b, c: -c, d: -d)
|
||||
}
|
||||
public static func + (lhs: Double, rhs: Quaternion) -> Quaternion {
|
||||
var result = rhs
|
||||
result.a += lhs
|
||||
return result
|
||||
}
|
||||
public static func + (lhs: Quaternion, rhs: Double) -> Quaternion {
|
||||
var result = lhs
|
||||
result.a += rhs
|
||||
return result
|
||||
}
|
||||
public static func * (lhs: Double, rhs: Quaternion) -> Quaternion {
|
||||
return Quaternion(a: lhs*rhs.a, b: lhs*rhs.b, c: lhs*rhs.c, d: lhs*rhs.d)
|
||||
}
|
||||
public static func * (lhs: Quaternion, rhs: Double) -> Quaternion {
|
||||
return Quaternion(a: lhs.a*rhs, b: lhs.b*rhs, c: lhs.c*rhs, d: lhs.d*rhs)
|
||||
}
|
||||
public static prefix func - (x: Quaternion) -> Quaternion {
|
||||
return Quaternion(a: -x.a, b: -x.b, c: -x.c, d: -x.d)
|
||||
}
|
||||
}
|
||||
|
||||
let q: Quaternion = 1 + 2 * .i + 3 * .j + 4 * .k // 1+2i+3j+4k
|
||||
let q1: Quaternion = 2 + 3 * .i + 4 * .j + 5 * .k // 2+3i+4j+5k
|
||||
let q2: Quaternion = 3 + 4 * .i + 5 * .j + 6 * .k // 3+4i+5j+6k
|
||||
let r: Double = 7
|
||||
|
||||
print("""
|
||||
q = \(q)
|
||||
q1 = \(q1)
|
||||
q2 = \(q2)
|
||||
r = \(r)
|
||||
-q = \(-q)
|
||||
‖q‖ = \(q.norm)
|
||||
conjugate of q = \(q.conjugate)
|
||||
r + q = q + r = \(r+q) = \(q+r)
|
||||
q₁ + q₂ = \(q1 + q2) = \(q2 + q1)
|
||||
qr = rq = \(q*r) = \(r*q)
|
||||
q₁q₂ = \(q1 * q2)
|
||||
q₂q₁ = \(q2 * q1)
|
||||
q₁q₂ ≠ q₂q₁ is \(q1*q2 != q2*q1)
|
||||
""")
|
||||
71
Task/Quaternion-type/VBA/quaternion-type.vba
Normal file
71
Task/Quaternion-type/VBA/quaternion-type.vba
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
Option Base 1
|
||||
Private Function norm(q As Variant) As Double
|
||||
norm = Sqr(WorksheetFunction.SumSq(q))
|
||||
End Function
|
||||
|
||||
Private Function negative(q) As Variant
|
||||
Dim res(4) As Double
|
||||
For i = 1 To 4
|
||||
res(i) = -q(i)
|
||||
Next i
|
||||
negative = res
|
||||
End Function
|
||||
|
||||
Private Function conj(q As Variant) As Variant
|
||||
Dim res(4) As Double
|
||||
res(1) = q(1)
|
||||
For i = 2 To 4
|
||||
res(i) = -q(i)
|
||||
Next i
|
||||
conj = res
|
||||
End Function
|
||||
|
||||
Private Function addr(r As Double, q As Variant) As Variant
|
||||
Dim res As Variant
|
||||
res = q
|
||||
res(1) = r + q(1)
|
||||
addr = res
|
||||
End Function
|
||||
|
||||
Private Function add(q1 As Variant, q2 As Variant) As Variant
|
||||
add = WorksheetFunction.MMult(Array(1, 1), Array(q1, q2))
|
||||
End Function
|
||||
|
||||
Private Function multr(r As Double, q As Variant) As Variant
|
||||
multr = WorksheetFunction.MMult(r, q)
|
||||
End Function
|
||||
|
||||
Private Function mult(q1 As Variant, q2 As Variant)
|
||||
Dim res(4) As Double
|
||||
res(1) = q1(1) * q2(1) - q1(2) * q2(2) - q1(3) * q2(3) - q1(4) * q2(4)
|
||||
res(2) = q1(1) * q2(2) + q1(2) * q2(1) + q1(3) * q2(4) - q1(4) * q2(3)
|
||||
res(3) = q1(1) * q2(3) - q1(2) * q2(4) + q1(3) * q2(1) + q1(4) * q2(2)
|
||||
res(4) = q1(1) * q2(4) + q1(2) * q2(3) - q1(3) * q2(2) + q1(4) * q2(1)
|
||||
mult = res
|
||||
End Function
|
||||
|
||||
Private Sub quats(q As Variant)
|
||||
Debug.Print q(1); IIf(q(2) < 0, " - " & Abs(q(2)), " + " & q(2));
|
||||
Debug.Print IIf(q(3) < 0, "i - " & Abs(q(3)), "i + " & q(3));
|
||||
Debug.Print IIf(q(4) < 0, "j - " & Abs(q(4)), "j + " & q(4)); "k"
|
||||
End Sub
|
||||
|
||||
Public Sub quaternions()
|
||||
q = [{ 1, 2, 3, 4}]
|
||||
q1 = [{2, 3, 4, 5}]
|
||||
q2 = [{3, 4, 5, 6}]
|
||||
Dim r_ As Double
|
||||
r_ = 7#
|
||||
Debug.Print "q = ";: quats q
|
||||
Debug.Print "q1 = ";: quats q1
|
||||
Debug.Print "q2 = ";: quats q2
|
||||
Debug.Print "r = "; r_
|
||||
Debug.Print "norm(q) = "; norm(q)
|
||||
Debug.Print "negative(q) = ";: quats negative(q)
|
||||
Debug.Print "conjugate(q) = ";: quats conj(q)
|
||||
Debug.Print "r + q = ";: quats addr(r_, q)
|
||||
Debug.Print "q1 + q2 = ";: quats add(q1, q2)
|
||||
Debug.Print "q * r = ";: quats multr(r_, q)
|
||||
Debug.Print "q1 * q2 = ";: quats mult(q1, q2)
|
||||
Debug.Print "q2 * q1 = ";: quats mult(q2, q1)
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
Option Compare Binary
|
||||
Option Explicit On
|
||||
Option Infer On
|
||||
Option Strict On
|
||||
|
||||
Structure Quaternion
|
||||
Implements IEquatable(Of Quaternion), IStructuralEquatable
|
||||
|
||||
Public ReadOnly A, B, C, D As Double
|
||||
|
||||
Public Sub New(a As Double, b As Double, c As Double, d As Double)
|
||||
Me.A = a
|
||||
Me.B = b
|
||||
Me.C = c
|
||||
Me.D = d
|
||||
End Sub
|
||||
|
||||
Public ReadOnly Property Norm As Double
|
||||
Get
|
||||
Return Math.Sqrt((Me.A ^ 2) + (Me.B ^ 2) + (Me.C ^ 2) + (Me.D ^ 2))
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public ReadOnly Property Conjugate As Quaternion
|
||||
Get
|
||||
Return New Quaternion(Me.A, -Me.B, -Me.C, -Me.D)
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public Overrides Function Equals(obj As Object) As Boolean
|
||||
If TypeOf obj IsNot Quaternion Then Return False
|
||||
Return Me.Equals(DirectCast(obj, Quaternion))
|
||||
End Function
|
||||
|
||||
Public Overloads Function Equals(other As Quaternion) As Boolean Implements IEquatable(Of Quaternion).Equals
|
||||
Return other = Me
|
||||
End Function
|
||||
|
||||
Public Overloads Function Equals(other As Object, comparer As IEqualityComparer) As Boolean Implements IStructuralEquatable.Equals
|
||||
If TypeOf other IsNot Quaternion Then Return False
|
||||
Dim q = DirectCast(other, Quaternion)
|
||||
Return comparer.Equals(Me.A, q.A) AndAlso
|
||||
comparer.Equals(Me.B, q.B) AndAlso
|
||||
comparer.Equals(Me.C, q.C) AndAlso
|
||||
comparer.Equals(Me.D, q.D)
|
||||
End Function
|
||||
|
||||
Public Overrides Function GetHashCode() As Integer
|
||||
Return HashCode.Combine(Me.A, Me.B, Me.C, Me.D)
|
||||
End Function
|
||||
|
||||
Public Overloads Function GetHashCode(comparer As IEqualityComparer) As Integer Implements IStructuralEquatable.GetHashCode
|
||||
Return HashCode.Combine(
|
||||
comparer.GetHashCode(Me.A),
|
||||
comparer.GetHashCode(Me.B),
|
||||
comparer.GetHashCode(Me.C),
|
||||
comparer.GetHashCode(Me.D))
|
||||
End Function
|
||||
|
||||
Public Overrides Function ToString() As String
|
||||
Return $"Q({Me.A}, {Me.B}, {Me.C}, {Me.D})"
|
||||
End Function
|
||||
|
||||
#Region "Operators"
|
||||
Public Shared Operator =(left As Quaternion, right As Quaternion) As Boolean
|
||||
Return left.A = right.A AndAlso
|
||||
left.B = right.B AndAlso
|
||||
left.C = right.C AndAlso
|
||||
left.D = right.D
|
||||
End Operator
|
||||
|
||||
Public Shared Operator <>(left As Quaternion, right As Quaternion) As Boolean
|
||||
Return Not left = right
|
||||
End Operator
|
||||
|
||||
Public Shared Operator +(q1 As Quaternion, q2 As Quaternion) As Quaternion
|
||||
Return New Quaternion(q1.A + q2.A, q1.B + q2.B, q1.C + q2.C, q1.D + q2.D)
|
||||
End Operator
|
||||
|
||||
Public Shared Operator -(q As Quaternion) As Quaternion
|
||||
Return New Quaternion(-q.A, -q.B, -q.C, -q.D)
|
||||
End Operator
|
||||
|
||||
Public Shared Operator *(q1 As Quaternion, q2 As Quaternion) As Quaternion
|
||||
Return New Quaternion(
|
||||
(q1.A * q2.A) - (q1.B * q2.B) - (q1.C * q2.C) - (q1.D * q2.D),
|
||||
(q1.A * q2.B) + (q1.B * q2.A) + (q1.C * q2.D) - (q1.D * q2.C),
|
||||
(q1.A * q2.C) - (q1.B * q2.D) + (q1.C * q2.A) + (q1.D * q2.B),
|
||||
(q1.A * q2.D) + (q1.B * q2.C) - (q1.C * q2.B) + (q1.D * q2.A))
|
||||
End Operator
|
||||
|
||||
Public Shared Widening Operator CType(d As Double) As Quaternion
|
||||
Return New Quaternion(d, 0, 0, 0)
|
||||
End Operator
|
||||
#End Region
|
||||
End Structure
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
Module Program
|
||||
Sub Main()
|
||||
Dim q As New Quaternion(1, 2, 3, 4),
|
||||
q1 As New Quaternion(2, 3, 4, 5),
|
||||
q2 As New Quaternion(3, 4, 5, 6),
|
||||
r As Double = 7
|
||||
|
||||
Console.WriteLine($"q = {q}")
|
||||
Console.WriteLine($"q1 = {q1}")
|
||||
Console.WriteLine($"q2 = {q2}")
|
||||
Console.WriteLine($"r = {r}")
|
||||
Console.WriteLine($"q.Norm = {q.Norm}")
|
||||
Console.WriteLine($"q1.Norm = {q1.Norm}")
|
||||
Console.WriteLine($"q2.Norm = {q2.Norm}")
|
||||
Console.WriteLine($"-q = {-q}")
|
||||
Console.WriteLine($"q.Conjugate = {q.Conjugate}")
|
||||
Console.WriteLine($"q + r = {q + r}")
|
||||
Console.WriteLine($"q1 + q2 = {q1 + q2}")
|
||||
Console.WriteLine($"q2 + q1 = {q2 + q1}")
|
||||
Console.WriteLine($"q * r = {q * r}")
|
||||
Console.WriteLine($"q1 * q2 = {q1 * q2}")
|
||||
Console.WriteLine($"q2 * q1 = {q2 * q1}")
|
||||
Console.WriteLine($"q1*q2 {If((q1 * q2) = (q2 * q1), "=", "!=")} q2*q1")
|
||||
End Sub
|
||||
End Module
|
||||
Loading…
Add table
Add a link
Reference in a new issue