Message Boards Message Boards

Replace some terms in an expression by an abbreviation?

GROUPS:

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?

POSTED BY: Werner Geiger
Answer
23 days ago
exp1 /. n k_ Sin[\[Pi]/n] -> n k pn
POSTED BY: Michael Helmle
Answer
23 days ago

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.

POSTED BY: Werner Geiger
Answer
23 days ago

Small correction: you wanted the result with n dropped, so this solution is more appropiate

exp1 /. n k_ Sin[\[Pi]/n] -> k pn
POSTED BY: Michael Helmle
Answer
23 days ago

I noticed that of course. It doesn't work either:

exp1 = 2 n (r + h) Sin[\[Pi]/n];
exp2 = exp1 /. n k_ Sin[\[Pi]/n] ->  k pn

2 n (h + r) Sin[\[Pi]/n]
POSTED BY: Werner Geiger
Answer
23 days ago

Hello Werner,

when I start with a fresh kernel I get this: enter image description here

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

POSTED BY: Michael Helmle
Answer
23 days ago

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.

For example think on:

In[189]:= exp1a = 
 2 n^2/Sin[\[Pi]/n] + Sin[2 \[Pi]/Sqrt[4 n^2]] (r + h) n

Out[189]= 2 n^2 Csc[\[Pi]/n] + n (h + r) Sin[\[Pi]/Sqrt[n^2]]

which is after the replacement with pn the same as:

In[194]:= exp2a = (2 n^3/pn + (r + h) pn)

Out[194]= 2 n^2 Csc[\[Pi]/n] + n (h + r) Sin[\[Pi]/n]

As you can verify by:

In[197]:= Simplify[exp1a - exp2a, n > 0]

Out[197]= 0

My basic question is, how could I calculate exp2a from exp1a by using the abbreviation pn.

POSTED BY: Werner Geiger
Answer
22 days ago

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)

Thank you very much.

POSTED BY: Werner Geiger
Answer
8 days ago
PolynomialReduce[exp1, 
  n*Sin[\[Pi]/n] - pn, {n, Sin[\[Pi]/n], pn}][[2]]

(* Out[11]= pn (2 h + 2 r) *)
POSTED BY: Daniel Lichtblau
Answer
23 days ago

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
POSTED BY: Werner Geiger
Answer
22 days ago

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. 1 2 3 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".

POSTED BY: Daniel Lichtblau
Answer
22 days ago

Sorry, Daniel, for coming back so late. I was occupied with other things.

Thank you very much. Your PolynomialReduce works indeed. I had pn not cleared as you assumed.

POSTED BY: Werner Geiger
Answer
8 days ago

Performing algebraic transformations with syntactic transformations sometimes requires indirection:

2 n (r + h) Sin[\[Pi]/n] /. Sin[\[Pi]/n] -> pn/n
(*  2 pn (h + r)  *)

Usually you need to replace a simple and unique (or characteristic) factor with its equivalent.

Alternatively, you can try algebraic functions:

Eliminate[{z == 2 n (r + h) Sin[\[Pi]/n], pn == n Sin[\[Pi]/n]}, Sin[\[Pi]/n]]

(*  z == pn (2 h + 2 r)  *)

z /. First@
   Solve[{z == 2 n (r + h) Sin[\[Pi]/n], pn == n Sin[\[Pi]/n]}, 
    z, {Sin[\[Pi]/n]}] // Factor

(* 2 pn (h + r)  *)

Reduce[{z == 2 n (r + h) Sin[\[Pi]/n], pn == n Sin[\[Pi]/n]}, {z}, {Sin[\[Pi]/n], n}] // Factor

(*  (pn == 0 && z == 0) || z == 2 pn (h + r)  *)
POSTED BY: Michael Rogers
Answer
23 days ago

Sorry, Michael Rogers, for coming back so late. I was occupied with other things. Thank you very much. All of your solutions work well.

POSTED BY: Werner Geiger
Answer
8 days ago

Group Abstract Group Abstract