Tag Info

Hot answers tagged

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 ...


15

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 ...


12

You can use any built in operator modified with subscripts, superscripts, etc, and retain its precedence, for your own purposes. For example, say you want a general Apply operator like @@ that could work at any level. One could use create the operator @@ with a number subscripted for the level of Apply seems appropriate MakeExpression[RowBox[{fun_, ...


10

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]


9

The following functions will load the expressions and erroneous cells from a notebook: notebookExpressions[path_, pattern_:_] := Cases[Import[path, "Notebook"] // First , c:Cell[_, "Input"|"Output"|"Print", ___] :> Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]] , Infinity ] eval[cell_] := Quiet @ Check[ ...


8

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


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 ...


8

For Simplify there is the option ExcludedForms: expr = Sqrt[x^4] Log[x^2] + Log[x^4]; Simplify[expr, Assumptions -> {x > 0}, ExcludedForms -> {_Log}] (* x^2 Log[x^2] + Log[x^4] *) For Refine, you can wrap the heads to be excluded with Hold: Refine[expr /. Log -> Hold[Log], x > 0] // ReleaseHold (* x^2 Log[x^2] + Log[x^4] *) or use ...


7

Your code is like a Rube Goldberg machine! Try this instead: fn[state_] := Outer[Coefficient[state, #*#2] &, ##] & @@ (Union @ Cases[state, #, -2] & /@ {_FF, _GG}) Test: test = 7 FF[1, 1] GG[1, 1] + 2 FF[1, 1] GG[2, 2] + 4 FF[2, 2] GG[2, 2] + 11 FF[2, 1] GG[2, 4]; fn[test] // MatrixForm $\left( \begin{array}{ccc} 7 & 2 & 0 ...


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 ...


7

You can use the new (in V10) ImplicitRegion function as follows: reg = ImplicitRegion[0 <= x <= 1, {x}]; Then: ArgMax[f1[x], x ∈ reg]


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

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:


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

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

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 ...


6

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


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

The reason for this behaviour is that Module does localization by renaming. For example: Module[{x}, x] (* x$982 *) That x inside Module is renamed to something like x$nnn with nnn being a different and unique number every time Module is evaluated. Module will not be able to do the renaming inside any strings, so ToExpression["MainVar"] will evaluate to ...



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