This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
matrix_gf2e_dense.pyx
1602 lines (1265 loc) · 56.1 KB
/
matrix_gf2e_dense.pyx
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Dense matrices over `\GF{2^e}` for `2 <= e <= 10` using the M4RIE library
The M4RIE library offers two matrix representations:
1) ``mzed_t``
m x n matrices over `\GF{2^e}` are internally represented roughly as
m x (en) matrices over `\GF{2}`. Several elements are packed into
words such that each element is filled with zeroes until the next
power of two. Thus, for example, elements of `\GF{2^3}` are
represented as ``[0xxx|0xxx|0xxx|0xxx|...]``. This representation is
wrapped as :class:`Matrix_gf2e_dense` in Sage.
Multiplication and elimination both use "Newton-John" tables. These
tables are simply all possible multiples of a given row in a matrix
such that a scale+add operation is reduced to a table lookup +
add. On top of Newton-John multiplication M4RIE implements
asymptotically fast Strassen-Winograd multiplication. Elimination
uses simple Gaussian elimination which requires `O(n^3)` additions
but only `O(n^2 * 2^e)` multiplications.
2) ``mzd_slice_t``
m x n matrices over `\GF{2^e}` are internally represented as slices
of m x n matrices over `\GF{2}`. This representation allows for very
fast matrix times matrix products using Karatsuba's polynomial
multiplication for polynomials over matrices. However, it is not
feature complete yet and hence not wrapped in Sage for now.
See http://m4ri.sagemath.org for more details on the M4RIE library.
EXAMPLES::
sage: K.<a> = GF(2^8)
sage: A = random_matrix(K, 3,4)
sage: A
[ a^6 + a^5 + a^4 + a^2 a^6 + a^3 + a + 1 a^5 + a^3 + a^2 + a + 1 a^7 + a^6 + a + 1]
[ a^7 + a^6 + a^3 a^7 + a^6 + a^5 + 1 a^5 + a^4 + a^3 + a + 1 a^6 + a^5 + a^4 + a^3 + a^2 + 1]
[ a^6 + a^5 + a + 1 a^7 + a^3 + 1 a^7 + a^3 + a + 1 a^7 + a^6 + a^3 + a^2 + a + 1]
sage: A.echelon_form()
[ 1 0 0 a^6 + a^5 + a^4 + a^2]
[ 0 1 0 a^7 + a^5 + a^3 + a + 1]
[ 0 0 1 a^6 + a^4 + a^3 + a^2 + 1]
AUTHOR:
* Martin Albrecht <martinralbrecht@googlemail.com>
TESTS::
sage: TestSuite(sage.matrix.matrix_gf2e_dense.Matrix_gf2e_dense).run(verbose=True)
running ._test_new() . . . pass
running ._test_pickling() . . . pass
Test hashing::
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K, 1000, 1000)
sage: A.set_immutable()
sage: {A:1}
{1000 x 1000 dense matrix over Finite Field in a of size 2^4: 1}
.. TODO::
Wrap ``mzd_slice_t``.
REFERENCES:
- [BB2009]_
"""
#*****************************************************************************
# Copyright (C) 2010 Martin Albrecht <martinralbrecht@googlemail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
#*****************************************************************************
from cysignals.signals cimport sig_check, sig_on, sig_off
cimport sage.matrix.matrix_dense as matrix_dense
from sage.structure.element cimport Matrix, Vector
from sage.structure.element cimport ModuleElement, Element, RingElement
from sage.structure.richcmp cimport rich_to_bool
from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF
from sage.misc.randstate cimport randstate, current_randstate
from sage.matrix.matrix_mod2_dense cimport Matrix_mod2_dense
from .args cimport SparseEntry, MatrixArgs_init
from sage.libs.m4ri cimport m4ri_word, mzd_copy, mzd_init
from sage.libs.m4rie cimport *
from sage.libs.m4rie cimport mzed_t
# we must keep a copy of the internal finite field representation
# around to avoid re-creating it over and over again. Furthermore,
# M4RIE assumes pointer equivalence of identical fields.
_m4rie_finite_field_cache = {}
cdef class M4RIE_finite_field:
"""
A thin wrapper around the M4RIE finite field class such that we
can put it in a hash table. This class is not meant for public
consumption.
"""
cdef gf2e *ff
def __dealloc__(self):
"""
EXAMPLES::
sage: from sage.matrix.matrix_gf2e_dense import M4RIE_finite_field
sage: K = M4RIE_finite_field(); K
<sage.matrix.matrix_gf2e_dense.M4RIE_finite_field object at 0x...>
sage: del K
"""
if self.ff:
gf2e_free(self.ff)
cdef m4ri_word poly_to_word(f):
return f.integer_representation()
cdef object word_to_poly(w, F):
return F.fetch_int(w)
cdef class Matrix_gf2e_dense(matrix_dense.Matrix_dense):
def __cinit__(self, *args, bint alloc=True, **kwds):
"""
INPUT:
- ``alloc`` - if ``True`` the matrix is allocated first (default: ``True``)
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = Matrix(K, 3, 4); A
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
sage: A.randomize(); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: K.<a> = GF(2^3)
sage: A = Matrix(K,3,4); A
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
sage: A.randomize(); A
[ a^2 + a a^2 + 1 a^2 + a a^2 + a]
[ a^2 + 1 a^2 + a + 1 a^2 + 1 a^2]
[ a^2 + a a^2 + 1 a^2 + a + 1 a + 1]
"""
cdef M4RIE_finite_field FF
R = self._base_ring
f = R.polynomial()
cdef long i
cdef m4ri_word poly = sum(((<m4ri_word>c) << i) for (i, c) in enumerate(f))
if alloc and self._nrows and self._ncols:
if poly in _m4rie_finite_field_cache:
self._entries = mzed_init((<M4RIE_finite_field>_m4rie_finite_field_cache[poly]).ff, self._nrows, self._ncols)
else:
FF = M4RIE_finite_field.__new__(M4RIE_finite_field)
FF.ff = gf2e_init(poly)
self._entries = mzed_init(FF.ff, self._nrows, self._ncols)
_m4rie_finite_field_cache[poly] = FF
# cache elements
self._zero = self._base_ring(0)
self._one = self._base_ring(1)
def __dealloc__(self):
"""
TESTS::
sage: K.<a> = GF(2^4)
sage: A = Matrix(K, 1000, 1000)
sage: del A
sage: A = Matrix(K, 1000, 1000)
sage: del A
"""
if self._entries:
mzed_free(self._entries)
self._entries = NULL
def __init__(self, parent, entries=None, copy=None, bint coerce=True):
r"""
Create new matrix over `GF(2^e)` for 2<=e<=10.
INPUT:
- ``parent`` -- a matrix space over ``GF(2^e)``
- ``entries`` -- see :func:`matrix`
- ``copy`` -- ignored (for backwards compatibility)
- ``coerce`` -- if False, assume without checking that the
entries lie in the base ring
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: l = [K.random_element() for _ in range(3*4)]; l
[a^2 + 1, a^3 + 1, 0, 0, a, a^3 + a + 1, a + 1, a + 1, a^2, a^3 + a + 1, a^3 + a, a^3 + a]
sage: A = Matrix(K, 3, 4, l); A
[ a^2 + 1 a^3 + 1 0 0]
[ a a^3 + a + 1 a + 1 a + 1]
[ a^2 a^3 + a + 1 a^3 + a a^3 + a]
sage: A.list()
[a^2 + 1, a^3 + 1, 0, 0, a, a^3 + a + 1, a + 1, a + 1, a^2, a^3 + a + 1, a^3 + a, a^3 + a]
sage: l[0], A[0,0]
(a^2 + 1, a^2 + 1)
sage: A = Matrix(K, 3, 3, a); A
[a 0 0]
[0 a 0]
[0 0 a]
"""
ma = MatrixArgs_init(parent, entries)
for t in ma.iter(coerce, True):
se = <SparseEntry>t
mzed_write_elem(self._entries, se.i, se.j, poly_to_word(se.entry))
cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value):
"""
A[i,j] = value without bound checks
INPUT:
- ``i`` - row index
- ``j`` - column index
- ``value`` - a finite field element (not checked but assumed)
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = Matrix(K,3,4,[K.random_element() for _ in range(3*4)]); A
[ a^2 + 1 a^3 + 1 0 0]
[ a a^3 + a + 1 a + 1 a + 1]
[ a^2 a^3 + a + 1 a^3 + a a^3 + a]
sage: A[0,0] = a # indirect doctest
sage: A
[ a a^3 + 1 0 0]
[ a a^3 + a + 1 a + 1 a + 1]
[ a^2 a^3 + a + 1 a^3 + a a^3 + a]
"""
mzed_write_elem(self._entries, i, j, poly_to_word(value))
cdef get_unsafe(self, Py_ssize_t i, Py_ssize_t j):
"""
Get A[i,j] without bound checks.
INPUT:
- ``i`` - row index
- ``j`` - column index
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K,3,4)
sage: A[2,3] # indirect doctest
a^3 + a^2 + a + 1
sage: K.<a> = GF(2^3)
sage: m,n = 3, 4
sage: A = random_matrix(K,3,4); A
[ a^2 + a a^2 + 1 a^2 + a a^2 + a]
[ a^2 + 1 a^2 + a + 1 a^2 + 1 a^2]
[ a^2 + a a^2 + 1 a^2 + a + 1 a + 1]
"""
cdef int r = mzed_read_elem(self._entries, i, j)
return word_to_poly(r, self._base_ring)
cpdef _add_(self, right):
"""
Return A+B
INPUT:
- ``right`` - a matrix
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K,3,4); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: B = random_matrix(K,3,4); B
[ a^2 + a a^2 + 1 a^2 + a a^3 + a^2 + a]
[ a^2 + 1 a^3 + a^2 + a + 1 a^2 + 1 a^2]
[ a^3 + a^2 + a a^2 + 1 a^2 + a + 1 a^3 + a + 1]
sage: C = A + B; C # indirect doctest
[ a a^3 + a^2 + a a^3 + 1 a^3 + a^2 + 1]
[a^3 + a^2 + 1 a^3 + a^2 + a a^3 + a^2 + a a^3 + 1]
[a^3 + a^2 + 1 a^3 + a^2 a^3 + a^2 a^2]
"""
cdef Matrix_gf2e_dense A
A = Matrix_gf2e_dense.__new__(Matrix_gf2e_dense, self._parent, 0, 0, 0, alloc=False)
if self._nrows == 0 or self._ncols == 0:
return A
A._entries = mzed_add(NULL, self._entries, (<Matrix_gf2e_dense>right)._entries)
return A
cpdef _sub_(self, right):
"""
EXAMPLES::
sage: from sage.matrix.matrix_gf2e_dense import Matrix_gf2e_dense
sage: K.<a> = GF(2^4)
sage: m,n = 3, 4
sage: MS = MatrixSpace(K,m,n)
sage: A = random_matrix(K,3,4); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: B = random_matrix(K,3,4); B
[ a^2 + a a^2 + 1 a^2 + a a^3 + a^2 + a]
[ a^2 + 1 a^3 + a^2 + a + 1 a^2 + 1 a^2]
[ a^3 + a^2 + a a^2 + 1 a^2 + a + 1 a^3 + a + 1]
sage: C = A - B; C # indirect doctest
[ a a^3 + a^2 + a a^3 + 1 a^3 + a^2 + 1]
[a^3 + a^2 + 1 a^3 + a^2 + a a^3 + a^2 + a a^3 + 1]
[a^3 + a^2 + 1 a^3 + a^2 a^3 + a^2 a^2]
"""
return self._add_(right)
def _multiply_classical(self, Matrix right):
"""
Classical cubic matrix multiplication.
EXAMPLES::
sage: K.<a> = GF(2^2)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A*B == A._multiply_classical(B)
True
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A*B == A._multiply_classical(B)
True
sage: K.<a> = GF(2^8)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A*B == A._multiply_classical(B)
True
sage: K.<a> = GF(2^10)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A*B == A._multiply_classical(B)
True
.. NOTE::
This function is very slow. Use ``*`` operator instead.
"""
if self._ncols != right._nrows:
raise ArithmeticError("left ncols must match right nrows")
cdef Matrix_gf2e_dense ans
ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
return ans
sig_on()
ans._entries = mzed_mul_naive(ans._entries, self._entries, (<Matrix_gf2e_dense>right)._entries)
sig_off()
return ans
cdef _matrix_times_matrix_(self, Matrix right):
"""
Return A*B
Uses the M4RIE machinery to decide which function to call.
INPUT:
- ``right`` - a matrix
EXAMPLES::
sage: K.<a> = GF(2^3)
sage: A = random_matrix(K, 51, 50)
sage: B = random_matrix(K, 50, 52)
sage: A*B == A._multiply_newton_john(B) # indirect doctest
True
sage: K.<a> = GF(2^5)
sage: A = random_matrix(K, 10, 50)
sage: B = random_matrix(K, 50, 12)
sage: A*B == A._multiply_newton_john(B)
True
sage: K.<a> = GF(2^7)
sage: A = random_matrix(K,100, 50)
sage: B = random_matrix(K, 50, 17)
sage: A*B == A._multiply_classical(B)
True
"""
if self._ncols != right._nrows:
raise ArithmeticError("left ncols must match right nrows")
cdef Matrix_gf2e_dense ans
ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
return ans
sig_on()
ans._entries = mzed_mul(ans._entries, self._entries, (<Matrix_gf2e_dense>right)._entries)
sig_off()
return ans
cpdef Matrix_gf2e_dense _multiply_newton_john(Matrix_gf2e_dense self, Matrix_gf2e_dense right):
"""
Return A*B using Newton-John tables.
We can write classical cubic multiplication (``C=A*B``) as::
for i in range(A.ncols()):
for j in range(A.nrows()):
C[j] += A[j,i] * B[j]
Hence, in the inner-most loop we compute multiples of ``B[j]``
by the values ``A[j,i]``. If the matrix ``A`` is big and the
finite field is small, there is a very high chance that
``e * B[j]`` is computed more than once for any ``e`` in the finite
field. Instead, we compute all possible
multiples of ``B[j]`` and re-use this data in the inner loop.
This is what is called a "Newton-John" table in M4RIE.
INPUT:
- ``right`` - a matrix
EXAMPLES::
sage: K.<a> = GF(2^2)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_newton_john(B) == A._multiply_classical(B) # indirect doctest
True
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_newton_john(B) == A._multiply_classical(B)
True
sage: K.<a> = GF(2^8)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_newton_john(B) == A._multiply_classical(B)
True
sage: K.<a> = GF(2^10)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_newton_john(B) == A._multiply_classical(B)
True
"""
if self._ncols != right._nrows:
raise ArithmeticError("left ncols must match right nrows")
cdef Matrix_gf2e_dense ans
ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
return ans
sig_on()
ans._entries = mzed_mul_newton_john(ans._entries, self._entries, (<Matrix_gf2e_dense>right)._entries)
sig_off()
return ans
cpdef Matrix_gf2e_dense _multiply_karatsuba(Matrix_gf2e_dense self, Matrix_gf2e_dense right):
"""
Matrix multiplication using Karatsuba over polynomials with
matrix coefficients over GF(2).
The idea behind Karatsuba multiplication for matrices over
`\GF{p^n}` is to treat these matrices as polynomials with
coefficients of matrices over `\GF{p}`. Then, Karatsuba-style
formulas can be used to perform multiplication, cf. [BB2009]_.
INPUT:
- ``right`` - a matrix
EXAMPLES::
sage: K.<a> = GF(2^2)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_karatsuba(B) == A._multiply_classical(B) # indirect doctest
True
sage: K.<a> = GF(2^2)
sage: A = random_matrix(K, 137, 11)
sage: B = random_matrix(K, 11, 23)
sage: A._multiply_karatsuba(B) == A._multiply_classical(B)
True
sage: K.<a> = GF(2^10)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_karatsuba(B) == A._multiply_classical(B)
True
"""
if self._ncols != right._nrows:
raise ArithmeticError("left ncols must match right nrows")
cdef Matrix_gf2e_dense ans
ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
return ans
sig_on()
ans._entries = mzed_mul_karatsuba(ans._entries, self._entries, (<Matrix_gf2e_dense>right)._entries)
sig_off()
return ans
cpdef Matrix_gf2e_dense _multiply_strassen(Matrix_gf2e_dense self, Matrix_gf2e_dense right, cutoff=0):
"""
Winograd-Strassen matrix multiplication with Newton-John
multiplication as base case.
INPUT:
- ``right`` - a matrix
- ``cutoff`` - row or column dimension to switch over to
Newton-John multiplication (default: 64)
EXAMPLES::
sage: K.<a> = GF(2^2)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_strassen(B) == A._multiply_classical(B) # indirect doctest
True
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_strassen(B) == A._multiply_classical(B)
True
sage: K.<a> = GF(2^8)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_strassen(B) == A._multiply_classical(B)
True
sage: K.<a> = GF(2^10)
sage: A = random_matrix(K, 50, 50)
sage: B = random_matrix(K, 50, 50)
sage: A._multiply_strassen(B) == A._multiply_classical(B)
True
"""
if self._ncols != right._nrows:
raise ArithmeticError("left ncols must match right nrows")
cdef Matrix_gf2e_dense ans
ans = self.new_matrix(nrows = self.nrows(), ncols = right.ncols())
if self._nrows == 0 or self._ncols == 0 or right._ncols == 0:
return ans
if cutoff == 0:
cutoff = _mzed_strassen_cutoff(ans._entries, self._entries, (<Matrix_gf2e_dense>right)._entries)
sig_on()
ans._entries = mzed_mul_strassen(ans._entries, self._entries, (<Matrix_gf2e_dense>right)._entries, cutoff)
sig_off()
return ans
cpdef _lmul_(self, Element right):
"""
Return ``a*B`` for ``a`` an element of the base field.
INPUT:
- ``right`` - an element of the base field
EXAMPLES::
sage: K.<a> = GF(4)
sage: A = random_matrix(K,10,10)
sage: A
[ 0 a + 1 a + 1 a + 1 0 1 a + 1 1 a + 1 1]
[a + 1 a + 1 a 1 a a 1 a + 1 1 0]
[ a 1 a + 1 a + 1 0 a + 1 a 1 a a]
[a + 1 a 0 0 1 a + 1 a + 1 0 a + 1 1]
[ a 0 a + 1 a a 0 a + 1 a 1 a + 1]
[ a 0 a a + 1 a 1 a + 1 a a a]
[a + 1 a 0 1 0 a + 1 a + 1 a 0 a + 1]
[a + 1 a + 1 0 a + 1 a 1 a + 1 a + 1 a + 1 0]
[ 0 0 0 a + 1 1 a + 1 0 a + 1 1 0]
[ 1 a + 1 0 1 a 0 0 a a + 1 a]
sage: a*A # indirect doctest
[ 0 1 1 1 0 a 1 a 1 a]
[ 1 1 a + 1 a a + 1 a + 1 a 1 a 0]
[a + 1 a 1 1 0 1 a + 1 a a + 1 a + 1]
[ 1 a + 1 0 0 a 1 1 0 1 a]
[a + 1 0 1 a + 1 a + 1 0 1 a + 1 a 1]
[a + 1 0 a + 1 1 a + 1 a 1 a + 1 a + 1 a + 1]
[ 1 a + 1 0 a 0 1 1 a + 1 0 1]
[ 1 1 0 1 a + 1 a 1 1 1 0]
[ 0 0 0 1 a 1 0 1 a 0]
[ a 1 0 a a + 1 0 0 a + 1 1 a + 1]
"""
cdef m4ri_word a = poly_to_word(right)
cdef Matrix_gf2e_dense C = Matrix_gf2e_dense.__new__(Matrix_gf2e_dense, self._parent, 0, 0, 0)
mzed_mul_scalar(C._entries, a, self._entries)
return C
def __neg__(self):
"""
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K, 3, 4); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: -A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
"""
return self.__copy__()
cpdef _richcmp_(self, right, int op):
"""
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K,3,4)
sage: B = copy(A)
sage: A == B
True
sage: A[0,0] = a
sage: A == B
False
"""
if self._nrows == 0 or self._ncols == 0:
return rich_to_bool(op, 0)
return rich_to_bool(op, mzed_cmp(self._entries,
(<Matrix_gf2e_dense>right)._entries))
def __copy__(self):
"""
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: m,n = 3, 4
sage: A = random_matrix(K,3,4); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: A2 = copy(A); A2
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: A[0,0] = 1
sage: A2[0,0]
a^2
"""
cdef Matrix_gf2e_dense A
A = Matrix_gf2e_dense.__new__(Matrix_gf2e_dense, self._parent, 0, 0, 0)
if self._nrows and self._ncols:
mzed_copy(A._entries, <const_mzed_t *>self._entries)
return A
def _list(self):
"""
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: m,n = 3, 4
sage: A = random_matrix(K,3,4); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1]
[ a^3 1 a^3 + a + 1 a^3 + a^2 + 1]
[ a + 1 a^3 + 1 a^3 + a + 1 a^3 + a^2 + a + 1]
sage: A.list() # indirect doctest
[a^2, a^3 + a + 1, a^3 + a^2 + a + 1, a + 1, a^3, 1, a^3 + a + 1, a^3 + a^2 + 1, a + 1, a^3 + 1, a^3 + a + 1, a^3 + a^2 + a + 1]
"""
cdef int i,j
l = []
for i from 0 <= i < self._nrows:
for j from 0 <= j < self._ncols:
l.append(self.get_unsafe(i,j))
return l
def randomize(self, density=1, nonzero=False, *args, **kwds):
"""
Randomize ``density`` proportion of the entries of this matrix,
leaving the rest unchanged.
INPUT:
- ``density`` - float; proportion (roughly) to be considered for
changes
- ``nonzero`` - Bool (default: ``False``); whether the new entries
are forced to be non-zero
OUTPUT:
- None, the matrix is modified in-place
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: A = Matrix(K,3,3)
sage: A.randomize(); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1]
[ a + 1 a^3 1]
[ a^3 + a + 1 a^3 + a^2 + 1 a + 1]
sage: K.<a> = GF(2^4)
sage: A = random_matrix(K,1000,1000,density=0.1)
sage: float(A.density())
0.0999...
sage: A = random_matrix(K,1000,1000,density=1.0)
sage: float(A.density())
1.0
sage: A = random_matrix(K,1000,1000,density=0.5)
sage: float(A.density())
0.4996...
Note, that the matrix is updated and not zero-ed out before
being randomized::
sage: A = matrix(K,1000,1000)
sage: A.randomize(nonzero=False, density=0.1)
sage: float(A.density())
0.0936...
sage: A.randomize(nonzero=False, density=0.05)
sage: float(A.density())
0.135854
"""
if self._ncols == 0 or self._nrows == 0:
return
cdef Py_ssize_t i,j
self.check_mutability()
self.clear_cache()
cdef m4ri_word mask = (1<<(self._parent.base_ring().degree())) - 1
cdef randstate rstate = current_randstate()
K = self._parent.base_ring()
if self._ncols == 0 or self._nrows == 0:
return
cdef float _density = density
if _density <= 0:
return
if _density > 1:
_density = 1.0
if _density == 1:
if not nonzero:
sig_on()
for i in range(self._nrows):
for j in range(self._ncols):
tmp = rstate.c_random() & mask
mzed_write_elem(self._entries, i, j, tmp)
sig_off()
else:
sig_on()
for i in range(self._nrows):
for j in range(self._ncols):
tmp = rstate.c_random() & mask
while tmp == 0:
tmp = rstate.c_random() & mask
mzed_write_elem(self._entries, i, j, tmp)
sig_off()
else:
if not nonzero:
sig_on()
for i in range(self._nrows):
for j in range(self._ncols):
if rstate.c_rand_double() <= _density:
tmp = rstate.c_random() & mask
mzed_write_elem(self._entries, i, j, tmp)
sig_off()
else:
sig_on()
for i in range(self._nrows):
for j in range(self._ncols):
if rstate.c_rand_double() <= _density:
tmp = rstate.c_random() & mask
while tmp == 0:
tmp = rstate.c_random() & mask
mzed_write_elem(self._entries, i, j, tmp)
sig_off()
def echelonize(self, algorithm='heuristic', reduced=True, **kwds):
"""
Compute the row echelon form of ``self`` in place.
INPUT:
- ``algorithm`` - one of the following
- ``heuristic`` - let M4RIE decide (default)
- ``newton_john`` - use newton_john table based algorithm
- ``ple`` - use PLE decomposition
- ``naive`` - use naive cubic Gaussian elimination (M4RIE implementation)
- ``builtin`` - use naive cubic Gaussian elimination (Sage implementation)
- ``reduced`` - if ``True`` return reduced echelon form. No
guarantee is given that the matrix is *not* reduced if
``False`` (default: ``True``)
EXAMPLES::
sage: K.<a> = GF(2^4)
sage: m,n = 3, 5
sage: A = random_matrix(K, 3, 5); A
[ a^2 a^3 + a + 1 a^3 + a^2 + a + 1 a + 1 a^3]
[ 1 a^3 + a + 1 a^3 + a^2 + 1 a + 1 a^3 + 1]
[ a^3 + a + 1 a^3 + a^2 + a + 1 a^2 + a a^2 + 1 a^2 + a]
sage: A.echelonize(); A
[ 1 0 0 a + 1 a^2 + 1]
[ 0 1 0 a^2 a + 1]
[ 0 0 1 a^3 + a^2 + a a^3]
sage: K.<a> = GF(2^3)
sage: m,n = 3, 5
sage: MS = MatrixSpace(K,m,n)
sage: A = random_matrix(K, 3, 5)
sage: copy(A).echelon_form('newton_john')
[ 1 0 a + 1 0 a^2 + 1]
[ 0 1 a^2 + a + 1 0 a]
[ 0 0 0 1 a^2 + a + 1]
sage: copy(A).echelon_form('naive')
[ 1 0 a + 1 0 a^2 + 1]
[ 0 1 a^2 + a + 1 0 a]
[ 0 0 0 1 a^2 + a + 1]
sage: copy(A).echelon_form('builtin')
[ 1 0 a + 1 0 a^2 + 1]
[ 0 1 a^2 + a + 1 0 a]
[ 0 0 0 1 a^2 + a + 1]
"""
if self._nrows == 0 or self._ncols == 0:
self.cache('in_echelon_form',True)
self.cache('rank', 0)
self.cache('pivots', [])
return self
cdef int k, n, full
full = int(reduced)
x = self.fetch('in_echelon_form')
if not x is None: return # already known to be in echelon form
self.check_mutability()
self.clear_cache()
if algorithm == 'naive':
sig_on()
r = mzed_echelonize_naive(self._entries, full)
sig_off()
elif algorithm == 'newton_john':
sig_on()
r = mzed_echelonize_newton_john(self._entries, full)
sig_off()
elif algorithm == 'ple':
sig_on()
r = mzed_echelonize_ple(self._entries, full)
sig_off()
elif algorithm == 'heuristic':
sig_on()
r = mzed_echelonize(self._entries, full)
sig_off()
elif algorithm == 'builtin':
self._echelon_in_place(algorithm="classical")
else:
raise ValueError("No algorithm '%s'."%algorithm)
self.cache('in_echelon_form',True)
self.cache('rank', r)
self.cache('pivots', self._pivots())
def _pivots(self):
"""
EXAMPLES::
sage: K.<a> = GF(2^8)
sage: A = random_matrix(K, 15, 15)
sage: A.pivots() # indirect doctest
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
"""
if not self.fetch('in_echelon_form'):
raise ValueError("self must be in reduced row echelon form first.")
pivots = []
cdef Py_ssize_t i, j, nc
nc = self._ncols
i = 0
while i < self._nrows:
for j from i <= j < nc:
if self.get_unsafe(i,j):
pivots.append(j)
i += 1
break
if j == nc:
break
return pivots
def __invert__(self):
"""
EXAMPLES::
sage: K.<a> = GF(2^3)
sage: A = random_matrix(K,3,3); A
[ a^2 a + 1 a^2 + a + 1]
[ a + 1 0 1]
[ a + 1 a^2 + 1 a + 1]
sage: B = ~A; B
[a^2 + a + 1 a^2 a^2]
[ a + 1 a^2 + a + 1 a + 1]
[ a a^2 + a a^2 + a + 1]
sage: A*B
[1 0 0]
[0 1 0]
[0 0 1]
"""
cdef Matrix_gf2e_dense A
A = Matrix_gf2e_dense.__new__(Matrix_gf2e_dense, self._parent, 0, 0, 0)
if self.rank() != self._nrows:
raise ZeroDivisionError("Matrix does not have full rank.")
if self._nrows:
sig_on()
mzed_invert_newton_john(A._entries, self._entries)
sig_off()
return A
cdef rescale_row_c(self, Py_ssize_t row, multiple, Py_ssize_t start_col):
"""
Return ``multiple * self[row][start_col:]``
INPUT:
- ``row`` - row index for row to rescale