Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 139 lines (115 sloc) 4.332 kB
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
1 use v6;
2
3 class Vector
4 {
5 has @.coordinates;
6
7 multi method new (*@x)
8 {
9 self.bless(*, coordinates => @x);
10 }
11
12 multi method new (@x)
13 {
14 self.bless(*, coordinates => @x);
15 }
16
17 our Str multi method Str()
18 {
19 "(" ~ @.coordinates.join(', ') ~ ")";
20 }
21
22 our Str multi method perl()
23 {
24 self.WHAT.perl ~ ".new(" ~ @.coordinates.map({.perl}).join(', ') ~ ")";
25 }
26
27 method Dim()
28 {
29 @.coordinates.elems;
30 }
bdccdbb @LastOfTheCarelessMen Use "is also" to rearrange definitions in a more natural order.
authored
31 }
32
33 multi sub infix:<⋅>(Vector $a, Vector $b where { $a.Dim == $b.Dim }) # is tighter(&infix:<+>) (NYI)
34 {
35 [+]($a.coordinates »*« $b.coordinates);
36 }
37
38 multi sub infix:<dot>(Vector $a, Vector $b)
39 {
40 $a$b;
41 }
42
43 class Vector is also
44 {
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
45 method Length()
46 {
bdccdbb @LastOfTheCarelessMen Use "is also" to rearrange definitions in a more natural order.
authored
47 sqrt(selfself);
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
48 }
49
50 method Unitize()
51 {
52 my $length = self.Length;
53 if $length > 1e-10
54 {
55 return Vector.new(@.coordinates >>/>> $length);
56 }
57 else
58 {
59 return Vector.new(@.coordinates);
60 }
61 }
62 }
63
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
64 multi sub infix:<+>(Vector $a, Vector $b where { $a.Dim == $b.Dim })
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
65 {
66 Vector.new($a.coordinates »+« $b.coordinates);
67 }
68
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
69 multi sub infix:<->(Vector $a, Vector $b where { $a.Dim == $b.Dim })
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
70 {
71 Vector.new($a.coordinates »-« $b.coordinates);
72 }
73
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
74 multi sub prefix:<->(Vector $a)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
75 {
76 Vector.new(0 <<-<< $a.coordinates);
77 }
78
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
79 multi sub infix:<*>(Vector $a, $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
80 {
81 Vector.new($a.coordinates >>*>> $b);
82 }
83
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
84 multi sub infix:<*>($a, Vector $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
85 {
86 Vector.new($a <<*<< $b.coordinates);
87 }
88
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
89 multi sub infix:</>(Vector $a, $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
90 {
91 Vector.new($a.coordinates >>/>> $b);
92 }
93
94 multi sub infix:<×>(Vector $a where { $a.Dim == 3 }, Vector $b where { $b.Dim == 3 })
95 {
96 Vector.new($a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
97 $a.coordinates[2] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[2],
98 $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0]);
99 }
100
101 multi sub infix:<×>(Vector $a where { $a.Dim == 7 }, Vector $b where { $b.Dim == 7 })
102 {
103 Vector.new($a.coordinates[1] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[1]
104 + $a.coordinates[2] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[2]
105 + $a.coordinates[4] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[4],
106 $a.coordinates[2] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[2]
107 + $a.coordinates[3] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[3]
108 + $a.coordinates[5] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[5],
109 $a.coordinates[3] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[3]
110 + $a.coordinates[4] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[4]
111 + $a.coordinates[6] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[6],
112 $a.coordinates[4] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[4]
113 + $a.coordinates[5] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[5]
114 + $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0],
115 $a.coordinates[5] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[5]
116 + $a.coordinates[6] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[6]
117 + $a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
118 $a.coordinates[6] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[6]
119 + $a.coordinates[0] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[0]
120 + $a.coordinates[2] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[2],
121 $a.coordinates[0] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[0]
122 + $a.coordinates[1] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[1]
123 + $a.coordinates[3] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[3]);
124 }
125
126 multi sub infix:<cross>(Vector $a, Vector $b)
127 {
128 $a × $b;
129 }
130
131 multi sub circumfix:<⎡ ⎤>(Vector $a)
132 {
133 $a.Length;
134 }
135
136 subset UnitVector of Vector where { (1 - 1e-10) < $^v.Length < (1 + 1e-10) };
137
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
138
Something went wrong with that request. Please try again.