# Function definition and delayed assignment

I need to define the following function

MyWavelet[n_]["PrimalLowpass", prec_ : MachinePrecision] :=
Table[(-1)^(j - 1) h[[2*n - j]], {j, 0, 2*n - 1}]


which depends on the following code

num = T;
precis = 32;
p[n_, x_] := Sum[ Binomial[n-1+j,j]*x^j, {j,0,n-1}]
pol[n_, z_] := Expand[z^(n-1) * p[n, (1 - (z + 1/z)/2)/2]]
roo[n_] := NSolve[pol[n, x] ==0, x, precis]
absgreater[x1_, x2_] := Abs[x1] < Abs[x2]
sroo[n_] := Sort[roo[n], absgreater]
insidehalf[n_] := Take[sroo[n], n-1]

rootlist = x /. insidehalf[num];
prod = Expand[Product[(w - rootlist[[j]]), {j, 1, num - 1}]];
normalizer = prod 2^(num - 1/2) /. w -> 1;
polyn = Expand[(1 + w)^num * prod/normalizer];
h = Reverse[N[CoefficientList[polyn, w], precis]];


where T is an integer.

My problem is I need to replace num with the value of n when calling the MyWavelet function and then dynamically evaluate, so I can get the h table values assigned to the specific MyWavelet[n].

I tried couple o'times using With, but with no success.

-
The Length of h appears to be 2num. You could feed h to MyWavelet and derive n from it. – Xerxes Mar 3 '13 at 5:29

## 1 Answer

Try specifying your function as MyWavelet[n_,opts:OptionsPattern[]] (documentation) and define Options to your function Method->"PrimalLowpass",Precision->$MachinePrecision, like this: Options[MyWavelet] ={Method->"PrimalLowpass",Precision->$MachinePrecision}


To actually construct all this as a function, you need to put certain steps as a Module, and some outside as auxiliary functions. First you will need to do this:

Clear[rootlist, prod, normalizer, polyn, h]


Then something like the following should work. Notice how I've cascaded the use of prec to some of the auxiliary functions and made them two-parameter functions. Because prec is used as a parameter in the definition of rootlist, it needs to be outside that set of Module definitions. If I had not put the definition of rootlist inside the curly braces defining the names of modularized variables, i.e. I had written Module[{rootlist, prod...},rootlist= x/.insidehalf[n,prec], this wouldn't have been necessary.

p[n_, x_] := Sum[Binomial[n - 1 + j, j]*x^j, {j, 0, n - 1}]
pol[n_, z_] := Expand[z^(n - 1)*p[n, (1 - (z + 1/z)/2)/2]]
roo[n_, prec_] := NSolve[pol[n, x] == 0, x, prec]
absgreater[x1_, x2_] := Abs[x1] < Abs[x2]
sroo[n_, prec_] := Sort[roo[n, prec], absgreater]
insidehalf[n_, prec_] := Take[sroo[n, prec], n - 1]

MyWavelet[n_Integer?Positive, opts : OptionsPattern[]] :=
With[{prec = OptionValue[Precision]},
Module[{rootlist = x /. insidehalf[n, prec], prod, normalizer,
polyn, h, w},
prod = Expand[Product[(w - rootlist[[j]]), {j, 1, n - 1}]];
normalizer = prod 2^(n - 1/2) /. w -> 1;
polyn = Expand[(1 + w)^n*prod/normalizer];
h = Reverse[N[CoefficientList[polyn, w], prec]];
Table[(-1)^(j - 1) h[[2*n - j]], {j, 0, 2*n - 1}]
]]


Note that you have small imaginary parts that you might not want:

MyWavelet[3, Precision -> 32]

{-0.03522629188570953660274066471551 +
0.*10^-33 I, -0.0854412738820266616928191691818 + 0.*10^-33 I,
0.1350110200102545886963899066994 + 0.*10^-32 I,
0.4598775021184915700951519421476 +
0.*10^-32 I, -0.8068915093110925764944936040887 + 0.*10^-32 I,
0.3326705529500826159985115891390 + 0.*10^-33 I}


You can get rid of them using Chop.

By the way, the Method doesn't currently do anything, but I assume this is a cut-down version of the real problem.

-
I made one small change - you were using the OP's symbol precis, where I suspect you wanted your symbol prec scoped by your With statement. – Mark McClure Mar 3 '13 at 13:44
Is there any specific reason that you're using number $MachinePrecision (which might be nice, if you want the precision tracked), rather than the symbol MachinePrecision (which might be nice, if you want speed)? – Mark McClure Mar 3 '13 at 13:46 The problem is the values I obtain and assign to the specific MyWavelet[n] are not a valid wavelet specification, i.e. {{0,val0},{1,val1},{2,val2},{3,val3}} for n = 2. I need to make this work so I can plot the mother and father functions, which I was unable to do using the old "change-everything-by-hand" method. – Sektor Mar 3 '13 at 15:01 Thanks @MarkMcClure - stupid typo from answering too quickly! No specific reason I used $MachinePrecision`, might have just used the one I remembered. – Verbeia Mar 3 '13 at 22:42