2016-02-23 24 views
2

總之,它是一個範德蒙德矩陣,我有一個問題在數組的第二維上運行for。Smalltalk範德蒙德矩陣

'add meg M-et majd N-et (enter kozotte)(az 1. sor az 1-es szam hatvanyai)' displayNl. 
M := stdin nextLine asInteger. 
N := stdin nextLine asInteger. 
|tomb| 
tomb := Array new: M. 
x := 1. 
y := 1. 
a := M + 1. 
b := N + 1. 
x to: a do: [ :i| 
    tomb at:x put: (Array new: N) y to: b do: [ :j | 
    x at: y put: (x raisedTo: y - 1) ] ]. 
tomb printNl. 
+2

是不是有(數組新之間缺少'點: N)'和'Y:b do:'?沒有點,看起來這兩個句子將被解釋爲單個關鍵字消息:put:to:do: –

+1

另外,'x at:y put:...'不能工作,因爲'x'是一個整數。這可能應該是:墳墓:Ÿ放:...'。還有一件事:'x'和'y'不會改變,所以將它們提升到任何東西都會回答'1'(1^n = 1)。 –

+0

我不明白代碼的意圖。根據https://en.wikipedia.org/wiki/Vandermonde_matrix,你應該從一個alpha向量開始,然後計算i,j中的每個元素作爲該行的alpha值,並將其提升到第1列。 –

回答

1

下面是創建一個我們有非專利藥品進入aij的表達矩陣的好方法:

Matrix class >> fromBlock: aBlock rows: n columns: m 
    | matrix | 
    matrix := self rows: n columns: m. 
    matrix indicesDo: [:i :j | | aij | 
    aij := aBlock value: i value: j. 
    matrix at: i at: j put: aij]. 
    ^matrix 

通過上述方法,你現在就可以實現

Matrix class >> vandermonde: anArray degree: anInteger 
    ^self 
    fromBlock: [:i :j | (anArray at: i) raisedTo: j - 1] 
    rows: anArray size 
    columns: anInteger + 1 

編輯

我ju ST意識到,菲羅有創造從其aij,它被命名爲rows:columns:tabulate:表達的矩陣的方式,所以我的答案簡化爲:

Matrix class >> vandermonde: anArray degree: anInteger 
    ^self 
    rows: anArray size 
    columns: anInteger + 1 
    tabulate: [:i :j | (anArray at: i) raisedTo: j - 1]