Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a formula that always evaluates to a certain number for any variable X under one condition that X is an integer and greater than 0.

So how can I evaluate Func[X] and specify that X is an integer greater than 0? is there a way to do that? I tried saying Func[Round[X]] but it didn't work, it just used Round[X] as a variable instead of understanding that it means X is an integer.

Ok I will give exactly what I'm trying to do, it's fairly simple:

$p= \begin{array}{cccccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0.5 & 0 & 0 & 0 & 0.5 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0.5 & 0 & 0 & 0 & 0.5 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0.5 & 0 & 0.5 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \end{array} ;$

$\text{mainWin}=\{0,12.4461,24.8921,37.3382,49.7842,62.2303,74.6763,87.1224,99.5685,112.015\};$

$u=\{0,0,0,1,0,0,0,0,0,0\};$

Now I'm evaluating: $(u.\text{MatrixPower}[p,x]).\text{money}$ where $x$ is a positive integer

This will always evaluate to: 37.3382 which if you notice is the fourth entry in $money$ which is also the state that we started with.

Now I want to know if mathematica can figure that out (that it always evaluates to this number) if I indicate that $x$ is a positive integer. If I just use $x$ normally:

(u.MatrixPower[p, x]).mainWin // Simplify

I get:

37.3382 + (2.77553*10^-24 - 1.91762*10^-31 I) (-0.5)^
x - (1.77636*10^-15 - 1.59872*10^-14 I) (-0.116393 - 0.396276 I)^
x + (3.55271*10^-15 - 1.95399*10^-14 I) (-0.116393 + 0.396276 I)^
x + (7.10543*10^-15 - 4.73403*10^-15 I) (-2.20245*10^-16)^
x - (5.68434*10^-14 + 2.27918*10^-16 I) 0.732786^x

As you can see I just want to get rid of that imaginary part

The answers here are quite educational (I'm new to Mathematica) but unfortunately they don't give the required outcome.

Just for the interested; this is modelling the outcome of a gamble where you have p as the probability matrix, your initial state is number 4. and what I'm doing here is guaranteeing that I get one outcome no matter how many times I gamble, and that outcome is equal to whatever I started with.

This is Mathematica code:

p = \!\(\*
TagBox[
RowBox[{"(", "", GridBox[{
{"1", "0", "0", "0", "0", "0", "0", "0", "0", "0"},
{"0.5`", "0", "0.5`", "0", "0", "0", "0", "0", "0", "0"},
{"0.5`", "0", "0", "0", "0.5`", "0", "0", "0", "0", "0"},
{"0", "0.5`", "0", "0", "0", "0.5`", "0", "0", "0", "0"},
{"0", "0", "0", "0.5`", "0", "0.5`", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0.5`", "0", "0.5`", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "1", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "1", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "1", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "1"}
},
GridBoxAlignment->{
        "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, 
         "Rows" -> {{Baseline}}, "RowsIndexed" -> {}, "Items" -> {}, 
         "ItemsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}, "Items" -> {}, 
         "ItemsIndexed" -> {}}], "", ")"}],
Function[BoxForm`e$, 
    MatrixForm[BoxForm`e$]]]\);
mainWin = {0, 12.4460563, 24.89211259, 37.33816889, 49.78422519, 
   62.23028148, 74.67633778, 87.12239407, 99.56845037, 112.0145067};
u = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0};
(u.MatrixPower[p, x]).mainWin // Simplify
share|improve this question
1  
Please post a minimal example of your problematic code. While you're at it, you may want to check out this question's answers regarding function definiton and this tutorial. –  VF1 Apr 10 '13 at 19:08
    
I don't see a simple way to copy your matrix p -- would you please include a copy of that in Mathematica code so I don't have to waste time on it? –  Mr.Wizard Apr 10 '13 at 20:36
    
Ok I added my code –  Space monkey Apr 10 '13 at 21:09
1  
@Mr.Wizard I discovered to my surprise that if you right click in the bottom right part of the LaTeX matrix above you get a MathJax menu with the option to copy as MathML which Mathematica can deal with. The resulting matrix is: {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0}, {0.5, 0, 0, 0, 0.5, 0, 0, 0, 0, 0}, {0, 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0}, {0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0}, {0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1}} –  Sjoerd C. de Vries Apr 10 '13 at 21:11
1  
@Spacemonkey Just use Chop to get rid of the small complex numbers. –  Sjoerd C. de Vries Apr 10 '13 at 21:15
add comment

4 Answers

Another method is to use PatternTest along with specifying the Head:

f[x_Integer?Positive] := (* ... *)

this is similar in intent to using Condition, above, but I find it is more straightforward for simple conditions. Of course, you can combine them using

f[x_Integer] /; x > 0 := (* ... *)
share|improve this answer
add comment

One interpretation of the question is to specify that a function only holds for an integer argument. This can be done like this:

f[x_Integer] := x^2

which specifies that the function only applies when x happens to be an integer. So for instance, f[3] evaluates to 9, but f[2.5] just returns f[2.5] unevaluated.

share|improve this answer
add comment

How about defining the function like this:

Func[x_] /; IntegerQ[x] && x > 0 := (* Function definition *)

This guarantees that only integers greater than zero will be accepted by the function Func

share|improve this answer
add comment

In your revised problem, it looks like you are calculating some properties of a Markov process with state transition matrix $P$. There is a large and powerful set of functions in Mathematica 9 that deal with Markov processes. See, for instance DiscreteMarkovProcess and MarkovProcessProperties which give things line the steady state distribution (which seems to be closely related to what you are calculating).

Fort he lower part of your problem, define the function:

f[x_]:=Chop[37.3382 + (2.77553*10^-24 - 1.91762*10^-31 I) (-0.5)^
x - (1.77636*10^-15 - 1.59872*10^-14 I) (-0.116393 - 0.396276 I)^
x + (3.55271*10^-15 - 1.95399*10^-14 I) (-0.116393 + 0.396276 I)^
x + (7.10543*10^-15 - 4.73403*10^-15 I) (-2.20245*10^-16)^
x - (5.68434*10^-14 + 2.27918*10^-16 I) 0.732786^x]

then you can see the simplified form using

Chop[f[x]]

which gives:

37.3382 + 0^(1 + x)

So as long as x is a positive integer, this is just 37.3382. In case this is not obvious,

Simplify[37.3382 + 0^(1 + x), Assumptions -> x > 1]

which returns just the constant.

share|improve this answer
    
Thanks a lot that's quite useful, but is there away to say that x is always positive so that Mathematica understands that it will always evaluate to 37.3382? –  Space monkey Apr 11 '13 at 9:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.