Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 149 lines (123 sloc) 4.45 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
f09c4bf Add Vector.abs and use it to get rid of the is_approx_vector test (si…
SF authored
50 multi method abs()
51 {
52 self.Length;
53 }
54
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
55 method Unitize()
56 {
57 my $length = self.Length;
58 if $length > 1e-10
59 {
60 return Vector.new(@.coordinates >>/>> $length);
61 }
62 else
63 {
64 return Vector.new(@.coordinates);
65 }
66 }
67 }
68
85bd658 @LastOfTheCarelessMen Muck around unsuccessfully with prefix +.
authored
69 # multi sub prefix:<+>(Vector $a)
70 # {
71 # $a;
72 # }
73
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
74 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
75 {
76 Vector.new($a.coordinates »+« $b.coordinates);
77 }
78
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
79 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
80 {
81 Vector.new($a.coordinates »-« $b.coordinates);
82 }
83
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
84 multi sub prefix:<->(Vector $a)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
85 {
86 Vector.new(0 <<-<< $a.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
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
94 multi sub infix:<*>($a, Vector $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
95 {
96 Vector.new($a <<*<< $b.coordinates);
97 }
98
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
99 multi sub infix:</>(Vector $a, $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
100 {
101 Vector.new($a.coordinates >>/>> $b);
102 }
103
104 multi sub infix:<×>(Vector $a where { $a.Dim == 3 }, Vector $b where { $b.Dim == 3 })
105 {
106 Vector.new($a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
107 $a.coordinates[2] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[2],
108 $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0]);
109 }
110
111 multi sub infix:<×>(Vector $a where { $a.Dim == 7 }, Vector $b where { $b.Dim == 7 })
112 {
113 Vector.new($a.coordinates[1] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[1]
114 + $a.coordinates[2] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[2]
115 + $a.coordinates[4] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[4],
116 $a.coordinates[2] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[2]
117 + $a.coordinates[3] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[3]
118 + $a.coordinates[5] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[5],
119 $a.coordinates[3] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[3]
120 + $a.coordinates[4] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[4]
121 + $a.coordinates[6] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[6],
122 $a.coordinates[4] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[4]
123 + $a.coordinates[5] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[5]
124 + $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0],
125 $a.coordinates[5] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[5]
126 + $a.coordinates[6] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[6]
127 + $a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
128 $a.coordinates[6] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[6]
129 + $a.coordinates[0] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[0]
130 + $a.coordinates[2] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[2],
131 $a.coordinates[0] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[0]
132 + $a.coordinates[1] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[1]
133 + $a.coordinates[3] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[3]);
134 }
135
136 multi sub infix:<cross>(Vector $a, Vector $b)
137 {
138 $a × $b;
139 }
140
141 multi sub circumfix:<⎡ ⎤>(Vector $a)
142 {
143 $a.Length;
144 }
145
146 subset UnitVector of Vector where { (1 - 1e-10) < $^v.Length < (1 + 1e-10) };
147
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
148
Something went wrong with that request. Please try again.