Home > Archive > Prolog > July 2004 > matrix diagonal
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I've express the matrix like a list of lists, I'd like to extract
diagonal element, so I write this code, but it doesn't work as i
expected:
Could you help me, pleaseeeee?
matrix([[1,0],[0,1]]).
find_n(0,[E|_],E).
find_n(N,[_|C],E) :- N > 0, N1 is N - 1, find_n(N1,C,E).
unisci2(A,L,[A|L]):- atomic(A),!.
diag(M,N,L1):-diag(M,N,L1,[],0).
diag(M,N,L1,L,N):-write(L1).
diag(M,N,L,L,Ind):- find_n(Ind,M,E1),find_n(Ind,E1,E2),merge
2(E2,L,L1),Ind2
is Ind+1,diag(M,N,L1,L1,Ind2).
The Prolog answer is:
matrix(M),tlength(M,R),diag(M,R,L1).
[1, 1]
M = [[1, 0], [0, 1]]
R = 2
L1 = []
I'd like to have L1=[1,1]...
Thanks in advance
Tommy
| |
|
| warf@inwind.it (Tommy) wrote in
news:f396d0ca.0407220251.2f4ec916@posting.google.com:
> diag(M,N,L1):-diag(M,N,L1,[],0).
> diag(M,N,L1,L,N):-write(L1).
> diag(M,N,L,L,Ind):- find_n(Ind,M,E1),find_n(Ind,E1,E2),merge
2(E2,L,L1),I
> nd2 is Ind+1,diag(M,N,L1,L1,Ind2).
>
>
> The Prolog answer is:
>
> matrix(M),tlength(M,R),diag(M,R,L1).
> [1, 1]
> M = [[1, 0], [0, 1]]
> R = 2
> L1 = []
>
> I'd like to have L1=[1,1]...
diag(M,N,L1,L1,N):-write(L1).
diag(M,N,L2,L,Ind):- find_n(Ind,M,E1),find_n(Ind,E1,E2),merge
2(E2,L,L1),Ind2
is Ind+1,diag(M,N,L2,L1,Ind2).
You see the difference ?
--
Pento
De wereld was soep, en het denken meestal een vork,
tot smakelijk eten leidde dat zelden. - H. Mulisch
| |
| student 2004-07-22, 3:58 pm |
| Tommy wrote:
> I've express the matrix like a list of lists, I'd like to extract
> diagonal element, so I write this code, but it doesn't work as i
> expected:
> Could you help me, please?
>
What is the k-th element of the sequence [x1,x2,x3,...] ?
If k is 1, the k-th element of a list is by definition the first element
in the list:
kth_element(1,[X|_],X).
Suppose k > 1. Then, by what we have just said, the k-th element of L
can't be the first element of L, it must be the (k-1)-th element of what
remains of L after we delete its first element. That is.
kth_element( K, L, XK) :-
K > 1,
L = [_|RestOfL],
K_minus_1 is K -1,
kth_element( K_minus_1, RestOfL, XK ).
Then, if L is a list of m lists of length n,
the (i,j)-th element of L is the j-th element of the i-th list in L.
That is,
elem( (I,J), L, XIJ ) :-
kth_element( I, L, RowI ),
kth_element( J, RowI, XIJ ).
whence
:- elem( (2,3) , [ [1,2,3,4], [5,6,7,8], [9,10,11,12] ], Q).
Q = 7
Keep your Prolog clauses as close to simple English as you can
and you won't go far wrong.
bill
--
sequitur AT sonic DOT net
| |
| Bart Demoen 2004-07-22, 3:58 pm |
| student wrote:
> Keep your Prolog clauses as close to simple English as you can
> and you won't go far wrong.
This might work for native English people - for others, it might
go wrong very badly.
Cheers
Bart Demoen
| |
|
| Thank you all!
Sorry for my Prolog, but I'm learning now.
And when I write code sometimes I've to take a breath, before start panic!!
Tommy
| |
| student 2004-07-22, 8:57 pm |
| Bart Demoen wrote:
> student wrote:
>
>
>
> This might work for native English people - for others, it might
> go wrong very badly.
>
Indeed.
Fortunately, I was addressing a particular Anglophone in first person,
in which context my "you" (see above) referred to him and my "your
Prolog clauses" (see above) referred to his Prolog clauses.
And he won't go far wrong if he takes the advice I gave him.
If you wish to argue the contrary position -- that keeping everything as
complicated as possible is the best way to learn Prolog -- please be my
guest.
--
|
|
|
|
|