[Date Index]
[Thread Index]
[Author Index]
Questions about rules
*To*: mathgroup at yoda.physics.unc.edu
*Subject*: Questions about rules
*From*: Jean-Paul Roy <roy at taloa.unice.fr>
*Date*: Thu, 7 Jan 93 14:59:57 +0100
3 packages of questions to Mma gurus about RULES (version 2.1).
A) I want my program to dynamically generate rules. Say that a global variable
z has value 3 at some moment, and the function foo is undefined:
z = 3; x = 10;
?foo
Information :: symbol foo not found
What then must my running *program* do to get after execution :
?foo
foo[x_] := x + 1 /; x < 3
where the 3 stands for the value of z at the moment of the definition of foo.
I want exactly *this*, not a more complicated form with Evaluate[z]. I tried
many forms with DownValues, Hold, ... but couldn't get the desired result.
B) I suppose it's the same problem if I want to generate a new rule like:
bar[x_] := Which[x < 1, 3, x < 2, 6, True, 9]
where 3 stands for z, 6 for 2z and 9 for 3z ? The 'True' may also
cause problems. I'm trying in A) and B) to generate piecewise curves from
interpolation points.
Notice that z and x both have values when the rule is generated (this is
normal for z but may cause problems for x, as I want to capture the value of z
but not the value of x).
Maybe am I trying to practise Lisp macros in Mma ? Apparently I didn't get
the right way to do it. Anyway, being able to dynamically build rules is
very important, and we should be able to do it *easily*...
C) What am I missing here ? The variable n is unbound but declared Integer:
n /: IntegerQ[n] = True;
1 + Sin[n Pi] /. Sin[k_Integer Pi]->0
(* unchanged *)
1 + Sin[n Pi] /. Sin[k_ Pi] -> 0
(* 0 but not always correct *)
Replace[Sin[n Pi],Sin[n_ Pi] -> 0 /; IntegerQ[n]]
(* obsolete warning, result 0 *)
Replace[Sin[n Pi],Sin[k_ Pi] -> 0 /; IntegerQ[k]]
(* obsolete warning, result Fail *)
1/Sqrt[x] /. Sqrt[y_]->2
(* unchanged *)
1/Sqrt[x] /. Sqrt[x]->2
(* unchanged but 1+Sqrt[x] /. Sqrt[x]->2 is correct, result 3 !!! *)
*Please* explain me...
Jean-Paul Roy
Departement d'Informatique
Faculte des Sciences de Nice
[roy at taloa.unice.fr]
Prev by Date:
**Re: What to do?**
Next by Date:
**Generating random numbers from any given distribution**
Previous by thread:
**Re: What to do?**
Next by thread:
**Generating random numbers from any given distribution**
| |