Python: Simple matrix library matlib.py now at version 0.0.3

Our simple matrix library is now at version 0.0.3 with new functions!

?View Code PYTHON
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
# -*- coding: utf-8 -*-
 
"""
file      matlib.py
 
author    Ernesto P. Adorio
          UPDEPP (UP Clarkfield) 
          ernesto.adorio@gmail.com
 
revisions 
    Ver 0.0.1 initial release
               2009.01.16 added matdots,matrandom, isiterable
    Ver 0.0.2  2009.10.12 added vec2colmat,vec2rowmat,  mat2vec, matSelectCols,
                          matDelCols, matInsertConstCol,matreadstring
    Ver 0.0.3  2009.12.22-25 added matSubmat, matRows, matCols,
                          matmatt, matxtx, matvectmatvec,matsplitbycolumn
                          revised matvec
"""
 
 
def matreadstring(s, sep = " "):
    """
    Each line must be complete row.
    comment lines start with a #.
    version 0.0.1
    no varnames yet!
    """
    M = []
    for line in s.split("\n"):
       if line:
          if not line.startswith("#"):
             M.append([float(x) for x in (line.split(sep))])
    return M
 
def matsplitbycolumn(X, col=-1):
    """
    Splits a matrix X by column.
    """ 
    if col == -1 or col == len(X[0])-1:
       # X2 will be a vector not a matrix.
       X1 = [x[:-1] for x in X]
       X2 = [x[-1] for x in X]
    else:
       #X2 is a matrix.
       X1 = [x[:col] for x in X]
       X2 = [X[col:-1] for x in X]
    return X1, X2
 
 
def tabular(hformat, headers, bformat, M):
    # added nov.29, 2008.
    # prints the table data.
    nrows = len(M)
    ncols = len(M[0])
 
    # print headers.
    for j, heading in enumerate(headers):
        print hformat[j] % heading, 
    print
 
    # print the body.
    for i, row in enumerate(M):
        for j, col in enumerate(M[i]):
            print bformat[j] % M[i][j],
        print
    print
 
def vecadd(X,Y):
    n = len(X)
    if n != len(Y): 
       return None
    return [x + y for x,y in zip(X,Y)]
 
def vecsub(X, Y):
    n = len(X)
    if n != len(Y): 
       raise ArgumentError, "incompatible vectors in vecsub."
    return [x - y for x,y in zip(X,Y)]
 
def eye(m, n= None):
    if n is None:
        n = m
    B= [[0]* n for i in range(m)]
    for i in range(m):
        B[i][i] = 1.0
    return B
matiden = eye
 
 
def vec2colmat(X):
    """
    Retuns a 1 column matrix out of array or vector X.
    """
    return ([[x] for x in X])
 
def vec2rowmat(X):
    """
    Retuns a 1 row matrix out of array or vector X.
    """
 
    return ([[x for x in X]])
 
def mat2vec(M, column=0):
    """ 
    Returns a vector from a column of matrix M.
    """
    try:
      M[0][column]
      return [m[column] for m in M]
    except:
      return M
 
def matvectmatvec(x,M):
    """
    Computes x^t M x where x is a column vector.
    Result is a scalar.
    """
    return dot(x,matvec(M,x))
 
 
 
def matSelectCols(M, jindices):
    """
    Extracts a submatrix from M with col indices in jindices.
    Negative indices are properly handled too by Python, no need for
    adjustment.
    """
    N = []
    n = len(M)
    for i in range(n):
        N.append([M[i][j] for j in jindices])
    return N  
 
matCols = matSelectCols
 
def matRows(M, iindices):
    """
    Returns selected rews of M as a matrix.
    """
    return [M[i:] for i in iindices]
 
def matDelCols(M, colindices):
    """
    M - input matrix
    colindices = array indices of columns to delete.
    Returns matrix from M with columns deleted.
    """
 
    ncols    = len(M[0])
    nindices = len(colindices)
    # Adjust for negative indices.
    for j in range(nindices):
        if colindices[j] < 0:
           colindices[j] += ncols
 
    jindices = []
    for j in range(ncols):
        if j not in colindices:
           jindices.append(j)
    return matSelectCols(M, jindices)
 
def matSubmat(M, rindices, jindices):
    # Returns a submatrix selected from M.
    N = []
    for i in rindices:
        N.append([M[i][j] for j in jindices])
    return N
 
 
def matInsertConstCol(X, column, c = 1.0, inplace= True):
    """
    Inserts a constant column to vector or matrix X at position column.
    NEVER forget that indicing starts at zero.
    """
    if not inplace:
       Xcopy = [x[:] for x in X]
    else:
       Xcopy = X
    for i in range(len(X)):
       Xcopy[i].insert(column, c)
    return Xcopy
 
def matxtx(X):
    # returns the matrix of the coefficients of the
    # normal equations for least squares computation.
    # This should be more efficient than calling a 
    # matrix multiplication on t(X) and X.
    # same as function call mattmat(X, X)
    m = len(X)
    n = len(X[0])
    M = [[0.0]* n for i in range(n)]
    for i in range(n):
        for j in range(i, n):
            dot = 0.0
            for r  in range(m):
                dot += X[r][i] * X[r][j]
            M[i][j] = dot
            if i != j:
               M[j][i] = dot
    return M 
 
 
 
def matzero(m, n = None):
    """
    Returns an m by n zero matrix.
    """
    if n is None:
        n = m
    return [[0]* n for i in range(m)]
 
def matdiag(D):
    """
    Returns a diagonal matrix with diagonal elements
    from D.
    """
    n = len(D)
    A = [[0] * n for i in range(n)]
    for i in range(n):
        A[i][i] = D[i]
    return A
 
def diag(A):
    """
    Returns diagonal elements of A as a matrix
    with 1 column
    """
    return [[A[i][i]] for i in range(len(A))]
 
    # Here is a version which returns a vector.
    # return A[i][i] for i in rang(len(A))
 
def matcol(X, j):
    # Returns the jth column of matrix X.
    nrows = len(X)
    return [X[i][j] for i in range(nrows)]
 
def trace(A):
    """
    Returns the trace of a matrix.
    """
    return sum([A[i][i] for i in range(len(A))])
 
 
def matadd(A, B):
    """
    Returns C = A + B.
    """
    try:
        m = len(A)
        if m != len(B):
            return None
        n = len(A[0])
        if n != len(B[0]):
            return None
        C = matzero(m, n)
        for i in range(m):
            for j in range(n):
                C[i][j] = A[i][j] + B[i][j]
        return C
 
    except:
        return None
 
 
def matsub(A, B):
    """
    returns C = A - B.
    """
    try:
        m = len(A)
        if m != len(B):
            return None
        n = len(A[0])
        if n != len(B[0]):
            return None
        C = matzero(m, n)
        for i in range(m):
            for j in range(n):
                C[i][j] = A[i][j] - B[i][j]
        return C
 
    except:
        return None
 
def matcopy(A):
    B = []
    for a in A:
       B.append(a[:])
    return B
 
def matkmul(A, k):
    """
    Multiplies each element of A by k.
    """
    B = matcopy(A) 
    for i in range(len(A)):
        for j in range(len(A[0])):
            B[i][j] *= k
    return B
 
 
def transpose(A):
    """
    Returns the transpose of A.
    """
    m,n = matdim(A)
    At = [[0] * m for j in range(n)]
    for i in range(m):
        for j in range(n):
            At[j][i] = A[i][j]
    m,n = matdim(At)
    return At
 
matt = transpose
mattrans = transpose
 
def matdim(A):
    # Returns the number of rows and columns of A.
    if hasattr(A, "__len__"):
       m = len(A)
       if hasattr(A[0], "__len__"):
          n = len(A[0])
       else:
          n = 0
    else:
       m = 0  # not a matrix!
       n = 0
    return (m, n)
 
def matprod(A, B):
    """
    Computes the product of two matrices.
    2009.01.16 Revised for matrix or vector B.
 
    A and B are matrices. If one of them is a vector,
    it must be transformed into a matrix with one row
    or one column.
    """
    m, n = matdim(A)
    p, q = matdim(B)
    if n!= p:
       return None
    try:
       if iter(B[0]):
          q = len(B[0])
    except:
       q = 1
    C = matzero(m, q)
    for i in range(m):
        for j in range(q):
            t = sum([A[i][k] * B[k][j] for k in range(p)])
            C[i][j] = t
    return C
 
matmul = matprod
 
def matvec(A, y):
    """
    Returns the product of matrix A with vector y.
    Revision:
       dec. 22, 2009: this version should work with one column matrices y.
    """
    m = len(A)
    n = len(A[0])
    try:
      y[0][0]
      out = [0] * m
      for i in range(m):
        for j in range(n):
            out[i] += A[i][j] * y[j][0]
      return out
    except:
      out = [0] * m
      for i in range(m):
        for j in range(n):
            out[i] += A[i][j] * y[j]
      return out
 
 
 
def mattvec(A, y):
    """
    Returns the vector A^t y.
    """
    At = transpose(A)
    return matvec(At, y)
 
def dot(X, Y):
    """
    Dot product of vectors X and Y.
    """
    return sum(x* y for (x,y) in zip(X,Y))
 
def matdots(X):
    # Added Jan 16, 2009.
    # Returns the matrix of dot products of the column vectors 
    # This is the same as X^t X.
    (nrow, ncol) = matdim(X)
    M = [[0.0] * ncol for i in range(ncol)]
    for i in range(ncol):
        for j in range(i+1):
            dot = sum([X[p][i]* X[p][j] for p in range(ncol)])
            M[i][j] = dot
            if i != j:
               M[j][i] = M[i][j]
    return M
 
 
def mattmat(A, B):
    """
    Returns the product [transpose(A) B]
    if B = A, use matxtx instead.
    """
    AtB = matprod(transpose(A), B)
    return AtB
 
def matmatt(A,B):
    """
    Returns A B^t
    added dec,22,2009
    """
    return matmul (A, mattrans(B))
 
 
def matrandom(nrow, ncol = None):
    # Added Jan. 16, 2009
    if ncol is None:
       ncol = nrow
    R = []
    for i in range(nrow):
        R.append([random.random() for j in range(ncol)])
    return R
 
def matunitize(X, inplace = False):
    # Added jan. 16, 2009
    # Transforms each vector in X to have unit length.
    if not inplace:
       V = [x[:] for x in X]
    else:
       V = X
    nrow = len(X)
    ncol = len(X[0])
    for j in range(ncol):
        recipnorm = sum([X[j][j]**2 for j in range(ncol)])
        for i in range(nrow):
            V[i][j] *= recipnorm
    return V
 
 
def matprint(A,format= "%8.4f"):
    #prints the matrix A using format
    if hasattr(A, "__len__"):
      for i,row in enumerate(A):
        try:
          if iter(row):        
             for c in row:
               print format % c,
             print 
        except:
           print row
    else:
        print "Not a matrix!"
    print # prints a blank line after matrix
 
 
def mataugprint(A,Y, format= "%8.4f"):
    #prints the augmented matrix A|Y using format
    try:
        ycols = len(Y[0])
    except:
        ycols = 1
    for i,row in enumerate(A):
        for c in row:
           print format % c,
        print "|",
        if ycols == 1:
           print format % Y[i]
        else:
           for y in Y[i]:
               print format % Y[i],
    print
 
def gjinv(AA,inplace = False):
    """
    Determines the inverse of a square matrix BB by Gauss-Jordan reduction.
    """
    n = len(AA)
    B = eye(n)
    if not inplace:
        A = [row[:] for row in AA]
    else:
        A = AA
 
    for i in range(n):
        #Divide the ith row by A[i][i]
        m = 1.0 / A[i][i]
        for j in range(i, n):
            A[i][j] *= m  # # this is the same as dividing by A[i][i]
        for j in range(n):
            B[i][j] *= m
 
        #lower triangular elements.
        for k in range(i+1, n):
            m = A[k][i] 
            for j in range(i+1, n):
                A[k][j] -= m * A[i][j]
            for j in range(n):
                B[k][j] -= m * B[i][j]
 
        #upper triangular elements.
        for k in range(0, i):
            m = A[k][i] 
            for j in range(i+1, n):
                A[k][j] -= m * A[i][j]
            for j in range(n):
                B[k][j] -= m * B[i][j]
    return B
 
matinverse = gjinv
matinv = gjinv
 
def Test():
    X = [1,1,1]
    print dot(X, X)
    AA = [[1,2,3],
          [4,5,8],
          [9,7,6]]
 
    BB = eye(3)
    print "Identity matrix eye(3):"
    matprint(BB)
 
    print "inputs:"
    print AA
 
    print "product"
    matprint(matprod(AA, AA))
 
    print "inverse of AA:"
    BB = gjinv(AA)
 
    matprint(BB)
    print "product of AA and its inverse:"
    matprint(matprod(AA ,BB))
 
if __name__ == "__main__":
    Test()

May we remind our readers that if you want the absolutely fastest , easiest to use matrix library nothing will beat numpy and scipy. The codes are absolutely for pedagogical purposes where the code is read to gain understanding on the computational details.

This version of the library is used in the previous diagnostic.py described in an earlier post.

2 Responses to “Python: Simple matrix library matlib.py now at version 0.0.3”

  1. Python, Econometrics: Diagnostic measures, studentized deleted residuals, dffits and Cook distances. · Digital explorations Says:

    [...] Python: Simple matrix library matlib.py now at version 0.0.3 [...]

  2. Solving least squares problem with the QR decomposition. · Digital explorations Says:

    [...] The matlib.py code can be copied from /p=4353 [...]

Leave a Reply