Tag Info

New answers tagged

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


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


3

A variation on @MrW's answer using a combination of Outer, Coefficient, Variables and GatherBy: func = Function[{state}, Coefficient[state, #] &@ Outer[Times, ## & @@ (Sort /@ GatherBy[Variables[state], Head])]]; Test: xx1 = FF[1, 1] GG[1, 1] + FF[1, 1] GG[2, 2] + FF[2, 2] GG[2, 2]; xx2 = 2*FF[1, 2] GG[1, 1] + FF[1, 1] GG[1, 2] + FF[2, 2] ...


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


2

For the example you gave FF[___] and GG[___] are the only non-number terms, therefore by polynomial sort order you could use simply: mtx1[[All, All, 1]] {{24, 24, 24}, {24, 24, 24}, {24, 24, 24}} I shall now look at your newer question where I anticipate a more representative example.


1

Given the structure of your SQUARE example matrix this should be fast: dim = First @ Dimensions @ mtx1 3 tup = Join [#, {1}] & /@ Tuples[Range@dim, 2] {{1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {2, 1, 1}, {2, 2, 1}, {2, 3, 1}, {3,1, 1}, {3, 2, 1}, {3, 3, 1}} Partition[mtx1[[#1, #2, #3]] & @@@ tup, dim] {{24, 24, 24}, {24, 24, 24}, {24, ...


0

Probably you will have to adapt my basic idea to your special needs. mem = 1/Sqrt[2] + 1/54 (34 - 3 π^2 + (-8 + Log[8]) Log[64]) // ExpandAll // Apply[List, #] & {mem[[1]], mem[[2]]/Sqrt[2], mem[[3]]/Pi^2, mem[[4]]/Log[2], mem[[5]]/Log[2]^2} // FullSimplify a possible adaption The key is: after ExpandAll the summands are sorted in the same ...


3

If you use ToString, make sure you specify InputForm (compare ToString[1/x] v.s. ToString[1/x, InputForm]). Why not something like this? pureify[f_, x_] := Function @@ {f /. x -> #} Table[InverseFunction[pureify[foo, x]][x], {foo, {Sin[x], Log[x], Sqrt[x]}}] (* {ArcSin[x], E^x, x^2} *)


1

Thanks to @hieron suggestion to start with the "pure" heads I found this solution: GetHeads[fun_] := ToExpression@First@StringSplit[ToString[fun], "["] f = GetHeads /@ {Sin[x], Log[x], Sqrt[x]} {Sin, Log, Sqrt} Table[InverseFunction[foo[#] &][x], {foo, f}] {ArcSin[x], E^x, x^2}


1

Without "Head-Substitution", you may achieve it: inv = InverseFunction /@ {Sin, Log, Sqrt} x // inv // Through out: {ArcSin[x], E^x, x^2} Edit1: Yes, to get rid of the arguments I just wanted to suggest you inv = InverseFunction /@ ToExpression /@ (StringTake[#, {1, -4}] & /@ToString /@ {Sin[x], Log[x], Sqrt[x]}) x // inv // Through But I ...


3

Just to add a bit to the xzczd answer given in a form of a comment above. In earlier Mma versions (that might be your case) it can be done as follows: f[x_, y_] := a y + x^2 ss = DSolve[{D[y[x], x] == f[x, y[x]], y[0] == c}, y, x][[1, 1]] yielding this: (* y -> Function[{x}, (-2 + 2 E^(a x) + a^3 c E^(a x) - 2 a x - a^2 x^2)/ a^3] *) Than ...


6

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


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


3

Please check the result by doing the following eqn = FullSimplify[ 2*a*b*n*c*Q - 3*a*b*n*p*c*Q + a*b*n*p^2*c*Q - 4*a^2*b*n*p*Q^2 - 4*a*b^2*n*p*Q^2 + 6*a^2*b*n*p^2*Q^2 + 6*a*b^2*n*p^2*Q^2 - 2*a^2*b*n*p^3*Q^2 - 2*a*b^2*n*p^3*Q^2 + a^2*b*c*Q^2 + a*b^2*c*Q^2 - 2*a^2*b*p*c*Q^2 - 2*a*b^2*p*c*Q^2 == 0] // TraditionalForm This returns ...


0

Another approach Clear[func] func[var_String] := Switch[var, "var1", "some words", "var2", "other words"]; {func["var1"], func["var2"]} (* {"some words", "other words"} *)


1

What you need is Block instead of Module. It changes the extent to which the variables are localized. func[var_String] := Block[{var1, var2},var1="some words";var2="other words";ToExpression[var]]; func["var1"] (*Out[2]="some words"*)



Top 50 recent answers are included