Tag Info

New answers tagged

1

Per my comment (this is not a fleshed out answer, just an example ): entX[p_] := With[{vars = Unique[] & /@ Range@Length@p}, Expectation[-Log[PDF[p, vars]], vars \[Distributed] p]] entX[nd2] (* 1+Log[2 π] *) Note, you'll want to use more sophistacated means to detrmine needed number, Length works here for your example, and is probably OK for some ...


1

This seems to work. q = "x"; w = "y"; kk[] := Module[{q, w, m}, q[a_] := a + 1; w[a_] := a^2; m[Symbol["q"]] = q; m[Symbol["w"]] = w; m] Cant imagine why you'd want to do this though.


8

Module works different than scoping constructs in other languages. Here's a simpler example which already gives a clue of what happens: x=3; Module[{x}, {x, Global`x, Context[x]}] (* ==> {x$81, x$81, Global`} *) You see, no matter whether you prefix it with Global`, x gets replaced with x$81, which indeed also has global context. Indeed, this is how ...


2

I am quite unable to understand why you just don't write kk[q][a_] := a + 1 kk[w][a_] := a^2 This gives {kk[q][1], kk[w][2]} {2, 4} Wouldn't the above satisfy your needs?


3

I like your elegant answer, and... Switch[#, pat : _Integer /; (val = pat; True), Print[val, " is integer"], pat : _Real /; (val = pat; True), Print[val, " is real"], pat_ /; (val = pat; True), Print[val, " is none of the above"]] & /@ {1, 1.1, a} (* 1 is integer 1.1 is real a is none of the above *)


8

I recently realised that the Replace function essentially solves this problem, but it is not the sort of function you tend to associate with conditional constructs. It also might surprise readers of the code, as it is not a common idiom. This solution is: Replace[expr, {pat1 :> val1, pat2 :> val2, _ :> valD}] e.g. Replace[x, ...


3

A very interesting question. I thought of a much plainer approach than the other responders but it proves to perform quite well. I simply PadRight the reference sequence to match the length of the test sequence. Functions cycQ[ref_][test_] := test === PadRight[ref, Length @ test, ref] cycpat[f_, r___] := p : PatternSequence[f, ___] /; cycQ[{f, r}][{p}] ...


0

One method I've found that works, at least for this case, is to use Position and MapAt: myReplaceListAll[l_, rule_] := MapAt[Function[x, Replace[x, rule]], l, #] & /@ Position[l, rule[[1]] ] This almost works, except that when Position matches the whole list it returns {}, and MapAt doesn't return the whole expression. list rule = ({x_, y_} ...


3

What's happening only indirectly involves the pattern matching. Mathematica, when dealing with operations that are Orderless, will put the arguments into a canonical ordering (described here). In this case, an expression like Subscript[Z, 2, 1] Subscript[Z, 3, 1] can be seen to be equivalent to Times[Subscript[Z,2,1], Subscript[Z,3,1]] using FullForm. ...


1

As mentioned in the comments, you can use either MatchQ: MatchQ[Subscript[x, 2], Subscript[x, _?(# > 1 &)]] or FreeQ with Not: !FreeQ[Subscript[x, 2], Subscript[x, _?(# > 1 &)]]


3

Jump straight down to Update 2 for the final code. I'll leave the previous iterations here as they explain how that solution developed. This is based on the following definition of similarity: Two expressions are similar if they become identical when all variables are replaced by the same generic variable. For example, both $a+b$ and $b+c$ become ...


1

data = {I (a^2 + b^2) (*1*), a (I b + e) (*2*), b (I a + d) (*3*), b (I a + f) (*4*), a b (*5*), a (I b + f) (*6*), I (b^2 + c^2) (*7*)} systemnames = Names["System`*"]; test[expr_] := Select[{Extract[expr, #], #} & /@ Position[expr, _Symbol, Infinity], MemberQ[systemnames , ToString@(#[[1]])] &] Gather[data, test[#1] == test[#2] ...


3

list = {1,3,2,0,1,0,1,2,0,1,1,4,0}; MatchQ[list, {PatternSequence[1, ___?(IntegerQ@# && Positive[#] &), 0] ...}] True This is an interesting problem. I'm not sure how to solve it using Condition rather than PatternTest.



Top 50 recent answers are included