MMA is a free country. But TOO free sometimes.

Just as an example:

a[x_] == a[y_] ^:= Round[x] == Round[y]
{a[3] == a[3.01], a[3] == a[4]}

is {True, False}. However,

{a[3] != a[3.01], a[3] != a[4]}

is NOT {False, True}. Of course I have to define for Unequal[]. MMA is flexible, but too much in some situation.

So, I want a "prison". Does the prison such as guidelines or "Eq class" exist ?

ps. sorry for my poor english.

  • 3
    $\begingroup$ What do you really need? Is a[x_]:=Round@x or (b : Equal | Unequal)[a[x_], a[y_]] ^:= b[Round[x], Round[y]] satisfying? BTW, what's your first language? $\endgroup$ – xzczd Oct 31 '15 at 6:22
  • 3
    $\begingroup$ @asoldat What do you mean saying that {a[3] != a[3.01], a[3] != a[4]} is NOT {False, True} ? If you don't define the UpSetDelayed for Unequal the result for {a[3] != a[3.01], a[3] != a[4]} is {a[3] != a[3.01], a[3] != a[4]} $\endgroup$ – Guido Oct 31 '15 at 6:25
  • 1
    $\begingroup$ As Guido notes, I would not consider this behavior wild: the behavior was only defined with respect to just one symbol, and Mathematica respected that. Thus, you have at least six relational operators to deal with, as well as Inequality[]. $\endgroup$ – J. M. will be back soon Oct 31 '15 at 6:33
  • 9
    $\begingroup$ eqClass = {Equal, SameQ, Unequal, UnsameQ, Less, LessEqual, Greater, GreaterEqual}; ClearAll[a]; a /: h_[a[x_], a[y_]] /; MemberQ[eqClass, h] := h[Round[x], Round[y]]; {a[3] == a[3.01], a[3] != a[4]} would be a way to go, but there's probably something being lost in translation here? $\endgroup$ – rm -rf Oct 31 '15 at 6:51
  • $\begingroup$ @R.M. the eqClass which ive wanted. Any other "class" in MMA ? $\endgroup$ – asoldat Oct 31 '15 at 8:53

Your definition

a[x_] == a[y_] ^:= Round[x] == Round[y]

Is structural. It tells Mathematica how to rewrite certain expressions. It has no mathematical meaning. Mathematica has no mechanism to infer mathematical meaning from the rewrite rules you provide. It cannot determine what mathematical consistency might mean for your symbols. Rewrite rules are not mathematics, but the material you use to construct mathematics. So, you must provide all of the necessary definitions to make your symbols behave as you wish.

R. M.'s comment (which should really be an answer) is a clever way to represent all of the necessary definitions in one definition for this case.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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