Coding the Matrix Week 2 The Vector Space作業

來源:互聯網
上載者:User
Coding the
Matrix: Linear Algebra through Computer Science Applications 

  本周的作業較少,只有一個編程任務hw2.作業比較簡單,如果大學學習過矩陣代數的話,基本上沒有什麼問題,不過要注意的一點是基2的Span的求法。

  基2空間上,在所有基向量中取任意個數個,疊加組合就得到了Span。但是如何取任意個呢?下面給出幾種方法。

  一種方法是對於任意可能的個數,利用Python中的排列組合module產生對應於此個數的所有排列,即得到Span。感興趣的話可以百度一下。這種方法概念較為清晰,但需要對Python的庫較為瞭解。

  第二種方法,利用了從n個元素中任選任意個的方法只有2^n個的排列組合知識。具體來說,就是對於任意從0到2^n-1的整數,使用bin()函數得到其二進位表示,對應位為1即代表選中此基向量。這種方法稍微Smart一些。但過程較為冗雜。

  第三種,就是下面程式給出的方法,一行語句就可以完成運算,原理和上一種方法相同,就不再贅述。但要注意結果為0的情況下要單獨考慮。

  其他的向量運算都比較簡單,最後幾道判斷是否為向量空間的問題,只需要牢記向量空間三特徵(包含0,加減和向量乘法組合仍在空間內)就不會出錯。

  作業代碼如下,模版中注釋部分給出了驗證範例。

# 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

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.