Assignment #6



In this assignment we are going to start using functions more precisely. Maple has two ways of representing functions. There is a one line notation that uses an arrow (some of you have started using this in your assignments) and there is a second way that is good for representing functions that take more than one line called a procedure. In this assignment we will consider lists of elements and functions on those lists.

Method 1 for creating a function:

nameoffunction := variable -> expression involving variable;

Example:
> f := x -> x^2;
Then this is how you would use this function:
> f(y);
y^2
> f(7);
49
> [seq(f(i),i=0..10)];
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Notice that x doesn't appear anywhere where the function is used, it appears only in the definition. The letter x is a dummy variable that gets replaced every time you use f( something ) then wherever x appears it is replaced with "something". This should be familiar because this is the notation that we use in mathematics all the time.

This method for creating a function is good only if your function can be expressed as a single command. If you want to make a function that takes several steps or several Maple commands, then you will need to make a 'procedure' or make a bunch of smaller functions which you combine. We will look more closely at procedures in our next assignments. In this assignment, we will will work with one line functions and the arrow -> notation. More generally we will work with functions of the form

nameoffunction := (var1, var2,...,vark) -> expression involving variables;

We will also work a command which can be used to create a sequence in Maple called seq. The command seq creates an expression for a bunch of values and puts it in a list.

For instance,
> [seq(i^2, i=0..10)];
makes a list of values of i^2 where i ranges from 0 to 10.

In general we will see this command used as
> seq( expression involving variable, variable=startval..endval );
This will create a sequence of values which isn't quite the same as a list. If we want to turn it into a list or a set we would put square brackets or curly braces around this expression (see the examples below. If I want to make a list of 100 zeros, I would use [seq(0,i=1..100)].

Understand what the following commands do by seeing what Maple returns, using the help functions, and by experimenting by changing the commands around so that you understand what they do. The command convert(list, `and`) (also see convert(list, `or`)) returns true if all elements in a list are true (respectively if one element is true and the others are false).

> S:={ seq( 3*r mod 8, r=1..7 )};
> L:=[ seq( 3*r mod 8, r=1..7 )];
> N:=seq( 3*r mod 8, r=1..7 );
> f:=(a,b,n)->a*b mod n;
> L:=[seq(f(3,r,8), r=1..7)];
> L:=[seq(f(5,r,8), r=1..7)];
> M:=[seq([seq(f(r,s,8),r=1..7)],s=1..7)];
> M:=[seq([seq(f(r,s,11),r=1..10)],s=1..10)];
> g:=L->[seq(L[-i], i=1..nops(L))];
> g([1,2,3,4,5,6]);
> g(6); # should be an error
> g(g([1,2,3,4,5,6]));
> combinat[partition](5);
> g(combinat[partition](5));
> map(g, combinat[partition](5));
> h:=n->n mod 2=0; h(5); h(6);
> map( h, [1,2,3,4,5] );
> convert( map(h, [1,2,3,4,5]), `and`);
> convert( map(h, [1,2,3,4,5]), `or`);
> convert( map(h, [1,3,5,7]), `or`);
> convert( map(h, [2,4,6,8]), `and`);
In the following two examples the function select(f, L) picks out the elements of L for which the function f returns true. The function nops counts how many elements are in a list.
> count:=(f,L)->nops(select(f, L));
> count(h,[1,2,3,4,5,6,7,9,11,13,15]);
Question #1:
Write a function called to_points which takes a list L of non-negative integers and returns a list of pairs [x, y] where 1 $\leq$ x $\leq$ the length of L and 1 $\leq$ y $\leq$ L[x]. Then write a function called transpose_points which takes a list of pairs of the form [x,y] and interchanges the x and y coordinates. Write a third function called from_points which does the inverse of the function to_points in that it takes a list of points points and returns a list L where L[i] is the number of points whose first coordinate is i where i ranges between 1 and max(map(p->p[1],points)). Then define
transpose_list:=L->from_points(transpose_points(to_points(L)));
If all your functions are working properly it should be that
> transpose_list([6,4,2,1,1,1,1]);
returns [7, 3, 2, 2, 1, 1] (but, of course, it should work on any input). Finally write a function of $n$ (where $n$ is a positive integer) called number_self_conjugates that counts the number of partitions of $n$ such that transpose_list(partitition) = partition.


Question #2:
* Write a function is_odd that accepts an integer $n$ and returns true if $n$ is odd.
* Write a function are_all_odd that returns true if all elements in a list are odd.
* Write a function which number_odd_partitions which returns the number of partitions of $n$ which are odd.
Calculate the number of odd partitions of $n$ for $n$ between 1 and 15. Now write a function odd_partitions(n) that list the odd partitions of n.


You should open up a new worksheet and start from scratch. You will have to save your work in a file and upload that file on to the course moodle. Your solution should be a sequence of commands where it is easy to change the input string and after you execute the sequence of commands you should have the correct output string. Please add documentation to your worksheet to explain how it works. Just a few sentences is sufficient, but imagine that you were opening up the worksheet for the first time and wanted to know what it did. You will be marked down if what you write is not clear and coherent.

You should finish your assignment before class Wednesday October 17 by 11:59pm. Assignments submitted after this date will be assessed a penalty of 10% per day.