Showing posts with label addl. Show all posts
Showing posts with label addl. Show all posts

Friday, May 12, 2017

Euclidean distance

Euclidean distance of a vector in n dimensions
Usage:


echo( len3( [1,1,2] ) );
echo( len3( [1,1,2] - [0,2,0] ) );


function len3(v) =len(v)>1?
sqrt(addl([for(i=[0:len(v)-1])pow(v[i], 2)]))
:len(v)==1?v[0]:v; 
function addl(l,c=0)= len(l)-1 >c?l[c]+addl(l,c+1):l[c];

Thursday, May 11, 2017

Sum all elements of a list

Recursively sums all elements of a list
Usage:


My_sum1=addl( [ 12, 3, 4, 62, 9 ]);
My_sum2=addl( [ [x,y,z], [x,y,z], [x,y,z], [x,y,z]]);

function addl(list, c = 0) = 
 c < len(list) - 1 ? 
 list[c] + addl(list, c + 1) 
 :
 list[c];