170 likes | 181 Views
Suprakash D at ta datta@cse.yorku.ca Office: CSEB 3043 Phone: 416-736-2100 ext 77875 Course page: http://www.cse.yorku.ca/course/1560. CSE/Math 1560: Introduction to Computing for Mathematics and Statistics Winter 2011. Announcements. 1. Midterm: Mar 1, 2:30-3:30 pm, room TBA.
E N D
Suprakash Datta datta@cse.yorku.ca Office: CSEB 3043 Phone: 416-736-2100 ext 77875 Course page: http://www.cse.yorku.ca/course/1560 CSE/Math 1560:Introduction to Computing for Mathematics and Statistics Winter 2011 Math/CSE 1560, Winter 2011
Math/CSE 1560, Winter 2011 Announcements 1. Midterm: Mar 1, 2:30-3:30 pm, room TBA. 2. Please practice the sample exercises before going to the labs. 3. Labs next week: everything covered in class before Feb 9. Focus on writing procedures and functions.
Math/CSE 1560, Winter 2011 Last class 1. If-then statement • Next: Sets, lists and sequences. Ch 4 in the textbook.
Math/CSE 1560, Winter 2011 Programming principle • print_toss := x -> piecewise(x = 1, "head", x = 0, "tail") flip := proc () local HT, x; HT := rand(0 .. 1); x := HT(); print_toss(x) end proc; • Note that print_toss is defined outside the procedure.
Math/CSE 1560, Winter 2011 Consider this variant • flip2 := proc () • local HT, x, print_toss; • print_toss := x-> piecewise(x = 1, "head", x = 0, "tail") ; • HT := rand(0 .. 1); • x := HT(); • print_toss(x); • end proc • Which is the better way?
Math/CSE 1560, Winter 2011 Global variables • Defined within a proc, visible outside the proc. • Use with care.
Math/CSE 1560, Winter 2011 Two more functions • trunc(x) • frac(x)
Math/CSE 1560, Winter 2011 Sets • Enclosed in {}, separated by commas • e.g.: {22,5,7,10,3,-1} • Sets cannot have duplicates • > set1 := {1, 2, 3}; {1, 2, 3} > set2 := {1, 2, 3, 2, 1}; {1, 2, 3} > set3 := {3, b, a, 2}; {2, 3, a, b}
Math/CSE 1560, Winter 2011 Set operations • S union T, S intersect T, S minus T • Equality: evalb(S=T); • Membership: member(1,set1) OR • evalb(1 in set1)
Math/CSE 1560, Winter 2011 Adding and removing elements • Use the union and minus.
Math/CSE 1560, Winter 2011 The map function • map(func,set) • e.g. • > mySeq := {seq(K, K = 1 .. 10)}; • {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} • > map(x-> x^2, mySeq); • {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
Math/CSE 1560, Winter 2011 Example: remove negative values from a set • Steps: • Write out the sketch of the algorithm • Think of the most efficient way to implement the algorithm
Math/CSE 1560, Winter 2011 Code • > f1 := x-> piecewise(x < 0, -100, x => 0, x); • > set1 := {-4, -3, -2, 5, 6, 7}; • {-4, -3, -2, 5, 6, 7} • > set2 := map(f1, set1); • {-100, 5, 6, 7} • > set2 := set2 minus {-100}; • {5, 6, 7}
Math/CSE 1560, Winter 2011 More on seq() • seq(func,index=a..b) • E.g.: • seq(n^2,n=1..10) • Question: For which values of n is 1+x^n factorizable?
Math/CSE 1560, Winter 2011 Code seq(factor(x^n+1), n = 2 .. 12); Does this imply all coefficients or 1 or –1 for all n?
Math/CSE 1560, Winter 2011 Exercise • Write a procedure that takes as input an integer n and outputs a set of all primes in1..n. • (there is a Maple function called ithprime(i) that you should not use, but may be aware of).