Smalltalk Vandermonde-matrix

146 Views Asked by At

Long in short it is a Vandermonde matrix and I have a problem to run a for in the second dimension of the array.

'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.
1

There are 1 best solutions below

0
On

Here is a good way to create a matrix for which we have an expression of the generic entry 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

With the above method you can now implement

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

EDIT

I just realized that in Pharo there is a way to create a matrix from the expression of its aij, it is named rows:columns:tabulate:, so my answer reduces to:

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