Thread: [Aimmath-commit] AIM/WEB-INF/maple Random.mpl,1.6,1.7
Brought to you by:
gustav_delius,
npstrick
From: <mo...@us...> - 2003-09-08 03:43:34
|
Update of /cvsroot/aimmath/AIM/WEB-INF/maple In directory sc8-pr-cvs1:/tmp/cvs-serv19664 Modified Files: Random.mpl Log Message: added documentation for Rand and fixed a bug in Rand(Partition()) Index: Random.mpl =================================================================== RCS file: /cvsroot/aimmath/AIM/WEB-INF/maple/Random.mpl,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Random.mpl 3 Sep 2003 15:49:11 -0000 1.6 --- Random.mpl 8 Sep 2003 03:43:31 -0000 1.7 *************** *** 53,57 **** `Package/Assign`( `Rand`::anything, ! "Implements Ken Monks' Rand() function, a recursive random thing-maker. See the <a href=\"http://math.scranton.edu/monks/software/Rand/Rand.html\">Help file for Rand()</a> for details.", proc() local f,a,b,n,L,wt,i,r,tot,cm,pick,s,m,w,ans,t,newargs,S,AreEqual,R,V,F,Fargs,finished,numargs,Nops,mx,mn,treesizes,j: --- 53,89 ---- `Package/Assign`( `Rand`::anything, ! "The @Rand()@ command is an all purpose recursive random-thing-maker. It allows the question author to recursively define an (abstract) set of expressions and randomly select an element from that (abstract) set. It also provides an easy mechanism for controlling the probability that various elements will be selected. ! <br/><br/> ! @Rand@ can take any number of arguments. When called with no arguments, it returns a random floating point number between 0 and 1. ! <br/><br/> ! When called with one or more arguments, Rand chooses one of the arguments at random, with each argument given a certain weight in making the choice. The weight for arguments is specified as follows: ! <br/><br/> ! <OL> ! <LI>an argument of the form @Wt(x[1],...,x[k],n)@ is given weight @n@ (which allows the user to manually set the weight of an argument or arguments) ! <LI>an integer range @a..b@ is given weight @b-a+1@. ! <LI>all other arguments are given weight 1 ! </OL> ! Once an argument is selected, it is evaluated as follows: ! <br/><br/> ! <OL> ! <LI>if the argument is of the form @Wt(x[1],...,x[k],n)@ then @Rand(x[1],..., x[k])@ is returned. ! <LI>if the argument is a list @[x[1],...,x[k]]@ then @Rand(x[1],...,x[k])@ is returned. (Thus wrapping a sequence of arguments in a list has the effect of giving the entire sequence weight 1, i.e. @[x[1],...,x[k]]@ is a shorthand for @Wt(x[1],...,x[k],1)@. It also has the effect of converting multiple arguments into a single argument which is useful when working with @Fn@, @Set@, @AIMSET@, @List@, @DupList@, @Seq@, @DupSeq@, @SUM@, and @Prod@ type arguments (see below)). ! <LI>if the argument is an integer range @a..b@ then a randomly seleceted integer between @a@ and @b@ (inclusive) is returned. ! <LI>if the argument is a range of the form @a..b@ where @a@ and @b@ are single character strings, then a random character between @a@ and @b@ is returned (using @convert(-,bytes)@ for the values of the characters). ! <LI>if the argument is a range of the form @a..b@ where @a@ and @b@ are floating point numbers, then a random float between @a@ and @b@ is returned (with precision similar to the precision specified by @a@ and @b@). ! <LI>if the argument is an AiM #SET# @S@, then @Rand(S['elements'])@ is returned ! (see documentation for SET). Note that in general you should not use ordinary Maple sets in your AiM questions, since Maple sets do not have a guaranteed order for their elements (which can produce different results in different instances of the same question). ! <LI>if the argument is of the form @Fn(f,x[1],...,x[k])@ where @f@ is a argument to @Rand@ which returns a function of @k@ variables and each @x[i]@ is an argument to @Rand@, then for each @i@, @a[i]:=Rand(x[i])@ is computed. The value @Rand(f)(a1,...,a[k])@ is then returned. ! <LI>if the argument is of the form @Set(A,B)@, @AIMSET(A,B)@, @List(A,B)@, @DupList(A,B)@, @Seq(A,B)@, @DupSeq(A,B)@, @SUM(A,B)@, or @Prod(A,B)@ then a natural number @n=Rand(B)@ is computed and a set, #SET#, list, sequence, sum, ! or product of n terms computed from @Rand(A)@ is returned. For @SUM@, @Prod@, @Set@, @SET@, @List@, and @Seq@ the @n@ elements will be distinct. For @DupSeq@ and @DupList@ there can be repetitions among the @n@ elements. ! <LI>if the argument is of the form @Set(A,B,f)@, @AIMSET(A,B,f)@, @List(A,B,f)@, @Seq(A,B,f)@, @SUM(A,B,f)@, or @Prod(A,B,f)@ then a natural number @n=Rand(B)@ is computed and a set, list, sequence, sum, or product of @n@ terms computed from @Rand(A)@ is returned containing distinct terms, where @f@ is a boolean function of two variables used to compare two terms to see if they are considered to be equal (thus allowing one to supply the definition of \"distinct\"). ! <LI>if the argument is of the form @Vec(x[1],...,x[k])@ then the Maple vector @vector([Rand(x[1]),...,Rand(x[k])])@ is returned. ! <LI>if the argument is of the form @Permute([x[1],...,x[k]])@ then a random permutation of the list @[Rand(x[1]),...,Rand(x[k])]@ is returned. Similarly if the argument is of the form @Permute(x[1],...,x[k])@ then a random permutation of the sequence @Rand(x[1]),...,Rand(x[k])@ is returned. ! <LI>if the argument is of the form @Partition(x)@ where @Rand(x)@ returns a positive integer @n@, then a list of positive integers whose sum is @n@ is returned (with elements in random order). If the argument is of the form @Partition(x,k)@ then a partition of @n@ is returned having exactly @Rand(k)@ terms. Similarly if the argument is of the form @Partition(n,k,M)@ then a partition of @n@ is returned having exactly @Rand(k)@ terms and no term greater than @Rand(M)@. Finally if the argument is of the form @Partition(n,k,M,m)@ then a partition of @n@ is returned having exactly @Rand(k)@ terms and no term greater than @Rand(M)@ and no term less than @Rand(m)@. ! <LI>if the argument is of the form @Frac(n,d)@ then an improper fraction whose integer part is @Rand(n)@ and whose denominator is @Rand(d)@ is returned, i.e. if @N:=Rand(n)@, @M:=Rand(d)@, and @a:=Rand(1..M-1)@ then @(N*M+a)/M@ is returned. ! <LI>if the argument is @Null@, then @NULL@ is returned (i.e. @Rand(Null)@ returns @NULL@). ! <LI<Any other argument is simply returned. ! </OL> ! ", proc() local f,a,b,n,L,wt,i,r,tot,cm,pick,s,m,w,ans,t,newargs,S,AreEqual,R,V,F,Fargs,finished,numargs,Nops,mx,mn,treesizes,j: *************** *** 157,164 **** while nops(s)<n do s:=s union {Rand(a)} od: ans:=s; ! # SET(...) (Note: this requires the SET class and utilities ! elif type(r,specfunc(anything,SET)) then if nops(r)<2 or nops(r)>3 then ! error "Syntax: SET(choosefrom,quantity,[isEqual])"; fi; if nops(r)=3 then --- 189,196 ---- while nops(s)<n do s:=s union {Rand(a)} od: ans:=s; ! # AIMSET(...) (Note: this requires the SET class and utilities ! elif type(r,specfunc(anything,AIMSET)) then if nops(r)<2 or nops(r)>3 then ! error "Syntax: AIMSET(choosefrom,quantity,[isEqual])"; fi; if nops(r)=3 then *************** *** 261,265 **** if n<s or mx*s<n then error "No such partition exists" fi; for i to s-1 do ! m:=Rand(1..min(n-(s-i),mx)); L:=L,m; n:=n-m; --- 293,297 ---- if n<s or mx*s<n then error "No such partition exists" fi; for i to s-1 do ! m:=Rand(max(1,n-(s-i)*mx)..min(n-(s-i),mx)); L:=L,m; n:=n-m; *************** *** 267,277 **** ans:=[L,n]; elif Nops=4 then # fourth argument specifies the minimum size of a term in the partition ! L:=NULL; n:=Rand(op(1,r)); s:=Rand(op(2,r)); mx:=Rand(op(3,r)); mn:=Rand(op(4,r)); ! for i to s-1 do ! m:=Rand(1..min(n-(s-i),mx)); ! L:=L,m; ! n:=n-m; ! od: ! ans:=[L,n]; fi; # Frac(integerpart,den) --- 299,305 ---- ans:=[L,n]; elif Nops=4 then # fourth argument specifies the minimum size of a term in the partition ! n:=Rand(op(1,r)); s:=Rand(op(2,r)); mx:=Rand(op(3,r)); mn:=Rand(op(4,r)); ! if n<s*mn or mx*s<n then error "No such partition exists" fi; ! ans:=[seq(mn-1,i=1..s)]+Rand(Partition(n-s*(mn-1),s,mx-mn+1)); fi; # Frac(integerpart,den) |