Tag Info

Hot answers tagged

13

Yes. It turns out that Normal accepts an undocumented second parameter which may be a Symbol or a list of Symbols and it will only affect those forms. Plain application converts all these forms (and more): abby = { SparseArray[{3 -> "a", 5 -> "b"}], <|1 -> "a", 2 -> "b", 3 -> "c"|>, Series[Exp[x], {x, 0, 5}], Quantity[1, ...


10

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


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]


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


8

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


8

You can start by simply creating a function that tests, whether a string is a list of rules or not isTransformable[str_String] := SyntaxQ[str] && MatchQ[MakeExpression[str], HoldComplete[{_Rule ..}]]; isTransformable[___] := False; Note that this function does much more that search for a "->" inside a string. First, it tests, whether the ...


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

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]


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

Here is how I would do it: expr = 1/((x + a + b) (c + d)); Limit[ ϵ expr /. Thread[# -> ϵ #] &@Select[Variables[expr], # =!= x &], ϵ -> 0 ] (* ==> 1/(c x + d x) *) I replaced all variables except x by ϵ times themselves and took the limit of ϵ times the original expression as ϵ goes to zero. This will leave only terms linear in the ...


7

You mean like this? Times @@ Map[ Beta[First[#] + a, Last[#] + b] &, {{1, 1}, {12, 1}, {7, 9}, {10, 14}}]


7

A pattern matching method: fn[x_, {var__}] := List @@ Pick[x, x, Alternatives[var]^_.] fn[a x^2 + b x y + c y^2, {x, y}] {x^2, x y, y^2} But a better approach I believe is (hopefully now corrected at last): fn2[x_, var_] := Collect[List @@ Expand @ x, var, 1 &] fn2[a x^2 + b x y + c y^2, {x, y}] {x^2, x y, y^2} fn2[x (x^2 + y^2), {x, y}] ...


7

You can generate the monomials by using CoefficientRules, like this In[55]:= monomialList[poly_, vars_] := Times @@ (vars^#) & /@ CoefficientRules[poly, vars][[All, 1]] monomialList[a x^2 + b x y + c y^2, {x, y}] Out[56]= {x^2, x y, y^2}


6

As Mr.Wizard indicated, you can also reconstruct the plot using the data. Here is an example: restylePlot2[p_, op : OptionsPattern[ListLinePlot]] := ListLinePlot[Cases[Normal@p, Line[x__] :> x, ∞], op, Options[p]] then we can set the style as we do in plot. For example restylePlot2[myplot2, PlotStyle -> {{Green, Thick, Dashed}, ...


6

Use the Variables command: Variables[a x^2 + b x + c] (* {a,b,c,x} *) You can easily remove the x. Alternatively: CoefficientList[a x^2 + b x + c, x] (* {a, b, c} *)


6

I believe Block will work for you here: mem : averageFreeEnergyDensityCompiled["square", n_] := mem = Block[{Part}, Compile[{{j, _Real}, {d, _Real}, {a, _Real}, {h, _Real}, {dx, _Real}, {th, _Real, 2}, {ph, _Real, 2}}, Evaluate[(*apply boundary conditions*) s[i1_, i2_] := Which[i1 == n + 1 && i2 == n + 1, s[1, 1], i1 == ...


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


5

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


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

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

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

While I was working on alternative TeX export, I had similar requirement. I wanted to export annotated Mathematica code to TeX, with annotations reflecting FrontEnd's syntax highlighting. Since I couldn't find a way to use front end itself to do it, I decided to write my own package. My SyntaxAnnotations package is now available on GitHub. It works by ...


5

Given expression = (20*t^2*Erf[b + t])/(E^t^2*Sqrt[a + t^2]) can be achieved via replacement of the Head List @@ expression To find constant factors: GatherBy[List @@ expression, NumberQ] It is so because Times here is single Head for all first-level factors: TreeForm[expression] This also can prove to be useful: FactorList[expression] ...


5

f[a_, b_] := Fold[#1*Beta[#2[[1]] + a, #2[[2]] + b] &, 1, {{1, 1}, {12, 1}, {7, 9}, {10, 14}}] f[a, b] Beta[1 + a, 1 + b] Beta[7 + a, 9 + b] Beta[10 + a, 14 + b] Beta[12 + a, 1 + b]


5

res = Rest@Select[Subsets@pol, PolynomialMod[#, factor] == 0 &] So, out of the 8192 (== 2^13) possible "sub-polys", only these ones are divisible by factor: Grid[Join[{{"pol == Non-factored", "Plus (factor by ...)"}}, {pol - # factor // Expand, #} & /@ (PolynomialQuotient[#, factor, z] & /@ res)], Frame -> All] ...


4

Another resource related to this question is the tutorial How Modules Work It explains how, as others have pointed out, the symbol that is actually created in Module[{x}, x] is x$nnn, where nnn is the current value of $ModuleNumber. $ModuleNumber is increased whenever Module is called (with local variables) and at other times. There has been ...


4

PaddedForm[Range[-2, -1, 0.2], {3, 2}] Plot[Sin[x], {x, -2, -1}, Frame -> True, Axes -> False, FrameTicks -> {{Automatic, Automatic},{Transpose[{#, Map[PaddedForm[#, {3, 2}] &, #]}] & [Range[-2, -1, 0.2]], Automatic}}]


4

Not that I'm aware of but perhaps this can work for you: x[a] Subscript[q, a] Superscript[m, a] d[a, b] /. y_ d[z_, p_] :> (y /. z -> p) d[z, p]


4

Try this: List @@ (a + b c - d e ff) Output is {a, b c, -d e ff} Likewise, to explode a product, do this: List @@ %[[2]] Output is {b, c} This works because List @@ deletes the head of (a + b c - d e ff), which is Plus, and replaces it with list, giving List[a, b c, -d e ff] (and similar for product explosion).


4

It looks like what you want is: list1 /. {_Integer -> 1}



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