Tag Info

Hot answers tagged

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


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


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


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


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} *)


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


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



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