Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 144 lines (119 sloc) 4.385 kb
2461443 LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa proj...
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 proj...
authored
45 method Length()
46 {
bdccdbb LastOfTheCarelessMen Use "is also" to rearrange definitions in a more natural order.
authored
47 sqrt(self ⋅ self);
2461443 LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa proj...
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
85bd658 LastOfTheCarelessMen Muck around unsuccessfully with prefix +.
authored
64 # multi sub prefix:<+>(Vector $a)
65 # {
66 # $a;
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 proj...
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 infix:<->(Vector $a, Vector $b where { $a.Dim == $b.Dim })
2461443 LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa proj...
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 prefix:<->(Vector $a)
2461443 LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa proj...
authored
80 {
81 Vector.new(0 <<-<< $a.coordinates);
82 }
83
3a8edae LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
84 multi sub infix:<*>(Vector $a, $b)
2461443 LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa proj...
authored
85 {
86 Vector.new($a.coordinates >>*>> $b);
87 }
88
3a8edae LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
89 multi sub infix:<*>($a, Vector $b)
2461443 LastOfTheCarelessMen Borrow Configure.pm, Configure, and Makefile.in from Masak's grampa proj...
authored
90 {
91 Vector.new($a <<*<< $b.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 proj...
authored
95 {
96 Vector.new($a.coordinates >>/>> $b);
97 }
98
99 multi sub infix:<×>(Vector $a where { $a.Dim == 3 }, Vector $b where { $b.Dim == 3 })
100 {
101 Vector.new($a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
102 $a.coordinates[2] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[2],
103 $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0]);
104 }
105
106 multi sub infix:<×>(Vector $a where { $a.Dim == 7 }, Vector $b where { $b.Dim == 7 })
107 {
108 Vector.new($a.coordinates[1] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[1]
109 + $a.coordinates[2] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[2]
110 + $a.coordinates[4] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[4],
111 $a.coordinates[2] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[2]
112 + $a.coordinates[3] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[3]
113 + $a.coordinates[5] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[5],
114 $a.coordinates[3] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[3]
115 + $a.coordinates[4] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[4]
116 + $a.coordinates[6] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[6],
117 $a.coordinates[4] * $b.coordinates[6] - $a.coordinates[6] * $b.coordinates[4]
118 + $a.coordinates[5] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[5]
119 + $a.coordinates[0] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[0],
120 $a.coordinates[5] * $b.coordinates[0] - $a.coordinates[0] * $b.coordinates[5]
121 + $a.coordinates[6] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[6]
122 + $a.coordinates[1] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[1],
123 $a.coordinates[6] * $b.coordinates[1] - $a.coordinates[1] * $b.coordinates[6]
124 + $a.coordinates[0] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[0]
125 + $a.coordinates[2] * $b.coordinates[3] - $a.coordinates[3] * $b.coordinates[2],
126 $a.coordinates[0] * $b.coordinates[2] - $a.coordinates[2] * $b.coordinates[0]
127 + $a.coordinates[1] * $b.coordinates[5] - $a.coordinates[5] * $b.coordinates[1]
128 + $a.coordinates[3] * $b.coordinates[4] - $a.coordinates[4] * $b.coordinates[3]);
129 }
130
131 multi sub infix:<cross>(Vector $a, Vector $b)
132 {
133 $a × $b;
134 }
135
136 multi sub circumfix:<⎡ ⎤>(Vector $a)
137 {
138 $a.Length;
139 }
140
141 subset UnitVector of Vector where { (1 - 1e-10) < $^v.Length < (1 + 1e-10) };
142
3a8edae LastOfTheCarelessMen Switch V+, V-, V*, and V/ to +, -, *, and /.
authored
143
Something went wrong with that request. Please try again.