-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclifford.go
386 lines (364 loc) · 14.7 KB
/
clifford.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Clifford.go, part of gochem.
*
*
* Copyright 2012 Janne Pesonen <janne.pesonen{at}helsinkiDOTfi>
* and Raul Mera <rmera{at}chemDOThelsinkiDOTfi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
*
*
*/
package chem
import (
"math"
"runtime"
v3 "github.com/rmera/gochem/v3"
)
//import "fmt" //debug
type paravector struct {
Real float64
Imag float64
Vreal *v3.Matrix
Vimag *v3.Matrix
}
//creates a new paravector
func makeParavector() *paravector {
R := new(paravector)
R.Real = 0 //I shouldnt need this
R.Imag = 0
R.Vreal = v3.Zeros(1)
R.Vimag = v3.Zeros(1)
return R
}
//Takes a vector and creates a paravector. Uses copy so the vector is not affected
//By future changes to the paravector.
func paravectorFromVector(A, B *v3.Matrix) *paravector {
R := new(paravector)
R.Real = 0 //I shouldnt need this
R.Imag = 0
R.Vreal = A
R.Vimag = B
return R
}
//Puts a copy fo the paravector P in the receiver
func (R *paravector) Copy(P *paravector) {
R.Real = P.Real
R.Imag = P.Imag
R.Vreal.Copy(P.Vreal)
R.Vimag.Copy(P.Vimag)
}
//Puts the reverse of paravector P in the received
func (R *paravector) reverse(P *paravector) {
if P != R {
R.Copy(P)
}
R.Vimag.Scale(-1, R.Vimag)
}
//Puts the normalized version of P in the receiver. If R and P are the same,
func (R *paravector) unit(P *paravector) {
norm := 0.0
norm += math.Pow(P.Real, 2) + math.Pow(P.Imag, 2)
for i := 0; i < 3; i++ {
norm += math.Pow(P.Vreal.At(0, i), 2) + math.Pow(P.Vimag.At(0, i), 2)
}
//fmt.Println("norm", norm)
R.Real = P.Real / math.Sqrt(norm)
R.Imag = P.Imag / math.Sqrt(norm)
for i := 0; i < 3; i++ {
R.Vreal.Set(0, i, P.Vreal.At(0, i)/math.Sqrt(norm))
R.Vimag.Set(0, i, P.Vimag.At(0, i)/math.Sqrt(norm))
}
//fmt.Println("normalized", R)
}
//Clifford product of 2 paravectors, the imaginary parts are simply set to zero, since this is the case
//when rotating 3D real vectors. The proper Cliffor product is in fullCliProduct
func (R *paravector) cliProduct(A, B *paravector) {
R.Real = A.Real*B.Real - A.Imag*B.Imag
for i := 0; i < 3; i++ {
R.Real += (A.Vreal.At(0, i)*B.Vreal.At(0, i) - A.Vimag.At(0, i)*B.Vimag.At(0, i))
}
R.Imag = A.Real*B.Imag + A.Imag*B.Real
for i := 0; i < 3; i++ {
R.Imag += (A.Vreal.At(0, i)*B.Vimag.At(0, i) + A.Vimag.At(0, i)*B.Vreal.At(0, i))
}
//Now the vector part
//First real
R.Vreal.Set(0, 0, A.Real*B.Vreal.At(0, 0)+B.Real*A.Vreal.At(0, 0)-A.Imag*B.Vimag.At(0, 0)-B.Imag*A.Vimag.At(0, 0)+
A.Vimag.At(0, 2)*B.Vreal.At(0, 1)-A.Vimag.At(0, 1)*B.Vreal.At(0, 2)+A.Vreal.At(0, 2)*B.Vimag.At(0, 1)-
A.Vreal.At(0, 1)*B.Vimag.At(0, 2))
//Second real
R.Vreal.Set(0, 1, A.Real*B.Vreal.At(0, 1)+B.Real*A.Vreal.At(0, 1)-A.Imag*B.Vimag.At(0, 1)-B.Imag*A.Vimag.At(0, 1)+
A.Vimag.At(0, 0)*B.Vreal.At(0, 2)-A.Vimag.At(0, 2)*B.Vreal.At(0, 0)+A.Vreal.At(0, 0)*B.Vimag.At(0, 2)-
A.Vreal.At(0, 2)*B.Vimag.At(0, 0))
//Third real
R.Vreal.Set(0, 2, A.Real*B.Vreal.At(0, 2)+B.Real*A.Vreal.At(0, 2)-A.Imag*B.Vimag.At(0, 2)-B.Imag*A.Vimag.At(0, 2)+
A.Vimag.At(0, 1)*B.Vreal.At(0, 0)-A.Vimag.At(0, 0)*B.Vreal.At(0, 1)+A.Vreal.At(0, 1)*B.Vimag.At(0, 0)-
A.Vreal.At(0, 0)*B.Vimag.At(0, 1))
/*
//First imag
R.Vimag.Set(0,0,A.Real*B.Vimag.At(0,0) + B.Real*A.Vimag.At(0,0) + A.Imag*B.Vreal.At(0,0) - B.Imag*A.Vreal.At(0,0) +
A.Vreal.At(0,1)*B.Vreal.At(0,2) - A.Vreal.At(0,2)*B.Vreal.At(0,1) + A.Vimag.At(0,2)*B.Vimag.At(0,1) -
A.Vimag.At(0,1)*B.Vimag.At(0,2))
//Second imag
R.Vimag.Set(0,1,A.Real*B.Vimag.At(0,1) + B.Real*A.Vimag.At(0,1) + A.Imag*B.Vreal.At(0,1) - B.Imag*A.Vreal.At(0,1) +
A.Vreal.At(0,2)*B.Vreal.At(0,0) - A.Vreal.At(0,0)*B.Vreal.At(0,2) + A.Vimag.At(0,0)*B.Vimag.At(0,2) -
A.Vimag.At(0,2)*B.Vimag.At(0,0))
//Third imag
R.Vimag.Set(0,2,A.Real*B.Vimag.At(0,2) + B.Real*A.Vimag.At(0,2) + A.Imag*B.Vreal.At(0,2) - B.Imag*A.Vreal.At(0,2) +
A.Vreal.At(0,0)*B.Vreal.At(0,1) - A.Vreal.At(0,1)*B.Vreal.At(0,0) + A.Vimag.At(0,1)*B.Vimag.At(0,0) -
A.Vimag.At(0,0)*B.Vimag.At(0,1))
*/
//fmt.Println("R slido del horno", R)
// A.Real, B.Vimag.At(0,0), "g2", B.Real,A.Vimag.At(0,0),"g3", A.Imag, B.Vreal.At(0,0),"g4" ,B.Imag,A.Vreal.At(0,0),
//"g5", A.Vreal.At(0,2), B.Vreal.At(0,1), -1*A.Vreal.At(0,1)*B.Vreal.At(0,2), A.Vimag.At(0,2)*B.Vimag.At(0,1), -1*
//A.Vimag.At(0,1)*B.Vimag.At(0,2))
// return R
}
//cliRotation uses Clifford algebra to rotate a paravector Aby angle radians around axis. Returns the rotated
//paravector. axis must be normalized.
func (R *paravector) cliRotation(A, axis, tmp, tmp2 *paravector, angle float64) {
// R := makeParavector()
R.Real = math.Cos(angle / 2.0)
for i := 0; i < 3; i++ {
R.Vimag.Set(0, i, math.Sin(angle/2.0)*axis.Vreal.At(0, i))
}
R.reverse(R)
// tmp:=makeParavector()
// tmp2:=makeParavector()
tmp.cliProduct(R, A)
tmp2.cliProduct(tmp, R)
R.Copy(tmp2)
}
//RotateSer takes the matrix Target and uses Clifford algebra to rotate each of its rows
//by angle radians around axis. Axis must be a 3D row vector. Target must be an N,3 matrix.
//The Ser in the name is from "serial". ToRot will be overwritten and returned
func RotateSer(Target, ToRot, axis *v3.Matrix, angle float64) *v3.Matrix {
cake := v3.Zeros(10) //Better ask for one chunk of memory than allocate 10 different times.
R := cake.VecView(0)
Rrev := cake.VecView(1)
tmp := cake.VecView(2)
Rotated := cake.VecView(3)
itmp1 := cake.VecView(4)
itmp2 := cake.VecView(5)
itmp3 := cake.VecView(6)
itmp4 := cake.VecView(7)
itmp5 := cake.VecView(8)
itmp6 := cake.VecView(9)
RotateSerP(Target, ToRot, axis, tmp, R, Rrev, Rotated, itmp1, itmp2, itmp3, itmp4, itmp5, itmp6, angle)
return ToRot
}
//RotateSerP takes the matrix Target and uses Clifford algebra to rotate each of its rows
//by angle radians around axis. Axis must be a 3D row vector. Target must be an N,3 matrix.
//The Ser in the name is from "serial". ToRot will be overwritten and returned. RotateSerP only allocates some floats but not
//any v3.Matrix. Instead, it takes the needed intermediates as arguments, hence the "P" for "performance" If performance is not an issue,
//use RotateSer instead, it will perform the allocations for you and call this function. Notice that if you use this function directly
//you may have to zero at least some of the intermediates before reusing them.
func RotateSerP(Target, ToRot, axis, tmpv, Rv, Rvrev, Rotatedv, itmp1, itmp2, itmp3, itmp4, itmp5, itmp6 *v3.Matrix, angle float64) {
tarr, _ := Target.Dims()
torotr := ToRot.NVecs()
if tarr != torotr || Target.Dense == ToRot.Dense {
panic(ErrCliffordRotation)
}
//Make the paravectors from the passed vectors:
tmp := paravectorFromVector(tmpv, itmp3)
R := paravectorFromVector(Rv, itmp4)
Rrev := paravectorFromVector(Rvrev, itmp5)
Rotated := paravectorFromVector(Rotatedv, itmp6)
//That is with the building of temporary paravectors.
paxis := paravectorFromVector(axis, itmp1)
paxis.unit(paxis)
R.Real = math.Cos(angle / 2.0)
for i := 0; i < 3; i++ {
R.Vimag.Set(0, i, math.Sin(angle/2.0)*paxis.Vreal.At(0, i))
}
Rrev.reverse(R)
for i := 0; i < tarr; i++ {
rowvec := Target.VecView(i)
tmp.cliProduct(Rrev, paravectorFromVector(rowvec, itmp2))
Rotated.cliProduct(tmp, R)
ToRot.SetMatrix(i, 0, Rotated.Vreal)
}
}
func getChunk(cake *v3.Matrix, i, j, r, c int) *v3.Matrix {
ret := cake.View(i, j, r, c)
return ret
}
//Rotate takes the matrix Target and uses Clifford algebra to _concurrently_ rotate each
//of its rows by angle radians around axis. Axis must be a 3D row vector.
//Target must be an N,3 matrix. The result is put in Res, which is also returned.
//This is a low-level function. Most commonly, you'll want to use RotateAbout instead.
func Rotate(Target, Res, axis *v3.Matrix, angle float64) *v3.Matrix {
// runtime.GOMAXPROCS(3)
gorut := runtime.GOMAXPROCS(-1)
rows := Target.NVecs()
// println("rows and goruts", rows,gorut) /////
if gorut > rows {
gorut = rows
}
cake := v3.Zeros(5 + gorut*4)
Rv := cake.VecView(0)
Rvrev := cake.VecView(1)
itmp1 := cake.VecView(2)
itmp2 := cake.VecView(3)
itmp3 := cake.VecView(4)
tmp1 := getChunk(cake, 5, 0, gorut, 3)
tmp2 := getChunk(cake, 5+gorut, 0, gorut, 3)
tmp3 := getChunk(cake, 5+2*gorut, 0, gorut, 3)
tmp4 := getChunk(cake, 5+3*gorut, 0, gorut, 3)
RotateP(Target, Res, axis, Rv, Rvrev, tmp1, tmp2, tmp3, tmp4, itmp1, itmp2, itmp3, angle, gorut)
return Res
}
//RotateP takes the matrix Target and uses Clifford algebra to _concurrently_ rotate each
//of its rows by angle radians around axis. Axis must be a 3D row vector.
//Target must be an N,3 matrix.
func RotateP(Target, Res, axis, Rv, Rvrev, tmp1, tmp2, tmp3, tmp4, itmp1, itmp2, itmp3 *v3.Matrix, angle float64, gorut int) {
//fmt.Println("Enter RotateP")//////////////
// gorut := runtime.GOMAXPROCS(-1) //Do not change anything, only query
rows := Target.NVecs()
rrows := Res.NVecs()
if rrows != rows || Target.Dense == Res.Dense {
panic(ErrCliffordRotation)
}
paxis := paravectorFromVector(axis, itmp1) //alloc
paxis.unit(paxis)
R := paravectorFromVector(Rv, itmp2) // makeParavector() //build the rotor (R) //alloc
R.Real = math.Cos(angle / 2.0)
for i := 0; i < 3; i++ {
R.Vimag.Set(0, i, math.Sin(angle/2.0)*paxis.Vreal.At(0, i))
}
Rrev := paravectorFromVector(Rvrev, itmp3) // makeParavector() //R-dagger //alloc
Rrev.reverse(R)
// if gorut > rows {
// gorut = rows
// }
ended := make(chan bool, gorut)
//If the gorutines are more than the rows, we will have problems afterwards, as we try to split the rows
//among the available gorutines, some gorutines are going to get invalid matrix positions.
fragmentlen := int(math.Floor(float64(rows) / float64(gorut))) //len of the fragment of target that each gorutine will handle
// println("fragmentlen", fragmentlen, rows, gorut, Target.String()) //////////
// println("gorutines!!!", gorut) ///////
for i := 0; i < gorut; i++ {
//These are the limits of the fragment of Target in which the gorutine will operate
ini := i * fragmentlen
end := i*fragmentlen + (fragmentlen - 1)
if i == gorut-1 {
end = rows - 1 //The last fragment may be smaller than fragmentlen
}
go func(ini, end, i int) {
t1 := tmp1.VecView(i)
//r,c:=tmp2.Dims()///
//this "print" causes a data race but it shouldn't matter, as it's only for debugging purposes.
//removing the print removes the race warning, so there isn't apparently any data race going on.
//fmt.Println("WTF",r,c,i,tmp2,"\n") /////////
//fmt.Println("") ////////////
t2 := tmp2.VecView(i)
t4 := tmp4.VecView(i)
pv := paravectorFromVector(t2, t4)
t3 := tmp3.VecView(i)
for j := ini; j <= end; j++ {
//Here we simply do R^dagger A R, and assign to the corresponding row.
Rotated := paravectorFromVector(Res.VecView(j), t3)
targetparavec := paravectorFromVector(Target.VecView(j), t1)
pv.cliProduct(Rrev, targetparavec)
Rotated.cliProduct(pv, R)
}
ended <- true
return
}(ini, end, i)
}
//Takes care of the concurrency
for i := 0; i < gorut; i++ {
<-ended
}
//fmt.Println("YEY!, funcion ql!!!!")////////////////////////////
return
}
/*
rows, _ := Target.Dims()
paxis := paravectorFromVector(axis,v3.Zeros(1))
paxis.unit(paxis)
R := makeParavector() //build the rotor (R)
R.Real = math.Cos(angle / 2.0)
for i := 0; i < 3; i++ {
R.Vimag.Set(0, i, math.Sin(angle/2.0)*paxis.Vreal.At(0, i))
}
Rrev := R.reverse() // R-dagger
Res := v3.Zeros(rows)
ended := make(chan bool, rows)
for i := 0; i < rows; i++ {
go func(i int) {
//Here we simply do R^dagger A R, and assign to the corresponding row.
targetrow := Target.VecView(i)
tmp := cliProduct(Rrev, paravectorFromVector(targetrow,v3.Zeros(1)))
Rotated := cliProduct(tmp, R)
//a,b:=Res.Dims() //debug
//c,d:=Rotated.Vreal.Dims()
//fmt.Println("rows",a,c,"cols",b,d,"i","rowss",)
Res.SetMatrix(i, 0, Rotated.Vreal)
ended <- true
return
}(i)
}
//Takes care of the concurrency
for i := 0; i < rows; i++ {
<-ended
}
return Res
}
*/
//Clifford product of 2 paravectors.
func fullCliProduct(A, B *paravector) *paravector {
R := makeParavector()
R.Real = A.Real*B.Real - A.Imag*B.Imag
for i := 0; i < 3; i++ {
R.Real += (A.Vreal.At(0, i)*B.Vreal.At(0, i) - A.Vimag.At(0, i)*B.Vimag.At(0, i))
}
R.Imag = A.Real*B.Imag + A.Imag*B.Real
for i := 0; i < 3; i++ {
R.Imag += (A.Vreal.At(0, i)*B.Vimag.At(0, i) + A.Vimag.At(0, i)*B.Vreal.At(0, i))
}
//Now the vector part
//First real
R.Vreal.Set(0, 0, A.Real*B.Vreal.At(0, 0)+B.Real*A.Vreal.At(0, 0)-A.Imag*B.Vimag.At(0, 0)-B.Imag*A.Vimag.At(0, 0)+
A.Vimag.At(0, 2)*B.Vreal.At(0, 1)-A.Vimag.At(0, 1)*B.Vreal.At(0, 2)+A.Vreal.At(0, 2)*B.Vimag.At(0, 1)-
A.Vreal.At(0, 1)*B.Vimag.At(0, 2))
//Second real
R.Vreal.Set(0, 1, A.Real*B.Vreal.At(0, 1)+B.Real*A.Vreal.At(0, 1)-A.Imag*B.Vimag.At(0, 1)-B.Imag*A.Vimag.At(0, 1)+
A.Vimag.At(0, 0)*B.Vreal.At(0, 2)-A.Vimag.At(0, 2)*B.Vreal.At(0, 0)+A.Vreal.At(0, 0)*B.Vimag.At(0, 2)-
A.Vreal.At(0, 2)*B.Vimag.At(0, 0))
//Third real
R.Vreal.Set(0, 2, A.Real*B.Vreal.At(0, 2)+B.Real*A.Vreal.At(0, 2)-A.Imag*B.Vimag.At(0, 2)-B.Imag*A.Vimag.At(0, 2)+
A.Vimag.At(0, 1)*B.Vreal.At(0, 0)-A.Vimag.At(0, 0)*B.Vreal.At(0, 1)+A.Vreal.At(0, 1)*B.Vimag.At(0, 0)-
A.Vreal.At(0, 0)*B.Vimag.At(0, 1))
//First imag
R.Vimag.Set(0, 0, A.Real*B.Vimag.At(0, 0)+B.Real*A.Vimag.At(0, 0)+A.Imag*B.Vreal.At(0, 0)-B.Imag*A.Vreal.At(0, 0)+
A.Vreal.At(0, 1)*B.Vreal.At(0, 2)-A.Vreal.At(0, 2)*B.Vreal.At(0, 1)+A.Vimag.At(0, 2)*B.Vimag.At(0, 1)-
A.Vimag.At(0, 1)*B.Vimag.At(0, 2))
//Second imag
R.Vimag.Set(0, 1, A.Real*B.Vimag.At(0, 1)+B.Real*A.Vimag.At(0, 1)+A.Imag*B.Vreal.At(0, 1)-B.Imag*A.Vreal.At(0, 1)+
A.Vreal.At(0, 2)*B.Vreal.At(0, 0)-A.Vreal.At(0, 0)*B.Vreal.At(0, 2)+A.Vimag.At(0, 0)*B.Vimag.At(0, 2)-
A.Vimag.At(0, 2)*B.Vimag.At(0, 0))
//Third imag
R.Vimag.Set(0, 2, A.Real*B.Vimag.At(0, 2)+B.Real*A.Vimag.At(0, 2)+A.Imag*B.Vreal.At(0, 2)-B.Imag*A.Vreal.At(0, 2)+
A.Vreal.At(0, 0)*B.Vreal.At(0, 1)-A.Vreal.At(0, 1)*B.Vreal.At(0, 0)+A.Vimag.At(0, 1)*B.Vimag.At(0, 0)-
A.Vimag.At(0, 0)*B.Vimag.At(0, 1))
//fmt.Println("R slido del horno", R)
// A.Real, B.Vimag.At(0,0), "g2", B.Real,A.Vimag.At(0,0),"g3", A.Imag, B.Vreal.At(0,0),"g4" ,B.Imag,A.Vreal.At(0,0),
//"g5", A.Vreal.At(0,2), B.Vreal.At(0,1), -1*A.Vreal.At(0,1)*B.Vreal.At(0,2), A.Vimag.At(0,2)*B.Vimag.At(0,1), -1*
//A.Vimag.At(0,1)*B.Vimag.At(0,2))
return R
}