From 843ef9305a988a4fa4059426f2f93aeaa927aba3 Mon Sep 17 00:00:00 2001 From: librasteve Date: Sun, 30 Jun 2024 18:40:13 +0100 Subject: [PATCH 01/18] add .round --- lib/Math/Vector.pm | 8 ++++++-- t/01-basics.t | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/Math/Vector.pm b/lib/Math/Vector.pm index 812628f..1bad61d 100644 --- a/lib/Math/Vector.pm +++ b/lib/Math/Vector.pm @@ -19,10 +19,10 @@ class Math::Vector does Positional multi method Str() { - "(" ~ @.coordinates.join(', ') ~ ")"; + "^(" ~ @.coordinates.join(', ') ~ ")"; } - multi method perl() + multi method raku() { "Math::Vector.new(" ~ @.coordinates.map({.perl}).join(', ') ~ ")"; } @@ -83,6 +83,10 @@ class Math::Vector does Positional return Math::Vector.new(@.coordinates); } } + + method round($r) { + Math::Vector.new(@.coordinates>>.round($r)); + } multi sub infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { diff --git a/t/01-basics.t b/t/01-basics.t index 6762bc5..5e0efb2 100644 --- a/t/01-basics.t +++ b/t/01-basics.t @@ -26,18 +26,18 @@ isa-ok($v7, Math::Vector, "Variable is of type Math::Vector"); isa-ok($vcrazy, Math::Vector, "Variable is of type Math::Vector"); does-ok($v1, Positional, "Vector is Positional"); -is(~$v1, "(1, 2, 3)", "Stringify works"); -is(~$v3, "(-1, 0, 2)", "Stringify works"); -is(~$origin3d, "(0, 0, 0)", "Stringify works"); -is(~$v5, "(1, 2, 3, 4, 5)", "Stringify works"); -is(~$vcrazy, "((1, 2, 3), (-1, 0, -1))", "Stringify works"); +is(~$v1, "^(1, 2, 3)", "Stringify works"); +is(~$v3, "^(-1, 0, 2)", "Stringify works"); +is(~$origin3d, "^(0, 0, 0)", "Stringify works"); +is(~$v5, "^(1, 2, 3, 4, 5)", "Stringify works"); +is(~$vcrazy, "^(^(1, 2, 3), ^(-1, 0, -1))", "Stringify works"); is($v1[0], 1, "[] works on Vector"); is($v1[1..2], (2, 3), "[] works on Vector"); -is(~EVAL($v1.perl), ~$v1, ".perl works"); -is(~EVAL($v9.perl), ~$v9, ".perl works"); -is(~EVAL($vcrazy.perl), ~$vcrazy, ".perl works"); +is(~EVAL($v1.raku), ~$v1, ".raku works"); +is(~EVAL($v9.raku), ~$v9, ".raku works"); +is(~EVAL($vcrazy.raku), ~$vcrazy, ".raku works"); is $v11.conj, Math::Vector.new(0-i,1-i), ".conj works"; @@ -48,8 +48,8 @@ is($v7.dim, 7, "dim works for 7D Math::Vector"); is-approx($v7 ⋅ $v8, 0, "Perpendicular Math::Vectors have 0 dot product"); #basic math tests -is(~($v1 + $v2), "(4, 6, 3)", "Basic sum works"); -is(~($v7 + $v9), "(2, 2, 3, 4, 5, 6, 7)", "Basic sum works, 7D"); +is(~($v1 + $v2), "^(4, 6, 3)", "Basic sum works"); +is(~($v7 + $v9), "^(2, 2, 3, 4, 5, 6, 7)", "Basic sum works, 7D"); is($v1 + $v2, $v2 + $v1, "Addition is commutative"); is(($v1 + $v2) + $v3, $v1 + ($v2 + $v3), "Addition is associative"); is($v1 + $origin3d, $v1, "Addition with origin leaves original"); @@ -61,11 +61,11 @@ is($v1 + $origin3d, $v1, "Addition with origin leaves original"); # } # is(~($v1 + $v2), "(4, 6, 3)", "Basic sum works"); -is(~($v1 - $v2), "(-2, -2, 3)", "Basic subtraction works"); +is(~($v1 - $v2), "^(-2, -2, 3)", "Basic subtraction works"); is($v1 - $v2, -($v2 - $v1), "Subtraction is anticommutative"); is($v1 - $origin3d, $v1, "Subtracting the origin leaves original"); is(-$origin3d, $origin3d, "Negating the origin leaves the origin"); -is(~(-$v2), "(-3, -4, 0)", "Negating works"); +is(~(-$v2), "^(-3, -4, 0)", "Negating works"); # { # my Math::Vector $a = $v1; # $a -= $v2; @@ -130,7 +130,7 @@ dies-ok( { $v7 dot $v5 }, "You can't do dot products of different dimensions"); } #cross product tests -is(~($v1 × $v2), "(-12, 9, -2)", "Basic cross product works"); +is(~($v1 × $v2), "^(-12, 9, -2)", "Basic cross product works"); for flat ($v1, $v2, $v3) X ($v1, $v2, $v3) -> $x, $y { @@ -209,7 +209,7 @@ dies-ok( { $v1.Num; }, "Make sure .Num does not work on 3D Math::Vector"); # # my Math::VectorWithLength $vl = Math::VectorWithLength.new($v7.coordinates); # isa-ok($vl, Math::VectorWithLength, "Variable is of type Math::VectorWithLength"); -# my $vlc = EVAL($vl.perl); -# isa-ok($vlc, Math::VectorWithLength, "EVAL'd perl'd variable is of type Math::VectorWithLength"); +# my $vlc = EVAL($vl.raku); +# isa-ok($vlc, Math::VectorWithLength, "EVAL'd raku'd variable is of type Math::VectorWithLength"); -done-testing; \ No newline at end of file +done-testing; From f8440a15c70859d029bf7d04315b4b4729182f6f Mon Sep 17 00:00:00 2001 From: librasteve Date: Wed, 3 Jul 2024 12:18:10 +0100 Subject: [PATCH 02/18] drop no precomp --- lib/Math/Vector.pm | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Math/Vector.pm b/lib/Math/Vector.pm index 1bad61d..b809104 100644 --- a/lib/Math/Vector.pm +++ b/lib/Math/Vector.pm @@ -1,5 +1,4 @@ use v6; -no precompilation; # avoid https://github.com/rakudo/rakudo/issues/1219 use Test; # so we can define is-approx-vector From 8c062f4de113033e35bab8b967185131ea05d8df Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 08:47:50 +0100 Subject: [PATCH 03/18] rename coordinates => components --- META6.json | 31 +++++++++++++-------- lib/Math/{Vector.pm => Vector.rakumod} | 38 +++++++++++++------------- t/{01-basics.t => 01-basics.rakutest} | 6 ++-- 3 files changed, 41 insertions(+), 34 deletions(-) rename lib/Math/{Vector.pm => Vector.rakumod} (79%) rename t/{01-basics.t => 01-basics.rakutest} (97%) diff --git a/META6.json b/META6.json index 6665a79..1c33a82 100644 --- a/META6.json +++ b/META6.json @@ -1,13 +1,20 @@ { - "perl" : "6.*", - "name" : "Math::Vector", - "version" : "0.5.1", - "description" : "Vector math class", - "provides" : { - "Math::Vector": "lib/Math/Vector.pm" - }, - "depends" : [], - "tags" : [ "Math" ], - "source-type" : "git", - "source-url" : "git://github.com/colomon/Math-Vector.git" -} + "name": "Math::Vector", + "description": "Vector math class", + "version": "0.5.1", + "perl": "6.d", + "authors": [ + "librasteve" + ], + "auth": "zef:librasteve", + "depends": [], + "provides": { + "Math::Vector": "lib/Math/Vector.rakumod" + }, + "license": "Artistic-2.0", + "tags": [ + "Math" + ], + "source-url": "git://github.com/colomon/Math-Vector.git", + "source-type": "git" +} \ No newline at end of file diff --git a/lib/Math/Vector.pm b/lib/Math/Vector.rakumod similarity index 79% rename from lib/Math/Vector.pm rename to lib/Math/Vector.rakumod index b809104..a62ebd6 100644 --- a/lib/Math/Vector.pm +++ b/lib/Math/Vector.rakumod @@ -1,29 +1,29 @@ -use v6; +use v6.d; -use Test; # so we can define is-approx-vector +use Test; class Math::Vector does Positional { - has @.coordinates handles ; + has @.components handles ; multi method new (*@x) { - self.bless(coordinates => @x); + self.bless(components => @x); } multi method new (@x) { - self.bless(coordinates => @x); + self.bless(components => @x); } multi method Str() { - "^(" ~ @.coordinates.join(', ') ~ ")"; + "^(" ~ @.components.join(', ') ~ ")"; } multi method raku() { - "Math::Vector.new(" ~ @.coordinates.map({.perl}).join(', ') ~ ")"; + "Math::Vector.new(" ~ @.components.map({.perl}).join(', ') ~ ")"; } multi method Num() @@ -36,12 +36,12 @@ class Math::Vector does Positional } method dim() { - @.coordinates.elems; + @.components.elems; } multi sub infix:<⋅>(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export # is tighter(&infix:<+>) (NYI) { - [+]($a.coordinates »*« $b.coordinates); + [+]($a.components »*« $b.components); } multi sub infix:(Math::Vector $a, Math::Vector $b) is export @@ -64,7 +64,7 @@ class Math::Vector does Positional multi method conj() { - Math::Vector.new(@.coordinates>>.conj); + Math::Vector.new(@.components>>.conj); } method Unitize() is DEPRECATED('unitize') { @@ -75,46 +75,46 @@ class Math::Vector does Positional my $length = self.length; if $length > 1e-10 { - return Math::Vector.new(@.coordinates >>/>> $length); + return Math::Vector.new(@.components >>/>> $length); } else { - return Math::Vector.new(@.coordinates); + return Math::Vector.new(@.components); } } method round($r) { - Math::Vector.new(@.coordinates>>.round($r)); + Math::Vector.new(@.components>>.round($r)); } multi sub infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { - Math::Vector.new($a.coordinates »+« $b.coordinates); + Math::Vector.new($a.components »+« $b.components); } multi sub infix:<->(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { - Math::Vector.new($a.coordinates »-« $b.coordinates); + Math::Vector.new($a.components »-« $b.components); } multi sub prefix:<->(Math::Vector $a) is export { - Math::Vector.new(0 <<-<< $a.coordinates); + Math::Vector.new(0 <<-<< $a.components); } multi sub infix:<*>(Math::Vector $a, $b) is export { - Math::Vector.new($a.coordinates >>*>> $b); + Math::Vector.new($a.components >>*>> $b); } multi sub infix:<*>($a, Math::Vector $b) is export { - Math::Vector.new($a <<*<< $b.coordinates); + Math::Vector.new($a <<*<< $b.components); } multi sub infix:(Math::Vector $a, $b) is export { - Math::Vector.new($a.coordinates >>/>> $b); + Math::Vector.new($a.components >>/>> $b); } multi sub infix:<×>(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export diff --git a/t/01-basics.t b/t/01-basics.rakutest similarity index 97% rename from t/01-basics.t rename to t/01-basics.rakutest index 5e0efb2..0458325 100644 --- a/t/01-basics.t +++ b/t/01-basics.rakutest @@ -193,12 +193,12 @@ dies-ok( { $v1.Num; }, "Make sure .Num does not work on 3D Math::Vector"); # # multi method new (*@x) # { -# self.bless(*, coordinates => @x, length => sqrt [+] (@x »*« @x)); +# self.bless(*, components => @x, length => sqrt [+] (@x »*« @x)); # } # # multi method new (@x) # { -# self.bless(*, coordinates => @x, length => sqrt [+] (@x »*« @x)); +# self.bless(*, components => @x, length => sqrt [+] (@x »*« @x)); # } # # submethod Length @@ -207,7 +207,7 @@ dies-ok( { $v1.Num; }, "Make sure .Num does not work on 3D Math::Vector"); # } # } # -# my Math::VectorWithLength $vl = Math::VectorWithLength.new($v7.coordinates); +# my Math::VectorWithLength $vl = Math::VectorWithLength.new($v7.components); # isa-ok($vl, Math::VectorWithLength, "Variable is of type Math::VectorWithLength"); # my $vlc = EVAL($vl.raku); # isa-ok($vlc, Math::VectorWithLength, "EVAL'd raku'd variable is of type Math::VectorWithLength"); From f45133cc5d74adfd3fe161b855a589f3ff97610f Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 09:30:21 +0100 Subject: [PATCH 04/18] restrict cross product to 3d --- lib/Math/Vector.rakumod | 31 +++---------------------------- t/01-basics.rakutest | 12 +----------- 2 files changed, 4 insertions(+), 39 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index a62ebd6..8d7901b 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -119,34 +119,9 @@ class Math::Vector does Positional multi sub infix:<×>(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export { - Math::Vector.new($a[1] * $b[2] - $a[2] * $b[1], - $a[2] * $b[0] - $a[0] * $b[2], - $a[0] * $b[1] - $a[1] * $b[0]); - } - - multi sub infix:<×>(Math::Vector $a where { $a.dim == 7 }, Math::Vector $b where { $b.dim == 7 }) is export - { - Math::Vector.new($a[1] * $b[3] - $a[3] * $b[1] - + $a[2] * $b[6] - $a[6] * $b[2] - + $a[4] * $b[5] - $a[5] * $b[4], - $a[2] * $b[4] - $a[4] * $b[2] - + $a[3] * $b[0] - $a[0] * $b[3] - + $a[5] * $b[6] - $a[6] * $b[5], - $a[3] * $b[5] - $a[5] * $b[3] - + $a[4] * $b[1] - $a[1] * $b[4] - + $a[6] * $b[0] - $a[0] * $b[6], - $a[4] * $b[6] - $a[6] * $b[4] - + $a[5] * $b[2] - $a[2] * $b[5] - + $a[0] * $b[1] - $a[1] * $b[0], - $a[5] * $b[0] - $a[0] * $b[5] - + $a[6] * $b[3] - $a[3] * $b[6] - + $a[1] * $b[2] - $a[2] * $b[1], - $a[6] * $b[1] - $a[1] * $b[6] - + $a[0] * $b[4] - $a[4] * $b[0] - + $a[2] * $b[3] - $a[3] * $b[2], - $a[0] * $b[2] - $a[2] * $b[0] - + $a[1] * $b[5] - $a[5] * $b[1] - + $a[3] * $b[4] - $a[4] * $b[3]); + Math::Vector.new($a[1] * $b[2] - $a[2] * $b[1], + $a[2] * $b[0] - $a[0] * $b[2], + $a[0] * $b[1] - $a[1] * $b[0]); } multi sub infix:(Math::Vector $a, Math::Vector $b) is export diff --git a/t/01-basics.rakutest b/t/01-basics.rakutest index 0458325..88660cf 100644 --- a/t/01-basics.rakutest +++ b/t/01-basics.rakutest @@ -142,17 +142,7 @@ for flat ($v1, $v2, $v3) X ($v1, $v2, $v3) -> $x, $y "|x × y|^2 = |x|^2 * |y|^2 - (x ⋅ y)^2"); } -for flat ($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.length ** 2, $x.length ** 2 * $y.length ** 2 - ($x ⋅ $y) ** 2, - "|x × y|^2 = |x|^2 * |y|^2 - (x ⋅ y)^2"); -} - -lives-ok { $v7 cross $v8, "7D cross product works writing out cross"} +dies-ok { $v7 cross $v8, "7D cross product works writing out cross"} dies-ok( { $v1 × $v7 }, "You can't do cross products of different dimensions"); dies-ok( { $v5 × $v6 }, "You can't do 5D cross products"); dies-ok( { $v1 cross $v7 }, "You can't do cross products of different dimensions"); From ed5ed1dcf1c7e127a8873a6c33dfe75836af29cd Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 09:31:04 +0100 Subject: [PATCH 05/18] drop deprecated items --- lib/Math/Vector.rakumod | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 8d7901b..9271321 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -30,10 +30,6 @@ class Math::Vector does Positional { die "Cannot call Num on Vector!"; } - - method Dim() is DEPRECATED('dim') { - self.dim; - } method dim() { @.components.elems; @@ -49,10 +45,6 @@ class Math::Vector does Positional $a ⋅ $b; } - method Length() is DEPRECATED('length') { - self.length; - } - method length() { sqrt(self ⋅ self.conj); } @@ -66,10 +58,6 @@ class Math::Vector does Positional { Math::Vector.new(@.components>>.conj); } - - method Unitize() is DEPRECATED('unitize') { - self.unitize; - } method unitize() { my $length = self.length; From 7c1c0b53cbaf65a14af8d5db5089c66391bf4aeb Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 09:34:15 +0100 Subject: [PATCH 06/18] drop commented out code --- t/01-basics.rakutest | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/t/01-basics.rakutest b/t/01-basics.rakutest index 88660cf..090e73f 100644 --- a/t/01-basics.rakutest +++ b/t/01-basics.rakutest @@ -176,30 +176,4 @@ dies-ok( { $v5 cross $v6 }, "You can't do 5D cross products"); # isa-ok(+$v1, Math::Vector, "Prefix + works on the Math::Vector class"); dies-ok( { $v1.Num; }, "Make sure .Num does not work on 3D Math::Vector"); -# test extensions -# class Math::VectorWithLength is Math::Vector -# { -# has $.length; -# -# multi method new (*@x) -# { -# self.bless(*, components => @x, length => sqrt [+] (@x »*« @x)); -# } -# -# multi method new (@x) -# { -# self.bless(*, components => @x, length => sqrt [+] (@x »*« @x)); -# } -# -# submethod Length -# { -# $.length; -# } -# } -# -# my Math::VectorWithLength $vl = Math::VectorWithLength.new($v7.components); -# isa-ok($vl, Math::VectorWithLength, "Variable is of type Math::VectorWithLength"); -# my $vlc = EVAL($vl.raku); -# isa-ok($vlc, Math::VectorWithLength, "EVAL'd raku'd variable is of type Math::VectorWithLength"); - done-testing; From 4ea0b50140305fcf0aa6e5f4a929119ffade71c2 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 09:36:16 +0100 Subject: [PATCH 07/18] multi sub => multi --- lib/Math/Vector.rakumod | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 9271321..74442ea 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -35,12 +35,12 @@ class Math::Vector does Positional @.components.elems; } - multi sub infix:<⋅>(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export # is tighter(&infix:<+>) (NYI) + multi infix:<⋅>(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export # is tighter(&infix:<+>) (NYI) { [+]($a.components »*« $b.components); } - multi sub infix:(Math::Vector $a, Math::Vector $b) is export + multi infix:(Math::Vector $a, Math::Vector $b) is export { $a ⋅ $b; } @@ -75,49 +75,49 @@ class Math::Vector does Positional Math::Vector.new(@.components>>.round($r)); } - multi sub infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export + multi infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { Math::Vector.new($a.components »+« $b.components); } - multi sub infix:<->(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export + multi infix:<->(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { Math::Vector.new($a.components »-« $b.components); } - multi sub prefix:<->(Math::Vector $a) is export + multi prefix:<->(Math::Vector $a) is export { Math::Vector.new(0 <<-<< $a.components); } - multi sub infix:<*>(Math::Vector $a, $b) is export + multi infix:<*>(Math::Vector $a, $b) is export { Math::Vector.new($a.components >>*>> $b); } - multi sub infix:<*>($a, Math::Vector $b) is export + multi infix:<*>($a, Math::Vector $b) is export { Math::Vector.new($a <<*<< $b.components); } - multi sub infix:(Math::Vector $a, $b) is export + multi infix:(Math::Vector $a, $b) is export { Math::Vector.new($a.components >>/>> $b); } - multi sub infix:<×>(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export + multi infix:<×>(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export { Math::Vector.new($a[1] * $b[2] - $a[2] * $b[1], $a[2] * $b[0] - $a[0] * $b[2], $a[0] * $b[1] - $a[1] * $b[0]); } - multi sub infix:(Math::Vector $a, Math::Vector $b) is export + multi infix:(Math::Vector $a, Math::Vector $b) is export { $a × $b; } - multi sub circumfix:<⎡ ⎤>(Math::Vector $a) is export + multi circumfix:<⎡ ⎤>(Math::Vector $a) is export { $a.length; } From a74c6791a1b6708574060c6de74a982cf1e7869f Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 09:43:03 +0100 Subject: [PATCH 08/18] multi sub => multi --- lib/Math/Vector.rakumod | 17 +++++------------ t/01-basics.rakutest | 6 ++++++ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 74442ea..b2e9f20 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -1,7 +1,5 @@ use v6.d; -use Test; - class Math::Vector does Positional { has @.components handles ; @@ -48,13 +46,13 @@ class Math::Vector does Positional method length() { sqrt(self ⋅ self.conj); } - - multi method abs() + + method abs() { self.length; } - multi method conj() + method conj() { Math::Vector.new(@.components>>.conj); } @@ -74,12 +72,12 @@ class Math::Vector does Positional method round($r) { Math::Vector.new(@.components>>.round($r)); } - + multi infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { Math::Vector.new($a.components »+« $b.components); } - + multi infix:<->(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { Math::Vector.new($a.components »-« $b.components); @@ -121,11 +119,6 @@ class Math::Vector does Positional { $a.length; } - - sub is-approx-vector(Math::Vector $a, Math::Vector $b, $desc) is export - { - ok(($a - $b).length < 0.00001, $desc); - } } subset Math::UnitVector of Math::Vector where { (1 - 1e-10) < $^v.length < (1 + 1e-10) }; diff --git a/t/01-basics.rakutest b/t/01-basics.rakutest index 090e73f..e378f8b 100644 --- a/t/01-basics.rakutest +++ b/t/01-basics.rakutest @@ -2,6 +2,12 @@ use v6; use Math::Vector; use Test; + +sub is-approx-vector(Math::Vector $a, Math::Vector $b, $desc) +{ + ok(($a - $b).length < 0.00001, $desc); +} + my $v1 = Math::Vector.new(1, 2, 3); my Math::Vector $v2 = Math::Vector.new(3, 4, 0); my @v3 = (-1, 0, 2); From 177381b205f247a15173f11f98b7adbbd0ded3bc Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 09:50:40 +0100 Subject: [PATCH 09/18] dot to method --- lib/Math/Vector.rakumod | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index b2e9f20..ee780f5 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -29,22 +29,18 @@ class Math::Vector does Positional die "Cannot call Num on Vector!"; } - method dim() { - @.components.elems; - } - - multi infix:<⋅>(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export # is tighter(&infix:<+>) (NYI) + method dim() { - [+]($a.components »*« $b.components); + @.components.elems; } - multi infix:(Math::Vector $a, Math::Vector $b) is export + method dot($other) { - $a ⋅ $b; + [+](self.components »*« $other.components); } method length() { - sqrt(self ⋅ self.conj); + sqrt(self.dot: self.conj); } method abs() @@ -121,4 +117,15 @@ class Math::Vector does Positional } } -subset Math::UnitVector of Math::Vector where { (1 - 1e-10) < $^v.length < (1 + 1e-10) }; + +multi infix:<⋅>(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export +{ + $a.dot: $b +} + +multi infix:(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export +{ + $a.dot: $b +} + +subset Math::UnitVector of Math::Vector where { (1 - 1e-10) < $^v.length < (1 + 1e-10) }; \ No newline at end of file From 8c61767d0f93dc0c460be8c8a95ea50e65e22958 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 10:04:41 +0100 Subject: [PATCH 10/18] overrides to body --- lib/Math/Vector.rakumod | 115 +++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 42 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index ee780f5..79e69e0 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -34,6 +34,26 @@ class Math::Vector does Positional @.components.elems; } + method add($other) + { + Math::Vector.new(self.components »+« $other.components); + } + + method subtract($other) + { + Math::Vector.new(self.components »-« $other.components); + } + + method negate() + { + Math::Vector.new(0 <<-<< self.components); + } + + method scale($other) + { + Math::Vector.new(self.components >>*>> $other); + } + method dot($other) { [+](self.components »*« $other.components); @@ -48,6 +68,17 @@ class Math::Vector does Positional self.length; } + method cross($other) + { + my ($a, $b) := self, $other; + + Math::Vector.new( + $a[1] * $b[2] - $a[2] * $b[1], + $a[2] * $b[0] - $a[0] * $b[2], + $a[0] * $b[1] - $a[1] * $b[0], + ); + } + method conj() { Math::Vector.new(@.components>>.conj); @@ -68,64 +99,64 @@ class Math::Vector does Positional method round($r) { Math::Vector.new(@.components>>.round($r)); } +} - multi infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export - { - Math::Vector.new($a.components »+« $b.components); - } - - multi infix:<->(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export - { - Math::Vector.new($a.components »-« $b.components); - } - multi prefix:<->(Math::Vector $a) is export - { - Math::Vector.new(0 <<-<< $a.components); - } +subset Math::UnitVector of Math::Vector where { (1 - 1e-10) < $^v.length < (1 + 1e-10) }; - multi infix:<*>(Math::Vector $a, $b) is export - { - Math::Vector.new($a.components >>*>> $b); - } - multi infix:<*>($a, Math::Vector $b) is export - { - Math::Vector.new($a <<*<< $b.components); - } +multi infix:<+> (Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export +{ + $a.add: $b; +} - multi infix:(Math::Vector $a, $b) is export - { - Math::Vector.new($a.components >>/>> $b); - } +multi infix:<->(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export +{ + $a.subtract: $b; +} - multi infix:<×>(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export - { - Math::Vector.new($a[1] * $b[2] - $a[2] * $b[1], - $a[2] * $b[0] - $a[0] * $b[2], - $a[0] * $b[1] - $a[1] * $b[0]); - } +multi prefix:<->(Math::Vector $a) is export +{ + $a.negate; +} - multi infix:(Math::Vector $a, Math::Vector $b) is export - { - $a × $b; - } +multi infix:<*>(Math::Vector $a, $b) is export +{ + $a.scale: $b; +} - multi circumfix:<⎡ ⎤>(Math::Vector $a) is export - { - $a.length; - } +multi infix:<*>($a, Math::Vector $b) is export +{ + $b.scale: $a; } +multi infix:(Math::Vector $a, $b) is export +{ + $a.scale: 1 / $b; +} multi infix:<⋅>(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { - $a.dot: $b + $a.dot: $b; } multi infix:(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export { - $a.dot: $b + $a.dot: $b; +} + +multi infix:<×>(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export +{ + $a.cross: $b; +} + +multi infix:(Math::Vector $a where { $a.dim == 3 }, Math::Vector $b where { $b.dim == 3 }) is export +{ + $a.cross: $b; +} + +multi circumfix:<⎡ ⎤>(Math::Vector $a) is export +{ + $a.length; } -subset Math::UnitVector of Math::Vector where { (1 - 1e-10) < $^v.length < (1 + 1e-10) }; \ No newline at end of file From 4fbdc7694b7ab8744e3edf795bfbfaf44b8bb4ea Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 10:31:31 +0100 Subject: [PATCH 11/18] ok cmp --- lib/Math/Vector.rakumod | 25 +++++++++++++++++++++++-- t/01-basics.rakutest | 16 ++-------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 79e69e0..55f6bb1 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -84,7 +84,8 @@ class Math::Vector does Positional Math::Vector.new(@.components>>.conj); } - method unitize() { + method unitize() + { my $length = self.length; if $length > 1e-10 { @@ -96,9 +97,24 @@ class Math::Vector does Positional } } - method round($r) { + method round($r) + { Math::Vector.new(@.components>>.round($r)); } + + method cmp($other) + { +# my $tol = 0.00001; + my $tol := $*TOLERANCE; + + given self.length - $other.length + { + when * > $tol { Less } + when -$tol < * < $tol { Same } + when -$tol > * { More } + } + + } } @@ -160,3 +176,8 @@ multi circumfix:<⎡ ⎤>(Math::Vector $a) is export $a.length; } +multi infix:(Math::Vector $a, Math::Vector $b where { $a.dim == $b.dim }) is export +{ + $a.cmp: $b; +} + diff --git a/t/01-basics.rakutest b/t/01-basics.rakutest index e378f8b..5477693 100644 --- a/t/01-basics.rakutest +++ b/t/01-basics.rakutest @@ -1,11 +1,11 @@ -use v6; +use v6.d; use Math::Vector; use Test; sub is-approx-vector(Math::Vector $a, Math::Vector $b, $desc) { - ok(($a - $b).length < 0.00001, $desc); + ok( ($a cmp $b) ~~ Same, $desc); } my $v1 = Math::Vector.new(1, 2, 3); @@ -60,23 +60,11 @@ is($v1 + $v2, $v2 + $v1, "Addition is commutative"); is(($v1 + $v2) + $v3, $v1 + ($v2 + $v3), "Addition is associative"); is($v1 + $origin3d, $v1, "Addition with origin leaves original"); -# { -# my Math::Vector $a = $v1; -# $a += $v2; -# is(~($v1 + $v2), ~$a, "+= works"); -# } -# is(~($v1 + $v2), "(4, 6, 3)", "Basic sum works"); - is(~($v1 - $v2), "^(-2, -2, 3)", "Basic subtraction works"); is($v1 - $v2, -($v2 - $v1), "Subtraction is anticommutative"); is($v1 - $origin3d, $v1, "Subtracting the origin leaves original"); is(-$origin3d, $origin3d, "Negating the origin leaves the origin"); is(~(-$v2), "^(-3, -4, 0)", "Negating works"); -# { -# my Math::Vector $a = $v1; -# $a -= $v2; -# is(~($v1 - $v2), ~$a, "+= works"); -# } #lengths is($origin3d.length, 0, "Origin has 0 length"); From 2ea0f96aac84d38584d1e94d7458b47a5d895b16 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 10:34:28 +0100 Subject: [PATCH 12/18] ok cmp --- lib/Math/Vector.rakumod | 5 ++--- t/01-basics.rakutest | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 55f6bb1..6f14bc8 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -104,14 +104,13 @@ class Math::Vector does Positional method cmp($other) { -# my $tol = 0.00001; my $tol := $*TOLERANCE; given self.length - $other.length { - when * > $tol { Less } + when * > $tol { More } when -$tol < * < $tol { Same } - when -$tol > * { More } + when -$tol > * { Less } } } diff --git a/t/01-basics.rakutest b/t/01-basics.rakutest index 5477693..fc59489 100644 --- a/t/01-basics.rakutest +++ b/t/01-basics.rakutest @@ -2,7 +2,6 @@ use v6.d; use Math::Vector; use Test; - sub is-approx-vector(Math::Vector $a, Math::Vector $b, $desc) { ok( ($a cmp $b) ~~ Same, $desc); From b56d1d06d1ba89c744a733a632db1ba7456e7096 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 10:58:59 +0100 Subject: [PATCH 13/18] add readme --- META6.json | 7 ++++--- README | 31 +++++++++++++++++++++++-------- bin/synopsis.raku | 17 +++++++++++++++++ lib/Math/Vector.rakumod | 10 +++++----- 4 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 bin/synopsis.raku diff --git a/META6.json b/META6.json index 1c33a82..719b8b1 100644 --- a/META6.json +++ b/META6.json @@ -1,7 +1,7 @@ { "name": "Math::Vector", "description": "Vector math class", - "version": "0.5.1", + "version": "0.6.0", "perl": "6.d", "authors": [ "librasteve" @@ -13,8 +13,9 @@ }, "license": "Artistic-2.0", "tags": [ - "Math" + "Math", + "Vector" ], - "source-url": "git://github.com/colomon/Math-Vector.git", + "source-url": "git://github.com/librasteve/Math-Vector.git", "source-type": "git" } \ No newline at end of file diff --git a/README b/README index d6e2bbc..42bf018 100644 --- a/README +++ b/README @@ -1,12 +1,27 @@ -This is a simple attempt to create a working Vector class. Initially intended -to support 3D vectors, it turned out to be easier to support vectors of -any dimension. +[![License: Artistic-2.0](https://img.shields.io/badge/License-Artistic%202.0-0298c3.svg)](https://opensource.org/licenses/Artistic-2.0) -Math::Vector requires Rakudo from 7/25/2010 or later. -Build sequence: +#Math::Vector - ufo - make - make test +This is a fork of https://github.com/colomon/Math-Vector from v0.6.0 + +# SYNOPSIS + +```raku +#!/usr/bin/env raku +use Math::Vector; + +my $v1 = Math::Vector.new(1, 2, 3); +my $v2 = Math::Vector.new(3, 4, 0); + +say "My vector $v1 has {$v1.dim} dimensions"; #^(1, 2, 3) + +$v1 + $v2; #^(4, 6, 3) +$v1 * 2; #^(2, 4, 6) +$v1 ⋅ $v2; #11 +$v1 × $v2; #^(-12, 9, -2) +⎡$v1⎤; #3.7416573867739413 +-$v1; #^(-1, -2, -3) +$v1 cmp $v2; #Less +``` diff --git a/bin/synopsis.raku b/bin/synopsis.raku new file mode 100644 index 0000000..27fe344 --- /dev/null +++ b/bin/synopsis.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use Math::Vector; + +my $v1 = Math::Vector.new(1, 2, 3); +my $v2 = Math::Vector.new(3, 4, 0); + +say "My vector $v1 has {$v1.dim} dimensions"; #^(1, 2, 3) + +$v1 + $v2; #^(4, 6, 3) +$v1 * 2; #^(2, 4, 6) +$v1 ⋅ $v2; #11 +$v1 × $v2; #^(-12, 9, -2) +⎡$v1⎤; #3.7416573867739413 +-$v1; #^(-1, -2, -3) +$v1 cmp $v2; #Less + + diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 6f14bc8..89938ca 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -79,11 +79,6 @@ class Math::Vector does Positional ); } - method conj() - { - Math::Vector.new(@.components>>.conj); - } - method unitize() { my $length = self.length; @@ -97,6 +92,11 @@ class Math::Vector does Positional } } + method conj() + { + Math::Vector.new(@.components>>.conj); + } + method round($r) { Math::Vector.new(@.components>>.round($r)); From fe915657e746fea59bb2c9d04a9b9bb62306828a Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 11:01:23 +0100 Subject: [PATCH 14/18] mv synopsis --- bin/{synopsis.raku => synopsis-mv.raku} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bin/{synopsis.raku => synopsis-mv.raku} (100%) diff --git a/bin/synopsis.raku b/bin/synopsis-mv.raku similarity index 100% rename from bin/synopsis.raku rename to bin/synopsis-mv.raku From c17937a19a8f5ce6a5b7d945909f4257b2a7dd53 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 11:04:34 +0100 Subject: [PATCH 15/18] fix syn --- README | 14 +++++++------- bin/synopsis-mv.raku | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README b/README index 42bf018..18f84f1 100644 --- a/README +++ b/README @@ -16,12 +16,12 @@ my $v2 = Math::Vector.new(3, 4, 0); say "My vector $v1 has {$v1.dim} dimensions"; #^(1, 2, 3) -$v1 + $v2; #^(4, 6, 3) -$v1 * 2; #^(2, 4, 6) -$v1 ⋅ $v2; #11 -$v1 × $v2; #^(-12, 9, -2) -⎡$v1⎤; #3.7416573867739413 --$v1; #^(-1, -2, -3) -$v1 cmp $v2; #Less +say ~ ($v1 + $v2); #^(4, 6, 3) +say ~ ($v1 * 2); #^(2, 4, 6) +say ~ ($v1 ⋅ $v2); #11 +say ~ ($v1 × $v2); #^(-12, 9, -2) +say ~ (⎡$v1⎤); #3.7416573867739413 +say ~ (-$v1); #^(-1, -2, -3) +say ~ ($v1 cmp $v2); #Less ``` diff --git a/bin/synopsis-mv.raku b/bin/synopsis-mv.raku index 27fe344..4c486c6 100644 --- a/bin/synopsis-mv.raku +++ b/bin/synopsis-mv.raku @@ -6,12 +6,12 @@ my $v2 = Math::Vector.new(3, 4, 0); say "My vector $v1 has {$v1.dim} dimensions"; #^(1, 2, 3) -$v1 + $v2; #^(4, 6, 3) -$v1 * 2; #^(2, 4, 6) -$v1 ⋅ $v2; #11 -$v1 × $v2; #^(-12, 9, -2) -⎡$v1⎤; #3.7416573867739413 --$v1; #^(-1, -2, -3) -$v1 cmp $v2; #Less +say ~ ($v1 + $v2); #^(4, 6, 3) +say ~ ($v1 * 2); #^(2, 4, 6) +say ~ ($v1 ⋅ $v2); #11 +say ~ ($v1 × $v2); #^(-12, 9, -2) +say ~ (⎡$v1⎤); #3.7416573867739413 +say ~ (-$v1); #^(-1, -2, -3) +say ~ ($v1 cmp $v2); #Less From 31d77a935cc0cdeaae31ff03b3bf8ae0db456ca5 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 11:36:17 +0100 Subject: [PATCH 16/18] README => .md --- README => README.md | 0 deps.proto | 1 - 2 files changed, 1 deletion(-) rename README => README.md (100%) delete mode 100644 deps.proto diff --git a/README b/README.md similarity index 100% rename from README rename to README.md diff --git a/deps.proto b/deps.proto deleted file mode 100644 index 61bfa8c..0000000 --- a/deps.proto +++ /dev/null @@ -1 +0,0 @@ -# Math::Vector has no dependencies at this time. \ No newline at end of file From 80b4ff23aeb66308010953e5a0df750a13aa5f06 Mon Sep 17 00:00:00 2001 From: librasteve <40125330+librasteve@users.noreply.github.com> Date: Thu, 4 Jul 2024 11:36:45 +0100 Subject: [PATCH 17/18] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 18f84f1..1230014 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ [![License: Artistic-2.0](https://img.shields.io/badge/License-Artistic%202.0-0298c3.svg)](https://opensource.org/licenses/Artistic-2.0) - -#Math::Vector +# Math::Vector This is a fork of https://github.com/colomon/Math-Vector from v0.6.0 -# SYNOPSIS +## SYNOPSIS ```raku #!/usr/bin/env raku From 74e3c908148b0e2e31f985d54566f92191b17508 Mon Sep 17 00:00:00 2001 From: librasteve Date: Thu, 4 Jul 2024 12:05:09 +0100 Subject: [PATCH 18/18] rm unwanted multis --- META6.json | 2 +- lib/Math/Vector.rakumod | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/META6.json b/META6.json index 719b8b1..bb19ed0 100644 --- a/META6.json +++ b/META6.json @@ -1,7 +1,7 @@ { "name": "Math::Vector", "description": "Vector math class", - "version": "0.6.0", + "version": "0.6.1", "perl": "6.d", "authors": [ "librasteve" diff --git a/lib/Math/Vector.rakumod b/lib/Math/Vector.rakumod index 89938ca..e8d63c4 100644 --- a/lib/Math/Vector.rakumod +++ b/lib/Math/Vector.rakumod @@ -4,27 +4,27 @@ class Math::Vector does Positional { has @.components handles ; - multi method new (*@x) + multi method new (*@x) { self.bless(components => @x); } - multi method new (@x) + multi method new (@x) { self.bless(components => @x); } - multi method Str() + method Str() { "^(" ~ @.components.join(', ') ~ ")"; } - multi method raku() + method raku() { "Math::Vector.new(" ~ @.components.map({.perl}).join(', ') ~ ")"; } - multi method Num() + method Num() { die "Cannot call Num on Vector!"; }