Coding
Matrix: Linear Algebra through Computer Science Applications
This week, there are only a few jobs. Only one programming task, hw2. the job is relatively simple. If you have studied matrix algebra in college, there is basically no problem, however, it should be noted that the method of span of base 2 is used.
In the base 2 space, take any number of base vectors, and overlay the combination to get the span. But how can we get any one? The following describes several methods.
One way is to use the arrangement and combination module in Python to generate all the arrays corresponding to the number to obtain the span. If you are interested, click Baidu. The concept of this method is clearer, but you need to know more about the python library.
The second method uses any of the n elements to have only 2 ^ n permutation and combination knowledge. Specifically, for any integer from 0 to 2 ^ n-1, the binary representation is obtained using the Bin () function. If the corresponding bit is 1, the base vector is selected. This method is a little more smart. However, the process is redundant.
The third method is the method provided by the following program. A row of statements can complete the operation. The principle is the same as that of the previous method, so we will not repeat it. However, when the result is 0, consider it separately.
Other vector operations are relatively simple. when determining whether a vector space is used, you only need to remember the three features of the vector space (including 0, addition, subtraction, and vector multiplication are still in the space) no error occurs.
The Job Code is as follows. The annotations in the template provide verification examples.
# version code 761# Please fill out this stencil and submit using the provided submission script.from vec import Vecfrom GF2 import one## Problem 1def vec_select(veclist, k): ''' >>> D = {'a','b','c'} >>> v1 = Vec(D, {'a': 1}) >>> v2 = Vec(D, {'a': 0, 'b': 1}) >>> v3 = Vec(D, { 'b': 2}) >>> v4 = Vec(D, {'a': 10, 'b': 10}) >>> vec_select([v1, v2, v3, v4], 'a') == [Vec(D,{'b': 1}), Vec(D,{'b': 2})] True ''' return [x for x in veclist if x[k]==0]def vec_sum(veclist, D): ''' >>> D = {'a','b','c'} >>> v1 = Vec(D, {'a': 1}) >>> v2 = Vec(D, {'a': 0, 'b': 1}) >>> v3 = Vec(D, { 'b': 2}) >>> v4 = Vec(D, {'a': 10, 'b': 10}) >>> vec_sum([v1, v2, v3, v4], D) == Vec(D, {'b': 13, 'a': 11}) True ''' return sum(veclist) if len(veclist)!=0 else Vec(D,{})def vec_select_sum(veclist, k, D): ''' >>> D = {'a','b','c'} >>> v1 = Vec(D, {'a': 1}) >>> v2 = Vec(D, {'a': 0, 'b': 1}) >>> v3 = Vec(D, { 'b': 2}) >>> v4 = Vec(D, {'a': 10, 'b': 10}) >>> vec_select_sum([v1, v2, v3, v4], 'a', D) == Vec(D, {'b': 3}) True ''' return vec_sum(vec_select(veclist,k),D)## Problem 2def scale_vecs(vecdict): ''' >>> v1 = Vec({1,2,3}, {2: 9}) >>> v2 = Vec({1,2,4}, {1: 1, 2: 2, 4: 8}) >>> scale_vecs({3: v1, 5: v2}) == [Vec({1,2,3},{2: 3.0}), Vec({1,2,4},{1: 0.2, 2: 0.4, 4: 1.6})] True ''' return [y/x for (x,y) in vecdict.items()]## Problem 3def GF2_span(D, L): ''' >>> from GF2 import one >>> D = {'a', 'b', 'c'} >>> L = [Vec(D, {'a': one, 'c': one}), Vec(D, {'b': one})] >>> len(GF2_span(D, L)) 4 >>> Vec(D, {}) in GF2_span(D, L) True >>> Vec(D, {'b': one}) in GF2_span(D, L) True >>> Vec(D, {'a':one, 'c':one}) in GF2_span(D, L) True >>> Vec(D, {x:one for x in D}) in GF2_span(D, L) True ''' if len(L)==0:return [] maxind=2**len(L)-1 res=[sum([L[j] for j in range(len(L)) if i//(2**j)%2]) for i in range(maxind+1)] res.append(Vec(D,{})) del res[0] return res## Problem 4# Answer with a boolean, please.is_it_a_vector_space_1 = Trueis_it_a_vector_space_2 = False## Problem 5is_it_a_vector_space_3 = Trueis_it_a_vector_space_4 = False## Problem 6is_it_a_vector_space_5 = Trueis_it_a_vector_space_6 = False