Hot answers tagged

17

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


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


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


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


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]]]; With[{...


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]] - Part[list[...


5

What you are looking for is called isomorphism for coloured graphs. Both vertices and edges may be "coloured" with colours chosen from a discrete set, and only those isomorphisms are admitted which map a vertex (or edge) into another one that has the same colour. Using edge/vertex weights for this is not really appropriate. These properties are meant to ...


4

MemberQ's operator form seems to be useful for searching for the same value in different lists. If you need to search for different values in the same list many times, I would build an association that has the list elements as keys, and then use Lookup. For example, list = RandomInteger[10, 10] (* {3, 8, 9, 3, 8, 3, 5, 8, 9, 5} *) asc = AssociationMap[...


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


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


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


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

I'm going to demonstrate Nearest, even though NearestFunction is about 10-20 times slower than Lookup. On the other hand Nearest is faster at creating the underlying data structure than AssociationMap/PositionIndex. The use of Lookup seems clearer than this indirect use of Nearest, so the interest may be only academic. list = RandomInteger[10^9, 10^6]; ...


1

Using george2079's suggestion, the code can be tweaked to ExactExpressionQ[expr_] := FreeQ[expr, _Real|_Complex?InexactNumberQ, {-1}] for better performance.


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?



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