Tag Info

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, Globalx, 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 ... 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, ... 5 For the first example Repeated, as already shown by Ymareth, is the simplest method: MatchQ[ {1, 2/3, 3.14, 4, 5.0, "x", "y"}, {Repeated[_?NumericQ, {5}], _, _} ] True For the case of arbitrary positions it seems to me that there are two natural approaches: Method 1: Build a pattern You can programmatically build a pattern like the one shown in ... 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 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. 3 If lists are large, something like list1[[;; (Position[list1, Except[0 |0.| Null], 1][[-1, 1]])]] should be quite a bit faster, e.g., a quick test on a 50K length list it is over 350X faster than the rule-based solution, and 4X faster than the Take solution. If the list is "front-loaded" with values (i.e., more wanted than not), using list1[[;; ... 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 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 ...

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

2

You say you want to be able to decide which columns have to be numeric and which ones don't. There are many approaches that aren't based on patterns and this is probably not the best one, but it will do the job. Let's define checkNumericColumns[entry_, blanks_] := Module[{shouldBeNumeric, areNumeric}, shouldBeNumeric = Complement[Range@Length@entry, ...

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

1

For the case with non numerical columns that are allowed. sel[dat_, nonNum_] := With[{n = Length@dat[[1]]}, Pick[ dat, And @@@ Map[NumericQ, dat[[;; , DeleteCases[Range[n], Alternatives @@ nonNum]]], {2}] ] ] so for the edit test: sel[mydataset, {2}] {{1, 2, 3, 4}, {3, 4, 5, 4}, {1, "", 3, 4}, {10, "", 12, 13}}

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