Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 157 lines (131 sloc) 4.628 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
5dfc043 Add Vector.Num trap so we can get better error messages.
SF authored
27 multi method Num()
28 {
29 die "Cannot call Num on Vector!";
30 }
31
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
32 method Dim()
33 {
34 @.coordinates.elems;
35 }
bdccdbb @LastOfTheCarelessMen Use "is also" to rearrange definitions in a more natural order.
authored
36 }
37
38 multi sub infix:<⋅>(Vector $a, Vector $b where { $a.Dim == $b.Dim }) # is tighter(&infix:<+>) (NYI)
39 {
40 [+]($a.coordinates »*« $b.coordinates);
41 }
42
43 multi sub infix:<dot>(Vector $a, Vector $b)
44 {
45 $a$b;
46 }
47
48 class Vector is also
49 {
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
50 method Length()
51 {
bdccdbb @LastOfTheCarelessMen Use "is also" to rearrange definitions in a more natural order.
authored
52 sqrt(selfself);
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
53 }
54
f09c4bf Add Vector.abs and use it to get rid of the is_approx_vector test (si…
SF authored
55 multi method abs()
56 {
57 self.Length;
58 }
59
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
60 method Unitize()
61 {
62 my $length = self.Length;
63 if $length > 1e-10
64 {
65 return Vector.new(@.coordinates >>/>> $length);
66 }
67 else
68 {
69 return Vector.new(@.coordinates);
70 }
71 }
72 }
73
85bd658 @LastOfTheCarelessMen Muck around unsuccessfully with prefix +.
authored
74 # multi sub prefix:<+>(Vector $a)
75 # {
76 # $a;
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 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
85 {
86 Vector.new($a.coordinates »-« $b.coordinates);
87 }
88
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
89 multi sub prefix:<->(Vector $a)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
90 {
91 Vector.new(0 <<-<< $a.coordinates);
92 }
93
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
94 multi sub infix:<*>(Vector $a, $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
95 {
96 Vector.new($a.coordinates >>*>> $b);
97 }
98
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
99 multi sub infix:<*>($a, Vector $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
100 {
101 Vector.new($a <<*<< $b.coordinates);
102 }
103
3a8edae @LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
104 multi sub infix:</>(Vector $a, $b)
2461443 @LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa p…
authored
105 {
106 Vector.new($a.coordinates >>/>> $b);
107 }
108
109 multi sub infix:<×>(Vector $a where { $a.Dim == 3 }, Vector $b where { $b.Dim == 3 })
110 {
111 Vector.new($a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
112 $a.coordinates[2] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[2],
113 $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0]);
114 }
115
116 multi sub infix:<×>(Vector $a where { $a.Dim == 7 }, Vector $b where { $b.Dim == 7 })
117 {
118 Vector.new($a.coordinates[1] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[1]
119 + $a.coordinates[2] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[2]
120 + $a.coordinates[4] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[4],
121 $a.coordinates[2] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[2]
122 + $a.coordinates[3] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[3]
123 + $a.coordinates[5] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[5],
124 $a.coordinates[3] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[3]
125 + $a.coordinates[4] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[4]
126 + $a.coordinates[6] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[6],
127 $a.coordinates[4] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[4]
128 + $a.coordinates[5] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[5]
129 + $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0],
130 $a.coordinates[5] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[5]
131 + $a.coordinates[6] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[6]
132 + $a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
133 $a.coordinates[6] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[6]
134 + $a.coordinates[0] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[0]
135 + $a.coordinates[2] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[2],
136 $a.coordinates[0] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[0]
137 + $a.coordinates[1] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[1]
138 + $a.coordinates[3] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[3]);
139 }
140
141 multi sub infix:<cross>(Vector $a, Vector $b)
142 {
143 $a × $b;
144 }
145
146 multi sub circumfix:<⎡ ⎤>(Vector $a)
147 {
148 $a.Length;
149 }
150
151 subset UnitVector of Vector where { (1 - 1e-10) < $^v.Length < (1 + 1e-10) };
152
b364122 Move is_approx_vector to Vector.pm.
SF authored
153 sub is_approx_vector(Vector $a, Vector $b, $desc)
154 {
155 ok(($a - $b).Length < 0.00001, $desc);
156 }
Something went wrong with that request. Please try again.