# Tag Info

## Hot answers tagged expression-manipulation

17

The following seems to work, however I think it's not general enough: At a clean nb, enter: For[i = 0, i < 4, i++, Print[{i, {33, i}}]] For[i = 0, i < 4, i++, Print[Graphics[Circle[], ImageSize -> 20]]] And then retrieve the Print[ ] output as: c = Cases[NotebookRead /@ Cells[GeneratedCell -> True], Cell[___, "Print", ___]]; ToExpression ...

14

I assume you have Maple to use. If so, Simply open Maple and type the Mathematica command itself directly into Maple using the FromMma package built-into Maple, like this: restart; with(MmaTranslator); #load the package (*[FromMma, FromMmaNotebook, Mma, MmaToMaple]*) and now can use it FromMma(Integrate[Cos[x],x]); One can also use Maple convert ...

9

You can use Normal, ConditionalExpression is not explicitly mentioned there but documentation says it deals with special forms. p1 = y /. {First[Solve[x^2 + y^2 + x == 1, y, Reals]]} // First ConditionalExpression[-Sqrt[1 - x - x^2], 1/2 (-1 - Sqrt[5]) < x < 1/2 (-1 + Sqrt[5])] Normal @ p1 -Sqrt[1 - x - x^2]

8

Please let me know if this is moving in the right direction: expr = a + b*3*c + d; Replace[expr, h_[x___] :> {x}, {0, -1}] {a, {3, b, c}, d} Given that heads are lost here, perhaps you want something like: Replace[expr, h_[x___] :> {h, x}, {0, -1}] {Plus, a, {Times, 3, b, c}, d} If this is close to what you a related question that you ...

8

I think you just need this: firstExternal[head_, expr_] := Module[{tag}, expr /. p_head :> Return[p, Module]] or perhaps, even much more elegantly: firstExternal[head_, expr_] := expr /. p_head :> Return[p, ReplaceAll] For example: firstExternal["Head of interest", deepExpression] (* "Head of interest"["This should be the first part of the ...

7

As of version 9 this has changed http://reference.wolfram.com/mathematica/Compatibility/tutorial/Utilities/FilterOptions.html So this package needs to use FilterRules. I Downloaded this package, and changed Summa.m according to the above, and now it loads ok. Changed every place it said FilterOptions to Sequence@@FilterRules. Run few tests from the ...

7

Convert Maple expressions to Mathematica: Through latex： Through free form input：

7

You can forcely specify the condition to be True: Solve[x^2 + y^2 + x == 1, y, Reals] /. ConditionalExpression[e_, _] :> ConditionalExpression[e, True] {{y -> -Sqrt[1 - x - x^2]}, {y -> Sqrt[1 - x - x^2]}} But you should always keep it in mind that this is not an identical transformation.

7

E.g. using Cases: Cases[expr, _F, Infinity] {F[0, 0], F[0, 1], F[2, 0]} Note that the 3rd argument is the levelspec. See e.g. expr//FullForm why it's needed EDIT (I wasn't careful!) Note that this does not work for expr = F[0,0] as by default, Cases does not match the whole expression (it starts at level 1). If that could be the case, you can ...

6

You can use FullSimplify and play with the ComplexityFunction Option until you obtain a satisfactory result. For example: Let's define our function in terms of LeafCount c[n_][e_] := n Count[e, _Sin | _ArcTan, Infinity] + LeafCount[e] Then: FullSimplify[Sin[1/2 ArcTan[(2 Log[5])/(Log[5]^2 - 2)]], ComplexityFunction -> c[#]] & /@ Range[40, 60, ...

6

Maybe we can learn something from what Compile produces. cf = Compile[ {{w, _Real}, {y, _Real}} , w - 4 (w - y) ((w - y)^2 y + 6 (1 + y) ((w - y) y + (1 + y)^2))/((w - y)^2 ((w - y) y + 6 y^2 + 8 y (1 + y)) + (1 + y)^2 (36 (w - y) y + 24 (1 + y)^2)) ] In the compiled code we see that at least the number of ...

6

Here is another way using Evaluate: ops1 = {Filling -> {1 -> {2}}, PlotStyle -> {Red, Green}, Frame -> True}; Plot[{Sin[x] + x/2, Sin[x] + x}, {x, 0, 10} , Evaluate@ops1 , PlotLabel -> Style["Using Evaluate", 20] ]

6

On Leonid's valued opinion that the Close was inappropriate I have reopened this question. By my interpretation this does what is requested: SetAttributes[setSpec, HoldAllComplete] setSpec[s_Symbol, spec__] := s /: h_[pre__, s, post___] := h[pre, spec, post] The usage is: setSpec[ops1, Filling -> {1 -> {2}}, PlotStyle -> {Red, Green}, Frame ...

6

The only method I can think of that will use the built-in simplification routines is to snoop on transformations using either TransformationFunctions or ComplexityFunction. Unfortunately neither of these will be restricted to the entire expression therefore what is produced may not be usable. Nevertheless as an example: FullSimplify[Gamma[1 - x] Gamma[x] ...

6

Sequence might be useful if your expressions come inside other expressions. For example: num = 10; lst = MapThread[ #1@#2 &, { RandomChoice[{Cos, Sin, Exp, Tan, Cot, ArcTan, ArcTanh}, num], RandomChoice[{x, y, z}, num] } ] (* {ArcTan[x], ArcTan[x], ArcTan[x], Cot[x], Cot[z], Cot[x], ArcTan[z], ArcTanh[y], ArcTanh[x], Cot[y]} *) ...

6

You can actually Delete the head of the expression, which is part 0: Delete[#, 0] & /@ {Cos[a], Sin[b], Tan[c]} {a, b, c} One case of interest may be held expressions. If our expression is: expr = HoldComplete[2 + 2]; And the head we wish to remove is Plus, we cannot use these: Identity @@@ expr Sequence @@@ expr expr /. Plus -> Identity ...

6

This is my take on the pad, thread normally, then remove padding approach. fred[expr_, head_: List, seq_: All] := Module[{myhold, maxlength, dummy, paddedexpr}, SetAttributes[myhold, HoldAllComplete]; maxlength = Max@Cases[expr, head[args___] :> Length@Hold@args, {1}]; paddedexpr = Replace[expr, head[args___] :> ...

6

Will a = -3; Print[Defer[\[FormalA] x + 5 + x^2] /. \[FormalA] -> a] -3 x + 5 + x^2 work for you?

5

In our case a simple and direct approach would be defining a list of rules. Here is an example: rules = { c_ Sum[n a[n] c_^(n-1), {n, 0, Infinity}] :> Sum[n c^n a[n], {n, 0, Infinity}], α_ Sum[a[n] c_^n, {n, 0, Infinity}] + Sum[n a[n] c_^n, {n, 0, Infinity}] :> Sum[(α + n) a[n] c^n, {n, 0, Infinity}]}; Let's define an appropriate ...

5

Using Nasser's expression code as an example: expr = Sin[x] + Cos[y] + z^3 + Exp[d] + h + 3 h^2 + 4 h^3 + Integrate[Exp[p], p] + D[Sin[m]^Exp[f], m]*Series[Sin[g], {g, 0, 3}] + 2 (E^a BesselK[0, 2 Sqrt[E^a]]) C[2]/D[Gamma[w], {w, 2}]; You might use: Variables @ Level[expr, {-1}] {a, d, f, g, h, m, p, w, x, y, z} To extract indexed ...

5

You can open a whole Mathematica notebook in Maple using its Open... menu, and all expressions in the notebook will be converted to Maple representation:

5

Maybe TimeConstraint is helpful: y = Gamma[1 - x] Gamma[x] Sin[Pi x] + Gamma[x] Gamma[1 - x] Sin[Pi (1 - x)]; FullSimplify[y, TimeConstraint -> 0.000001] FullSimplify[y, TimeConstraint -> 0.0001] FullSimplify[y, TimeConstraint -> 0.01] Gamma[1 - x] Gamma[x] Sin[π (1 - x)] + Gamma[1 - x] Gamma[x] Sin[π x] 2 Gamma[1 - x] Gamma[x] Sin[π x] 2 π

5

I'm not sure why you don't like your merge function. This is the same pattern matching approach but using ReplaceAll: {e1, e2} /. {Hold[x_], Hold[y_]} :> Hold[x; y] If you would rather avoid patterns here is one (ugly) option: Block[{CompoundExpression}, (e1; e2) ~Thread~ CompoundExpression ~Thread~ Hold]

5

No, this is not possible. A cell can only be evaluated if it contains a complete and syntactically correct expression. You might want to try Code style cells (Alt-8 or Command-8) which contain plain text and allow arbitrary formatting with spaces, tabs and newlines.

5

Since it's the only "answer" I can see to post (as CW) there is also BooleanFunction as originally pointed out by Sasha. In version 10 Dispatch tables are atomic.(1) Array[# -> 2 # &, 5] // Dispatch // AtomQ True

5

Using CForm and some prior replacements: expr = -0.0000289725287527177708 - 2.52403420408155732 x + 138.677105376831122 x^2 - 3402.37981527828424 x^3 + 34440.8443628217428 x^4 + 158064.877964911022 x^5 - 8.04498826077845134*10^6 x^6; expr /. {x_^3 :> third[x], x_^6 :> sixth[x]} // CForm -0.000028972528752717771 - 2.5240342040815573*x ...

5

// timidly raises hand Maybe this approach? ClearAll[Thread2]; SetAttributes[Thread2, HoldAllComplete]; Thread2[expr_, etc___] := DeleteCases[ Thread[ With[ {max = Length /@ {expr} // Max}, Quiet[ Replace[ expr, h_[c___] /; 1 < Length@{c} < max :> RuleCondition[ h[c, Sequence ...

5

You remove a head by replacing it with Identity Cos[a] /. Cos -> Identity For doing this over lots of expressions: list = {ArcTan[x], ArcTan[x], ArcTan[x], Cot[x], Cot[z], Cot[x], ArcTan[z], ArcTanh[y], ArcTanh[x], Cot[y]}; list[[All, 0]] = Identity or Identity @@@ list etc

5

You can call Wolfram Alpha directly from the notebook, Part[#, 2] & /@ WolframAlpha[ "cos(a+b)^2",{{"AlternativeRepresentations:MathematicalFunctionIdentityData", All}, "Content"},PodStates ->{"AlternativeRepresentations:MathematicalFunctionIdentityData__More"}] it should give you all the alternate forms. {HoldForm[Cos[a ...

Only top voted, non community-wiki answers of a minimum length are eligible