-
Notifications
You must be signed in to change notification settings - Fork 0
/
integer_rref.py
2748 lines (2416 loc) · 57.5 KB
/
integer_rref.py
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
#! /usr/bin/env python3
#
def i4_gcd ( i, j ):
#*****************************************************************************80
#
## i4_gcd() finds the greatest common divisor of I and J.
#
# Discussion:
#
# Only the absolute values of I and J are
# considered, so that the result is always nonnegative.
#
# If I or J is 0, i4_gcd is returned as max ( 1, abs ( I ), abs ( J ) ).
#
# If I and J have no common factor, i4_gcd is returned as 1.
#
# Otherwise, using the Euclidean algorithm, i4_gcd is the
# largest common factor of I and J.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 04 April 2013
#
# Author:
#
# John Burkardt
#
# Input:
#
# integer I, J, two numbers whose greatest common divisor
# is desired.
#
# Output:
#
# integer VALUE, the greatest common divisor of I and J.
#
value = 1
#
# Return immediately if either I or J is zero.
#
if ( i == 0 ):
value = max ( 1, abs ( j ) )
return value
elif ( j == 0 ):
value = max ( 1, abs ( i ) )
return value
#
# Set IP to the larger of I and J, IQ to the smaller.
# This way, we can alter IP and IQ as we go.
#
ip = max ( abs ( i ), abs ( j ) )
iq = min ( abs ( i ), abs ( j ) )
#
# Carry out the Euclidean algorithm.
#
while ( True ):
ir = ( ip % iq )
if ( ir == 0 ):
break
ip = iq
iq = ir
value = iq
return value
def i4_gcd_test ( ):
#*****************************************************************************80
#
## i4_gcd_test() tests i4_gcd().
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 09 May 2013
#
# Author:
#
# John Burkardt
#
import platform
test_num = 7
i_test = [ 36, 49, 0, 12, 36, 1, 91 ]
j_test = [ 30, -7, 71, 12, 49, 42, 28 ]
print ( '' )
print ( 'i4_gcd_test' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' i4_gcd computes the greatest common factor' )
print ( '' )
print ( ' I J i4_gcd' )
print ( '' )
for test in range ( 0, test_num ):
i = i_test[test]
j = j_test[test]
k = i4_gcd ( i, j )
print ( ' %6d %6d %6d' % ( i, j, k ) )
#
# Terminate.
#
print ( '' )
print ( 'i4_gcd_test' )
print ( ' Normal end of execution' )
return
def i4mat_is_integer ( m, n, a ):
#*****************************************************************************80
#
## i4mat_is_integer is TRUE if all entries of an I4MAT are integer.
#
# Discussion:
#
# An I4MAT is an MxN array of I4's.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 20 August 2018
#
# Author:
#
# John Burkardt
#
# Input:
#
# integer M, N, the number of rows and columns.
#
# integer A(M,N), the array.
#
# Output:
#
# bool VALUE, is true if all entries are integer.
#
for i in range ( 0, m ):
for j in range ( 0, n ):
if ( a[i,j] != round ( a[i,j] ) ):
return False
return True
def i4mat_is_integer_test ( ):
#*****************************************************************************80
#
## i4mat_is_integer_test() tests i4mat_is_integer.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 19 August 2018
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
print ( '' )
print ( 'i4mat_is_integer_test' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' i4mat_is_integer is TRUE if every entry of an I4MAT' )
print ( ' is an integer.' )
print ( '' )
print ( ' Example 1: Obviously integer:' )
print ( '' )
m = 2
n = 3
a = np.array ( [ \
[ 1, 2, 3 ], \
[ 4, 5, 6 ] ] )
print ( a )
if ( i4mat_is_integer ( m, n, a ) ):
print ( ' A is an integer matrix.' )
else:
print ( ' A is NOT an integer matrix.' )
print ( '' )
print ( ' Example 2: Obviously NOT integer:' )
print ( '' )
m = 2
n = 3
a = np.array ( [ \
[ 1, 2, 3 ], \
[ 4, 5, 6.5 ] ] )
print ( a )
if ( i4mat_is_integer ( m, n, a ) ):
print ( ' A is an integer matrix.' )
else:
print ( ' A is NOT an integer matrix.' )
print ( '' )
print ( ' Example 3: Not Integer, Not obvious:' )
print ( '' )
m = 2
n = 3
a = np.array ( [ \
[ 1, 2, 3 ], \
[ 4, 5.000000001, 6 ] ] )
print ( a )
if ( i4mat_is_integer ( m, n, a ) ):
print ( ' A is an integer matrix.' )
else:
print ( ' A is NOT an integer matrix.' )
print ( '' )
print ( ' Example 4: Not Integer, Not obvious:' )
print ( '' )
m = 2
n = 3
a = np.array ( [ \
[ 1.0, 2, 300000000.2 ], \
[ 4, 5, 6 ] ] )
print ( a )
if ( i4mat_is_integer ( m, n, a ) ):
print ( ' A is an integer matrix.' )
else:
print ( ' A is NOT an integer matrix.' )
#
# Terminate.
#
print ( '' )
print ( 'i4mat_is_integer_test' )
print ( ' Normal end of execution.' )
return
def i4mat_print ( m, n, a, title ):
#*****************************************************************************80
#
## i4mat_print prints an I4MAT.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 12 October 2014
#
# Author:
#
# John Burkardt
#
# Input:
#
# integer M, the number of rows in A.
#
# integer N, the number of columns in A.
#
# integer A(M,N), the matrix.
#
# string TITLE, a title.
#
i4mat_print_some ( m, n, a, 0, 0, m - 1, n - 1, title )
def i4mat_print_test ( ):
#*****************************************************************************80
#
## i4mat_print_test() tests i4mat_print.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 25 May 2015
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
print ( '' )
print ( 'i4mat_print_test:' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' Test i4mat_print, which prints an I4MAT.' )
m = 5
n = 6
a = np.array ( ( \
( 11, 12, 13, 14, 15, 16 ), \
( 21, 22, 23, 24, 25, 26 ), \
( 31, 32, 33, 34, 35, 36 ), \
( 41, 42, 43, 44, 45, 46 ), \
( 51, 52, 53, 54, 55, 56 ) ) )
title = ' A 5 x 6 integer matrix:'
i4mat_print ( m, n, a, title )
#
# Terminate.
#
print ( '' )
print ( 'i4mat_print_test:' )
print ( ' Normal end of execution.' )
return
def i4mat_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ):
#*****************************************************************************80
#
## i4mat_print_some prints out a portion of an I4MAT.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 08 September 2018
#
# Author:
#
# John Burkardt
#
# Input:
#
# integer M, N, the number of rows and columns of the matrix.
#
# integer A(M,N), an M by N matrix to be printed.
#
# integer ILO, JLO, the first row and column to print.
#
# integer IHI, JHI, the last row and column to print.
#
# string TITLE, a title.
#
incx = 10
print ( '' )
print ( title )
if ( m <= 0 or n <= 0 ):
print ( '' )
print ( ' (None)' )
return
for j2lo in range ( max ( jlo, 0 ), min ( jhi + 1, n ), incx ):
j2hi = j2lo + incx - 1
j2hi = min ( j2hi, n )
j2hi = min ( j2hi, jhi )
print ( '' )
print ( ' Col: ', end = '' )
for j in range ( j2lo, j2hi + 1 ):
print ( '%7d ' % ( j ), end = '' )
print ( '' )
print ( ' Row' )
i2lo = max ( ilo, 0 )
i2hi = min ( ihi, m )
for i in range ( i2lo, i2hi + 1 ):
print ( ' %4d: ' % ( i ), end = '' )
for j in range ( j2lo, j2hi + 1 ):
print ( '%7d ' % ( a[i,j] ), end = '' )
print ( '' )
return
def i4mat_print_some_test ( ):
#*****************************************************************************80
#
## i4mat_print_some_test() tests i4mat_print_some.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 31 October 2014
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
print ( '' )
print ( 'i4mat_print_some_test' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' i4mat_print_some prints some of an I4MAT.' )
m = 4
n = 6
v = np.array ( [ \
[ 11, 12, 13, 14, 15, 16 ],
[ 21, 22, 23, 24, 25, 26 ],
[ 31, 32, 33, 34, 35, 36 ],
[ 41, 42, 43, 44, 45, 46 ] ], dtype = np.int32 )
i4mat_print_some ( m, n, v, 0, 3, 2, 5, ' Here is I4MAT, rows 0:2, cols 3:5:' )
#
# Terminate.
#
print ( '' )
print ( 'i4mat_print_some_test:' )
print ( ' Normal end of execution.' )
return
def i4mat_ref ( m, n, a ):
#*****************************************************************************80
#
## i4mat_ref computes the integer row echelon form (IREF) of an I4MAT.
#
# Discussion:
#
# If a matrix A contains only integer entries, then when it is reduced
# to row echelon form, it is likely that many entries will no longer
# be integers, due to the elimination process.
#
# In some cases, tiny arithmetic errors in this elimination process can
# result in spurious, tiny nonzero values which can invalidate the
# calculation, particular if the elimination is being done in an effort
# to determine the rank of the matrix. These serious errors can easily
# occur in very small matrices, such as of size 7x10.
#
# If we, instead, insist on using only integer operations on an integer
# matrix, we can guarantee that tiny roundoff errors will not cause
# such problems. On the other hand, as the elimination process proceeds,
# we may instead calculate integer matrix entries of increasingly
# large, and then ultimately meaningless magnitude. I imagine this is
# likely to happen for moderate size matrices of order 50x50, say, but
# this is a huge improvement over the unreliability of the real
# arithmetic case.
#
#
# Thus, we define "integer row echelon form" (IREF).
#
#
# A matrix is in integer row echelon form if:
#
# * The leading nonzero in each row is positive.
#
# * Each row has no common factor greater than 1.
#
# * The leading nonzero in each row occurs in a column to
# the right of the leading nonzero in the previous row.
#
# * Rows which are entirely zero occur last.
#
# Example:
#
# Input matrix:
#
# 1 3 0 2 6 3 1
# -2 -6 0 -2 -8 3 1
# 3 9 0 0 6 6 2
# -1 -3 0 1 0 9 3
#
# Output matrix:
#
# 1 3 0 2 6 3 1
# 0 0 0 2 4 9 3
# 0 0 0 0 0 3 1
# 0 0 0 0 0 0 0
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 28 August 2018
#
# Author:
#
# John Burkardt
#
# Reference:
#
# Charles Cullen,
# An Introduction to Numerical Linear Algebra,
# PWS Publishing Company, 1994,
# ISBN: 978-0534936903,
# LC: QA185.D37.C85.
#
# Input:
#
# integer M, N, the number of rows and columns.
#
# integer A(M,N), the matrix to be analyzed.
#
# Output:
#
# integer A(M,N), the IREF of the matrix.
#
# integer DET, the pseudo-determinant of the REF.
#
if ( not i4mat_is_integer ( m, n, a ) ):
print ( '' )
print ( 'i4mat_ref - Fatal error!' )
print ( ' Input matrix A is not integral.' )
raise Exception ( 'i4mat_ref - Fatal error!' )
lead = 0
det = 1
for r in range ( 0, m ):
if ( n <= lead ):
break
#
# Start I at row R, and search for nonzero pivot entry A(I,LEAD).
#
i = r
while ( a[i,lead] == 0.0 ):
i = i + 1
#
# If reach last row, reset I to R, and increment LEAD.
#
if ( m <= i ):
i = r
lead = lead + 1
#
# If reach last column, we can find no more pivots.
#
if ( n <= lead ):
lead = -1
break
if ( lead < 0 ):
break
#
# Move pivot I into row R.
#
if ( i != r ):
i4mat_row_swap ( m, n, a, i, r )
#
# Ensure pivot is positive.
#
if ( a[r,lead] < 0 ):
a[r,0:n] = - a[r,0:n]
det = - det
#
# Update the pseudo-determinant.
#
det = det * a[r,lead]
#
# Remove any common factor from row R.
#
a[r,0:n], ifact = i4vec_red ( n, a[r,0:n], 1 )
#
# Use a multiple of A(R,LEAD) to eliminate A(R+1:M,LEAD).
#
for i in range ( r + 1, m ):
a[i,0:n] = a[r,lead] * a[i,0:n] - a[i,lead] * a[r,0:n]
a[i,0:n], ifact = i4vec_red ( n, a[i,0:n], 1 )
lead = lead + 1
return a, det
def i4mat_ref_test ( ):
#*****************************************************************************80
#
## i4mat_ref_test() tests i4mat_ref.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 18 August 2018
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
m = 4
n = 7
a = np.array ( [ \
[ 1, 3, 0, 2, 6, 3, 1 ], \
[ -2, -6, 0, -2, -8, 3, 1 ], \
[ 3, 9, 0, 0, 6, 6, 2 ], \
[ -1, -3, 0, 1, 0, 9, 3 ] ] )
print ( '' )
print ( 'i4mat_ref_test' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' i4mat_ref computes the integer row echelon form of an I4MAT.' )
i4mat_print ( m, n, a, ' Input A:' )
a, det = i4mat_ref ( m, n, a )
print ( '' )
print ( ' The pseudo-determinant = %d' % ( det ) )
i4mat_print ( m, n, a, ' IREF of A:' )
#
# Terminate.
#
print ( '' )
print ( 'i4mat_ref_test' )
print ( ' Normal end of execution.' )
return
def i4mat_row_swap ( m, n, a, i1, i2 ):
#*****************************************************************************80
#
## i4mat_row_swap swaps rows in an I4MAT.
#
# Discussion:
#
# Because Python/Numpy makes it fiendishly difficult to do simple things.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 30 August 2018
#
# Author:
#
# John Burkardt
#
# Input:
#
# integer M, N, the number of rows and columns.
#
# integer A(M,N), the matrix to be flipped.
#
# integer I1, I2, the indices of the rows.
# 0 <= I1, I2 < M.
#
# Output:
#
# integer B(M,N), the flipped matrix.
#
if ( i1 != i2 ):
for j in range ( 0, n ):
t = a[i1,j]
a[i1,j] = a[i2,j]
a[i2,j] = t
return a
def i4mat_row_swap_test ( ):
#*****************************************************************************80
#
## i4mat_row_swap_test() tests i4mat_row_swap.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 16 April 2018
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
print ( '' )
print ( 'i4mat_row_swap_test:' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' i4mat_row_swap swaps two rows in an I4MAT.' )
m = 6
n = 5
a = np.zeros ( [ m, n ] )
for i in range ( 0, m ):
for j in range ( 0, n ):
a[i,j] = 10 * ( i + 1 ) + ( j + 1 )
i4mat_print ( m, n, a, ' The original matrix:' )
i1 = 1
i2 = 4
a2 = i4mat_row_swap ( m, n, a, i1, i2 )
i4mat_print ( m, n, a2, ' After swapping rows 1 and 4:' )
#
# Terminate.
#
print ( '' )
print ( 'i4mat_row_swap_test:' )
print ( ' Normal end of execution.' )
return
def i4mat_rref ( m, n, a ):
#*****************************************************************************80
#
## i4mat_rref computes the reduced row echelon form of an I4MAT.
#
# Discussion:
#
# If a matrix A contains only integer entries, then when it is transformed
# to row reduced echelon form, it is likely that many entries will no longer
# be integers, due to the elimination process.
#
# In some cases, tiny arithmetic errors in this elimination process can
# result in spurious, tiny nonzero values which can invalidate the
# calculation, particular if the elimination is being done in an effort
# to determine the rank of the matrix. These serious errors can easily
# occur in very small matrices, such as of size 7x10.
#
# If we, instead, insist on using only integer operations on an integer
# matrix, we can guarantee that tiny roundoff errors will not cause
# such problems. On the other hand, as the elimination process proceeds,
# we may instead calculate integer matrix entries of increasingly
# large, and then ultimately meaningless magnitude. I imagine this is
# likely to happen for moderate size matrices of order 50x50, say, but
# this is a huge improvement over the unreliability of the real
# arithmetic case.
#
#
# Thus, we define "integer row reduced echelon form" (IRREF):
#
#
# A matrix is in integer row reduced echelon form if:
#
# * The leading nonzero in each row is positive.
#
# * Each row has no common factor greater than 1.
#
# * The leading nonzero in each row occurs in a column to
# the right of the leading nonzero in the previous row.
#
# * Rows which are entirely zero occur last.
#
# * When a row contains a leading nonzero in column J, then column J
# is otherwise entirely zero.
#
# Example:
#
# Input matrix:
#
# 1 3 0 2 6 3 1
# -2 -6 0 -2 -8 3 1
# 3 9 0 0 6 6 2
# -1 -3 0 1 0 9 3
#
# Output matrix:
#
# 1 3 0 0 2 0 0
# 0 0 0 1 2 0 0
# 0 0 0 0 0 3 1
# 0 0 0 0 0 0 0
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 30 August 2018
#
# Author:
#
# John Burkardt
#
# Reference:
#
# Charles Cullen,
# An Introduction to Numerical Linear Algebra,
# PWS Publishing Company, 1994,
# ISBN: 978-0534936903,
# LC: QA185.D37.C85.
#
# Input:
#
# integer M, N, the number of rows and columns.
#
# integer A(M,N), the matrix to be analyzed.
#
# Output:
#
# integer A(M,N), the IRREF of the matrix.
#
# integer DET, the pseudo-determinant.
#
if ( not i4mat_is_integer ( m, n, a ) ):
print ( '' )
print ( 'i4mat_ref - Fatal error!' )
print ( ' Input matrix A is not integral.' )
raise Exception ( 'i4mat_ref - Fatal error!' )
lead = 0
det = 1
for r in range ( 0, m ):
if ( n <= lead ):
break
#
# Start I at row R, and search for nonzero pivot entry A(I,LEAD).
#
i = r
while ( a[i,lead] == 0.0 ):
i = i + 1
#
# If reach last row, reset I to R, and increment LEAD.
#
if ( m <= i ):
i = r
lead = lead + 1
#
# If reach last column, we can find no more pivots.
#
if ( n <= lead ):
lead = -1
break
if ( lead < 0 ):
break
#
# Move pivot I into row R.
#
if ( i != r ):
i4mat_row_swap ( m, n, a, i, r )
#
# Ensure pivot is positive.
#
if ( a[r,lead] < 0 ):
a[r,0:n] = - a[r,0:n]
det = - det
#
# Update the pseudo-determinant.
#
det = det * a[r,lead]
#
# Remove any common factor from row R.
#
a[r,0:n], ifact = i4vec_red ( n, a[r,0:n], 1 )
#
# Use a multiple of A(R,LEAD) to eliminate A(R+1:M,LEAD).
#
for i in range ( 0, m ):
if ( i != r ):
a[i,0:n] = a[r,lead] * a[i,0:n] - a[i,lead] * a[r,0:n]
a[i,0:n], ifact = i4vec_red ( n, a[i,0:n], 1 )
lead = lead + 1
return a, det
def i4mat_rref_test ( ):
#*****************************************************************************80
#
## i4mat_rref_test() tests i4mat_rref.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 21 August 2018
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
m = 4
n = 7
a = np.array ( [ \
[ 1, 3, 0, 2, 6, 3, 1 ], \
[ -2, -6, 0, -2, -8, 3, 1 ], \
[ 3, 9, 0, 0, 6, 6, 2 ], \
[ -1, -3, 0, 1, 0, 9, 3 ] ] )
print ( '' )
print ( 'i4mat_rref_test' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' i4mat_rref computes the integer reduced row echelon form (IREF)' )
print ( ' of an I4MAT.' )
i4mat_print ( m, n, a, ' Input A:' )
a, det = i4mat_rref ( m, n, a )
print ( '' )
print ( ' The pseudo-determinant = %d' % ( det ) )
i4mat_print ( m, n, a, ' IRREF form:' )
#
# Terminate.
#
print ( '' )
print ( 'i4mat_rref_test' )
print ( ' Normal end of execution.' )
return
def i4mat_rref_solve_binary_nz ( m, n, nz, a, b ):
#*****************************************************************************80
#
## i4mat_rref_solve_binary_nz seeks binary solutions of an IRREF system.
#
# Discussion:
#
# An MxN linear system A*x = b is considered.
#
# The matrix A and right hand side B are assumed to have been converted
# to integer row-reduced echelon form (IRREF).
#
# In order to solve a particular combinatorial problem, only binary
# solutions x are of interest that is, each entry of x is either 0 or 1.
#
# Moreover, we know that exactly NZ of the variables are 1.
#
# The solution procedure involves two steps:
# * assign each free variable a value of 0 or 1, but never assign more
# that NZ nonzeroes
# * solve for the dependent variables.
#
# We consider every possible assignment of free variables, and we save
# the solutions in which all the variables take on only 0 or 1 values.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 09 September 2018
#
# Author:
#
# John Burkardt
#
# Input:
#
# integer M, N, the number of rows and columns.
#
# integer NZ, the number of nonzeros required in any binary solution.
#
# real A(M,N), the IRREF matrix to be analyzed.
#
# real B(M), the right hand side.
#
# Output: