Commits

Peer Stritzinger committed 3377886

Add to_list and handle dimensions like Mathematica

  • Participants
  • Parent commits 53eb18c

Comments (0)

Files changed (1)

 
 -module(ndarray). 
 
--export([new/4]). 
+-export([new/3, new/4, to_list/1]). 
 
 -record(ndarray, {dimensions, strides, type, elem_size, data}).
 
+new(Dims, Type, Elem_size) ->
+    new(Dims, Type, Elem_size, 0).
+
 new(Dims, Type, Elem_size, Value) ->
     Count = lists:foldl(fun(X, Prod) -> X*Prod end, 1, Dims),
     {Strides, _Bc} = lists:mapfoldl(fun(X, Prod) -> {Prod, X*Prod} end,  
-				    Elem_size, Dims),
+				    Elem_size, lists:reverse(Dims)),
     Data = repl_value(Count, Type, Elem_size, Value),
     #ndarray{dimensions=Dims, data=Data, strides=Strides, 
 	     type=Type, elem_size=Elem_size}.
 repl_value(Count, float, Elem_size, Value) ->
     << <<X:Elem_size/float>> || X <- lists:duplicate(Count, Value) >>.
 
+to_list(#ndarray{strides=Strides, data=Data, 
+		  type=Type, elem_size=Elem_size}) ->
+    to_list(lists:reverse(Strides), Type, Elem_size, Data).
+
+to_list([Elem_size], integer, Elem_size, Data) ->
+    [ X || <<X:Elem_size/integer>> <= Data ];
+to_list([S|Strides], Type, Elem_size, Data) ->
+    [ to_list(Strides, Type, Elem_size, X) 
+      || <<X:S/bitstring>> <= Data ].
+    
 %% Copyright 2011 Peer Stritzinger GmbH, Aumuellerstr. 14, 82216
 %% Ueberacker, Germany http://www.stritzinger.com. All rights reserved. 
 %%