Re: Simplifying Problems
- To: mathgroup at smc.vnet.net
- Subject: [mg22480] Re: Simplifying Problems
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Wed, 8 Mar 2000 02:22:19 -0500 (EST)
- Organization: University of Western Australia
- References: <89ce39$brr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Jordan Rosenthal wrote:
> Let's say I declare a variable to be an Integer with
>
> j \[Element] Integers
>
> Now I set up a function which should only work on integers
>
> f[x_?IntegerQ] = x+2
>
> This, however, does not recognize that the variable j has been declared an
> integer:
>
> In[3]:=
> f[2]
>
> Out[3]=
> 4
>
> In[4]:=
> f[j]
>
> Out[4]=
> f[j]
>
> Is there a way I can get the function to work for variables declared as
> integers with the Element function?
Here is one way:
Notebook[{
Cell[TextData[{
"I assume that you declared ",
Cell[BoxData[
FormBox[
RowBox[{"j", "\[Element]",
TagBox["\[DoubleStruckCapitalZ]",
(Integers&)]}], TraditionalForm]]],
" as follows?"
}], "Text"],
Cell[CellGroupData[{
Cell[BoxData[
FormBox[
RowBox[{"j", "/:",
RowBox[{"j", "\[Element]",
TagBox["\[DoubleStruckCapitalZ]",
(Integers&)]}], "=", "True"}], TraditionalForm]], "Input"],
Cell[BoxData[
\(TraditionalForm\`True\)], "Output"]
}, Open ]],
Cell[TextData[{
"Note that this does ",
StyleBox["not",
FontSlant->"Italic"],
" work,"
}], "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(TraditionalForm\`IntegerQ[j]\)], "Input"],
Cell[BoxData[
\(TraditionalForm\`False\)], "Output"]
}, Open ]],
Cell["but this does.", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
FormBox[
RowBox[{"j", "\[Element]",
TagBox["\[DoubleStruckCapitalZ]",
(Integers&)]}], TraditionalForm]], "Input"],
Cell[BoxData[
\(TraditionalForm\`True\)], "Output"]
}, Open ]],
Cell["Defining a test function:", "Text"],
Cell[BoxData[
FormBox[
RowBox[{\(test(x_)\), ":=",
RowBox[{
RowBox[{"x", "\[Element]",
TagBox["\[DoubleStruckCapitalZ]",
(Integers&)]}], "\[Or]", \(IntegerQ[x]\)}]}],
TraditionalForm]], "Input"],
Cell["You can define your function as follows:", "Text"],
Cell[BoxData[
\(TraditionalForm\`f(x_?test) := x + 2\)], "Input"],
Cell["Examples:", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(TraditionalForm\`f(2)\)], "Input"],
Cell[BoxData[
\(TraditionalForm\`4\)], "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
\(TraditionalForm\`f(j)\)], "Input"],
Cell[BoxData[
\(TraditionalForm\`j + 2\)], "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
\(TraditionalForm\`f(k)\)], "Input"],
Cell[BoxData[
\(TraditionalForm\`f(k)\)], "Output"]
}, Open ]]
}
]