Hot answers tagged expression-test
19
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.
14
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 ...
14
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]]
11
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 ...
11
As @MikeHoneychurch observes, the formatted form of an SQLConnection expression:
SQLConnection["db", 3, "Open", "Catalog" -> "db", "ReadOnly" -> True]
differs from its FullForm:
SQLConnection[JDBC[...], JLink`Objects`vm1`JavaObject18126325894086657, 1, ...]
Pattern matching uses the FullForm.
One way to work around this is to convert the ...
11
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]]] ...
10
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 ...
7
Needs["DatabaseLink`"]
conn = DatabaseLink`OpenSQLConnection[
DatabaseLink`JDBC[ "MySQL(Connector/J)",
"localhost:3306/railfreight"], "Username" -> "",
"Password" -> ""]
(* SQLConnection[1, "Open", "Catalog" -> "railfreight",
"TransactionIsolationLevel" -> "RepeatableRead"]*)
But when you check out the FullForm (removed ...
7
Since you are looking for duplicates you could adapt any of the methods shown in this answer.
Using the first one for example:
dupeQ =
Module[{f},
f[y_] := (f[y] := Return[True, Module]; y);
Scan[f, #]; False
] &;
This particular one has an advantage on long lists in that it will "short-circuit" on the first duplicate found rather than ...
6
You could use Gather and then check the length of each group :
Gather[{{1, 2, 3}, {2, 0, 0}, {1, 2, 3}, {2, 1, 2}}]
(* {{{1, 2, 3}, {1, 2, 3}}, {{2, 0, 0}}, {{2, 1, 2}}} *)
Length[#] & /@ Gather[{{1, 2, 3}, {2, 0, 0}, {1, 2, 3}, {2, 1, 2}}]
(* {2, 1, 1} *)
6
A frivolous implementation using patterns:
duplicateQ[list_]:=MemberQ[Tally[list],{_,_?(#>1&)}]
This function uses Tally to arrange the elements of list in bins. For example,
In[2]:= Tally[{1,2,3,1,2}]
Out[2]= {{1,2},{2,2},{3,1}}
Then we look for an element in the output of Tally which looks like {_,n} with $n>1$.
In[3]:= ...
4
Element (of) is a mathematical operation, which is why it (correctly) says that 0 is an element of Integers, Reals and Complexes. However, $0.$ is a floating point representation of zero and is not an exact integer, hence it returns False for Element[0., Integer]. On the other hand, 0. certainly is an element of Reals, and by extension, an element of ...
4
I assume the latitude and longitude lists should be the same length, and that SLon should be WLon.
qLon = {-7.48333, -10.4667, -8.66667, -7.48333, -8, 3, 99};
qLat = {53.5, 52.5, 53.1167, 51.9833, 51.0167, 62.1, 50};
{{SLat, NLat}, {ELon, WLon}} = {{47, 55}, {-15, -5}};
MapThread[
If[ELon <= #1 <= WLon && SLat <= #2 <= NLat, "In", ...
2
More info as requested by @Mr.Wizard.
For $n$ below the $\approx 2*10^9$ limit, Compile gives the fastest solutions. For larger $n$, Sasha used JacobiSymbol with four primes 13, 19, 17, and 23, before resorting to the expensive IntegerQ[Sqrt[n]]. The number of ambiguous cases where JacobiSymbol[n,p]=0 decreases as the size of the prime $p$ increases. So ...
2
DeleteCases uses the pattern matcher which matches against the FullForm of expressions. Just use Alternative:
In[]:= DeleteCases[{1, 0., 0}, 0. | 0]
Out[]= {1}
or a conditional pattern
In[]:= DeleteCases[{1, 0., 0}, _?(# == 0 &)]
Out[]= {1}
or
In[]:= DeleteCases[{1, 0., 0}, x_ /; x==0]
Out[]= {1}
This happens a lot with infinity be careful of ...
1
Which[num === EndOfFile, res = "end",
num == 1, res = "1",
num == 2, res = "2",
True, res = "non"]
Edit
Perhaps you may want to experiment a little with SameQ[]. For example:
ClearAll[h, j]
h == j
h === j
(*
h == j
False
*)
Only top voted, non community-wiki answers of a minimum length are eligible

