• 沒有找到結果。

矩陣相乘

N/A
N/A
Protected

Academic year: 2022

Share "矩陣相乘"

Copied!
42
0
0

加載中.... (立即查看全文)

全文

(1)

Michael Tsai 2011/03/04

(2)

矩陣相乘

問題:

Input: A, B 都是 n x n 的Square Matrix

Ouput: C, n x n 的square matrix, C A ∙

(3)

矩陣相乘 – 基本法

n個 n個

n個乘法, n‐1個加法, 產生了一個entry, 共有 個entries

Running time = ? Θ

(4)

矩陣相乘 – D&C嘗試一

可得

可得

Recursive  case:

(5)

矩陣相乘 – D&C嘗試一

n=1

則 ∙ 直接算 (A,B,C各自為一個數)

Base case:

(6)

矩陣相乘 – D&C嘗試一

以下面的等式可求得C的四個小分塊的解:

∙ ∙

∙ ∙

∙ ∙

∙ ∙

(7)

Square-Matrix-Multiply-Recursive(A,B) n=A.rows

let C be a new n x n matrix if n==1

else partition the matrix into 4 n/2 x n/2 matrices

Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )

Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )

Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )

Square-Matrix-Multiply-Recursive( , ) +Square-Matrix-Multiply-Recursive( , )

return C

Base case

Recursive  case

8 2 Combine

1 Θ 1 Θ 1

Θ

Θ 1 8

2 Θ 8

2 Θ

Θ 1

8 2 Θ

,if  1

,if  1

Θ

(8)

矩陣相乘 – Strassen’s method

課本說: “Not at all obvious”

可達到Θ Θ .

Overview:

1. 將A, B及C都切成四塊n/2大小的matrix

2. 做出10個matrices,  , , … , .這些matrix都 是以步驟1中n/2大小的matrices加減後得到 的結果.

3. 使用步驟1&2中得到的n/2大小的matrices做 乘法後得到 , , … , .

4. 以 , , … , 相加減後得到的結果產生 , , , 四個matrix

細節先略過, 但我們可以計算running time了…

德國數學家 Volker Strassen 攝於2009年

(9)

矩陣相乘 – Strassen’s method

1. 將A, B及C都切成四塊n/2大小的matrix

2. 做出10個matrices,  , , … , .這些matrix都是以 步驟1中n/2大小的matrices加減後得到的結果.

3. 使用步驟1&2中得到的n/2大小的matrices做乘法後 得到 , , … , .

4. 以 , , … , 相加減後得到的結果產生 , , , 四個matrix

Θ 1 Θ

7 2

Θ

Θ 1 Θ 7

2 Θ 7

2 Θ

Θ

(10)

矩陣相乘 – Strassen’s method

Not at all obvious

(11)

矩陣相乘 – Strassen’s method

 , , 用類似的方法

 <Reading assignment> Textbook 4.2

 <Homework for yourself> Exercise 4.2‐1

Not at all obvious

(12)

矩陣相乘 – Strassen’s method

實用嗎? 好用嗎? 讓我們來挑毛病:

1. Θ 的constant比Θ

2. 那些sub‐matrices要花額外的空間

3. 當是Sparse Matrix時, 特別為Sparse Matrix設計的方法比較

4. Strassen’s method is not as numerically stable as 基本法.

1990年代的部分研究減輕了2&4的壞處

所以, Strassen’s method有什麼用呢? 

Key: find the crossover point and combine the two algorithms.

目前所知, the most asymptotically efficient algorithm has a  running time of  . . (Coppersmith and Winograd)

(13)

接下來…

問題: 我們要怎麼解遞迴式?

取代法

遞迴樹法

大師定理法

http://www.origin‐zero.com/senzi/JOKE1.jpg

(14)

取代法

猜答案的形式 用數學歸納法證明此

形式成立 得到遞迴式的解 成功

失敗

(15)

取代法‐例子

問題:  2

猜測:  log

用歸納法證明 c n log n, for a constant  0

假設上面的bound在m<n時成立

則 時亦成立.

也就是說 c log

(16)

取代法‐例子

2

2 2 log

2 log 2

log log 2 log

log as long as  1

栗子助教

舉個栗子

(17)

取代法‐例子

接著必須也證明邊界條件成立.

有時候需要多一點努力….

假設 1 1.

我們必須證明 log , 1

天不從人願:  1時,  log 0

T 1 1 0 log

娃~

(18)

取代法‐例子

事實上, 我們只須證明, 當 時,  log 即可. (把不聽話的 1拔掉)

從原本的遞迴式, 我們可以得到 2 4, 3 5.

接著設 2. 我們發現:

2 2 log 2 &   3 3 log 3

(只要 2) 至此可以使邊界條件成立.

喔耶.

(19)

取代法‐怎麼猜?

靠經驗.

跟沒講一樣.

一些小方法:

1.

根據以前看過類似的遞迴式來猜測

2.

使用等一下要介紹的遞迴樹

3.

證明比較鬆的upper bound或lower bound來慢慢 接近tight bound

2 2 17

老工匠

(20)

取代法‐小技巧1

題目: T 1

猜測: 

歸納法證明:

1 1 ≰

爛掉了…

(21)

取代法‐小技巧1

方法: 改使用 ,  0

(減掉一個order較低的term)

1 2 1

(as long as  1)

(然後繼續選c, 使得boundary condition成立, 在 此省略)

(22)

取代法–小技巧2

看起來挺嚇人的:  2 log

替換變數:  log

2 2 2

定義:  2

2

log

2 log

log log log

暫時不管flooring

(23)

遞迴樹法

畫出遞迴樹

用數學歸納法證明此 解成立

用比較不嚴謹的方法 加總得到解

(24)

例子:  3 Θ

4 44 4

4 4

16 16 16 16 16 16 16 16 16

16 16 16 16 16 16 16 16 16

………

1 1

3 16

3 16

Θ level log

level 0

level 1

level 2

3 個node

(25)

Θ

3

16 Θ

3

16 Θ

1 1 3

16

Θ 16

13 Θ

(26)

遞迴樹法–例子1

用歸納法證明:  for some  0.

3

3 4

3 4 3

16

as long as 

(27)

遞迴樹法–例子2

例子: 

請一位同學上來畫遞迴樹 (有點跛腳的遞迴樹)

(28)

遞迴樹法–例子2

歸納法證明: log

3 log 3

2

3 log2 3

3 log 2

3 log

3 log 3 2

3 log 3 2 log 3 log 3 2

3 log 3 2

3 log 2 cn log log 3 2

3

log

as long as 

(29)

大師定理

Master Theorem:

Let  1 and  1 be constants, let  be a function, and let  be defined on the nonnegative integers by the recurrence

where we interpret  to mean either  or  . Then  has the  following asymptotic bounds:

1. if  for some constant  0, then  Θ

2. if  Θ , then  Θ log

3. if  Ω for some constant  0, and if 

for some constant  1 and all sufficiently  large  , then  Θ

(30)

大師定理

1. if  for some constant  0, then  Θ

2. if  Θ , then  Θ log

3. if  Ω for some constant  0, and if 

for some constant  1 and all sufficiently  large  , then  Θ

Which one is polynomially larger/smaller?

is larger is larger

The same order

Note: not all possibilities for  can be covered by these 3 cases!

(31)

9

a=9, b=3, f(n)=n

log log 9 2

satisfies case 1:  , 1

so  Θ Θ

For your reference: 

舉個栗子

(32)

大師定理‐例子2

1

a=1, b=3/2, f(n)=1

n n 1

satisfies case 2:  Θ log Θ log

For your reference: 

(33)

大師定理‐更多例子

3 log

2 log

2 Θ

8 Θ

7 Θ

上台解題時間…

(34)

取數問題

Selection Problem

問題:

Input: n個數字之集合

Output: 取出此n個數字之中位數

中位數之定義: n個數字中第k= 小的數字

(35)

取數問題

菜瓜布解法:

先把n個數sort好

從最小的數過去算到第 小的數字即為答案

running time=?

Ω log

Can we do a better job?

(36)

取數問題

n個數

小於等於a的

某數a (pivot)

大於a的

if  ,找 中第 小的

if  ,找 中第 小的

(37)

取數問題

下一個問題: 怎麼選a?

選不好的話… , 0

最好是可以平均分成兩分. 

那就是選中位數.

咦, 我們不是就要找中位數嗎?

能不能花少一點時間, 找個”差不多”的中位數

(38)

取數問題

差不多的中位數:

1.

把n個數分成很多大小為5個的sub list (大約共 有n/5個sub list

2.

這些sub list中各自找中位數

3.

找出n/5個中位數中的中位數

此為差不多的中位數

(39)

取數問題

有多差不多呢?

比”差不多中位數”小 的至少有

/5個中位數

中位數的中位數

3n 5 10

(40)

取數問題

Algorithm:

1. if  5 then 直接找出其中位數

2. else

3. 把數列拆成 個大小為5的小數列

4. 每個小數列找出其中位數

5. 找出 個中位數的中位數m

6. 用此中位數把原本的數列拆成兩部分: 比m大( )

及不比m大的( )

7. if  , 找 中第 小的

8. if  , 找 中第 小的

Θ 1 Θ

Θ T 5

Θ

Max: T

Θ Θ

5 Θ 7

10 5

7

10 Θ Θ

(41)

下一次…

Programming on a piece of paper (well, not  exactly programming)

Quicksort running time analysis: revisit

(42)

作業一上線

5 problem sets, 100 points

1 programming assignment, 4 writing  assignments

Judge girl system accounts are sent last night

Please give it a try

Due 2 weeks from yesterday

參考文獻

相關文件

Which keywords below shall have large positive weights in a good perceptron for the task.. 1 coffee, tea,

 lower triangular matrix: 下三角矩陣.  upper triangular matrix:

The aim of this paper is to summarize some of the bibliographical data for the more than 230 mountain and temple gazetteers of which the archive is comprised, to compare the

If the subset has constant extrinsic curvature and is a smooth manifold (possibly with boundary), then it has an explicit intrinsic lower curvature bound which is sharp in

If x or F is a vector, then the condition number is defined in a similar way using norms and it measures the maximum relative change, which is attained for some, but not all

We were particularly impressed by the large garden which is looked after by the students and used to grow fruit, herbs and vegetables for the midday meal which the school serves free

Then, we tested the influence of θ for the rate of convergence of Algorithm 4.1, by using this algorithm with α = 15 and four different θ to solve a test ex- ample generated as

Then, it is easy to see that there are 9 problems for which the iterative numbers of the algorithm using ψ α,θ,p in the case of θ = 1 and p = 3 are less than the one of the