Skip to content

Commit

Permalink
Add GNU Scientific Library (under development)
Browse files Browse the repository at this point in the history
  • Loading branch information
infradig committed Jul 4, 2024
1 parent c307dff commit 4e9a9c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
27 changes: 27 additions & 0 deletions library/gsl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
gsl_linalg_LU_solve/5,
gsl_linalg_LU_det/3,

mat_lup_det/3,

new_vec/2,
vec_read/3,
vec_write/2,
Expand Down Expand Up @@ -103,6 +105,29 @@

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mat_lup_det(M0, Size, Det) :-
gsl_matrix_alloc(Size, Size, M),
gsl_matrix_memcpy(M, M0, _),
gsl_permutation_alloc(Size, P),
gsl_linalg_LU_decomp(M, P, Signum, _),
gsl_vector_alloc(Size, B),
(
between(1, Size, I),
I2 is I - 1,
V is float(I),
gsl_vector_set(B, I2, V),
fail; true
),
gsl_vector_alloc(Size, X),
gsl_linalg_LU_solve(M, P, B, X, _),
gsl_vector_free(X),
gsl_vector_free(B),
gsl_permutation_free(P),
gsl_linalg_LU_det(M, Signum, Det),
gsl_matrix_free(M).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- use_module(library(lists)).

% new_vec(V, [1,2,3])
Expand All @@ -128,6 +153,8 @@
gsl_vector_alloc(Size1, V),
'$gsl_vector_read'(V, S).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% new_mat(M, [[1.1,1.2,1.3],[2.1,2.2,2.3],[3.1,3.2,3.3]])

new_mat(M, L) :-
Expand Down
20 changes: 0 additions & 20 deletions samples/test_matrix_det.pl
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
:- use_module(library(gsl)).

% M is mutable here, should probably work on a copy

mat_lup_det(M,Size,Det) :-
gsl_permutation_alloc(Size,P),
gsl_linalg_LU_decomp(M,P,Signum,_),
gsl_vector_alloc(Size,B),
(
between(1,Size,I),
I2 is I - 1,
V is float(I),
gsl_vector_set(B,I2,V),
fail; true
),
gsl_vector_alloc(Size,X),
gsl_linalg_LU_solve(M,P,B,X,_),
gsl_permutation_free(P),
gsl_vector_free(B),
gsl_vector_free(X),
gsl_linalg_LU_det(M,Signum,Det).

main :-
open('samples/test_matrix_det.mat',read,S),
mat_read(M,S,Rows,Cols),
Expand Down

0 comments on commit 4e9a9c0

Please sign in to comment.