4
$\begingroup$

I want to define a named constant standing for a more complicated symbolic expression, for example, PlasticConstant for the plastic constant:

((9 - √69)^(1/3) + (9 + √69)^(1/3))/(2^(1/3) 3^(2/3))

and I want it to have evaluation rules similar to GoldenRatio:

  • it remains in its named form during normal evaluation (so, simple Set does not solve my problem);
  • it can be numerically evaluated to an arbitrary precision using N;
  • it can be converted to the underlying expression in radicals by FunctionExpand, can be converted to a Root object by RootReduce;
  • FullSimplify is able to replace it with its underlying expression when it helps reducing overall complexity of a containing expression, but leaves it in its named form otherwise;

Equality and inequality operators between the named constant and its underlying expression should automatically evaluate to Boolean values:

PlasticConstant == ((9 - √69)^(1/3) + (9 + √69)^(1/3))/(2^(1/3) 3^(2/3))
(* True *)

similar to how it happens here:

GoldenRatio == (1 + √5)/2
(* True *)

How can we do it?

$\endgroup$
2
  • 2
    $\begingroup$ Related (128444). It doesn't cover FunctionExpand, RootReduce or FullSimplify. $\endgroup$
    – Carl Woll
    Commented Jun 4, 2018 at 23:37
  • $\begingroup$ I'd try it with upvalues, but I'm not sure which cases will suffice. $\endgroup$
    – John Doty
    Commented Jun 4, 2018 at 23:47

1 Answer 1

4
$\begingroup$

Rather long for a comment:

I think the OP's goals are going to be hard to achieve. GoldenRatio is embedded in the system in ways I don't know. Here is some examples of the embedding that come from an internal Trace of FunctionExpand@GoldenRation:

GoldenRatio /. SimplifyDump`PositiveRules
SimplifyDump`$FSTab /@ %
(*
  {901}
  {GoldenRatio :> 1/2 (1 + Sqrt[5])}
*)

Some of the OP's goals can be handled programmatically. For instance:

ClearAll[OneThird];
SetAttributes[OneThird, {Constant}];
N[OneThird, p_: MachinePrecision] := N[1/3, p];

OneThird // N[#, 22] &
(*  0.3333333333333333333333  *)

However, automatic conversion to its numerical value does not happen for OneThird as it does for GoldenRatio:

3. OneThird
3. GoldenRatio
(*
  3. OneThird
  4.8541
*)

Maybe someone else will know how to do it. An internal trace reveals nothing.

$\endgroup$
6
  • 3
    $\begingroup$ You need NumericQ[OneThird] = True as in @Szabolcs' answer to the related question. $\endgroup$
    – Carl Woll
    Commented Jun 4, 2018 at 23:49
  • $\begingroup$ @CarlWoll Thanks. $\endgroup$
    – Michael E2
    Commented Jun 4, 2018 at 23:51
  • $\begingroup$ Also, I usually do N[OneThird, _] = 1/3 instead, but I don't think it makes a difference. $\endgroup$
    – Carl Woll
    Commented Jun 4, 2018 at 23:51
  • $\begingroup$ I think rules for FunctionExpand and FullSimplify can be added somehow via SimplifyDump`PositiveRules and SimplifyDump`$FSTab. What do numeric indexes in the latter mean? What index do I use to add my own entry? $\endgroup$ Commented Jun 5, 2018 at 17:34
  • $\begingroup$ @VladimirReshetnikov Yes, I think you're right. The numbers are indices for $FSTab. You can look at them with ?SimplifyDump`$FSTab. Obviously you would use an index that is not present in Cases[DownValues[SimplifyDump`$FSTab], HoldPattern@SimplifyDump`$FSTab[n_Integer] :> n, Infinity]. (I don't know for sure that any missing one is safe.) $\endgroup$
    – Michael E2
    Commented Jun 5, 2018 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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