wedged 0.1.1

A robust and generalized library for Geometric Algebra in Rust.
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

use super::*;

//
//Index Ops
//

macro_rules! impl_index {
    ($Ty:ident<T:$Alloc:ident, $($N:ident),*>) => {

        //Currently, we just to indexing wrt `usize`. There may be additions in the future, but
        //unlike with Matrices or slices, ranges don't really make much sense, and while indexing wrt
        //basis blades _may_ make sense, it's slow *and* we have to deal with a potential minus sign.

        impl<T:$Alloc<$($N),*>, $($N:Dim),*> Index<usize> for $Ty<T,$($N),*> {
            type Output = T;
            fn index(&self, i: usize) -> &T { &self.data[i] }
        }

        impl<T:$Alloc<$($N),*>, $($N:Dim),*> IndexMut<usize> for $Ty<T,$($N),*> {
            fn index_mut(&mut self, i: usize) -> &mut T { &mut self.data[i] }
        }

    }
}

impl_index!(Blade<T:AllocBlade, N, G>);
impl_index!(Even<T:AllocEven, N>);
impl_index!(Odd<T:AllocOdd, N>);
impl_index!(Multivector<T:AllocMultivector, N>);

//
//Fn calls
//

//This is probably stupid and will not get used... but it's cool anyway!
#[cfg(feature = "fn_traits")]
mod fn_impl {

    use super::*;

    macro_rules! impl_fn {
        ($Ty:ident<T:$Alloc:ident, $($N:ident),*>) => {

            impl<T:$Alloc<$($N),*>, U:$Alloc<$($N),*>, $($N:Dim,)* Args> FnOnce<Args> for $Ty<T,$($N),*> where
                T: FnOnce<Args, Output=U>,
                Args: Clone
            {
                type Output = $Ty<U,$($N),*>;
                extern "rust-call" fn call_once(self, args: Args) -> $Ty<U,$($N),*> {
                    #[allow(non_snake_case, unused_parens)]
                    let ($($N),*) = self.shape();
                    $Ty::from_iter_generic(
                        $($N,)* self.into_iter().map(|f| f.call_once(args.clone()))
                    )
                }
            }

            impl<T:$Alloc<$($N),*>, U:$Alloc<$($N),*>, $($N:Dim,)* Args> FnMut<Args> for $Ty<T,$($N),*> where
                T: FnMut<Args, Output=U>,
                Args: Clone
            {
                extern "rust-call" fn call_mut(&mut self, args: Args) -> $Ty<U,$($N),*> {
                    #[allow(non_snake_case, unused_parens)]
                    let ($($N),*) = self.shape();
                    $Ty::from_iter_generic(
                        $($N,)* self.iter_mut().map(|f| f.call_mut(args.clone()))
                    )
                }
            }

            impl<T:$Alloc<$($N),*>, U:$Alloc<$($N),*>, $($N:Dim,)* Args> Fn<Args> for $Ty<T,$($N),*> where
                T: Fn<Args, Output=U>,
                Args: Clone
            {
                extern "rust-call" fn call(&self, args: Args) -> $Ty<U,$($N),*> {
                    #[allow(non_snake_case, unused_parens)]
                    let ($($N),*) = self.shape();
                    $Ty::from_iter_generic(
                        $($N,)* self.iter().map(|f| f.call(args.clone()))
                    )
                }
            }

        }
    }

    impl_fn!(Blade<T:AllocBlade, N, G>);
    impl_fn!(Even<T:AllocEven, N>);
    impl_fn!(Odd<T:AllocOdd, N>);
    impl_fn!(Multivector<T:AllocMultivector, N>);

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn circle_path() {

            //have to use a dyn type because every function (including closures) is a seperate type
            type F<'a> = &'a dyn Fn(f64) -> f64;

            let circle = Vec2::<F>::new(&f64::cos, &f64::sin);

            for i in 0..360 {
                let t = i as f64 * 2.0*std::f64::consts::PI / 360.0;
                assert_eq!(circle(t), Vec2::new(t.cos(), t.sin()))
            }

        }

    }

}

//
//Addition and Subtraction
//

//we do this as a macro to make it easier to do with both references and values
macro_rules! check {
    ($self:ident, $rhs:ident, Blade) => {

        //for ease of use
        let (d1, d2) = ($self.dim_generic(), $rhs.dim_generic());
        let (g1, g2) = ($self.grade_generic(), $rhs.grade_generic());

        //if the dims or grades don't match, panic
        //this check should optimize away for statically allocated blades
        //TODO: maybe have blades with same grade but different dim add together once we
        //have specialization and can use `Zero` conditionally
        if d1!=d2 { panic!("Cannot add blades of different dimension: {}!={}", d1.value(), d2.value()); }
        if g1!=g2 { panic!("Cannot add blades of different grade: {}!={}", g1.value(), g2.value()); }

    };

    ($self:ident, $rhs:ident, $Ty:ident) => {

        //for ease of use
        let (d1, d2) = ($self.dim_generic(), $rhs.dim_generic());

        //if the dims or grades don't match, panic
        //this check should optimize away for statically allocated values
        //TODO: maybe allow non-equal dims in the future
        if d1!=d2 { panic!("Cannot add values of different dimension: {}!={}", d1.value(), d2.value()); }

    };
}

//to make the code shorter
macro_rules! uninit {
    ($self:ident, $ty:ty) => {
        <DefaultAllocator as Alloc<$ty>>::uninit($self.shape())
    };
}

macro_rules! impl_assign_binops {

    //implements operation with an optional reference
    (impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() for $Ty:ident; $($a:lifetime)?) => {

        impl<$($a,)? T1, T2, $($N:Dim),*> $Op<$(&$a)? $Ty<T2,$($N),*>> for $Ty<T1,$($N),*>
        where
            T1: $Alloc<$($N),*> + $Op<$(&$a)? T2>,
            T2: $Alloc<$($N),*>
        {

            fn $op(&mut self, rhs: $(&$a)? $Ty<T2,$($N),*>) {

                check!(self, rhs, $Ty);

                //simple enough...
                for (t1, t2) in self.iter_mut().zip(rhs) {
                    t1.$op(t2);
                }

            }

        }

    };

    //do for value and reference
    (impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() for $Ty:ident) => {
        impl_assign_binops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty;   );
        impl_assign_binops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty; 'a);
    };

}

macro_rules! impl_binops {

    //implements operation with optional references
    (
        impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() for $Ty:ident
        with $Op2:ident.$op2:ident();
        $($a:lifetime)?; $($b:lifetime)?
    ) => {

        impl<$($a,)? $($b,)? T1,T2,U,$($N:Dim),*> $Op<$(&$b)? $Ty<T2,$($N),*>> for $(&$a)? $Ty<T1,$($N),*>
        where
            T1: $Alloc<$($N),*>,
            T2: $Alloc<$($N),*>,
            U: $Alloc<$($N),*>,
            T1: $Op2<$($a, )? $(&$b)? T2, Output=U>
        {

            type Output = $Ty<U,$($N),*>;

            fn $op(self, rhs: $(&$b)? $Ty<T2,$($N),*>) -> $Ty<U,$($N),*> {

                check!(self, rhs, $Ty);

                let mut dest = uninit!(self, Self::Output);
                for ((t1, t2), u) in self.into_iter().zip(rhs).zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t1.$op2(t2));
                }

                $Ty { data: unsafe { dest.assume_init() } }

            }

        }

    };

    (impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() -> Self for $Ty:ident) => {
        impl<T, $($N:Dim),*> $Op for $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*> + $Op
        {
            fn $op(&self, v: &Self) -> Self {

                check!(self, v, $Ty);

                let mut dest = uninit!(self, Self);
                for ((t1, t2), u) in self.into_iter().zip(v).zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t1.$op(t2));
                }

				$Ty { data: unsafe { dest.assume_init() } }
            }
        }
    };

    (impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() -> Option<Self> for $Ty:ident) => {
        impl<T, $($N:Dim),*> $Op for $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*> + $Op
        {
            fn $op(&self, v: &Self) -> Option<Self> {

                check!(self, v, $Ty);

                let mut dest = uninit!(self, Self);
                for ((t1, t2), u) in self.into_iter().zip(v).zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t1.$op(t2)?);
                }

				Some($Ty { data: unsafe { dest.assume_init() } })
            }
        }
    };

    //do every combination of reference and value
    (impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() for $Ty:ident with $Ref:ident.$ref:ident()) => {
        impl_binops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty with $Op.$op()  ;   ;   );
        impl_binops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty with $Ref.$ref(); 'a;   );
        impl_binops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty with $Op.$op()  ;   ; 'b);
        impl_binops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty with $Ref.$ref(); 'a; 'b);
    };

    //impl for Add, AddAssign, Sub, and SubAssign
    (impl<T:$Alloc:ident,$($N:ident),*> for $Ty:ident) => {
        impl_binops!(impl<T:$Alloc,$($N),*> Add.add() for $Ty with RefAdd.ref_add());
        impl_binops!(impl<T:$Alloc,$($N),*> Sub.sub() for $Ty with RefSub.ref_sub());
        impl_binops!(impl<T:$Alloc,$($N),*> WrappingAdd.wrapping_add() -> Self for $Ty);
        impl_binops!(impl<T:$Alloc,$($N),*> WrappingSub.wrapping_sub() -> Self for $Ty);
        impl_binops!(impl<T:$Alloc,$($N),*> SaturatingAdd.saturating_add() -> Self for $Ty);
        impl_binops!(impl<T:$Alloc,$($N),*> SaturatingSub.saturating_sub() -> Self for $Ty);
        impl_binops!(impl<T:$Alloc,$($N),*> CheckedAdd.checked_add() -> Option<Self> for $Ty);
        impl_binops!(impl<T:$Alloc,$($N),*> CheckedSub.checked_sub() -> Option<Self> for $Ty);
        impl_assign_binops!(impl<T:$Alloc,$($N),*> AddAssign.add_assign() for $Ty);
        impl_assign_binops!(impl<T:$Alloc,$($N),*> SubAssign.sub_assign() for $Ty);
    };

}

impl_binops!(impl<T:AllocBlade,N,G> for Blade);
impl_binops!(impl<T:AllocEven,N> for Even);
impl_binops!(impl<T:AllocOdd,N> for Odd);
impl_binops!(impl<T:AllocMultivector,N> for Multivector);

//
//Sum and Product
//

impl_fold!(impl<T:AllocBlade,U:AllocBlade,N,G> Sum<Blade<T,N,G>> for Blade<U,N,G>);
impl_fold!(impl<T:AllocEven,U:AllocEven,N> Sum<Even<T,N>> for Even<U,N>);
impl_fold!(impl<T:AllocOdd,U:AllocOdd,N> Sum<Odd<T,N>> for Odd<U,N>);
impl_fold!(impl<T:AllocMultivector,U:AllocMultivector,N> Sum<Multivector<T,N>> for Multivector<U,N>);

impl_fold!(impl<T:AllocEven,U:AllocEven,N> Product<Even<T,N>> for Even<U,N>);
impl_fold!(impl<T:AllocBlade,U:AllocMultivector,N,G> Product<Blade<T,N,G>> for Multivector<U,N>);
impl_fold!(impl<T:AllocMultivector,U:AllocMultivector,N> Product<Multivector<T,N>> for Multivector<U,N>);

//
//Zero
//

//currently, this only works with static allocation, but once we have specialization and
//can implement addition between dynamics with different dimensions, we can change that

impl<T:AllocBlade<N,G>+Zero, N:DimName, G:DimName> Zero for Blade<T,N,G> {
    fn zero() -> Self { Self::zeroed() }
    fn is_zero(&self) -> bool { self.iter().all(|e| e.is_zero()) }
    fn set_zero(&mut self) { self.iter_mut().for_each(|x| x.set_zero()) }
}

impl<T:AllocEven<N>+Zero, N:DimName> Zero for Even<T,N> {
    fn zero() -> Self { Self::zeroed() }
    fn is_zero(&self) -> bool { self.iter().all(|e| e.is_zero()) }
    fn set_zero(&mut self) { self.iter_mut().for_each(|x| x.set_zero()) }
}

impl<T:AllocOdd<N>+Zero, N:DimName> Zero for Odd<T,N> {
    fn zero() -> Self { Self::zeroed() }
    fn is_zero(&self) -> bool { self.iter().all(|e| e.is_zero()) }
    fn set_zero(&mut self) { self.iter_mut().for_each(|x| x.set_zero()) }
}

impl<T:AllocMultivector<N>+Zero, N:DimName> Zero for Multivector<T,N> {
    fn zero() -> Self { Self::zeroed() }
    fn is_zero(&self) -> bool { self.iter().all(|e| e.is_zero()) }
    fn set_zero(&mut self) { self.iter_mut().for_each(|x| x.set_zero()) }
}

//
//Neg
//

//don't really know what unary op we'd do besides neg, but here's a general macro anyway :)
macro_rules! impl_unary_ops {
    (
        impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() for $Ty:ident
        with $Op2:ident.$op2:ident();
        $($a:lifetime)?
    ) => {

        impl<$($a,)? T, U, $($N:Dim),*> $Op for $(&$a)? $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*>,
            U: $Alloc<$($N),*>,
            T: $Op2<$($a, )? Output=U>
        {

            type Output = $Ty<U,$($N),*>;

            fn $op(self) -> $Ty<U,$($N),*> {
                let mut dest = uninit!(self, Self::Output);
                for (t, u) in self.into_iter().zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t.$op2());
                }

                $Ty { data: unsafe { dest.assume_init() } }
            }

        }

    };

    //do for value and reference
    (impl<T:$Alloc:ident,$($N:ident),*> $Op:ident.$op:ident() for $Ty:ident with $Ref:ident.$ref:ident()) => {
        impl_unary_ops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty with $Op.$op()  ;   );
        impl_unary_ops!(impl<T:$Alloc,$($N),*> $Op.$op() for $Ty with $Ref.$ref(); 'a);
    };

    (impl<T:$Alloc:ident,$($N:ident),*> for $Ty:ident) => {
        impl_unary_ops!(impl<T:$Alloc,$($N),*> Neg.neg() for $Ty with RefNeg.ref_neg());

		impl<T, $($N:Dim),*> WrappingNeg for $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*> + WrappingNeg
        {
            fn wrapping_neg(&self) -> Self {
                let mut dest = uninit!(self, Self);
                for (t, u) in self.into_iter().zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t.wrapping_neg());
                }
                $Ty { data: unsafe { dest.assume_init() } }
            }
        }

        impl<T, $($N:Dim),*> CheckedNeg for $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*> + CheckedNeg
        {
            fn checked_neg(&self) -> Option<Self> {
                let mut dest = uninit!(self, Self);
                for (t, u) in self.into_iter().zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t.checked_neg()?);
                }
                Some($Ty { data: unsafe { dest.assume_init() } })
            }
        }
    }
}

impl_unary_ops!(impl<T:AllocBlade,N,G> for Blade);
impl_unary_ops!(impl<T:AllocEven,N> for Even);
impl_unary_ops!(impl<T:AllocOdd,N> for Odd);
impl_unary_ops!(impl<T:AllocMultivector,N> for Multivector);

//
//Scalar Multiplication and Division
//

macro_rules! impl_scalar_ops {

    (
        impl<T:$Alloc:ident, $($N:ident),*>
            $Op:ident.$op:ident(), $Scale:ident.$scale:ident(), $Op2:ident.$op2:ident()
        for $Ty:ident; $($a:lifetime)?
    ) => {

        impl<$($a,)? T1,T2,U,$($N:Dim),*> $Scale<T2> for $(&$a)? $Ty<T1,$($N),*>
        where
            T1: $Alloc<$($N),*>,
            T2: Clone,
            U: $Alloc<$($N),*>,
            T1: $Op2<$($a, )? T2, Output=U>,
        {

            type Output = $Ty<U,$($N),*>;

            fn $scale(self, t2: T2) -> $Ty<U,$($N),*> {
                let mut dest = uninit!(self, Self::Output);
                for (t1, u) in self.into_iter().zip(dest.borrow_mut()) {
                    *u = MaybeUninit::new(t1.$op2(t2.clone()));
                }

                $Ty { data: unsafe { dest.assume_init() } }
            }

        }

        impl<$($a,)? T,U,$($N:Dim),*> $Op<T> for $(&$a)? $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*>+Clone,
            U: $Alloc<$($N),*>,
            T: $Op2<$($a, )? T, Output=U>
        {
            type Output = $Ty<U,$($N),*>;
            fn $op(self, t2: T) -> $Ty<U,$($N),*> { self.$scale(t2) }
        }

        impl<$($a,)? 'b, T,U,$($N:Dim),*> $Op<&'b T> for $(&$a)? $Ty<T,$($N),*>
        where
            T: $Alloc<$($N),*>,
            U: $Alloc<$($N),*>,
            T: $Op2<$($a, )? &'b T, Output=U>
        {
            type Output = $Ty<U,$($N),*>;
            fn $op(self, t2: &'b T) -> $Ty<U,$($N),*> { self.$scale(t2) }
        }

    };

    //do every combination of reference and value
    (
        impl<T:$Alloc:ident, $($N:ident),*>
            $Op:ident.$op:ident(), $Scale:ident.$scale:ident(), $Ref:ident.$ref:ident()
            for $Ty:ident
    ) => {
        impl_scalar_ops!(impl<T:$Alloc, $($N),*> $Op.$op(), $Scale.$scale(), $Op.$op() for $Ty;);
        impl_scalar_ops!(impl<T:$Alloc, $($N),*> $Op.$op(), $Scale.$scale(), $Ref.$ref() for $Ty; 'a);
    };

    (impl<T:$Alloc:ident, $($N:ident),*> for $Ty:ident) => {
        impl_scalar_ops!(impl<T:$Alloc, $($N),*> Mul.mul(), Scale.scale(), RefMul.ref_mul() for $Ty);
        impl_scalar_ops!(impl<T:$Alloc, $($N),*> Div.div(), InvScale.inv_scale(), RefDiv.ref_div() for $Ty);
    };

}

impl_scalar_ops!(impl<T:AllocBlade,N,G> for Blade);
impl_scalar_ops!(impl<T:AllocEven,N> for Even);
impl_scalar_ops!(impl<T:AllocOdd,N> for Odd);
impl_scalar_ops!(impl<T:AllocMultivector,N> for Multivector);

macro_rules! impl_scalar_assign_binops {
    (impl<T:$Alloc:ident, $($N:ident),*> $Op:ident.$op:ident() for $Ty:ident) => {

        impl<T:$Alloc<$($N),*>+$Op<T>+Clone, $($N:Dim),*> $Op<T> for $Ty<T, $($N),*> {
            fn $op(&mut self, rhs: T) {
                //simple enough...
                for t1 in self {
                    t1.$op(rhs.clone());
                }
            }
        }

        impl<'a, T:$Alloc<$($N),*>+$Op<&'a T>, $($N:Dim),*> $Op<&'a T> for $Ty<T, $($N),*> {
            fn $op(&mut self, rhs: &'a T) {
                //simple enough...
                for t1 in self {
                    t1.$op(rhs);
                }
            }
        }

    }
}

impl_scalar_assign_binops!(impl<T:AllocBlade,N,G> MulAssign.mul_assign() for Blade);
impl_scalar_assign_binops!(impl<T:AllocBlade,N,G> DivAssign.div_assign() for Blade);
impl_scalar_assign_binops!(impl<T:AllocEven,N> MulAssign.mul_assign() for Even);
impl_scalar_assign_binops!(impl<T:AllocEven,N> DivAssign.div_assign() for Even);
impl_scalar_assign_binops!(impl<T:AllocOdd,N> MulAssign.mul_assign() for Odd);
impl_scalar_assign_binops!(impl<T:AllocOdd,N> DivAssign.div_assign() for Odd);
impl_scalar_assign_binops!(impl<T:AllocMultivector,N> MulAssign.mul_assign() for Multivector);
impl_scalar_assign_binops!(impl<T:AllocMultivector,N> DivAssign.div_assign() for Multivector);

impl_forward_scalar_binops!(impl<T:AllocBlade,N,G> Mul.mul() for Blade);
// impl_forward_scalar_binops!(impl<T:AllocBlade,N,G> Div.div() for Blade);
impl_forward_scalar_binops!(impl<T:AllocEven,N> Mul.mul() for Even);
// impl_forward_scalar_binops!(impl<T:AllocEven,N> Div.div() for Even);
impl_forward_scalar_binops!(impl<T:AllocOdd,N> Mul.mul() for Odd);
// impl_forward_scalar_binops!(impl<T:AllocOdd,N> Div.div() for Odd);
impl_forward_scalar_binops!(impl<T:AllocMultivector,N> Mul.mul() for Multivector);
// impl_forward_scalar_binops!(impl<T:AllocMultivector,N> Div.div() for Multivector);

//
//MulAssign and DivAssign
//

macro_rules! impl_mul_assign {
    (
        $Ty1:ident<T:$Alloc1:ident $(,$N1:ident)*> *=
        $(&$a:lifetime)? $Ty2:ident<T:$Alloc2:ident $(,$N2:ident)*>; $($N:ident)*
    ) => {

        impl<$($a,)? T $(,$N:Dim)*> MulAssign<$(&$a)? $Ty2<T$(,$N2)*>> for $Ty1<T $(,$N1)*> where
            T: $Alloc1<$($N1),*> + $Alloc2<$($N2),*> + AddGroup + AllRefMul<T,AllOutput=T>
        {
            fn mul_assign(&mut self, rhs: $(&$a)? $Ty2<T$(,$N2)*>) {
                *self = &*self * rhs
            }
        }

    }
}


impl_mul_assign!( Multivector<T:AllocMultivector,N> *= Blade<T:AllocBlade,N,G>; N G);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= &'a Blade<T:AllocBlade,N,G>; N G);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= Even<T:AllocEven,N>; N);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= &'a Even<T:AllocEven,N>; N);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= Odd<T:AllocOdd,N>; N);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= &'a Odd<T:AllocOdd,N>; N);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= Multivector<T:AllocMultivector,N>; N);
impl_mul_assign!( Multivector<T:AllocMultivector,N> *= &'a Multivector<T:AllocMultivector,N>; N);


impl_mul_assign!( Even<T:AllocEven,N> *= Even<T:AllocEven,N>; N);
impl_mul_assign!( Even<T:AllocEven,N> *= &'a Even<T:AllocEven,N>; N);


macro_rules! impl_pow {
    ($Ty:ident<T:$Alloc:ident $(,$N:ident)*>) => {
        impl<T:$Alloc<$($N),*>+RefUnitRing $(, $N:Dim)*> $Ty<T $(,$N)*> {

            pub fn powu(self, p:u32) -> Self {
                let n = self.dim_generic();
                repeated_doubling(self, Self::one_generic(n), p)
            }

            pub fn powi(self, p:i32) -> Self where Self:ClosedInv {
                let n = self.dim_generic();
                repeated_doubling_inv(self, Self::one_generic(n), p)
            }

        }
    };
}

impl_pow!(Even<T:AllocEven,N>);
impl_pow!(Multivector<T:AllocMultivector,N>);