Hot answers tagged

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


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

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


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


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

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


9

Using the data and list as defined: if = Interpolation[list, InterpolationOrder -> 1] regm[{x_, y_}] := 0 <= x <= 20 && 0 <= y <= if[x] Show[ListPlot[list, Joined -> True, PlotRange -> {0, 10}], ListPlot[GatherBy[data, regm], PlotStyle -> {Black, {Red, PointSize[0.02]}}, PlotLegends -> {"Above", "Below"}], Frame ...


9

If you are going to do a lot of membership tests on the same list it may be useful to look at Dispatch On my virtual PC: AbsoluteTiming[MemberQ[list, 123]] {0.163837, True} dlist = Dispatch[list -> True // Thread]; TrueQ[1222000023 /. dlist] // AbsoluteTiming {0.0000128508, False} TrueQ[1223 /. dlist] // AbsoluteTiming {0.0000243048, ...


7

See How to make the computer consider two numbers equal up to a certain precision and the linked SO answer for more details. This question is similar to the first one, except the OP here wants the tolerance to be $-\infty$, instead of greater. Block[{Internal`$EqualTolerance = -∞}, 1 + $MachineEpsilon == 1. ] 1 + $MachineEpsilon == 1. (* False ...


7

I like the answers using Reduce and FunctionDomain. Here's a numerical possibility that uses Minimize to find the global minimum on the domain and tests to see if it's positive. f[x_] = -x^3 + x^2 + 7*x; 0 <= First@Minimize[{f[x], 0 <= x <= 4}, x] (* False *) Alternatively, if needed, you can use NMinimize: NMinimize[{f[x], 0 <= x <= 4}, ...


7

Short answer It is not a good idea to make a parallel version of MemberQ. This is mainly because membership testing is generally faster than copying data and copying the data is necessary in Mathematica to allow other kernels to work with the data properly. About the rest of this answer This answer addresses some tricky stuff specific to the code ...


7

It seems you are calculating legs of Pythagorean triples, $\{a,b\}$, for $b<a/\sqrt{2}$. I used your isSq function, and added a different test for GCD[a,b]==1. RelativePrimesA[n_Integer] := Block[{max = Floor[n/Sqrt[2]]}, Complement[Range[max - 1], Apply[Sequence, Map[Range[#, max - 1, #] &, FactorInteger[n][[All, 1]]]]] ] The ...


7

This is likely equivalent to ubpdqn's answer internally, but I wanted to show off an answer with a "geometric" flavor: BlockRandom[SeedRandom[42, Method -> "Legacy"]; (* for reproducibility *) data = Transpose[{RandomReal[20, 200], RandomReal[10, 200]}]; list = MapIndexed[Append[#2 - 1, #1] &, RandomReal[{5, 6}, 21]]]; ...


6

Your curve, plus two points makes a region. Select[data, Element[#, Polygon[Join[list, {{20, 0}, {0, 0}}]]] &] or GatherBy[data, Element[#, reg] &] where reg = Polygon[Join[list, {{20, 0}, {0, 0}}]] last two points defined by the range of $x$ and $y$. Visual Block[{reg = Polygon[Join[list, {{20, 0}, {0, 0}}]]}, Show[ ListPlot[ { ...


5

Pick[data, UnitStep[data[[All, 2]] - Part[list[[All, 2]], Ceiling[data[[All, 1]]]]], 0] Example: data = Table[{RandomReal[20], RandomReal[10]}, {200}]; list = Table[{i, RandomReal[{5, 6}]}, {i, 0, 20}]; Show[{ListStepPlot[list, Right, Mesh -> Full], ListPlot[data, PlotStyle -> Black], ListPlot[ Pick[data, UnitStep[data[[All, 2]] - ...


4

expr = n * Subscript[e, 3] * Subscript[e, 4] * Subscript[e, 7] * Subscript[e, k] * Subscript[e, k + 1]; expr /. HoldPattern[ Times[s : Repeated[Subscript[e, _]]] ] :> Subscript[e, Times @@ Last /@ {s}] (* n Subscript[e, 84 k (1 + k)] *)


4

Position[Map[NumericQ, testData, {-1}], False] {{1, 4}, {3, 2}, {3, 3}, {3, 4}, {3, 5}}


4

f[x] === HoldForm[f[x]] does not provide the desired result, because HoldForm stays attached to f[x] on the right side of the expression. Thus, the left and right sides never are identical. Instead use, f[x] === Unevaluated[f[x]] (* True *) And, when the function does something, g[x_] := x^2 g[x] === Unevaluated[g[x]] (* False *) Unevaluated prevents ...


4

Sorry it's kind of a mess (I'm a little bit sleep-deprived at the moment), but here's something that might work for you: Module[{cf = ColorData[97], $color = 1}, style["", c_] := Style[Overscript[Overscript["", \[OverBracket]], c], {Darker@cf[c], Bold}]; style[str_, c_] := Style[Overscript[Overscript[str, \[OverBracket]], c], {Darker@cf[c], Bold}]; ...


3

This is not a bug. It's a misunderstanding about what TrueQ does. From the documentation, TrueQ will return True only if the input is explicitly True To put it more explicitly, it's equivalent to trueQ[expr_] := If[expr === True, True, False]. The expression (2*I*k)^(1 + 0.1*I) (2 I k)^(I*m) == (2*I*k)^(1 + 0.1*I + I*m) is not the symbol True ...


2

tl;dr Use this modified valueQ function. SetAttributes[valueQ, {HoldFirst}]; valueQ[h_[args__]] := With[{eval = args}, ! Hold[Evaluate[h[eval]]] === Hold[h[eval]] ]; How ValueQ works. I learned from @halirutan's answer in ValueQ returns false positive for one argument type only that ValueQ simply tests whether an expression is equal to ...


2

As suggested by J.M., the solution is to use Mod: f[k_] := Piecewise[{{1, k==0}, {x^k/k!, Mod[k, 2]==1}, {x^k/k!, Mod[k, 2]==0}}] This returns the expected values for both Sum[f[k], {k,0,5}] and for Sum[f[k], {k, 0, Infinity}] As an interface-design issue, the choice to make *Q functions return a value even for generic symbols seems like a very poor ...


2

It has already been pointed out that the problem originates from the fact that OddQ[x] evaluates to False if x is not a numerical value. Note that it is not hard to define test function that returns unevaluated for symbolic arguments: CEvenQ[n_Integer] := EvenQ[n] COddQ[n_Integer] := OddQ[n] Now, COddQ[2] gives False and COddQ[x] is returned unevaluated. ...


1

Best thing I can come up with is cntSummands[expr_]:=If[Head[expr]===Plus,Length[expr],If[expr === 0, 0, 1]] but this sounds like a terrible workaround. I am sure there are better ways?


1

What you are encountering is something all Mathematica users encounter because it is the way Mathematica works. Szabolcs has explained this well. However, I would like to add that you can fix the "problem" by using Simplify Simplify[(2*I*k)^(1 + 0.1*I) (2*I*k)^(I*m) == (2*I*k)^(1 + 0.1*I + I*m)] True Simplify[(I k)^(N[Pi]) (I k)^(I m) == (I k)^(N[Pi] ...


1

Using Complement on two lists could be used as follows: Complement[l1, l2] == {} True If you have more than one list, for example, l1 = {a, b, c}; l2 = {b, c, a}; l3 = {c, b, z}; you could also implement it with Tuples and compare the lists pairwise: ((Complement @@ #) == {}) & /@ Tuples[{l1, l2, l3}, 2] {True, True, False, True, True, ...


1

Daniel Lichtblau confirmed that this was a bug. It has been corrected in version 10.1.0.



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