Tag Info

27

As it happens there is a built-in function that already does this: Signature. If any two elements of list are the same, Signature[list] gives 0. dupeQ = 0 === Signature@# &; I believe this is the "canonical" answer. It is fast on both packed arrays and unpacked lists.

23

You have several options: foo[arg_?(VectorQ[#,NumericQ]&)] foo[arg: {_?NumericQ ..}] foo[arg: {__?NumericQ}] For matrices or higher dimensional arrays, the equivalent of VectorQ is MatrixQ and ArrayQ. It's worth noting that VectorQ[..., NumericQ] (and its relatives MatrixQ and ArrayQ) are highly optimized and will avoid unpacking packed arrays: ...

21

Note: I am not particularly knowledgable in the field of this question, so what I write below may well be wrong. I don't know whether or not this should be considered a bug, but to my mind this is an instance of a clash of programming and mathematical functionality. To put it differently, predicates (functions ending with Q) seem to be a wrong match for ...

20

I usually use ToExpression["symbol", InputForm, ValueQ] ToExpression will wrap the result in its 3rd argument before evaluating it. Generally, all functions that extract parts (Extract, Level, etc.) have such an argument. This is useful when extracting parts of held expressions. ToExpression acts on strings or boxes, but both the problem with ...

17

Update: InternalRealValuedNumericQ /@ {1, N[Pi], 1/2, Sin[1.], Pi, 3/4, aa, I} (* {True, True, True, True, True, True, False, False} *) or InternalRealValuedNumberQ /@ {1, N[Pi], 1/2, Sin[1.], Pi, 3/4, aa, I} (* {True, True, True, True, False, True, False, False} *) Using @RM's test list listRM = With[{n = 10^5}, RandomSample[Flatten[{...

17

As the responses show, there are a number of quick "probably real" tests. In general, the problem is undecidable, however. This is an easy corollary of Richardson's theorem, which says that it is impossible to decide if two real expressions $x$ and $y$ are equal. Assuming Richardson's theorem, note that $(x-y)i$ is real if and only if $x=y$. As a more ...

16

There is in fact an easy test to determine if an integer is a power of $2$, thanks to bit twiddling: hadamardMatrix[1] := {{1}} hadamardMatrix[2] := {{1, 1}, {1, -1}} hadamardMatrix[n_Integer /; Positive[n] && BitAnd[n, n - 1] == 0] := KroneckerProduct[hadamardMatrix[2], hadamardMatrix[n/2]]

16

If there are repeated elements in the list, then calling Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths: test[list_] := Length[Union[list]] != Length[list] If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect ...

16

Unless both lists given to Equal are packed arrays Equal will first unpack. Unfortunately for this case {} is not a packable expression, therefore list == {} will always unpack list, assuming it starts packed. That unpacking takes time: test = RandomInteger[100000000, 10000000]; DeveloperFromPackedArray[test]; // AbsoluteTiming {0.207012, Null} ...

16

Others have argued in the comments that this behaviour makes sense mathematically, and I fully agree. But further than that, it is also very practical. Mathematica's functions are usually designed to give reasonable results for edge cases in the sense that if you put these functions together and write some more complex calculation, this compound function ...

15

Because the assumption system is not called during the standard evaluation sequence, it is only called when Simplify, FullSimplify, Sum, Integrate etc... are used. Thus, x>0 remains unevaluated: Assuming[x > 0, x > 0] (* ==> x > 0 *) and TrueQ then returns False: Assuming[x > 0, TrueQ[x > 0]] (* ==> False *) If, however, you ...

14

Preamble This has been discussed before, and this problem was also identified and partially addressed in the same question. I will use a slightly simpler implementation which also covers UpValues. It is probably not complete either, but it covers many common cases of interest. Implementation Here is the code: ClearAll[symbolicHead]; SetAttributes[...

14

You can simply do: Position[testData, _?(Not@*NumericQ), {2}, Heads -> False] {{1, 4}, {3, 2}, {3, 3}, {3, 4}, {3, 5}} Notice the use of level specification so that you only look inside the sublists and the option Head -> False prevents you from including the position of Heads, since they are non-numeric. An alternative is to use Except as Kglr ...

13

As @MikeHoneychurch observes, the formatted form of an SQLConnection expression: SQLConnection["db", 3, "Open", "Catalog" -> "db", "ReadOnly" -> True] differs from its FullForm: SQLConnection[JDBC[...], JLinkObjectsvm1JavaObject18126325894086657, 1, ...] Pattern matching uses the FullForm. One way to work around this is to convert the ...

13

An interesting question which I've never specifically considered before. Some observations: Log[8]/Log[2] // FullSimplify Log2[8] Log[2, 8] 3 3 3 And @@ IntegerQ /@ Log2[2^Range[50000]] And @@ Table[IntegerQ@Log2[2^RandomInteger[5*^8]], {500}] True True Mathematica documentation explicitly states: Log2 gives exact integer or rational ...

13

A simple workaround is to re-build the graph object by cycling it through some other representation. Here are two possible solutions: rebuildGraph[g_] := Uncompress@Compress[g] (* solution 1 *) rebuildGraph[g_] := Graph[VertexList[g], EdgeList[g]] (* solution 2 destroys properties but it's fine for isomorphism testing purposes *) isomorphicGraphQ[g1_, g2_]...

13

Well, the documentation of ValueQ states ValueQ gives False only if expr would not change if it were to be entered as Wolfram Language input. This explains pretty much everything you are experiencing. Very easy example: Hold[1/2]//FullForm (* Hold[Times[1,Power[2,-1]]] *) You see that you enter 1/2 as a multiplication but what if we don't hold it? ...

12

duplicatesQ = # != DeleteDuplicates[#] & Usage: duplicatesQ[{1, 4, 6, 1}] (* ===> True *) duplicatesQ@{1, 4, 6, 2} (* ==> False *) duplicatesQ@{{1, 2, 3}, {2, 0, 0}, {1, 2, 3}, {2, 1, 2}} (* ==> True *) duplicatesQ /@ {{1, 2, 3}, {2, 0, 0}, {1, 2, 3}, {2, 1, 2}} (* ==> {False, True, False, True} ) or (to also get the duplicate ...

12

Using the core of my new step function: SetAttributes[valueQ1, HoldAll] valueQ1[expr_] := Module[{P, R = False}, P = (P = Return[R = True, TraceScan] &) &; TraceScan[P, expr, TraceDepth -> 1]; R ] SetAttributes[valueQ2, HoldAll] valueQ2[expr_] := Module[{P, R = False}, P = (P = Return[R = True, TraceScan] &) &; ...

12

If I understand the question you are essentially displeased (for your application) that: MemberQ[{x[1, 2], x[3, 4]}, x[1, _]] True What you need is Verbatim: MemberQ[{x[1, 2], x[3, 4]}, Verbatim[ x[1, _] ]] False Select[ {{"foo", "bar"} -> "a", {"foo", "baz"} -> "a", {"foo", _} -> "a"}, MemberQ[{{"foo", "bar"}}, Verbatim @ #[[1]]] &...

12

IF you can assume 1) they are integers, 2) they are ascending, and 3) no repeats, THEN your last idea should work Last[list]-First[list]==Length[list]-1 Or you could Union[Differences[list]]=={1} Without assumptions (2) and (3): Union[Differences[Sort[list]]]=={1}

12

Maybe upperTriangularMatrixQ2[mat_?MatrixQ] /; Equal @@ Dimensions@mat := UpperTriangularize@mat == mat; test = RandomInteger[{1, 100}, {1000, 1000}]; upperTriangularMatrixQ@test // AbsoluteTiming {2.126050, False} upperTriangularMatrixQ2@test // AbsoluteTiming {0.003277, False} test2 = UpperTriangularize@test; upperTriangularMatrixQ@test2 ...

12

Now fixed in version 10.2. In[1]:= m = {{0, 1}, {-1, 0}}; In[2]:= {AntihermitianMatrixQ[m], HermitianMatrixQ[m], AntihermitianMatrixQ[m]} Out[2]= {True, False, True} As per the comments, yes, there is information stored in the internal representation of matrices (for example, a symmetry flag) and no, it is not accessible from top level code.

12

tl;dr If these functions cannot decide, they will simply return False. A False result means that the selected equality testing method wasn't able to prove equality, but it does not mean that it was able to prove inequality. Interpret the result relative to the used SameTest option value. I will try to explain what I think is happening, though some of ...

11

It has been explained in good detail why your inputs did not work the way you wanted them; however, there is still a way to get what you want: Resolve[Exists[n, Element[n, Primes] && Mod[n, 2] == 0]] True FindInstance[Element[n, Primes] && Mod[n, 2] == 0, n, Integers] {{n -> 2}} In general, use Element[n, Primes] whenever you need to ...

11

Update Sorry for my ignorance not taking into account that the question specifically asked for a Mathematica 7 solution. I updated the complete post. Mathematica 7 In Mathematica 7 we don't have the option the compile code into a C-library which includes the thread parallelization which can be turned on when using RuntimeAttributes->Listable and ...

11

I think a solution based on pattern matching will be much faster than using Element (which is more mathematical in nature) or only pattern tests or anything else that forces evaluation, since we can bypass the main evaluator. However, it is not possible to completely escape evaluation, because there can be infinitely large number of possibilities for a real ...

11

=== (SameQ) is structural equality. a === b is True if a and b are exactly the same data structure (expressions), and False otherwise. For === it doesn't matter what a and b represent. Also, like nearly all Mathematica functions ending in Q, === always evaluates to either True or False (but nothing else). == is mathematical equality. a == b represents the ...

11

To find the intervals for which f[x] is positive f[x_] = -x^3 + x^2 + 7*x; g[x_] = Piecewise[{{f[x], f[x] > 0}}, I]; Plot[{f[x], g[x]}, {x, -3, 4}, PlotStyle -> {Directive[Red, Dashed], Blue}] FunctionDomain[g[x], x] (* x < (1/2)*(1 - Sqrt[29]) || 0 < x < (1/2)*(1 + Sqrt[29]) *) % // N (* x < -2.19258 || 0. < x < 3....

10

A short one: consecutiveQ = Most[#] == Rest[#] - 1 &

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