Docsity

ITK 327, Fall 2008 Assignment 5: ML Functions for Problems & Matrix Multiplication - Prof., Assignments of Programming Languages

Toggle sidebar
bg1
ITK 327, Fall 2008 Assignment 5, 40 points
Programming Assignment: due on Oct. 30, Thursday
Write ML functions for the following problems and save them in asg5.sml. As usual, follow the instructions
given for the previous assignments to submit a soft-copy of your works through the web-submit. You also
have to submit a hard-copy of your programs. Use the same function names required in the problems.
Part I. Chapter 9 Exercises: 3, 6, 10, 26, 27, and 28.
Part II.
1. Write a function convert of type string -> int list that takes a string and converts it into a list
of integers such that, each integer in the list is the ascii value of the corresponding character in the
string. For example, convert "abc" will return [65,98,67].
2. Write a function diff of type int list -> int that takes integer list and returns the difference
between the maximum and minimum integers. For example, diff [2,9,6,1] will return 8. Note, for
empty list and a list with only one integer, let diff simply return 0.
3. Write a function powerset of type ’a list -> ’a list list that takes a list of ’a as a set and
returns its powerset. For example, powerset [1,2] will return [[],[1],[2],[1,2]].
Note: The powerset of set Sis the collection of all subsets of S.powerset [] should return [[]],not
[]. You may assume the elements in the input list are all distinct.
4. Write a function factorize of type int -> int list that takes an integer and returns all its factors
in a list. For example, factorize 12 will return [1, 2, 3, 4, 6, 12].
Part III. (Extra Bonus: 10 points) Write a function matrixMult of type
int list list * int list list -> int list list
to perform a matrix multiplication. The matrix multiplication is given as follows:
1. Let [a1, a2, . . . , ak] and [b1, b2, . . . , bk] be two vectors of kcomponents. The product of the two vectors
is simply the value of a1×b1+a2×b2+·········+ak×bk.
2. Consider Mm×nan m×nmatrix, i.e., a matrix with mrows and ncolumns as follows.
Mm×n=
a1,1a1,2· · · a1,j · · · a1,n
a2,1a2,2· · · a2,j · · · a2,n
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
ai,1ai,2· · · ai,j · · · ai,n
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
am,1am,2· · · am,j · · · am,n
The ith row of Mforms a vector (ai,1, ai,2,· · · · · · , ai,n)called ith row-vector of M. Likewise, the
jth column of Mforms a vector (a1,j, a2,j ,· · · · · · , am,j )called jth column-vector of M.
3. For the matrix multiplication Am×kBk×n=Cm×n,the entry, ci,j , is simply the product of ith row-
vector of Aand jth column-vector of B. I.e.,
ci,j =ai,1×b1,j +ai,2×b2,j +· · · · · · +ai,k ×bk,j .
For our ML function, let a int list represent a row-vector of a matrix. Thus, a list of vectors represents a
matrix. I.e, a matrix is of type int list list. For example, let Abe [[1,2],[3,4]]. Then Ais a 2 ×2
matrix, and matrixMult (A,A) should return [[7,10], [15,22]] because of the following calculation.
1 2
3 4 ×1 2
3 4 =1×1+2×3 1 ×2+2×4
3×1+4×3 3 ×2+4×4=7 10
15 22
Due: Oct. 30, Thursday c
Chung-Chih Li P-1

Partial preview of the text

Download ITK 327, Fall 2008 Assignment 5: ML Functions for Problems & Matrix Multiplication - Prof. and more Assignments Programming Languages in PDF only on Docsity!

ITK 327, Fall 2008 Assignment 5, 40 points

Programming Assignment: due on Oct. 30, Thursday

Write ML functions for the following problems and save them in asg5.sml. As usual, follow the instructions given for the previous assignments to submit a soft-copy of your works through the web-submit. You also have to submit a hard-copy of your programs. Use the same function names required in the problems.

Part I. Chapter 9 Exercises: 3, 6, 10, 26, 27, and 28.

Part II.

  1. Write a function convert of type string -> int list that takes a string and converts it into a list of integers such that, each integer in the list is the ascii value of the corresponding character in the string. For example, convert "abc" will return [65,98,67].
  2. Write a function diff of type int list -> int that takes integer list and returns the difference between the maximum and minimum integers. For example, diff [2,9,6,1] will return 8. Note, for empty list and a list with only one integer, let diff simply return 0.
  3. Write a function powerset of type ’a list -> ’a list list that takes a list of ’a as a set and returns its powerset. For example, powerset [1,2] will return [[],[1],[2],[1,2]]. Note: The powerset of set S is the collection of all subsets of S. powerset [] should return [[]], not []. You may assume the elements in the input list are all distinct.
  4. Write a function factorize of type int -> int list that takes an integer and returns all its factors in a list. For example, factorize 12 will return [1, 2, 3, 4, 6, 12].

Part III. (Extra Bonus: 10 points) Write a function matrixMult of type

int list list * int list list -> int list list

to perform a matrix multiplication. The matrix multiplication is given as follows:

  1. Let [a 1 , a 2 ,... , ak] and [b 1 , b 2 ,... , bk] be two vectors of k components. The product of the two vectors is simply the value of a 1 × b 1 + a 2 × b 2 + · · · · · · · · · + ak × bk.
  2. Consider Mm×n an m × n matrix, i.e., a matrix with m rows and n columns as follows.

Mm×n =

     

a 1 , 1 a 1 , 2 · · · a 1 ,j · · · a 1 ,n a 2 , 1 a 2 , 2 · · · a 2 ,j · · · a 2 ,n .. .

.. .

.. .

.. .

.. .

.. . ai, 1 ai, 2 · · · ai,j · · · ai,n .. .

.. .

.. .

.. .

.. .

.. . am, 1 am, 2 · · · am,j · · · am,n

     

The ith^ row of M forms a vector (ai, 1 , ai, 2 , · · · · · · , ai,n) called ith^ row-vector of M. Likewise, the jth^ column of M forms a vector (a 1 ,j , a 2 ,j , · · · · · · , am,j ) called jth^ column-vector of M.

  1. For the matrix multiplication Am×kBk×n = Cm×n, the entry, ci,j , is simply the product of ith^ row- vector of A and jth^ column-vector of B. I.e., ci,j = ai, 1 × b 1 ,j + ai, 2 × b 2 ,j + · · · · · · + ai,k × bk,j.

For our ML function, let a int list represent a row-vector of a matrix. Thus, a list of vectors represents a matrix. I.e, a matrix is of type int list list. For example, let A be [[1,2],[3,4]]. Then A is a 2 × 2 matrix, and matrixMult (A,A) should return [[7,10], [15,22]] because of the following calculation. [ 1 2 3 4

]

×

[

]

[

1 × 1 + 2 × 3 1 × 2 + 2 × 4

3 × 1 + 4 × 3 3 × 2 + 4 × 4

]

[

]

Due: Oct. 30, Thursday ©c Chung-Chih Li P-