Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'functions with conditions' topicStudent Support Forum > General > Archives > "functions with conditions"

Next Comment >Help | Reply To Topic
Author Comment/Response
Andrew DuBuisson
10/21/04 2:09pm

I am having trouble using conditions with functions. If this has been answered before, please just refer me. I'll do the work, I just haven't found an answer yet.
The text below is all in the notebook too.

----------------
This is an example notebook. In my actual notebook, z is a huge function that I would not like to type in.

Independent varables: y, and x of form {x1,x2}
Arbitrary funciton: z, its the part of the problem
Arbitrary function: func[{y_,x_}]

I define an arbitrary value z:

z = x[[1]]^2;

I understand and accept the warning messages regarding the depth of x. I use immeadiate assignment because I do not want the delayed assignment.

Here's an arbitrary function that uses a condition. Notice arbitrary variable z in the third line.

In[20]:=
Clear[func]
func[{y_, x_}] := Evaluate[x + y] /; x[[1]] > y
func[{y_, x_}] := Evaluate[x + 4] /; x[[1]] < z
func[{2, {3, 4}}]
func[{9, {0, 8}}]
func[{-9, {-9, 7}}]
Out[23]={5,6}
Out[24]=func[{9,{0,8}}]
Out[25]=func[{-9,{-9,7}}]

Unfortunately the condition doesn't work with the variable z in there.

I checked to see if it would work with the value of z instead of z itself.

In[32]:=
Clear[func]
func[{y_,x_}]:=Evaluate[x+y]/;x[[1]]>y
func[{y_,x_}]:=Evaluate[x+4]/;x[[1]]<x[[1]]^2
func[{2,{3,4}}]
func[{9,{0,8}}]
func[{-9,{-9,7}}]
Out[35]={5,6}
Out[36]=func[{9,{0,8}}]
Out[37]={-5,11}

Hey it works! Unfortunately the functions I am dealing with are much longer than z = x[[1]]^2. So I need a way to get them into the condition, without hand typing them in.

I even tried reformatting the condition like one of the examples in the help notebook.

In[81]:=
Clear[func]
func[{y_,x_}]/;x[[1]]>y:=Evaluate[x+y]/;x[[1]]>y
func[{y_,x_}]/;x[[1]]<z:=Evaluate[x+4]
func[{2,{3,4}}]
func[{9,{0,8}}]
func[{-9,{-9,7}}]
Out[84]={5,6}
Out[85]=func[{9,{0,8}}]
Out[86]=func[{-9,{-9,7}}]

--------------------

Thanks,
Andrew

Attachment: conditions.nb, URL: ,

Subject (listing for 'functions with conditions')
Author Date Posted
functions with conditions Andrew DuBui... 10/21/04 2:09pm
Re: functions with conditions yehuda ben-s... 10/26/04 7:55pm
Re: functions with conditions Andrew DuBui... 10/27/04 11:03am
Next Comment >Help | Reply To Topic