Replacing expressions with smaller atoms

Mathematica

    Next

  • 1. Threading 'Append'
    If I run Thread[Append[{{1,2},{3,4},{5,6}},{a,b,c}]] I would expect to get {{1,2,a},{3,4,b},{5,6,c}} but in fact I get an 'Objects of unequal length' error. On the other hand, Thread[foo[{{1,2},{3,4},{5,6}},{a,b,c}]] %/.foo->Append gives what I wanted. Can someone explain what's going on?
  • 2. Formatting GridBox with ColumnAlignments
    A formatting question: If we let r1 = DisplayForm[RowBox[{"(", 1, 2, 3, ")"}]] and r2 = DisplayForm[Overscript[RowBox[{1, 2, 3, 4, 5}], \[OverBracket]]] and then put r1 and r2 together in r3 = DisplayForm[GridBox[{{r1}, {r2}}, ColumnAlignments -> Left]] then we get something that's not very aligned at all. (Evaluating r3 shows quit a bit of whitespace prior to the open parenthesis in r2, making the two rows in the grid look somewhat messy). Question: is it possible to make the "1"s align exactly in r3? (ColumnAlignments -> "1" isn't doing the trick.) Trevor.

Replacing expressions with smaller atoms

Postby Ben Forbes » Sun, 07 Jun 2009 05:04:38 GMT

If I define an atom eg L=x^2+x+1, is there a way to rewrite an
expression with these atoms? For example:

L=x^2+x+1
M=x+(x(x^2+x+1))^(1/2)

I would like some way to express this as x+(xL)^(1/2). Is this possible?


Re: Replacing expressions with smaller atoms

Postby Leonid Shifrin » Sun, 07 Jun 2009 05:07:18 GMT

Ben,

one way would be to use a local rule like L:>(x^2+x+1). Your L then does not
have a global value, you use it in your expression until you want to
substitute it with x^2+x+1, then you just use (your
expression)/.L:>(x^2+x+1)

It would be easier to answer your question if you'd provide more information
about your problem/goals.

Regards,
Leonid





Re: Replacing expressions with smaller atoms

Postby DrMajorBob » Sun, 07 Jun 2009 05:10:25 GMT

What you want could be difficult, since M can be expressed in terms of L  
alone (in 4 ways):

Clear[M,L]
Factor /@ (M /.
    Solve[{L == x^2 + x + 1, M == x + (x (x^2 + x + 1))^(1/2)}, M, x])

{1/2 (-1 - Sqrt[-3 + 4 L] - Sqrt[2] Sqrt[-L (1 + Sqrt[-3 + 4 L])]),
  1/2 (-1 - Sqrt[-3 + 4 L] + Sqrt[2] Sqrt[-L (1 + Sqrt[-3 + 4 L])]),
  1/2 (-1 + Sqrt[-3 + 4 L] - Sqrt[2] Sqrt[L (-1 + Sqrt[-3 + 4 L])]),
  1/2 (-1 + Sqrt[-3 + 4 L] + Sqrt[2] Sqrt[L (-1 + Sqrt[-3 + 4 L])])}

But pattern matching saves the day:

m = x + (x (x^2 + x + 1))^(1/2);
ell = x^2 + x + 1;
m /. ell -> L

x + Sqrt[L x]

That required FullForm[ell] to be plainly visible in FullForm[m], so  
things won't always be so simple.

Bobby







-- 
 XXXX@XXXXX.COM 


Re: Replacing expressions with smaller atoms

Postby Bob Hanlon » Sun, 07 Jun 2009 05:10:48 GMT

M = x + (x (x^2 + x + 1))^(1/2);

Simplify[M, L == x^2 + x + 1]

Sqrt[L*x] + x

M /. x^2 + x + 1 -> L

Sqrt[L*x] + x


Bob Hanlon




=============
If I define an atom eg L=x^2+x+1, is there a way to rewrite an
expression with these atoms? For example:

L=x^2+x+1
M=x+(x(x^2+x+1))^(1/2)

I would like some way to express this as x+(xL)^(1/2). Is this possible?




Re: Replacing expressions with smaller atoms

Postby Curtis Osterhoudt » Sun, 07 Jun 2009 16:46:05 GMT

The first of those solutions (with Simplify) looks brilliant, but I don't think I've ever seen that sort of form anywhere in the Mathematica documentation. I've used the second argument(s) to Simplify often, but didn't know it'd work like that. Is there some obvious way to see that substitutions will work that way, or did you figure it out some other way. 

    In any case, thanks, Bob. I'm going to see what else Simplify can do.

                     Cheers, 
                                C.O.






-- 
==================================
Curtis Osterhoudt
 XXXX@XXXXX.COM 
PGP Key ID: 0x4DCA2A10
==================================


Re: Replacing expressions with smaller atoms

Postby Ben Forbes » Sun, 07 Jun 2009 16:48:56 GMT

Thanks this worked fairly well.







Return to Mathematica

 

Who is online

Users browsing this forum: No registered users and 49 guest