You can work interactively with Python using:
See this tutorial Tutorial for an introduction to interactive Python. This is a syntetic tutorial only about the tools that can be useful for the digital tests during the course.
Copy this preamble in your script to load the needed modules and implement a function printm
and verbatim
for pretty printing. (You can also put the code in a file, for example, utils.py
and import that file from your script from utils import *
or, if you are using Jupyter, using the magic function: %run utils.py
.
from numpy import *
from fractions import Fraction as f
set_printoptions(precision=3,suppress=True)
def printm(a):
"""Prints the array as strings
:a: numpy array
:returns: prints the array
"""
def p(x):
return str(x)
p = vectorize(p,otypes=[str])
print(p(a))
def tableau(a,W=7):
"""Returns a string for verbatim printing
:a: numpy array
:returns: a string
"""
if len(a.shape) != 2:
raise ValueError('verbatim displays two dimensions')
rv = []
rv+=[r'|'+'+'.join('{:-^{width}}'.format('',width=W) for i in range(a.shape[1]))+"+"]
rv+=[r'|'+'|'.join(map(lambda i: '{0:>{width}}'.format("x"+str(i+1)+" ",width=W), range(a.shape[1]-2)) )+"|"+
'{0:>{width}}'.format("-z ",width=W)+"|"
'{0:>{width}}'.format("b ",width=W)+"|"]
rv+=[r'|'+'+'.join('{:-^{width}}'.format('',width=W) for i in range(a.shape[1]))+"+"]
for i in range(a.shape[0]-1):
rv += [r'| '+' | '.join(['{0:>{width}}'.format(str(a[i,j]),width=W-2) for j in range(a.shape[1])])+" |"]
rv+=[r'|'+'+'.join('{:-^{width}}'.format('',width=W) for i in range(a.shape[1]))+"+"]
i = a.shape[0]-1
rv += [r'| '+' | '.join(['{0:>{width}}'.format(str(a[i,j]),width=W-2) for j in range(a.shape[1])])+" |"]
rv+=[r'|'+'+'.join('{:-^{width}}'.format('',width=W) for i in range(a.shape[1]))+"+"]
print('\n'.join(rv))
To do elementary row operations on a matrix one can do as follows:
A = array( [[1, f(9,4), 2, f(4,5)],
[f(3,4), 1, f(5,4), 0]])
A[0,:] = f(4,9) * A[0,:]
printm(A)
tableau(A)
Note that indices start from 0, hence a vector of size $n$ has indices from $0$ to $n-1$.
zeros((3,1))
A_1 = concatenate([A,identity(2)],axis=1)
printm(A_1)
A_2 = concatenate([ A[0:1,:], array( [[0,2,1,0]] ), A[1:2,:] ],axis=0)
printm(A_2)
A.shape
The operations +,*,/,-
are elementwise. For the operations of scalar multiplication and matrix multiplication the dot
method must be used. Compare the following two cells:
A = ones((4,2)) # a matrix of 1s of size 4x2
b = array([1,2])
A*b
which is element-wise and hence not matrix-vector multiplication. The correct way to do matrix-vector and matrix-matrix multiplication is:
dot(A,b)
A = random.randint(1,10,size=(4,4))
A_i = linalg.inv(A)
around( dot(A,A_i), decimals=2) # let's verify
A.T
linalg.matrix_rank(A)
You should calculate the reduced row echelon form by applying elementary row operations
A=array([[f(3,1),1,1,1],
[2,4,1,1],
[2,1,2,2]])
b=array([2,2,1])
AA=column_stack([A,b])
AA[0,:] = f(1,3) * AA[0,:] # remember indices start from 0
AA[1,:] = -2 * AA[0,:] + AA[1,:]
AA[2,:] = -2 * AA[0,:] + AA[2,:]
printm(AA)
AA[1,:] = f(3,10)* AA[1,:]
AA[2,:] += -f(1,3) * AA[1,:]
printm(AA)
AA[2,:] = f(10,13) * AA[2,:]
printm(AA)
AA[1,:] += -f(1,10) * AA[2,:]
AA[0,:] += -f(1,3) * AA[2,:]
printm(AA)
AA[0,:] += -f(1,3) * AA[1,:]
printm(AA)
You can check your result by importing the module for symbolic computation sympy
. You are however discouraged to use this for your calculations:
import sympy as sy
# np.linalg.solve(A,b)
sy.Matrix(AA).rref()