Re: functions

*To*: mathgroup at smc.vnet.net*Subject*: [mg45513] Re: functions*From*: Bill Rowe <readnewsciv at earthlink.net>*Date*: Sat, 10 Jan 2004 16:43:35 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

On 1/10/04 at 12:00 AM, lorenzo.keegan at handbag.com wrote: > How do write expressions in Mathematica for functions and sequences > such as the following: > f(x) = {1/x, x is irrational > {x^2, x rational > and > f(n) = 1/n, n odd > n^2, n even f(n) can be defined as follows to achieve what you want f[n_Integer]:= 1/n /;Mod[n, 2]==1 f[n_Integer]:= n^2 /;Mod[n, 2]==0 What I've done here is use the _Integer construct to define f for integers and /; to define conditions. If there was a way for Mathematica to distinguish between rational and irrational numbers, it would be possible to define something similar for your first case. The problem is in any computer algebra system irrational numbers can only be approximated with a finite number of digits. Since any number with a finite number of digits is rational, there is no way to distinguish between a string of digits meant as an approximation to an irrational number and the rational number represented by the same string of digits. You can write a function g[x_Rational]:= x^2 which will output x^2 for all exact rational numbers. That is g[1/2] will output 1/4. But, g[.5] will not be evaluated. Mathematica restricts the definition of Rational to exact numbers. Since .5 is an approximate number, it will not be considered rational for a function defined this way. -- To reply via email subtract one hundred and four