Post Reply 
delcols bug
01-02-2024, 07:08 PM (This post was last modified: 01-02-2024 08:29 PM by Albert Chan.)
Post: #1
delcols bug
Below produce permanent of square matrix.

delcols does not work (tested on both HP emulators 2018/10/16, 2023/4/13)
I had to add mydelcols := delcols, and use mydelcols to make it work.

Code:
#cas
per(m) :=
BEGIN
 LOCAL r, mydelcols := delcols;
 IF len(m)<2 THEN RETURN m[1][1] END
 r := map(x->sum(!x),m);
 r := index(r,max(r)); /* most zeroes row */
 r, m := m[r], delrows(m,r);
 sum(when(r[k],r[k]*per(mydelcols(m,k)),0), k=1..len(r));
END;
#end

Lets debug with print(m); right after LOCAL r

> per([[0,1,2],[3,4,5],[6,7,8]])      → 144

Terminal:
m:[[0,1,2],[3,4,5],[6,7,8]]
m:[[3,5],[6,8]]
m:[[8]]
m:[[6]]
m:[[3,4],[6,7]]
m:[[7]]
m:[[6]]

This is using delcols directly inside sum:

Terminal:
m:[[0,1,2],[3,4,5],[6,7,8]]
m:[[4],[7]]
m:Error: Bad Argument Type
m:Error: Invalid Dimension
Find all posts by this user
Quote this message in a reply
01-02-2024, 09:20 PM
Post: #2
RE: delcols bug
Albert, thanks for bringing this up. I’ve filed a ticket for this in the bug tracker I’ve set up to help organize development. The programs given in the ticket are a bit shorter, namely
Code:
#cas
shortper(m) :=
BEGIN
 sum(delcols(m,k),k=1..1);
END;
#end
vs
Code:
#cas
shortper(m) :=
BEGIN
 sum(delcols(m,1),k=1..1);
END;
#end
Find all posts by this user
Quote this message in a reply
01-05-2024, 09:49 PM (This post was last modified: 01-05-2024 10:01 PM by jte.)
Post: #3
RE: delcols bug
Prof. Parisse has clarified what is going on here: delcols(-) is modifying its first argument (the matrix) inside the sum(-). For delcols(-)’ modification to its matrix argument to be observed from the outside, the matrix must be passed as a variable (not evaluated to a temporary).

One way of passing the matrix argument as a variable is to quote it (another is to use delcols(-) inside sum(-)…). A screenshot showing how quoting the matrix argument makes the modification visible afterwards follows:

[Image: attachment.php?aid=13117]

If a temporary is passed, the change is not made to variables that contributed to the construction of the temporary, as shown by the following screenshot:

[Image: attachment.php?aid=13118]


Attached File(s) Thumbnail(s)
       
Find all posts by this user
Quote this message in a reply
01-06-2024, 10:45 AM
Post: #4
RE: delcols bug
delcols does not auto-quote it's argument, sum does. If the first argument of delcols is an identifier of a variable containing a matrix, then the modified matrix is stored in the variable.
That should explain the issue.
Find all posts by this user
Quote this message in a reply
01-06-2024, 11:55 AM
Post: #5
RE: delcols bug
(01-06-2024 10:45 AM)parisse Wrote:  delcols does not auto-quote it's argument, sum does.

Thanks!
OP sum line should avoid matrix to be modified, like this:

> sum(when(r[k],r[k]*per(delcols(m+0,k)),0), k=1..len(r));

Although this does not explain why OP hack work too.
I would have expected mydelcols := delcols line have no effect.

> mydelcols := delcols;
> sum(when(r[k],r[k]*per(mydelcols(m,k)),0), k=1..len(r));

Quote: If the first argument of delcols is an identifier of a variable containing a matrix,
then the modified matrix is stored in the variable.

Perhaps delcols/delrows possibly modify matrix behavior should be in documentation?

m2 := delcols(m,k)      // m unchanged
m2 := delcols('m',k)     // m changed to m2

If this is not yet in documentation (i.e. undefined behavior), perhaps we can change it?
I think m should not be modified, unless explicitly asked, say m := delcols('m',k)
Find all posts by this user
Quote this message in a reply
01-08-2024, 04:24 PM
Post: #6
RE: delcols bug
If I decide to change something, then I'll certainly simplify, delrows/delcols shoud not modify the matrix.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)