R. J. Cano on Fri, 19 Sep 2014 02:29:10 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Proposal for overriding vecsum( ) in future versions.



Greetings,

In the attachment.

Cheers,

R. J.
/* R. J. Cano <eerrrraattaa@gmail.com>, Sept 19 2014 */

? vecsum
vecsum(v): return the sum of the component of the vector v

? vecsum_
vecsum(v)_: if b is omitted or 1, return the sum of component of the vector v,
            otherwise if b is zero read v from left to right as the digits of
            a number in the least base (1 + greatest component in v), else if
            the components in v are smaller than b read in the base b, else
            return -1;

Motivation: In some cases vecsum_() might be considered the inverse of digits.

Source:

vecsum_(v,b=1)=if(b==1,vecsum(v),if(v==v*0,0,my(B=vecsort(v,,8));if(!b,vecsum_(v,B[#B]+1),if(B[#B]<b,sum(i=1,#v,v[i]*b^(#v-i)),-1))));

Examples:

? vecsum_([0,1,2,3])
%1 = 6
? vecsum_([0,0,0,1,1,9])
%2 = 11
? vecsum_([0,0,0,1,1,9],0)
%3 = 119
? vecsum_([0,0,0,1,1,9],7)
%4 = -1
? vecsum_(vector(1000),0)
%5 = 0
? vecsum_(digits(243106,4))
%6 = 16
? vecsum_(digits(243106,4),4)
%7 = 243106
? vecsum_(digits(243106,4),7)
%8 = 19314430
? vecsum_(digits(243106,4),0)
%9 = 243106
? digits(243106,4)
%10 = [3, 2, 3, 1, 1, 2, 2, 0, 2]

Note: The endless loop detecting 0 as the base is avoided with an extra if.