strange result in array2M

Discussions related to Visual Prolog
B.Hooijenga
Active Member
Posts: 45
Joined: 11 Jul 2002 23:01

strange result in array2M

Unread post by B.Hooijenga » 16 Apr 2020 10:00

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

User avatar
Thomas Linder Puls
VIP Member
Posts: 1168
Joined: 28 Feb 2000 0:01

Re: strange result in array2M

Unread post by Thomas Linder Puls » 16 Apr 2020 11:11

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.
Regards Thomas Linder Puls
PDC

B.Hooijenga
Active Member
Posts: 45
Joined: 11 Jul 2002 23:01

Re: strange result in array2M

Unread post by B.Hooijenga » 16 Apr 2020 12:53

Thank you Thomas,

All is working well,

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

Kind regards
Ben

Post Reply