Message Boards Message Boards

0
|
4758 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Can any one explain a few lines of code to me?

Posted 10 years ago

Hello there ! I am pretty sure that this kind of question is not very welcomed in here, but I have no choice other than posting my question here.

I know that defining functions in Mathematica is like f[x_] := x, but I have came across a function definition that I cant figure out its functionallity the function is like below:

update2[0,0,p_] := if[RandomReal[] <= p, {1,1, p}, {3,0,p}];
update2[0,_,p_] := {5,0,0};
update2[1,0,p_] := {2,1,0};
update2[1,b_,p_] := {0,0, p*(1/2)};
update2[2,_,_] := {2,0,0};
update2[3,0,p_] := {0,0,Min[p * 2, 1/2]};
update2[3,b_,p_] := {0,0,p*(1/2)};
update2[5,_,_] := {5,0,0}

this function is called on elements of three arrays like s,b, p that are of length n

It would be very great If anyone could explain what this function is doing.

POSTED BY: Hadi Amiri
4 Replies

The problem must be somewhere else

In[154]:= states = Range[0, 5];
Count[states, 0 | 1 | 3]

Out[155]= 3

that's correct (Mathematica 10.1, Windows 7 64 Bit Home Premium), because 3 entries of {0,1,2,3,4,5} fullfill the pattern of being 0 or 1 or 3. Possibly the Alternatives sign | got the wrong key on problem owners key board?

POSTED BY: Udo Krause
Posted 10 years ago

I have another question regarding the function Count , Inside a code that I am reviewing I see the function Count is called like this, but I am getting an error that I am giving it too many arguments.

enter image description here

state is just an array of values 0,1,2,3,5 what is the problem??

POSTED BY: Hadi Amiri

It may have only been a typo, but be sure to change the

if

to

If
POSTED BY: David Reiss

It's using pattern matching to handle general and specific cases of the arguments. The specific cases take precedence. For example:

In[6]:= f[x_, _] := x^2

In[7]:= f[x_, 3] := x^3

In[8]:= f[2, 5]

Out[8]= 4

In[9]:= f[2, 3]

Out[9]= 8
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract