This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
for $*IN.lines() -> $text | |
{ | |
say $text.subst(/x_(\d) y_(\d)/, { "\$a.coordinates[{$0 - 1}] * \$b.coordinates[{$1 - 1}]" }, :g); | |
} |
I ended up using
where
to make dispatch to the correct version of cross product (or non at all for most dimensions).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multi sub infix:<×>(Vector $a where { $a.Dim == 3 }, Vector $b where { $b.Dim == 3 }) | |
{ | |
Vector.new($a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1], | |
$a.coordinates[2] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[2], | |
$a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0]); | |
} | |
multi sub infix:<×>(Vector $a where { $a.Dim == 7 }, Vector $b where { $b.Dim == 7 }) | |
{ | |
Vector.new($a.coordinates[1] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[1] | |
+ $a.coordinates[2] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[2] | |
+ $a.coordinates[4] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[4], | |
$a.coordinates[2] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[2] | |
+ $a.coordinates[3] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[3] | |
+ $a.coordinates[5] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[5], | |
$a.coordinates[3] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[3] | |
+ $a.coordinates[4] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[4] | |
+ $a.coordinates[6] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[6], | |
$a.coordinates[4] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[4] | |
+ $a.coordinates[5] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[5] | |
+ $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0], | |
$a.coordinates[5] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[5] | |
+ $a.coordinates[6] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[6] | |
+ $a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1], | |
$a.coordinates[6] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[6] | |
+ $a.coordinates[0] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[0] | |
+ $a.coordinates[2] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[2], | |
$a.coordinates[0] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[0] | |
+ $a.coordinates[1] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[1] | |
+ $a.coordinates[3] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[3]); | |
} | |
multi sub infix:<cross>(Vector $a, Vector $b) | |
{ | |
$a × $b; | |
} |
Having the 7D cross product is a bit silly -- but it feels like a very nice use of
where
. I think I will soon go back through most of the other operators and add where
clauses to the second parameter, to make sure it has the same dimension as the first.Of course, I don't have any idea what the 7D solutions should look like. Luckily, I have now have a test script, so I can write tests to make sure that the results of the 7D cross product obey the proper identities:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for ($v7, $v8, $v9, $v10) X ($v7, $v8, $v9, $v10) -> $x, $y | |
{ | |
my $cross = $x × $y; | |
is_approx($cross ⋅ $x, 0, "(x × y) ⋅ x = 0"); | |
is_approx($cross ⋅ $y, 0, "(x × y) ⋅ y = 0"); | |
is_approx_vector($cross, ∇($y × $x), "x × y = -y × x"); | |
is_approx($cross.LengthSquared, $x.LengthSquared * $y.LengthSquared - ($x ⋅ $y) ** 2, | |
"|x × y|^2 = |x|^2 * |y|^2 - (x ⋅ y)^2"); | |
} |
General: I forgot to mention last time that pmichaud is looking at fixing the bug that stops you from building a more complex version of an operator out of simpler versions of the same operator. When that gets fixed, the vector addition and subtraction and vector/scalar multiplication and division will look a lot nicer.
No comments:
Post a Comment