def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' # Subset basis is not unique. Depends on where one starts. # For example the above problems also has a basis given by # >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1}), Vec({'c', 'b', 'a', 'd'},{'a':1, 'c': 3})] inT = T[:] for i in range(len(list(inT))): if is_superfluous (inT, i): del(inT[i]) break return inT
def findbasis(V): while not hw4.is_independent(V): for i in range(len(V)): if hw4.is_superfluous(V,i): V.remove(V[i]) break return V
def findbasis(V): while not hw4.is_independent(V): for i in range(len(V)): if hw4.is_superfluous(V, i): V.remove(V[i]) break return V
def rec_shrink_basis(S, i): if i == len(S): return S if is_superfluous(S,i): S.pop(i) else: i=i+1 return rec_shrink_basis(S,i)
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' S = list(T) for i, v in enumerate(S): if is_superfluous(S, i): S.remove(v) return S
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' S = list(T) for i,v in enumerate(S): if is_superfluous(S, i): S.remove(v) return S
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' from hw4 import is_independent,is_superfluous T_temp = list(T) if not is_independent(T): for i in range(len(T)-1,-1,-1): if is_superfluous(T_temp,i): T_temp.remove(T[i]) return T_temp
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' from hw4 import is_independent, is_superfluous T_temp = list(T) if not is_independent(T): for i in range(len(T) - 1, -1, -1): if is_superfluous(T_temp, i): T_temp.remove(T[i]) return T_temp
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' S = [] for i, l in enumerate(T): S.append(l) if hw4.is_superfluous(S, len(S)-1): S.remove(l) return S
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' S = [] for i, l in enumerate(T): S.append(l) if hw4.is_superfluous(S, len(S) - 1): S.remove(l) return S
def subset_basis(T): ''' input: A list, T, of Vecs output: A list, S, containing Vecs from T, that is a basis for the space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1}) >>> a1 = Vec({'a','b','c','d'}, {'b':1}) >>> a2 = Vec({'a','b','c','d'}, {'c':1}) >>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3}) >>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})] True ''' from hw4 import is_superfluous i = 0 # initializing index for list T S = T.copy() # creating copy of T while i < len(S): if is_superfluous(S, i): S = S[:i] + S[i + 1:] #remove entry i else: i += 1 return S