260 likes | 364 Views
> f:= n -> [seq([n-k, n-k], k=0..n)]; f := n -> [seq([n - k, n - k], k = 0 .. n)] > f(6); [[6, 6], [5, 5], [4, 4], [3, 3], [2, 2], [1, 1], [0, 0]] > h:= n -> [seq([n-k, k], k=0..n)]; h := n -> [seq([n - k, k], k = 0 .. n)] > h(6);
E N D
> f:= n -> [seq([n-k, n-k], k=0..n)]; f := n -> [seq([n - k, n - k], k = 0 .. n)] > f(6); [[6, 6], [5, 5], [4, 4], [3, 3], [2, 2], [1, 1], [0, 0]] > h:= n -> [seq([n-k, k], k=0..n)]; h := n -> [seq([n - k, k], k = 0 .. n)] > h(6); [[6, 0], [5, 1], [4, 2], [3, 3], [2, 4], [1, 5], [0, 6]] > {$-4..4}; {-4, -3, -2, -1, 0, 1, 2, 3, 4} > h:= n ->[{$0..k}, k = 0..n]; h := n -> [{`$`(0 .. k)}, k = 0 .. n]
> h(5); [{$(0 .. k)}, k = 0 .. 5] > g := x -> {$0..x}; > g(5); g := x -> {`$`(0 .. x)} {0, 1, 2, 3, 4, 5} > h:= n ->[g(n)]; h := n -> [g(n)] > h(6); [{0, 1, 2, 3, 4, 5, 6}] > h:= n ->[g(k),k = 0..n ]; h := n -> [g(k), k = 0 .. n]
> h(6); [{$(0 .. k)}, k = 0 .. 6] > h:= n ->[{$0..k},k = 0..n]; h := n -> [{`$`(0 .. k)}, k = 0 .. n] > h(6); [{$(0 .. k)}, k = 0 .. 6] > [$-4..4]; [-4, -3, -2, -1, 0, 1, 2, 3, 4] > h := n -> [$ 0..n]; h := n -> [`$`(0 .. n)] > h(6); [0, 1, 2, 3, 4, 5, 6]
> {$0..4}; {0, 1, 2, 3, 4} > {$0..0}; {0} > {$0..1}; {0, 1} > {$0..k}; {$(0 .. k)} > A := $1..12; A := 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 > h := n -> [seq({$0..k}, k=1..n)]; h := n -> [seq({`$`(0 .. k)}, k = 1 .. n)]
> h(6); [{0, 1}, {0, 1, 2}, {0, 1, 2, 3}, {0, 1, 2, 3, 4}, {0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5, 6}] > g:= x ->{$0..x}; g := x -> {`$`(0 .. x)} > g(5); {0, 1, 2, 3, 4, 5} > s:n ->[seq([g(k)], k = 0..n)]; n -> [seq([g(k)], k = 0 .. n)] > s(5); s(5) > map(abs, [-1, 3, -32, 4]); [1, 3, 32, 4] > map(abs, {1, -1, 2, -2}); {1, 2} > map(abs, [1, -1, 2, -2]); [1, 1, 2, 2] >map(f, (1, 2, 3)); 6 > map(f, {1, 2, 3}, 0, 0); {1, 4, 9}
The map2 function is similar to map, except that for each operand of expr, arg1 is passed as the first argument to fcn, the operand of expr is passed as the second argument, and arg3, ..., argn are passed as the third, ..., nth arguments. • > map2(k, m, {a,b,c}); • {k(m, a), k(m, b), k(m, c)} • 2.3 • A := [a, a, b, b, b]; • B := [b, c]; • hd := x -> x[1]; // all in section 1 • > hd(A); • > map(hd,[a, b], [a, b, c], [b, d]); • [a[1], b[1]] • > map(hd,[a, b]); • [a[1], b[1]]
C:=[a, b]; C := [a, b] > map(hd,C); [a[1], b[1]] > hd(C); a > map(hd,C); [a[1], b[1]] > map(hd,([a, b])); [a[1], b[1]] > map(hd,{[a, b]}); {a} > map(hd,[[a, b]]); [a] >
map(tl2,[[a, b]]); # again in section 1 page 14 [[b]] > map(tl2,[[a, b]]); [[b]] > map(tl2,[a, b]); [a[2 .. 1], b[2 .. 1]] > map(tl2,[[a, b,c,d]]); [[b, c, d]] > map(tl2,[[a, b,c,d],B]);
2.3 • > C:=[a, b, c,d]; • C := [a, b, c, d] • tl2 := x -> x[2..nops(x)]; • tl2 := x -> x[2 .. nops(x)] • catLists := (x, y) -> [op(x), op(y)]; • catLists := (x, y) -> [op(x), op(y)] • > catLists([1,2,3,4],[3,4,5]); • [1, 2, 3, 4, 3, 4, 5]
> • > catLists(hd(C), tl2(C)); • [a, b, c, d] • cons := catLists; • cons := catLists • > g:=x -> (hd(x), tl2(x)); • g := x -> (hd(x), tl2(x)) • > g([1,2,3,4]); • 1, [2, 3, 4] • > h := cons@g; • h := catLists@g • > h([1,2,3,4]); • [1, 2, 3, 4] • > evalb(h(C) = C); • true
2.8 • f:=x->1/(x+1); • 1 • f := x -> ----- • x + 1 • solve(f(x) = f(y), x); • y • > solve(f(x) = y, x); • -1 + y • - ------ • y • > f(%); • 1 • ------------ • -1 + y • - ------ + 1 • y
> simplify(%); • y
3.1.2 > tl := x ->x[2..nops(x)]; > last := x -> if nops(x) = 1 then x else last(tl(x)) fi;
3.7 > cons := (x, S) -> [x, op(S)]; cons := (x, S) -> [x, op(S)] > cons(b,[a, b, c]); [b, a, b, c] > cons(d,[a, b, c]); [d, a, b, c]
> concat := (x, y) -> if x = [ ] then y else cons(hd(x), concat(tl(x), y)) fi; concat := proc(x, y) option operator, arrow; if x = [] then y else cons(hd(x), concat(tl(x), y)) end if end proc > trace(concat); concat
> concat([a, b, c], [d, e]); {--> enter concat, args = [a, b, c], [d, e] {--> enter concat, args = [b, c], [d, e] {--> enter concat, args = [c], [d, e] {--> enter concat, args = [], [d, e] <-- exit concat (now in concat) = [d, e]} <-- exit concat (now in concat) = [c, d, e]} <-- exit concat (now in concat) = [b, c, d, e]} <-- exit concat (now at top level) = [a, b, c, d, e]}
3.7.1 Recursive way hd:= x -> x[1]; hd := x -> x[1] > tl := x ->x[2..nops(x)]; tl := x -> x[2 .. nops(x)] > subset:=(x,y)->if x = {} then true > else member(hd(x),y) and subset (tl(x),y) > fi; > > A := {a,b,c,d,e}; A := {a, b, c, d, e} > B := {e,z}; B := {e, z} > C:= {a,b}; C := {a, b}
> B := {e,z}; B := {e, z} > C:= {a,b}; C := {a, b} > subset(C,A); true > subset(B,A); false > subset({},A); true > subset(A,C); false
3.7.1 Non recursive version subset:=(x,y)->if `intersect`(x,y) =x then print(yes) else print(no)fi; subset := proc(x, y) option operator, arrow; if x intersect y = x then print(yes) else print(no) end if end proc > subset({d,e},{a,b,c}); no > subset({a,b},{a,b,c}); yes
3.7.2 • subset1:=(x,y)->if x = {} then true else if evalb(hd(x) in y) then subset1(tl(x),y) else false fi fi; • power:=x->if x = {} then {{}} • else power(tl(x)) union map(`union`,power(tl(x)),{hd(x)})fi;
5.1.4.b • > sum(a[n+2]*x^n, n = 0..k); • > sum(a[n]*x^(n-2), n = 2..k+2); • > simplify(sum(a[n+2]*x^n, n = 0..k)); • > evalb(simplify(sum(a[n+2]*x^n, n = 0..k))= simplify(sum(a[n]*x^(n-2), n = 2..k+2))); • False; • > sum('a[n+2]*x^n','n'=0..4); • > a[2]+a[3]*x+a[4]*x^2+a[5]*x^3+a[6]*x^4 • > sum('a[n]*x^(n-2)','n'=2..6); • > a[2]+a[3]*x+a[4]*x^2+a[5]*x^3+a[6]*x^4 • > evalb(sum('a[n+2]*x^n','n'=0..4) = sum('a[n]*x^(n-2)','n'=2..6)); • true
> evalb(sum('a[n+2]*x^n','n'=0..k) = sum('a[n]*x^(n-2)','n'=2..(k+2) )); false. WHY????????????
5.2 permute({a, b, c}); [[a, b, c], [a, c, b], [b, a, c], [b, c, a], [c, a, b], [c, b, a]] > > permute([r, a, d, a, r]); [[r, a, d, a, r], [r, a, d, r, a], [r, a, a, d, r], [r, a, a, r, d], [r, a, r, d, a], [r, a, r, a, d], [r, d, a, a, r], [r, d, a, r, a], [r, d, r, a, a], [r, r, a, d, a], [r, r, a, a, d], [r, r, d, a, a], [a, r, d, a, r],
[a, r, d, r, a], [a, r, a, d, r], [a, r, a, r, d], [a, r, r, d, a], [a, r, r, a, d], [a, d, r, a, r], [a, d, r, r, a], [a, d, a, r, r], [a, a, r, d, r], [a, a, r, r, d], [a, a, d, r, r], [d, r, a, a, r], [d, r, a, r, a], [d, r, r, a, a], [d, a, r, a, r], [d, a, r, r, a], [d, a, a, r, r]]
5.2.3 n:=2;numbcomb(n,n/2); n := 2 > n:=3;numbcomb(n,n/2); n := 3 Error, (in numbcomb) 2nd argument must be an integer > n:=4;numbcomb(n,n/2); n := 4 6 n:=6;numbcomb(n,n/2); n := 6 20 > numbcomb(8,4); 70
5.3.2 binomial(m, k); binomial(m, k) > m:=6; k:=2; m := 6 k := 2 evalb(binomial(m, k)=binomial(m-1, k)+binomial(m-1, k-1)); true