Tag Info

Hot answers tagged

9

One function comes to mind that already implements matching of multidimensonal rules: CellularAutomaton. Allow me to represent your board data like this: board = SparseArray[ a /. h_[x_, y_] :> ({-y - 1, x + 1} -> h) /. {black -> \[FilledCircle], white -> \[EmptyCircle]}, {7, 7}, " "]; For my example I shall show a generic 3x3 rule ...


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


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


5

This may be a bit un-mathematicaesque, but it turns out to be convenient to store the board as a flat vector: (larger board for illustration) n = 12; board0 = Flatten[ Table[0, {n^2}], 1]; v[icol_, jrow_] = icol + n (jrow - 1); Now we can create lists of indices representing structures such as rows,columns, and diagonals. Here the function diag ...


4

EDIT Making this code runnable with Java reloader. Load the Java reloader (run the code from that post. For Mac OS X, see the comments below the post for a link to the Mac version) Compile the class: - JCompileLoad @ "package javaapplicationsim; /** * @author developer */ public class JavaApplicationSIM { final byte E = 0; // EDGE final byte _ = ...


4

Here is my own rough answer - it turns out that asking a question on SE helps clarifying one's thinking! I would still appreciate if some of the experts can weigh in. First, we'll store the board as a square matrix of symbols B, W and ".": m = Partition[RandomChoice[{B, W, "."}, 25], 5] // MatrixForm $\left( \begin{array}{ccccc} W & . & B & ...


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


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


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


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



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