Page 1 of 1

strange result in array2M

Posted: 16 Apr 2020 10:00
by B.Hooijenga
Hello,

I try to initialize an array2M, but get a strange result.
Am I doiing something wrong?

Code: Select all

class facts - matrixDB     linsys : array2M{real} := erroneous.     sizeX : positive := erroneous. % 4 colums     sizeY : positive := erroneous. % 3 rows   class facts     v : (real*).   clauses     v([2, 1, -1, 8]).     v([-3, -1, 2, -11]).     v([-2, 1, 2, -3]).   clauses     solve() :-         System = [ X || v(X) ],         sizeY := list::length(System), % 3  Rows         System = [H | _],         sizeX := list::length(H), % 4 Columns         linsys := array2M::new(sizeX - 1, sizeY - 1),         stdio::write("System = ", System, "\n"),         fillArray2M(System),         stdio::write("Output = \n"),         printArray2M(),         !.     solve() :-         stdio::write("Could not initialise").   class predicates     printArray2M : (). clauses     printArray2M() :-         foreach RowCounter = std::fromTo(0, sizeY - 1) do             foreach ColCounter = std::fromTo(0, sizeX - 1) do                 Value = linsys:get(ColCounter, RowCounter),                 stdio::write(Value, " ")             end foreach,             stdio::write("\n")         end foreach.   class predicates     fillArray2M : (real** System). clauses     fillArray2M(System) :-         foreach RowCounter = std::fromTo(0, sizeY - 1) do             foreach ColCounter = std::fromTo(0, sizeX - 1) do                 Row = list::nth(RowCounter, System),                 Value = list::nth(ColCounter, Row),                 linsys:set(ColCounter, RowCounter, Value)             end foreach         end foreach.
And this is the output:

System = [[2,1,-1,8],[-3,-1,2,-11],[-2,1,2,-3]]
Output =
2 1 -1 -3
-3 -1 2 -2
-2 1 2 -3

I am using VIP904 (32bit)

Kind regards
Ben

Re: strange result in array2M

Posted: 16 Apr 2020 11:11
by Thomas Linder Puls
Your array is too small:

Code: Select all

        linsys := array2M::new(sizeX - 1, sizeY - 1),
You should supply the size not the largest index.

Code: Select all

        linsys := array2M::new(sizeX, sizeY),
Your initialization code is very inefficient (which does not matter for such a little array, but anyway ...).

The predicate list::getMemberIndex_nd can help in such contexts:

Code: Select all

class predicates     fillArray2M : (real** System). clauses     fillArray2M(System) :-         foreach Row = list::getMemberIndex_nd(System, RowIndex) do             foreach Value = list::getMemberIndex_nd(Row, ColIndex) do                 linsys:set(ColIndex, RowIndex, Value)             end foreach         end foreach.
The key point is to avoid list::nth, which iterates to the n'th element.

Re: strange result in array2M

Posted: 16 Apr 2020 12:53
by B.Hooijenga
Thank you Thomas,

All is working well,

But: list::getMemberIndex_nd() does not exist in my helpfile too.

Kind regards
Ben