[erlang-questions] [Erlang-Question] Is Erlang good for Matrix manipulation and AI related algorithms
Barco You
<>
Thu Nov 17 10:10:10 CET 2011
Sorry, your formula is right and the indices in the formula should start
form 0. I changed the snippet as below:
-module(lattice).
-export([build/1]). %%One list argument specifying the dimensions of the
lattice
build([]) ->
[];
build([H|T]) ->
build([H|T], []).
build([H|[]], D) ->
build_innerlist(H, D, [] );
build([H|T], D) ->
Bu = fun(X) -> build(T, [X|D]) end,
build_outterlist(H, Bu, []).
build_innerlist(0, _D, L) ->
L;
build_innerlist(N, D, L) when N > 0 ->
build_innerlist(N-1, D,
[factorial(N-1+lists:sum(D)-length(D))/lists:foldl(fun(E,A) ->
A*factorial(E-1) end, factorial(N-1), D) | L]).
build_outterlist(0, _X, L) ->
L;
build_outterlist(N, X, L) when N > 0 ->
build_outterlist(N-1, X, [X(N)|L]).
factorial(0) ->
1;
factorial(N) when N > 0 ->
factorial(N,1).
factorial(0, V) ->
V;
factorial(N, V) ->
factorial(N-1, N*V).
Eshell V5.8.4 (abort with ^G)
1> lattice:build([2,2,2]).
[[[1.0,1.0],[1.0,2.0]],[[1.0,2.0],[2.0,6.0]]]
On Thu, Nov 17, 2011 at 4:09 PM, Barco You <> wrote:
> Hi Peer,
>
> Thank you for your links, but the formula is seemingly not right!
>
> The number of paths to [n,m,k] should equals to (n+m+k)! / (n!*(m+k)!).
>
> I write down a snippet of erlang cold as following. Please correct me if I
> did it in an inefficient way.
>
> -module(lattice).
>
> -export([build/1]). %%One list as argument specifying the dimensions of
> the lattice
>
> build([]) ->
> [];
> build([H|T]) ->
> build([H|T], []).
>
> build([H|[]], D) ->
> build_innerlist(H, D, [] );
> build([H|T], D) ->
> Bu = fun(X) -> build(T, [X|D]) end, %%Any better way here?
> build_outterlist(H, Bu, []).
>
> build_innerlist(0, _D, L) ->
> L;
> build_innerlist(N, D, L) when N > 0 ->
> build_innerlist(N-1, D,
> [factorial(N+lists:sum(D))/(factorial(N)*factorial(lists:sum(D))) | L]).
>
> build_outterlist(0, _X, L) ->
> L;
> build_outterlist(N, X, L) when N > 0 ->
> build_outterlist(N-1, X, [X(N)|L]).
>
> factorial(0) ->
> 1;
> factorial(N) when N > 0 ->
> factorial(N,1).
>
> factorial(0, V) ->
> V;
> factorial(N, V) ->
> factorial(N-1, N*V).
>
>
> Eshell V5.8.4 (abort with ^G)
> 1> lattice:build([2,2,2]).
> [[[3.0,6.0],[4.0,10.0]],[[4.0,10.0],[5.0,15.0]]]
>
>
>
> On Thu, Nov 17, 2011 at 12:33 AM, Peer Stritzinger <>wrote:
>
>> Forget what I said about Catalan Numbers (these count paths staying
>> below the diagonal).
>>
>> What you need is this: http://mathworld.wolfram.com/LatticePath.html
>>
>> From this you could easily derive that the number of paths in a n x m
>> x k cuboid is:
>>
>> (n+m+k)! / (n!*m!*k!)
>>
>> Well that was easier than I thought.
>> -- Peer
>>
>> On Tue, Nov 15, 2011 at 6:19 AM, Barco You <> wrote:
>> > Hi Peer,
>> > Could you please show me the one-line operation in erlang? Thank you!
>> >
>> > Regards,
>> > Barco
>> >
>> > On Tue, Nov 15, 2011 at 1:18 PM, Peer Stritzinger <>
>> wrote:
>> >>
>> >> On Thu, Nov 10, 2011 at 6:39 AM, Barco You <> wrote:
>> >>
>> >> > I hope to know there are how many paths across a specific point in a
>> >> > cubic
>> >> > lattice if we walk from the origin to the far-most diagonal point.
>> >>
>> >> It is not difficult to derive a closed form for this number, then
>> >> It'll be a fast O(1) operation in one line in any language.
>> >>
>> >> Cheers,
>> >> -- Peer
>> >
>> >
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111117/12a9b9ab/attachment.html>
More information about the erlang-questions
mailing list