#OK to post homework #Yukun Yao, Feb. 23, Assignment 9 ###Problem 2### Generalize ArData(L1,H1,N,c) to an arbitary number of dimensions. Write a procedure but with random points from the n-dimensinal cube [0,A]^n . More generally, write a procedure #ArDataMV(A,v,N) #that inputs a pos. number A, and a vector of numbers v (given as a list), (let n:=nops(v)) and outputs a list of length N where each entry is a pair obtained by picking a random point (a list of length n) in [-A,A]^n and returns the pair [x,0] if v.x (the dot product of v and x) is negative and [x,1] if it is zero or positive. ArDataMV:=proc(A,v,N) local n, L, ra, i, new, j: ra:=rand(-A..A): L:=[]: n:=nops(v): for i from 1 to N do new:=[seq(ra(), j=1..n)]: if add(new[j]*v[j], j=1..n) < 0 then L:=[op(L), [new, 0]]: else L:=[op(L), [new, 1]]: fi: od: L: end: ###Problem 3### Implement the Perceptron algorithm in n dimensions (n arbitrary) by writing a procedure #PAmv(L,n) #that inputs a list containing data of the form [VectorOfLenghn,ZeroOrOne] where, VectorOfLengthn is a list of length n, and n is the dimension. PAmv:=proc(L, n) local para, l: if not (type(L, list) and type(n, integer) and n>0 and {seq(nops(l), l in L)} = {2} and {seq(nops(l[1]), l in L)} = {n} and {seq(type(l[1], list), l in L)} = {true}) then return fail: fi: para:=[0\$n]: for l in L do if convert(para *~ l[1], `+`) <0 and l[2]=1 then para:=para + l[1]: elif convert(para *~ l[1], `+`) >=0 and l[2] = 0 then para:=para - l[1]: fi: od: para: end: ###Problem 4### Write an n-dimensional extension to TestP(L,c). Call it TestPMV(L,v) where v is a vector (list) with n components. TestPMV:=proc(L, v) local fn, fp, l: fn:=0: fp:=0: for l in L do if convert(v *~ l[1], `+`) <0 and l[2]=1 then fn:=fn+1: elif convert(v *~ l[1], `+`) >=0 and l[2]<1 then fp:=fp+1: fi: od: [fn, fp, nops(L)]: end: ###Problem 5### Write a procedure PA1corrected(L) that first transfoms the list L to the format [[data,1],ZeroOrOne], and uses your procedure PAmv(L,n) with n=2 to outputs two numbes a and b such that L[i][2]=1 iff ax+b>=0. Compare its performance to PA1(L). PA1corrected:=proc(L) local S, l: S:=[ seq([[l[1],1], l[2]], l in L) ]: PAmv(S, 2): end: