Tag Info

New answers tagged

0

another version Clear@"`*" variables = {}; expr = f[x] + g[x] + 3 f[y] + f[x + y] f[x]; var[x_] := CompoundExpression[variables = variables~Join~{x}, x] (* usage *) expr /. f[x_] :> fNew@var@x variables (* out *) fNew[x] + 3 fNew[y] + fNew[x] fNew[x + y] + g[x] {x, y, x, x + y}


3

Sow and Reap: {result, variables} = Reap[ expr /. f[x_] :> fNew[Sow@x] ] variables (* Out: {{x, y}} *) In your original solution you do not need Block: variables = {}; expr /. f[x_] :> (variables = Union[variables, {x}]; fNew[x]) And AppendTo can make your code shorter: variables = {}; expr /. f[x_] :> (AppendTo[variables, x]; fNew[x]) ...


0

variables = Union@Cases[expr, f[x__] :> x, {0, Infinity}] (* {x, y} *)


3

Superscript is not interpreted as Power: Presumably you are referring to what happens when you enter a power in superscript notation using the key combination Ctrl+6. Mathematica is capable of representing both this power notation and a formatted plain Superscript. In my opinion it is a failing that the power notation appears in the Typesetting menu ...


1

Quantity["Speed of Light"] == QuantityVariable[\[Nu], "Frequency"] (2 \[Pi])/ QuantityVariable[k, "wavenumber"] /. QuantityVariable[\[Nu], "Frequency"] -> Quantity[2, "THz"] // Solve[#, QuantityVariable[k, "wavenumber"]][[1]] & {QuantityVariable[k,"wavenumber"] -> Quantity[(2000000000000 [Pi])/149896229, 1/("Meters")]} % // N ...


1

Because the latter is equivalent to {1,2,3,4,5}[[2]] = 0 which is invalid. The former stays as listCopy[[2]] = 0 You can't change (i.e. assign to) parts of a literal like this. You can change (i.e. assign to) a variable. Generally, f[x_] := x[[2]] will substitute the value of x directly. So will With[{x=...}, x[[2]] ]. In contrast, ...


1

Despite the accepted excellent answer by Mr.Wizard I think it is worth to point out that the standard idiomatic approach to the problem in Mathematica is to use indexed variables: In[1]:= i = 10; d[i] = 30; Definition[d] d[10] = 10; Definition[d] Out[3]= d[10] = 30 Out[5]= d[10] = 10


6

I shall assume that you want a compact syntax to make this practical to use. I shall choose cs, standing for compound symbol: func_[cs[x__], arg___] ^:= ToHeldExpression @ ToString @ Row @ {x} /. {_[s_Symbol] :> func[s, arg], _ :> $Failed} Test: i = 10; cs["d", i] = 30; d10 30 Any expressions can be used so long as their evaluated ...


0

ReplaceAll[x/(1 + 3 c^2) + y/(1 + 3 c^2), 1/(1 + 3 c^2) -> z] x z + y z


6

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


10

In V10, another option is to use Association. par=<|"mu"->1,"sigma"->1,"lb"->0,"ub"->10|>; f[x_, p_Association:par] := PDF[LogNormalDistribution[p["mu"], p["sigma"]], x] Plot[f[x, ##], {x, #lb, #ub}] &@par Another form for Plot is: Plot[f[x, par], {x, par@"lb", par@"ub"}] And as @Mr.Wizard commented, you can use the default ...


13

There are a number of options and their attractiveness will depend on the scenario for their use, therefore it is difficult to make any broad recommendations of best practice. I will say that generally it is not recommended to rely on global assignments as in your first example, because this method scales poorly and because it is easy to make mistakes and ...



Top 50 recent answers are included