Commit 90421afd authored by Mathieu Hemery's avatar Mathieu Hemery
Browse files

Add a solve_linear_problem function and its test

parent a90325f7
......@@ -18,7 +18,8 @@
sub_matrix/1,
row_sum/3,
transpose_matrix/2,
lusubst/2
lusubst/2,
solve_linear_problem/3
]
).
/* Module Matrix
......@@ -300,7 +301,7 @@ scalar_prod(VecX, VecY, SP) :-
scalar_prod([], [], SP, SP).
scalar_prod([X|TailX], [Y|TailY], Res_tempo, Acc):-
Res is Res_tempo + X*Y.
Res is Res_tempo + X*Y,
scalar_prod(TailX, TailY, Res, Acc).
......@@ -456,3 +457,12 @@ lusubst(B,X):-
)
),
nb_getval(formal_vector,X).
%! solve_linear_problem(+A, +B, -X)
%
% Solve the pbm A.X = B through delegate to ludcmp and lusubst
solve_linear_problem(A,B,X) :-
ludcmp_matrix(A, _Decomposed_A),
lusubst(B,X).
:- use_module(matrix).
%! check_error(Result, Target, Max_Error)
%
% check if result and target differ by no more than Max_Error
check_error([], [], _) :- !.
check_error([R|TR], [T|TT], Epsilon) :-
abs(R-T) < Epsilon,
check_error(TR, TT, Epsilon).
:- begin_tests(matrix).
test(ludcmp_matrix_22) :-
......@@ -12,4 +23,12 @@ test(lusubst_33) :-
Decmp = [[1,0,1],[6,4,-4],[2,0.25,3.0]],
X = [1.0,-1.0,2.0].
:- end_tests(matrix).
\ No newline at end of file
test(solve_linear_algebra_1) :-
matrix:solve_linear_problem([[ 1.0, 2.0, 0.0,-1.0],
[ 3.0,-2.0, 1.0, 0.0],
[-2.0, 0.0, 0.0, 4.0],
[ 1.0, 1.0,-1.0, 0.0]],
[-2.0, 7.0, 2.0,-2.0],X),
check_error(X, [1,-1,2,1], 0.001).
:- end_tests(matrix).
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment