I have a symbolic expression (don’t ask why, that doesn’t matter), say:
exp1 = 2 n (r + h) Sin[\[Pi]/n]
Since I know, that n*Sin[[Pi]/n] is something special (namely [Pi] for large n), I want to replace that terms by an abbreviation pn:
pn = n Sin[\[Pi]/n]
and get something like exp2 from exp1 using that abbreviation. I.e.:
exp2 = 2 (r+h) pn
I tried several things like Replace, Reduce, Evaluate and the like, but could get no reasonable result. Does anybody know, how to do that kind of things?
Hmm. I think your Replace-Rule is wrong. And it does not work. And - worst of all: By that kind of things I’m doing the calculation myself and not Wolfram. Imagine that my terms n and Sin[[Pi]/n] occur in a much larger and more complicated exp1.
I also did not get your point “I’m doing the calculation myself and not Wolfram”: You wanted to replace an expression by another (simpler) expression and this what the code does
Very strange, Michael Helmle. For me this does not work. pn is not replaced in exp1.
Whatever you mean by a fresh kernel, this would not help. In my tests there is only a ClearAll[“Global`*”]. Then just an assignment for the abbreviation pn and another for exp1. Then pn should be replaced within exp1. Actually it is not replaced.
By “I’m doing the calculation myself and not Wolfram” means that within your solution - if it would work - we would do some kind of textual substitution. Hence I have to know the structure of exp1. Which I do not know of course, since exp1 came from some earlier Wolfram-calculations. exp1 can be some arbitrarily complex expression containing terms of n and Sin[[Pi]/n] in any sequence and any formular constellation.
Hi Daniel, pretty strange to do that simple task with PolynomialReduce. For me at least this does not work:
In[204]:=
PolynomialReduce[exp1, n*Sin[\[Pi]/n] - pn, {n, Sin[\[Pi]/n], pn}][[2]]
During evaluation of In[204]:= General::ivar: n Sin[\[Pi]/n] is not a valid variable.
Out[204]= 0
I cannot replicate the particular problem you are having with it. Probably you have set pn=n*Sin[Pi/n] and not cleared it. With that set the PolynomialReduce will give such a message (it does not accept explicit products as “variables” and the polynomial that defines the replacement will be useless anyway (it will evaluate to zero). This strikes me as the sort of thing that could probably have been diagnosed before posting.
There is nothing strange about performing algebraic replacements with PolynomialReduce. That’s something it is meant to do. Various forums are littered with a generalization of this method. See e.g. 123 and the many links at that last one. By the way, I found some of those links by web search, looking up “algebraic replacement in Mathematica”.
Sorry, Michael Helmle, for coming back so late. I was occupied with other things.
Your Replace-Solution works indeed. I had pn not cleared before the replacement. Pretty stupid.
And that Replace obviously does more than just textual replacement what I had assumed. It works even if I rearrange the terms in my expression.
In[44]:= pn =.;
In[45]:= 2 n (r + h) Sin[\[Pi]/n] /. n k_ Sin[\[Pi]/n] -> k pn
Out[45]= 2 pn (h + r)
In[56]:= 2 Sin[\[Pi]/n] (r + h) n /. n k_ Sin[\[Pi]/n] -> k pn
Out[56]= 2 pn (h + r)
In[55]:= 2 /n (r + h) Sin[\[Pi]/n] n^2 /. n k_ Sin[\[Pi]/n] -> k pn
Out[55]= 2 pn (h + r)