# Tag Info

## Hot answers tagged expression-manipulation

11

11

y = a[b, c][d]; y[[0, 0]] = w; y (* w[b, c][d] *)

7

In this example, I am assuming that the structure of your expressions will always be similar to your input: input = a*b*c + Cos[x - y]; The method below is not exactly general. First remove the outer-most Plus: list = List @@ Expand[input] (* {a b c, Cos[x - y]} *) We then Map Apply over the list while being careful to avoid those expressions which ...

7

I believe this is what $PrePrint is for, since you only want to affect how the expression looks, and I'm guessing you want it to happen automatically for every input. Using$PrePrint thus allows you to use Out[n] without worrying about the held expressions. This seems to work (but I would like to find a better way to take care of the signs between terms in ...

6

There seems to be a subtlety in the way delayed rules are used. Have a look at the following: {a,a,a} /. a/;(Print["lhs evaluated"];True) :>(Print["rhs valuated"]; RandomReal[]) (*lhs evaluated lhs evaluated lhs evaluated rhs evaluated rhs evaluated rhs evaluated *) (* {0.797753,0.567294,0.91182} *) This shows that when we use a delayed ...

6

One can use Operate[] with Apply[]: Operate[w @@ # &, a[b, c][d]] w[b, c][d]

4

Update 2: Fixed! (I believe) With help from this answer. Clear[replaceFirstHead] replaceFirstHead[newHead_, expr_] := Module[{cond = True} , expr /. _Symbol[x__] :> newHead[x] /; If[cond, cond = False; True, False] ] replaceFirstHead[newHead_][expr_] := replaceFirstHead[newHead, expr] Then: replaceFirstHead[w] /@ {a[y[z]][x[a]], a[b], z[b, ...

4

One possibility would be the simple asc["A"]["a"] = 99; As @m_goldberg commented this can be shortened to asc["A", "a"] = 99; To change several keys: asc[[1]][[2 ;; 3]] = 4

3

As was commented, you should probably tell us also about the problematic rule you cannot use but anyway you might be interested in the following approach: As you (should) know, everything in Mathematica is an expression, in other words it can be expressed as f[x,y,...] Your expression: expr = p[{1, 2}] p[{2, 3}]^2 p[{4, 5}] is actually interpreted as: ...

3

Try the following: Coefficient[f[r, ϕ], Cos[ϕ]]

2

This works: replaceFirstHead[expression_, replacement_] := If[ Head[expression] === Symbol, replacement, ReplacePart[ expression, 0 -> replaceFirstHead[expression[[0]], replacement] ] ]

2

This seems to admit a simple recursive approach. terms[(Times | Plus)[args__]] := terms /@ {args} terms[x_] := x Then terms[b c + a b c] {{b, c}, {a, b, c}} terms[a b c + Sin[x] Cos[x - y]] {{a, b, c}, {Cos[x - y], Sin[x]}} However, terms[d/e Cos[x - y]] {d, 1/e, Cos[x - y]} Will this last result will be acceptable? I can not make ...

2

2

Try rather using the following syntax for your replacement: ( <expr> ) /. { x -> 3.14 } In your case, probably something like: FullSimplify[ (\[Tau] - ArcCos[a[\[Tau]]/b[\[Tau]]]/ Sqrt[b[\[Tau]]^2 - a[\[Tau]]^2]) /. {\[Delta] -> 0.05, \[Beta] -> 1.77, n -> 12} ] See the explanation on Applying Transformation Rules: On the ...

1

It seems the current version (10.3) is now aware of the Meijer $G$ expressions for the order derivatives (see this math.SE answer as well): Derivative[1, 0][StruveL][0, z] // FunctionExpand BesselK[0, z] - MeijerG[{{1/2, 1/2}, {}}, {{0, 0, 1/2, 1/2}, {}}, z/2, 1/2]/(2 π^2) (The last version I used, version 8, was unable to do this, if memory ...

1

If[Read[StringToStream[\$Version], Number] >= 9 ,FilterOptions[a_,b___] := Sequence @@ FilterRules[{b}, Options[a]] ]; would be a general use bridge to make notebooks and packages before version 9 and later compatible with respect to FilterOptions being superseded by FilterRules having a different call interface. You may place it into ...

1

First I would suggest not using Print at all. Print is mainly a debugging tool in Mathematica. Next, assuming that by labeling you mean putting the set of numbers generated by an iteration into a structure that you can retrieve them from later, here are two approaches. I build a table of values of Sin[x] with x going from 0 to 90 degrees in 10 degree ...

1

Update to add an actual reasonable solution This is actually very straightforward with Position, if you limit it to returning only the first result when you match everything. Pillsy`ReplaceFirstHead[expr_, new_] := ReplacePart[expr, Position[expr, _, All, 1] -> new] Old, silly approach Nothing says clear, maintainable code like using ReplaceRepeated ...

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