01-02-2024, 07:08 PM
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.
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
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