1
$\begingroup$

I want to define a variable for a set of computations to be a positive integer (or more generally, say I want to define it as integer or real, since Mathematica seems to take a variable as a complex one by definition). I am familiar with Fortran 95, where we define variables in the beginning to be integer or real, what is the corresponding set of command(s) in Mathematica for the same purpose. Thanks

$\endgroup$

marked as duplicate by Szabolcs, MarcoB, garej, b3m2a1, m_goldberg Jun 16 '17 at 0:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Variables are all general type in Mathematica. There is no static typing of the kind your describe. Mathematica is a bit special in that it doesn't even check that a variable has any assigned value (being a symbolic language), but otherwise it treats its variable similar to, e.g., python. However, a good practice is to give the variable a name that makes it clear to you what type it is. $\endgroup$ – b3m2a1 Jun 15 '17 at 2:57
  • $\begingroup$ @b3m2a1 keeping in mind what you said, so is there any condition or assumption that I can put on the variables so as to make it, say a positive integer (or real)? $\endgroup$ – Bruce Lee Jun 15 '17 at 3:52
  • 1
    $\begingroup$ Outside of simply assigning a it a real or integer value? Not really. You can check the type using, e.g. Assert and MatchQ. I think you might be well served by reading a tutorial. It'll clear up your confusions and help you get started with the language. $\endgroup$ – b3m2a1 Jun 15 '17 at 3:55
  • 1
    $\begingroup$ See the documentation for IntegerQ and Element for a partial answer to your question. $\endgroup$ – bbgodfrey Jun 15 '17 at 4:19
  • 2
    $\begingroup$ Assuming that a is such an integer, positive variable, try to evaluate $Assumptions[a > 0, a \[Element] Integer]; in the very first cell of your notebook. Mma will know this definition through the whole session. $\endgroup$ – Alexei Boulbitch Jun 15 '17 at 6:26