Tag Info

Hot answers tagged

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


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


9

res1 = data /. Plus -> List {a, b c, -d e f} res1[[2]] /. Times -> List {b, c} As a function: explodeOp[expr_, op_] := expr /. op -> List Then: res1 = explodeOp[data, Plus]; explodeOp[res1[[2]], Times]


9

As far as I know, there is no easy, general way to handle this kind of algebra with Sum expressions. What follows is an attempt to use replacement rules to handle a wider range of cases than chris's example. I don't consider it to be the canonical answer that is required, but perhaps someone might be able to use it as a starting point. I use Inactive on ...


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

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


7

It sounds like you're merely looking for Row: Cp = 1.5; deltastar = 0.123; Row[{ "The value for ", HoldForm[Subscript[C, p]], " is ", Cp, " and the value for ", HoldForm[Superscript[\[Delta], "*"]], " is ", deltastar, "." }] If this does not work for you please clearly state how it fails so that those issues can be directly addressed.


7

Looks like StringForm can achieve this: Cp = 1.5; deltastar = 0.123; Then: StringForm["The value for `1` is `2` and the value for `3` is `4`.", HoldForm @ Subscript[C, p], Cp, HoldForm @ Superscript[\[Delta], "*"], deltastar]


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

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?


6

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


6

Yes we can ! MapAt[Integrate[#, {x, -Infinity, Infinity}] &, f[x], 1] // PowerExpand (* n *) tt = f[x]^2 /. Power[Sum[a__, b__], 2] :> sum[a (a /. i -> j) // Release, b, b /. {i -> j}] MapAt[Integrate[#, {x, -Infinity, Infinity}] &, tt, 1] /. sum -> Sum // PowerExpand


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

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

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


5

Start here: What are the use cases for different scoping constructs? Module works by replacing explicit appearances of a given Symbol with a different one with a derived name, e.g.: Module[{x}, x] x$715 Since MainVar appears nowhere in Print[ToExpression[Carrier]] the Module will not affect it. A far simpler example of the same behavior that ...


5

With V10 we can write expr = Sqrt[x^4] Log[x^2] + Log[x^4] /. x_Log :> Inactivate[x]; Refine[expr, x > 0] // Activate EDIT Thanks to Chip Hurst's comment the above should, of course, be written as expr = Inactivate[Sqrt[x^4] Log[x^2] + Log[x^4], Log] One of the advantages of Inactivate is that we can selectively Activate: expr = ...


5

primeFactorForm[n_Integer] := Module[{fact = FactorInteger[n]}, If[Length[fact] == 1, Superscript @@ fact[[1]], CenterDot @@ Superscript @@@ fact]] primeFactorForm[9] primeFactorForm[72]


5

The existing answers work but I offer two improvements: terse code via pattern replacement making an actual formatting wrapper This replacement rule strips the heads of any expressions with only one argument: foo[1] /. _[x_] :> x foo[1, 2] /. _[x_] :> x 1 foo[1, 2] Format is used to describe the output format without losing the original ...


4

You have found the snags and you're right -- it's a simple matter. You just need the right rules. For a pure symbolic expression you can use Kuba's suggestion. a^2 - b + c*d /. -1 -> 1 a^2 + b + c d For dealing with complex numbers you can use (1 - b I) a /. x_Complex /; Im[x] < 0 -> Conjugate[x] (1 + b I) a If your expressions are ...


4

Does this come close? Cos[a] /. Cos[a] -> a Or Cos[a] /. _[a] -> a Or First@Cos[a] Or list = {Sin@a, Cos@b}; First /@ list {a, b}



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