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 package net.sourceforge.kamiwaai.geometricalgebra;
44
45 public class MultiVector {
46 private static ComplexNumber czero = new ComplexNumber(0.0, 0.0);
47 private static ComplexNumber[] cqz = { czero, czero, czero, czero };
48 private static ComplexQuat cqzero = new ComplexQuat(cqz);
49
50 private ComplexQuat[] MV = new ComplexQuat[4];
51 public static MultiVector n = MultiVectors.n();
52 public static MultiVector nbar = MultiVectors.nbar();
53 private static MultiVector N = MultiVectors.N();
54 public static MultiVector Zero = MultiVectors.Zero();
55 public static MultiVector One = MultiVectors.One();
56 public static MultiVector I = MultiVectors.I();
57 public static MultiVector e1 = MultiVectors.e1();
58 public static MultiVector e2 = MultiVectors.e2();
59 public static MultiVector e3 = MultiVectors.e3();
60
61 public MultiVector() {
62
63
64 MV[0] = cqzero;
65 MV[1] = cqzero;
66 MV[2] = cqzero;
67 MV[3] = cqzero;
68 }
69
70 public MultiVector(ComplexQuat[] mv) {
71 MV[0] = mv[0];
72 MV[1] = mv[1];
73 MV[2] = mv[2];
74 MV[3] = mv[3];
75 }
76
77 private ComplexQuat getScPart() {
78 return MV[0];
79 }
80
81 private ComplexQuat getnPart() {
82 return MV[1];
83 }
84
85 private ComplexQuat getnbarPart() {
86 return MV[2];
87 }
88
89 public ComplexQuat getnhnbPart() {
90 return MV[3];
91 }
92
93 public MultiVector get3DMVector() {
94 MultiVector MM = new MultiVector(MV);
95 MultiVector M3 = MM.getGrade(1);
96 return M3.OutProd(N).mult(N);
97 }
98
99 public MultiVector getGrade0() {
100 double grade0 = MV[0].getScPart().RealPart();
101 ComplexNumber grade0ScSc = new ComplexNumber(grade0, 0.0);
102 ComplexNumber[] grade0Sc = { grade0ScSc, czero, czero, czero };
103 ComplexQuat cqgrade0Sc = new ComplexQuat(grade0Sc);
104 ComplexQuat[] Grade0 = { cqgrade0Sc, cqzero, cqzero, cqzero };
105 return new MultiVector(Grade0);
106
107 }
108
109 public MultiVector getGrade1() {
110 ComplexNumber grade1n0 =
111 new ComplexNumber(MV[1].getScPart().RealPart(), 0.0);
112 ComplexNumber[] grade1n = { grade1n0, czero, czero, czero };
113
114 ComplexNumber grade1nb0 =
115 new ComplexNumber(MV[2].getScPart().RealPart(), 0.0);
116 ComplexNumber[] grade1nb = { grade1nb0, czero, czero, czero };
117
118 ComplexNumber grade1nnb1 =
119 new ComplexNumber(0.0, MV[3].getBvPart()[0].ImaginaryPart());
120 ComplexNumber grade1nnb2 =
121 new ComplexNumber(0.0, MV[3].getBvPart()[1].ImaginaryPart());
122 ComplexNumber grade1nnb3 =
123 new ComplexNumber(0.0, MV[3].getBvPart()[2].ImaginaryPart());
124 ComplexNumber[] grade1nnb =
125 { czero, grade1nnb1, grade1nnb2, grade1nnb3 };
126
127 ComplexQuat cqGrade1n = new ComplexQuat(grade1n);
128 ComplexQuat cqGrade1nb = new ComplexQuat(grade1nb);
129 ComplexQuat cqGrade1nnb = new ComplexQuat(grade1nnb);
130 ComplexQuat[] Grade1 = { cqzero, cqGrade1n, cqGrade1nb, cqGrade1nnb };
131
132 return new MultiVector(Grade1);
133 }
134
135 public MultiVector getGrade2() {
136 ComplexNumber grade2Sc1 =
137 new ComplexNumber(MV[0].getBvPart()[0].RealPart(), 0.0);
138 ComplexNumber grade2Sc2 =
139 new ComplexNumber(MV[0].getBvPart()[1].RealPart(), 0.0);
140 ComplexNumber grade2Sc3 =
141 new ComplexNumber(MV[0].getBvPart()[2].RealPart(), 0.0);
142 ComplexNumber[] grade2Sc = { czero, grade2Sc1, grade2Sc2, grade2Sc3 };
143
144 ComplexNumber grade2n1 =
145 new ComplexNumber(0.0, MV[1].getBvPart()[0].ImaginaryPart());
146 ComplexNumber grade2n2 =
147 new ComplexNumber(0.0, MV[1].getBvPart()[1].ImaginaryPart());
148 ComplexNumber grade2n3 =
149 new ComplexNumber(0.0, MV[1].getBvPart()[2].ImaginaryPart());
150 ComplexNumber[] grade2n = { czero, grade2n1, grade2n2, grade2n3 };
151
152 ComplexNumber grade2nb1 =
153 new ComplexNumber(0.0, MV[2].getBvPart()[0].ImaginaryPart());
154 ComplexNumber grade2nb2 =
155 new ComplexNumber(0.0, MV[2].getBvPart()[1].ImaginaryPart());
156 ComplexNumber grade2nb3 =
157 new ComplexNumber(0.0, MV[2].getBvPart()[2].ImaginaryPart());
158 ComplexNumber[] grade2nb = { czero, grade2nb1, grade2nb2, grade2nb3 };
159
160 ComplexNumber grade2nnb0 =
161 new ComplexNumber(MV[3].getScPart().RealPart(), 0.0);
162 ComplexNumber[] grade2nnb = { grade2nnb0, czero, czero, czero };
163
164 ComplexQuat cqGrade2sc = new ComplexQuat(grade2Sc);
165 ComplexQuat cqGrade2n = new ComplexQuat(grade2n);
166 ComplexQuat cqGrade2nb = new ComplexQuat(grade2nb);
167 ComplexQuat cqGrade2nnb = new ComplexQuat(grade2nnb);
168 ComplexQuat[] Grade2 =
169 { cqGrade2sc, cqGrade2n, cqGrade2nb, cqGrade2nnb };
170
171 return new MultiVector(Grade2);
172 }
173
174 public MultiVector getGrade3() {
175 ComplexNumber grade3Sc1 =
176 new ComplexNumber(0.0, MV[0].getBvPart()[0].ImaginaryPart());
177 ComplexNumber grade3Sc2 =
178 new ComplexNumber(0.0, MV[0].getBvPart()[1].ImaginaryPart());
179 ComplexNumber grade3Sc3 =
180 new ComplexNumber(0.0, MV[0].getBvPart()[2].ImaginaryPart());
181 ComplexNumber[] grade3Sc = { czero, grade3Sc1, grade3Sc2, grade3Sc3 };
182
183 ComplexNumber grade3n1 =
184 new ComplexNumber(MV[1].getBvPart()[0].RealPart(), 0.0);
185 ComplexNumber grade3n2 =
186 new ComplexNumber(MV[1].getBvPart()[1].RealPart(), 0.0);
187 ComplexNumber grade3n3 =
188 new ComplexNumber(MV[1].getBvPart()[2].RealPart(), 0.0);
189 ComplexNumber[] grade3n = { czero, grade3n1, grade3n2, grade3n3 };
190
191 ComplexNumber grade3nb1 =
192 new ComplexNumber(MV[2].getBvPart()[0].RealPart(), 0.0);
193 ComplexNumber grade3nb2 =
194 new ComplexNumber(MV[2].getBvPart()[1].RealPart(), 0.0);
195 ComplexNumber grade3nb3 =
196 new ComplexNumber(MV[2].getBvPart()[2].RealPart(), 0.0);
197 ComplexNumber[] grade3nb = { czero, grade3nb1, grade3nb2, grade3nb3 };
198
199 ComplexNumber grade3nnb0 =
200 new ComplexNumber(0.0, MV[3].getScPart().ImaginaryPart());
201 ComplexNumber[] grade3nnb = { grade3nnb0, czero, czero, czero };
202
203 ComplexQuat cqGrade3Sc = new ComplexQuat(grade3Sc);
204 ComplexQuat cqGrade3n = new ComplexQuat(grade3n);
205 ComplexQuat cqGrade3nb = new ComplexQuat(grade3nb);
206 ComplexQuat cqGrade3nnb = new ComplexQuat(grade3nnb);
207 ComplexQuat[] Grade3 =
208 { cqGrade3Sc, cqGrade3n, cqGrade3nb, cqGrade3nnb };
209
210 return new MultiVector(Grade3);
211 }
212
213 public MultiVector getGrade4() {
214 ComplexNumber grade4n0 =
215 new ComplexNumber(0.0, MV[1].getScPart().ImaginaryPart());
216 ComplexNumber[] grade4n = { grade4n0, czero, czero, czero };
217
218 ComplexNumber grade4nb0 =
219 new ComplexNumber(0.0, MV[2].getScPart().ImaginaryPart());
220 ComplexNumber[] grade4nb = { grade4nb0, czero, czero, czero };
221
222 ComplexNumber grade4nnb1 =
223 new ComplexNumber(MV[3].getBvPart()[0].RealPart(), 0.0);
224 ComplexNumber grade4nnb2 =
225 new ComplexNumber(MV[3].getBvPart()[1].RealPart(), 0.0);
226 ComplexNumber grade4nnb3 =
227 new ComplexNumber(MV[3].getBvPart()[2].RealPart(), 0.0);
228 ComplexNumber[] grade4nnb =
229 { czero, grade4nnb1, grade4nnb2, grade4nnb3 };
230
231 ComplexQuat cqGrade4n = new ComplexQuat(grade4n);
232 ComplexQuat cqGrade4nb = new ComplexQuat(grade4nb);
233 ComplexQuat cqGrade4nnb = new ComplexQuat(grade4nnb);
234 ComplexQuat[] Grade4 = { cqzero, cqGrade4n, cqGrade4nb, cqGrade4nnb };
235
236 return new MultiVector(Grade4);
237 }
238
239 public MultiVector getGrade5() {
240 ComplexNumber grade5Sc0 =
241 new ComplexNumber(0.0, MV[0].getScPart().ImaginaryPart());
242 ComplexNumber[] grade5Sc = { grade5Sc0, czero, czero, czero };
243
244 ComplexQuat cqGrade5Sc = new ComplexQuat(grade5Sc);
245 ComplexQuat[] Grade5 = { cqGrade5Sc, cqzero, cqzero, cqzero };
246
247 return new MultiVector(Grade5);
248 }
249
250 public MultiVector getGrade(int g) {
251 MultiVector M = new MultiVector(MV);
252 switch (g) {
253 case 0 :
254 return M.getGrade0();
255 case 1 :
256 return M.getGrade1();
257 case 2 :
258 return M.getGrade2();
259 case 3 :
260 return M.getGrade3();
261 case 4 :
262 return M.getGrade4();
263 case 5 :
264 return M.getGrade5();
265 default :
266 throw new IllegalArgumentException("Strange grade choice");
267 }
268 }
269
270 public double magnitude() {
271
272
273
274
275
276 MultiVector mv1 = new MultiVector(MV);
277 double mag =
278 Math.sqrt(
279 (mv1.mult(mv1.reverse())).getScPart().getScPart().RealPart());
280 return mag;
281 }
282
283 public MultiVector normalize() {
284
285 MultiVector mv1 = new MultiVector(MV);
286 MultiVector mv1norm = mv1.multSc(1.0 / mv1.magnitude());
287 return mv1norm;
288 }
289
290 public MultiVector reverse() {
291 MultiVector mv1 = new MultiVector(MV);
292 MultiVector rev;
293 rev =
294 (mv1.getGrade0())
295 .add(mv1.getGrade1())
296 .sub(mv1.getGrade2())
297 .sub(mv1.getGrade3())
298 .add(mv1.getGrade4())
299 .add(mv1.getGrade5());
300 return rev;
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 public MultiVector add(MultiVector mv2) {
327 ComplexQuat[] MVsum = new ComplexQuat[4];
328
329 MVsum[0] = MV[0].add(mv2.getScPart());
330 MVsum[1] = MV[1].add(mv2.getnPart());
331 MVsum[2] = MV[2].add(mv2.getnbarPart());
332 MVsum[3] = MV[3].add(mv2.getnhnbPart());
333
334 return new MultiVector(MVsum);
335 }
336
337 public MultiVector sub(MultiVector mv2) {
338 ComplexQuat[] MVdif = new ComplexQuat[4];
339
340 MVdif[0] = MV[0].sub(mv2.getScPart());
341 MVdif[1] = MV[1].sub(mv2.getnPart());
342 MVdif[2] = MV[2].sub(mv2.getnbarPart());
343 MVdif[3] = MV[3].sub(mv2.getnhnbPart());
344
345 return new MultiVector(MVdif);
346 }
347
348 public MultiVector mult(MultiVector mv2) {
349 ComplexQuat M1, Mn, Mnb, Mnnb;
350 ComplexQuat N1, Nn, Nnb, Nnnb;
351 ComplexQuat[] MVprod = new ComplexQuat[4];
352
353 M1 = MV[0];
354 Mn = MV[1];
355 Mnb = MV[2];
356 Mnnb = MV[3];
357 N1 = mv2.getScPart();
358 Nn = mv2.getnPart();
359 Nnb = mv2.getnbarPart();
360 Nnnb = mv2.getnhnbPart();
361
362
363
364 MVprod[0] =
365 (((M1.mult(N1)).add(Mnnb.mult(Nnnb))).sub(Mn.mult(Nnb))).sub(
366 Mnb.mult(Nn));
367
368
369 MVprod[1] =
370 (M1.mult(Nn)).add(Mn.mult(N1)).add(Mn.mult(Nnnb)).sub(
371 Mnnb.mult(Nn));
372
373
374 MVprod[2] =
375 (M1.mult(Nnb)).add(Mnb.mult(N1)).sub(Mnb.mult(Nnnb)).add(
376 Mnnb.mult(Nnb));
377
378
379 MVprod[3] =
380 (M1.mult(Nnnb)).add(Mnnb.mult(N1)).sub(Mnb.mult(Nn)).add(
381 Mn.mult(Nnb));
382
383 return new MultiVector(MVprod);
384 }
385
386 public MultiVector Powerof(int power) {
387 if (power < 0)
388 throw new IllegalArgumentException(" Negative powers not yet defined! ");
389 if (power == 0)
390 return MultiVectors.One();
391 MultiVector MP = MultiVectors.One();
392 MultiVector M = new MultiVector(MV);
393 for (int k = 1; k <= power; k++) {
394 MP = MP.mult(M);
395 }
396 return MP;
397 }
398
399 public double ScProd(MultiVector mv2) {
400 return new MultiVector(MV).mult(mv2).getScPart().getScPart().RealPart();
401 }
402
403 public MultiVector OutProd(MultiVector mv2) {
404 MultiVector[] Mg = new MultiVector[6];
405 MultiVector[] Ng = new MultiVector[6];
406 ComplexQuat[] outp0 = {cqzero,cqzero,cqzero,cqzero};
407 MultiVector outp = new MultiVector(outp0);
408 MultiVector M = new MultiVector(MV);
409 Mg[0] = M.getGrade0();
410 Mg[1] = M.getGrade1();
411 Mg[2] = M.getGrade2();
412 Mg[3] = M.getGrade3();
413 Mg[4] = M.getGrade4();
414 Mg[5] = M.getGrade5();
415
416 Ng[0] = mv2.getGrade0();
417 Ng[1] = mv2.getGrade1();
418 Ng[2] = mv2.getGrade2();
419 Ng[3] = mv2.getGrade3();
420 Ng[4] = mv2.getGrade4();
421 Ng[5] = mv2.getGrade5();
422
423 for (int i = 0; i <= 5; i++) {
424 for (int j = 0; j <= 5 - i; j++) {
425 outp = outp.add((Mg[i].mult(Ng[j])).getGrade(i + j));
426 }
427 }
428 return outp;
429 }
430
431 public MultiVector Lcontract(MultiVector mv2) {
432 MultiVector[] Mg = new MultiVector[6];
433 MultiVector[] Ng = new MultiVector[6];
434
435 ComplexQuat[] Lcontr0 = {cqzero,cqzero,cqzero,cqzero};
436 MultiVector Lcontr = new MultiVector(Lcontr0);
437 MultiVector M = new MultiVector(MV);
438
439 Mg[0] = M.getGrade0();
440 Mg[1] = M.getGrade1();
441 Mg[2] = M.getGrade2();
442 Mg[3] = M.getGrade3();
443 Mg[4] = M.getGrade4();
444 Mg[5] = M.getGrade5();
445
446 Ng[0] = mv2.getGrade0();
447 Ng[1] = mv2.getGrade1();
448 Ng[2] = mv2.getGrade2();
449 Ng[3] = mv2.getGrade3();
450 Ng[4] = mv2.getGrade4();
451 Ng[5] = mv2.getGrade5();
452
453 for (int r = 0; r <= 5; r++) {
454 for (int s = r; s <= 5; s++) {
455 Lcontr = Lcontr.add((Mg[r].mult(Ng[s])).getGrade(s - r));
456 }
457 }
458 return Lcontr;
459 }
460
461 public MultiVector Rcontract(MultiVector mv2) {
462 ComplexQuat[] Rcontr0 = {cqzero,cqzero,cqzero,cqzero};
463 MultiVector[] Mg = new MultiVector[6];
464 MultiVector[] Ng = new MultiVector[6];
465
466 MultiVector Rcontr = new MultiVector(Rcontr0);
467
468 MultiVector M = new MultiVector(MV);
469 Mg[0] = M.getGrade0();
470 Mg[1] = M.getGrade1();
471 Mg[2] = M.getGrade2();
472 Mg[3] = M.getGrade3();
473 Mg[4] = M.getGrade4();
474 Mg[5] = M.getGrade5();
475
476 Ng[0] = mv2.getGrade0();
477 Ng[1] = mv2.getGrade1();
478 Ng[2] = mv2.getGrade2();
479 Ng[3] = mv2.getGrade3();
480 Ng[4] = mv2.getGrade4();
481 Ng[5] = mv2.getGrade5();
482
483 for (int r = 0; r <= 5; r++) {
484 for (int s = 0; s <= r; s++) {
485 Rcontr = Rcontr.add((Mg[r].mult(Ng[s])).getGrade(r - s));
486 }
487 }
488
489 return Rcontr;
490 }
491
492 public MultiVector multSc(double factor) {
493 ComplexNumber[] grade0Sc = {new ComplexNumber(factor, 0.0),czero,czero,czero};
494 ComplexQuat cqgrade0Sc = new ComplexQuat(grade0Sc);
495 ComplexQuat[] Grade0 = {cqgrade0Sc,cqzero,cqzero,cqzero};
496 MultiVector SCtoMV = new MultiVector(Grade0);
497 return (new MultiVector(MV)).mult(SCtoMV);
498 }
499
500 public void show() {
501 System.out.println(
502 "This multivector has the components: "
503 + "\n"
504 + MV[0].getScPart().RealPart()
505 + " +I "
506 + MV[0].getScPart().ImaginaryPart()
507 + " "
508 + "\t"
509 + MV[0].getBvPart()[0].RealPart()
510 + " +I "
511 + MV[0].getBvPart()[0].ImaginaryPart()
512 + " "
513 + "\t"
514 + MV[0].getBvPart()[1].RealPart()
515 + " +I "
516 + MV[0].getBvPart()[1].ImaginaryPart()
517 + " "
518 + "\t"
519 + MV[0].getBvPart()[2].RealPart()
520 + " +I "
521 + MV[0].getBvPart()[2].ImaginaryPart());
522 System.out.println(
523 MV[1].getScPart().RealPart()
524 + " +I "
525 + MV[1].getScPart().ImaginaryPart()
526 + " "
527 + "\t"
528 + MV[1].getBvPart()[0].RealPart()
529 + " +I "
530 + MV[1].getBvPart()[0].ImaginaryPart()
531 + " "
532 + "\t"
533 + MV[1].getBvPart()[1].RealPart()
534 + " +I "
535 + MV[1].getBvPart()[1].ImaginaryPart()
536 + " "
537 + "\t"
538 + MV[1].getBvPart()[2].RealPart()
539 + " +I "
540 + MV[1].getBvPart()[2].ImaginaryPart());
541 System.out.println(
542 MV[2].getScPart().RealPart()
543 + " +I "
544 + MV[2].getScPart().ImaginaryPart()
545 + " "
546 + "\t"
547 + MV[2].getBvPart()[0].RealPart()
548 + " +I "
549 + MV[2].getBvPart()[0].ImaginaryPart()
550 + " "
551 + "\t"
552 + MV[2].getBvPart()[1].RealPart()
553 + " +I "
554 + MV[2].getBvPart()[1].ImaginaryPart()
555 + " "
556 + "\t"
557 + MV[2].getBvPart()[2].RealPart()
558 + " +I "
559 + MV[2].getBvPart()[2].ImaginaryPart());
560 System.out.println(
561 MV[3].getScPart().RealPart()
562 + " +I "
563 + MV[3].getScPart().ImaginaryPart()
564 + " "
565 + "\t"
566 + MV[3].getBvPart()[0].RealPart()
567 + " +I "
568 + MV[3].getBvPart()[0].ImaginaryPart()
569 + " "
570 + "\t"
571 + MV[3].getBvPart()[1].RealPart()
572 + " +I "
573 + MV[3].getBvPart()[1].ImaginaryPart()
574 + " "
575 + "\t"
576 + MV[3].getBvPart()[2].RealPart()
577 + " +I "
578 + MV[3].getBvPart()[2].ImaginaryPart());
579 }
580
581
582
583
584 public boolean equals(Object obj) {
585 if (obj == null)
586 throw new NullPointerException("obj is null");
587 if (!(obj instanceof MultiVector))
588 return false;
589 MultiVector mv = (MultiVector) obj;
590 return (
591 (mv.getScPart().equals(getScPart()))
592 && (mv.getnPart().equals(getnPart()))
593 && (mv.getnbarPart().equals(getnbarPart()))
594 && (mv.getnhnbPart().equals(getnhnbPart())));
595 }
596
597 public double[] get3DEuclidianPoint(){
598 double[] result = new double[3];
599 result[0] = -getnhnbPart().getBvPart()[0].ImaginaryPart();
600 result[1] = -getnhnbPart().getBvPart()[1].ImaginaryPart();
601 result[2] = -getnhnbPart().getBvPart()[2].ImaginaryPart();
602 return result;
603 }
604
605
606
607 public int hashCode() {
608
609 return super.hashCode();
610 }
611
612
613
614
615 public String toString() {
616 return "["
617 + MV[0]
618 + ", "
619 + MV[1]
620 + ", "
621 + MV[2]
622 + ","
623 + MV[3]
624 + "]";
625 }
626
627 }