Quaternion type: Difference between revisions
Content added Content deleted
m (→Using the matrix form: typo) |
(Add Dart implementation) |
||
Line 2,614: | Line 2,614: | ||
exp(log(s)): [2, 0.33427, 0.445694, 0.557117] |
exp(log(s)): [2, 0.33427, 0.445694, 0.557117] |
||
log(exp(s)): [2, 0.33427, 0.445694, 0.557117]</pre> |
log(exp(s)): [2, 0.33427, 0.445694, 0.557117]</pre> |
||
=={{header|Dart}}== |
|||
{{trans|Kotlin}} |
|||
<syntaxhighlight lang="Dart"> |
|||
import 'dart:math' as math; |
|||
class Quaternion { |
|||
final double a, b, c, d; |
|||
Quaternion(this.a, this.b, this.c, this.d); |
|||
Quaternion operator +(Object other) { |
|||
if (other is Quaternion) { |
|||
return Quaternion(a + other.a, b + other.b, c + other.c, d + other.d); |
|||
} else if (other is double) { |
|||
return Quaternion(a + other, b, c, d); |
|||
} |
|||
throw ArgumentError('Invalid type for addition: ${other.runtimeType}'); |
|||
} |
|||
Quaternion operator *(Object other) { |
|||
if (other is Quaternion) { |
|||
return Quaternion( |
|||
a * other.a - b * other.b - c * other.c - d * other.d, |
|||
a * other.b + b * other.a + c * other.d - d * other.c, |
|||
a * other.c - b * other.d + c * other.a + d * other.b, |
|||
a * other.d + b * other.c - c * other.b + d * other.a, |
|||
); |
|||
} else if (other is double) { |
|||
return Quaternion(a * other, b * other, c * other, d * other); |
|||
} |
|||
throw ArgumentError('Invalid type for multiplication: ${other.runtimeType}'); |
|||
} |
|||
Quaternion operator -() => Quaternion(-a, -b, -c, -d); |
|||
Quaternion conj() => Quaternion(a, -b, -c, -d); |
|||
double norm() => math.sqrt(a * a + b * b + c * c + d * d); |
|||
@override |
|||
String toString() => '($a, $b, $c, $d)'; |
|||
} |
|||
void main() { |
|||
var q = Quaternion(1.0, 2.0, 3.0, 4.0); |
|||
var q1 = Quaternion(2.0, 3.0, 4.0, 5.0); |
|||
var q2 = Quaternion(3.0, 4.0, 5.0, 6.0); |
|||
var r = 7.0; |
|||
print("q = $q"); |
|||
print("q1 = $q1"); |
|||
print("q2 = $q2"); |
|||
print("r = $r\n"); |
|||
print("norm(q) = ${q.norm().toStringAsFixed(6)}"); |
|||
print("-q = ${-q}"); |
|||
print("conj(q) = ${q.conj()}\n"); |
|||
print("r + q = ${q + r}"); |
|||
print("q + r = ${q + r}"); |
|||
print("q1 + q2 = ${q1 + q2}\n"); |
|||
print("r * q = ${q * r}"); |
|||
print("q * r = ${q * r}"); |
|||
var q3 = q1 * q2; |
|||
var q4 = q2 * q1; |
|||
print("q1 * q2 = $q3"); |
|||
print("q2 * q1 = $q4\n"); |
|||
print("q1 * q2 != q2 * q1 = ${q3 != q4}"); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
q = (1.0, 2.0, 3.0, 4.0) |
|||
q1 = (2.0, 3.0, 4.0, 5.0) |
|||
q2 = (3.0, 4.0, 5.0, 6.0) |
|||
r = 7.0 |
|||
norm(q) = 5.477226 |
|||
-q = (-1.0, -2.0, -3.0, -4.0) |
|||
conj(q) = (1.0, -2.0, -3.0, -4.0) |
|||
r + q = (8.0, 2.0, 3.0, 4.0) |
|||
q + r = (8.0, 2.0, 3.0, 4.0) |
|||
q1 + q2 = (5.0, 7.0, 9.0, 11.0) |
|||
r * q = (7.0, 14.0, 21.0, 28.0) |
|||
q * r = (7.0, 14.0, 21.0, 28.0) |
|||
q1 * q2 = (-56.0, 16.0, 24.0, 26.0) |
|||
q2 * q1 = (-56.0, 18.0, 20.0, 28.0) |
|||
q1 * q2 != q2 * q1 = true |
|||
</pre> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |