trealla-prolog / trealla

A compact, efficient Prolog interpreter written in plain-old C.
MIT License
274 stars 13 forks source link

GSL: Info+Question #563

Closed flexoron closed 4 months ago

flexoron commented 4 months ago

Misguided, I installed current stable version: GSL 2.8 which is, well, unstable. (in my case). To make a long story short: Better not using 2.8 (uncontrollable segfaults a lot).

No segfaults with version 2.7.1 (A reason distros install this version, I guess)

%Adapted test_matrix_det.pl %gsl_set_error_handleroff() %new_mat(Mat,Matmn) %mat_lup_det(Mat,Det) %write(Det) %gsl_matrix_free(Mat)

500x500 % Time elapsed 0.272s, 2007561 Inferences, 5.875 MLips

1000x1000 % Time elapsed 1.107s, 8015061 Inferences, 5.698 MLips

10000x10000 % Time elapsed 310.973s, 800150061 Inferences, 2.319 MLips

flexoron commented 4 months ago

gsl returns an ugly result and tpl handles it but I don't know how:

Det is the result ("write(d/Det)"),
and a/value is the result of  "A is Det, write(a/A)."

d/ -1.0 a/ -1.0
d/0.0 a/0.0
d/-.0nan a/-.0nan  % "A is -.0nan" but this should be a syntax error?
infradig commented 4 months ago

I could add a check for NANs.

On Sat, Jul 6, 2024 at 11:19 AM flexoron @.***> wrote:

gsl returns an ugly result and tpl handles it but I don't know how:

Det is the result ("write(d/Det)"), and a/value is the result of "A is Det, write(a/A)."

d/ -1.0 a/ -1.0 d/0.0 a/0.0 d/-.0nan a/-.0nan % "A is -.0nan" but this should be a error?

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/563#issuecomment-2211559781, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSER5VK7RZHXJNMLT4S3ZK5AYJAVCNFSM6AAAAABKNQ566CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJRGU2TSNZYGE . You are receiving this because you are subscribed to this thread.Message ID: @.***>

flexoron commented 4 months ago

I get this result with gsl 2.7.1, didn't see it with 2.8. How does tpl evaluate this? A is -.0nan ?

$ cat samples/test_matrix_det.mat
#25,5x5,0.0
2,3,4,5,6
3,4,5,6,7
4,5,6,7,8
5,6,7,8,9
6,7,8,9,10
#det=-.0nan

$ tpl samples/test_matrix_det.pl
?- gsl_set_error_handler_off(_).
   true.
?- main.
-.0nan
   true.
?-  
flexoron commented 4 months ago
Strange, too:
The result of this 3x3 was 0.0 and not -.0nan 
$ cat ../test_matrix_det.mat
#9,3x3,0.0
2,3,4
3,4,5
4,5,6
#det=0.0

$ tpl samples/test_matrix_det.pl
?- gsl_set_error_handler_off(_).
   true.
?- main.
0.0  % this is a number, no? (false now, v2.53.38)
   true.
?-  
flexoron commented 4 months ago

What now? mat_lup_det(M,Det), % returns false? I think returning 0.0 is better than false or -.0nan gsl_matrix_free(M), % leakiness

I demand gsl_set_error_handleroff() so I'm responsible for what to do with a singular matrix.

infradig commented 4 months ago

It's supposed to throw an error when NAN is detected.

On Sat, Jul 6, 2024 at 12:39 PM flexoron @.***> wrote:

What now? mat_lup_det(M,Det), % returns false? gsl_matrix_free(M),

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/563#issuecomment-2211597287, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEUGDJJPBLHVRQDDB4TZK5KEPAVCNFSM6AAAAABKNQ566CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJRGU4TOMRYG4 . You are receiving this because you commented.Message ID: @.***>

flexoron commented 4 months ago

Well but the matrix itself is a 'number' and I like to 'patch' the spot(s).

infradig commented 4 months ago

I get this...

$ cat x.mat

9,3x3,0.0

2,3,4 3,4,5 4,5,6

det=0.0

~/trealla (devel) $ tpl ?- use_module(library(gsl)). true. ?- open('x.mat',read,S),mat_read(M,S,Rows,Cols),mat_write(M,stdout),mat_lup_det(M,Det),gsl_matrix_free(M),close(S).

9,3x3,0.0

2,3,4 3,4,5 4,5,6 gsl: lu.c:311: ERROR: matrix is singular Default GSL error handler invoked. Aborted (core dumped)

I haven;t tried with error handling off.

On Sat, Jul 6, 2024 at 12:32 PM flexoron @.***> wrote:

Strange, too: The result of this 3x3 was 0.0 and not -.0nan $ cat ../test_matrix_det.mat

9,3x3,0.0

2,3,4 3,4,5 4,5,6

det=0.0

$ tpl samples/test_matrix_det.pl ?- gsl_set_error_handleroff(). true. ?- main. 0.0 % this is a number, no? (false now, v2.53.38) true. ?-

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/563#issuecomment-2211595032, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEWDJTLPG7KWV4WV3YDZK5JKVAVCNFSM6AAAAABKNQ566CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJRGU4TKMBTGI . You are receiving this because you commented.Message ID: @.***>

infradig commented 4 months ago

Without error handler i get this...

$ tpl ?- use_module(library(gsl)). true. ?- gsl_set_error_handleroff(). true. ?- open('x.mat',read,S),mat_read(M,S,Rows,Cols),mat_write(M,stdout),mat_lup_det(M,Det),gsl_matrix_free(M),close(S).

9,3x3,0.0

2,3,4 3,4,5 4,5,6 false.

On Sat, Jul 6, 2024 at 12:32 PM flexoron @.***> wrote:

Strange, too: The result of this 3x3 was 0.0 and not -.0nan $ cat ../test_matrix_det.mat

9,3x3,0.0

2,3,4 3,4,5 4,5,6

det=0.0

$ tpl samples/test_matrix_det.pl ?- gsl_set_error_handleroff(). true. ?- main. 0.0 % this is a number, no? (false now, v2.53.38) true. ?-

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/563#issuecomment-2211595032, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSEWDJTLPG7KWV4WV3YDZK5JKVAVCNFSM6AAAAABKNQ566CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJRGU4TKMBTGI . You are receiving this because you commented.Message ID: @.***>

infradig commented 4 months ago

gsl_linalg_LU_solve is setting its return status to 1 indicating an error, hence the false.

On Sat, Jul 6, 2024 at 12:57 PM flexoron @.***> wrote:

Well but the matrix itself is a 'number' and I like to 'patch' the spot(s).

— Reply to this email directly, view it on GitHub https://github.com/trealla-prolog/trealla/issues/563#issuecomment-2211603934, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFNKSETOKRB5SNC2DCC3RVDZK5MLHAVCNFSM6AAAAABKNQ566CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJRGYYDGOJTGQ . You are receiving this because you commented.Message ID: @.***>

flexoron commented 4 months ago

Switching the handler off I got 0.0 and with 5x5 I got '-.0nan' Now I ask the system: Give me all singular matrices (in a certain range). Thats why I told GSL to switch off the default. Now: 0.0 is Det % ok, but "-.0nan is Det" did not work.

flexoron commented 4 months ago

GSL says: This is my handler and if you don't like it switch it off or activate your handler. It makes no sense that tpl now has to check all sorts of errors and parameters. You see: GSLs handler says its a singular matrix and for certain things a standard error but not for all things. Note: NAN is true for the inverse of the matrix. I do not request the inverse of the matrix but requesting the determinant of the matrix which is 0.0

flexoron commented 4 months ago

GSL already throws an error. Why do you imitate this now. This makes no sense.

I switched GSLs handler off because I do not want its default behaviour. Now you switch it on. Do you want to reimplement each and every GSL handler behaviour?

Its ok. Maybe we see 'tpl_set_error_handleroff()' one day.

flexoron commented 4 months ago

v2.53.39

$ tpl samples/test_matrix_det.pl
?- gsl_set_error_handler_off(_).
   true.
?- main.
34637561460.0
   true
; Segmentation fault (core dumped)