Highest voted questions tagged expression-test - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-18T14:38:58Z https://mathematica.stackexchange.com/feeds/tag?tagnames=expression-test&sort=votes https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/9405 79 How to check if a 2D point is in a polygon? nilo de roock https://mathematica.stackexchange.com/users/156 2012-08-14T08:29:52Z 2017-01-15T14:02:01Z <p>Background: I use code from <a href="http://demonstrations.wolfram.com/AnEfficientTestForAPointToBeInAConvexPolygon/">An Efficient Test For A Point To Be In A Convex Polygon</a> Wolfram Demonstration to check if a point ( mouse pointer ) is in a ( convex ) polygon. Clearly this code fails for non-convex polygons.</p> <p><strong>Question: I am looking for an efficient routine to check if a 2D-point is in a polygon.</strong></p> https://mathematica.stackexchange.com/q/16694 27 _?NumericQ equivalent for lists Cuboid https://mathematica.stackexchange.com/users/2034 2012-12-20T13:52:06Z 2016-01-12T08:38:46Z <p>A few months ago I got a simple answer (see: <a href="https://mathematica.stackexchange.com/questions/10918/findminimum-nminimize-etc-with-external-process">1</a> ) to the question of how to force numeric evaluation during use of functions such as <code>NMinimize[]</code> - namely the use of <code>_?NumericQ</code> in the argument list. </p> <p>I now have the same problem, only this time the argument to the user-defined function I'm trying to minimize is a list. Is there an equivalent to <code>_?NumericQ</code> for lists?</p> https://mathematica.stackexchange.com/q/88342 26 Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)? march https://mathematica.stackexchange.com/users/29734 2015-07-15T21:04:41Z 2018-10-14T19:32:07Z <p>For various reasons related to workflow associated with creating figures for journals, I am creating functions that will accept graphics primitives and a set of options and spit out a <code>Graphics</code> object with those primitives and those options in some configuration.</p> <p>(Of course, this sounds like all I'm doing is re-creating <code>Graphics</code>, in that I can call</p> <pre><code>Graphics[ &lt;List of primitives&gt;, &lt;Sequence of options&gt;] </code></pre> <p>but it's more complicated than that.)</p> <p>In any case, I would like to be able to check whether the inputs to the function are graphics primitives or not. We can create our own <code>GraphicsPrimitiveQ</code> if we have a list of the <code>Head</code>s of graphics primitives. For instance,</p> <pre><code>Clear[GraphicsPrimitiveQ, listOfHeads] listOfHeads = {Line, Polygon, Point, Arrow, Tube}; GraphicsPrimitiveQ[x_] := MatchQ[Head[x], Alternatives @@ listOfHeads] </code></pre> <p>in which case if we define</p> <pre><code>f[x__?GraphicsPrimitiveQ] := {x} </code></pre> <p>then</p> <pre><code>f[Point[{0, 0}], Line[{{1, 1}, {0, 0}}]] (* {Point[{0, 0}], Line[{{1, 1}, {0, 0}}} *) </code></pre> <p>and</p> <pre><code>f[Point[{0, 0}], 1] (* f[Point[{0, 0}], 1] *) </code></pre> <ul> <li><p>So: <strong>Is there something like a GraphicsPrimitiveQ?</strong></p></li> <li><p>Or: <strong>Is there hidden somewhere in Mathematica an easy way to get a list of all graphics primitives?</strong></p></li> <li><p>Alternatively, if there's no built-in <code>ListOfGraphicsPrimitives[]</code>, then <strong>Is there a programmatic way of making the function <code>GraphicsPrimitiveQ</code>?</strong> (Added this because I for some reason didn't anticipate the clever answers below.)</p></li> </ul> https://mathematica.stackexchange.com/q/82854 25 Does Mathematica store hidden information for matrix objects? user3126814 https://mathematica.stackexchange.com/users/29318 2015-05-07T21:17:47Z 2015-07-15T10:01:34Z <p><strong>Bug introduced in 10.0 and fixed in 10.2.0</strong></p> <hr> <p>I found a case where calling one built-in function (<code>HermitianMatrixQ</code>) for a given matrix changes the behavior of another built-in function (<code>AntihermitianMatrixQ</code>) for the <i>same</i> matrix object. Here is the code:</p> <pre><code>M = {{0, 1}, {-1, 0}} AntihermitianMatrixQ[M] (* output: True *) HermitianMatrixQ[M] (* output: False *) AntihermitianMatrixQ[M] (* output: False -- WRONG! *) </code></pre> <p>I observed this in Mathematica 10.0.0.0 on Microsoft Windows (64-bit).</p> <p>The behavior is "stateful" in the sense that re-evaluating the assignment to <code>M</code> restores the correct behavior of <code>AntihermitianMatrixQ</code> until the next time the function <code>HermitianMatrixQ</code> is called for the same object, from which point on <code>AntihermitianMatrixQ</code> gives the incorrect result again.</p> <p>I did some experimentation and found the following:</p> <ul> <li>The "state" is per object: When you define several array variables, the functions behave independently for each of the array objects, in the way described above.</li> <li>The problem does not occur for all matrices, but it is not limited to the given 2x2 case, either. (In fact I found it when debugging a calculation with 4x4 Dirac gamma matrices.)</li> <li>The function <code>SymmetricMatrixQ</code> intereferes with the behavior of <code>AntihermitianMatrixQ</code> in the same way.</li> <li>The result of <code>AntisymmetricMatrixQ</code> is affected in the same way.</li> <li>Printing the <code>FullForm</code> of the object does not give any indication of a change to the object itself.</li> </ul> <p><strong>My question is therefore:</strong></p> <ul> <li>Does Mathematica store any per-object data for matrices that is not displayed in <code>FullForm</code> but that can affect the behavior of functions as described above?</li> <li>If so, is there any way to display or to clear this kind of additional data?</li> </ul> <p>Note: I created a support request with Wolfram Research regarding this strange behavior. So far I got no response, however. I would also appreciate independent confirmation of this behavior, to exclude the possibility that something is wrong with my particular installation of Mathematica.</p> <p><strong>UPDATE</strong>: I got a response to my support request (CASE:3202743) from WRI. They confirm that <code>AntisymmetricMatrixQ</code> is not behaving properly and that an incident report has been created. Many thanks also to @ilian for pursuing this matter. I would still be interested in answers to my questions as formulated -- both out of curiosity and in order to improve debugging options.</p> https://mathematica.stackexchange.com/q/184470 23 Which second arguments is VectorQ/MatrixQ/ArrayQ optimized for? Szabolcs https://mathematica.stackexchange.com/users/12 2018-10-23T14:37:42Z 2018-10-29T16:56:49Z <p><code>VectorQ</code> and similar functions (<code>MatrixQ</code>, <code>ArrayQ</code>) treat certain second arguments specially.</p> <p>For example, this is very fast:</p> <pre><code>rr = RandomReal[{0.01, 1}, 100000]; VectorQ[rr, NumericQ]; // RepeatedTiming (* {5.*10^-7, Null} *) </code></pre> <p>While this is not:</p> <pre><code>VectorQ[rr, Positive]; // RepeatedTiming (* {0.022, Null} *) </code></pre> <p>This is because when <code>rr</code> is a packed array, <code>VectorQ</code> will not evaluate <code>NumericQ</code> for each array element separately. Since all elements of a packed array are of the same type, it is clear what the answer should be without checking each of them. </p> <p><strong>Question: Which functions are treated specially by <code>VectorQ</code>?</strong></p> <p>Why is this important? Constructs like <code>VectorQ[#, NumericQ]&amp;</code> are frequently used for argument checking. You'd want to know what patterns to use for optimal performance. Argument checking should ideally take negligible time compared to the actual computation the function does. </p> https://mathematica.stackexchange.com/q/18647 22 How to check if an expression is a real-valued number Joel Klein https://mathematica.stackexchange.com/users/594 2013-01-28T23:28:24Z 2016-04-27T14:52:10Z <p>What is a simple, fast way to test whether an expression is a real-valued number? I ask since there is no <code>RealQ</code> function.</p> <p>If we call this test <code>realQ</code>, it should satisfy these constraints:</p> <ul> <li><code>realQ["text"]</code> is <code>False</code> (non numerics are all false) </li> <li><code>realQ</code> is <code>True</code> (integers are true)</li> <li><code>realQ[3.0]</code> is <code>True</code> (reals are true)</li> <li><code>realQ[1/2]</code> is <code>True</code> (rationals are true)</li> <li><code>realQ[I]</code> is <code>False</code> (anything with an imaginary component is false)</li> </ul> https://mathematica.stackexchange.com/q/181761 22 Why no RationalQ or RealQ? Andreas Lauschke https://mathematica.stackexchange.com/users/1598 2018-09-12T13:31:15Z 2019-04-17T21:16:54Z <p>Interesting pattern came up as I go through the homework replies of my students. Why is there no <code>RationalQ</code> or <code>RealQ</code>? We have <code>Rational</code> and <code>Real</code> as type restrictors / heads in pattern matching, like <code>_Rational</code> or <code>_Real</code>. Why no Qs for them?</p> https://mathematica.stackexchange.com/q/1474 22 Implementing a safe ValueQ that does not evaluate its argument Szabolcs https://mathematica.stackexchange.com/users/12 2012-02-08T00:59:42Z 2013-06-11T11:15:27Z <p><em>Mathematica</em> has a built-in function <code>ValueQ</code>. Quoting the docs,</p> <blockquote> <p><code>ValueQ[expr]</code> gives <code>True</code> if a value has been defined for <code>expr</code>, and gives <code>False</code> otherwise. </p> <p>...</p> <p><code>ValueQ</code> gives <code>False</code> only if <code>expr</code> would not change if it were to be entered as <em>Mathematica</em> input. </p> </blockquote> <p>This works nicely with symbols that only have <code>OwnValues</code>:</p> <pre><code>x = Print["boo!"] ValueQ[x] (* ==&gt; True *) </code></pre> <p>It doesn't work so nicely with functions, though:</p> <pre><code>f[x_] := Print[x] ValueQ[f] (* 1 ==&gt; True *) </code></pre> <p>This gives <code>True</code>, but also evaluates <code>f</code> completely, printing <code>1</code>. This is clearly dangerous, and it is important to be aware of this. Unfortunately, the docs make no mention of this under the "Possible Issues" section!</p> <p>Why this happens is clear if we look at the implementation of <code>ValueQ</code> for non-symbols:</p> <pre><code>ValueQ[expr_] := ! Hold[Evaluate[expr]] === Hold[expr] </code></pre> <p><strong>Question:</strong> Is it possible to implement a more robust version of <code>ValueQ</code>, which does not evaluate its argument in an unexpected an possibly dangerous way?</p> https://mathematica.stackexchange.com/q/6745 21 How do you check if there are any equal arguments(even sublist) in a list? Gianpiero Cea https://mathematica.stackexchange.com/users/1463 2012-06-13T08:34:09Z 2013-08-02T20:27:53Z <p>I would like to set up a function which has to return True if at least two arguments of a given List are equal.</p> <p>So if I give <code>{1,4,6,2}</code> to the function it has to return <code>False</code> (since none of his arguments are equal) and the same would happen if I gave <code>{{1,2,3},{2,3,4}}</code>, while if I gave <code>{1,2,3,1,4}</code> or <code>{{1,2,3},{2,0,0},{1,2,3},{2,1,2}}</code> it has to return <code>True</code>.</p> <p>I know this is a very simple problem and i think you can easily tell me a convenient way to achieve that.</p> https://mathematica.stackexchange.com/q/601 21 Why does Mathematica claim there is no even prime? celtschk https://mathematica.stackexchange.com/users/129 2012-01-24T11:57:11Z 2015-04-17T10:18:23Z <p>I wonder if this is a bug, or if I'm misunderstanding something:</p> <pre><code>Exists[n, EvenQ[n] &amp;&amp; PrimeQ[n]] // Resolve (* ==&gt; False *) </code></pre> <p>So if I interpret this result correctly, according to Mathematica there does not exist an even prime. However, if given the number directly, it gives the correct result:</p> <pre><code>EvenQ &amp;&amp; PrimeQ (* ==&gt; True *) </code></pre> <p>So did I find a bug in Mathematica (and if so, is it fixed in the latest version)? Or did I misunderstand <code>Resolve</code>?</p> https://mathematica.stackexchange.com/q/9772 20 Determine whether some expression contains a given symbol Ian Hincks https://mathematica.stackexchange.com/users/1167 2012-08-23T17:31:49Z 2018-03-31T18:56:41Z <p>Given a symbol <code>t</code> and an expression <code>expr</code>, how can I determine whether or not the symbol <code>t</code> appears somewhere in <code>expr</code>?</p> <p>The best solution I have up with so far is:</p> <pre><code>Block[{t,s},(expr/.t-&gt;s)=!=expr] </code></pre> <p>which will return <code>True</code> if <code>t</code> is in <code>expr</code>, and <code>False</code> otherwise.</p> <p>But this feels a bit like a hack because it's not really using <code>/.</code> because it's the right tool, but rather because <code>/.</code> happens to need to search through <code>expr</code> in order to do its unrelated task. This results in having to search through <code>expr</code> at least three times (I think?): once for the <code>/.</code>, and twice for each side of the <code>=!=</code>, when clearly its possible to find <code>t</code> in only one search.</p> https://mathematica.stackexchange.com/q/442 18 Fastest square number test Mr.Wizard https://mathematica.stackexchange.com/users/121 2012-01-21T14:02:47Z 2019-06-14T04:50:27Z <p>What is the fastest possible square number test in <em>Mathematica</em>, both for machine size and big integers?</p> <p>I presume starting in version 8 the fastest will be a dedicated C LibraryLink function.</p> https://mathematica.stackexchange.com/q/197 18 How can I test properties of a symbol from the string name without the symbol completely evaluating Brett Champion https://mathematica.stackexchange.com/users/69 2012-01-18T19:02:50Z 2013-06-11T14:31:23Z <p>Suppose I have a few symbols, one of which has a value:</p> <pre><code>{abc1, abc2 = 5, abc3}; </code></pre> <p>I can use <code>Names</code> to get the list of names, as strings:</p> <pre><code>InputForm[names = Names["Globalabc*"]] (* {"abc1", "abc2", "abc3"} *) </code></pre> <p>Now I want to find which symbols have values. This fails, because <code>ValueQ</code> expects the first argument to be a <code>Symbol</code>, not a <code>String</code>:</p> <pre><code>Select[names, ValueQ] (* {} *) </code></pre> <p>This fails (with lots of messages), because <code>ValueQ</code> doesn't evaluate the argument enough:</p> <pre><code>Cases[names, st_ /; ValueQ[Symbol[st]]] (* {"abc1", "abc2", "abc3"} *) </code></pre> <p>If we force evaluation, we go too far, and this fails because we get <code>ValueQ</code> instead of <code>ValueQ[abc2]</code>:</p> <pre><code>Cases[names, st_ /; ValueQ[Evaluate[Symbol[st]]]] (* {} *) </code></pre> <p><strong>This approach works,</strong> but is far from elegant:</p> <pre><code>Cases[names, st_ /; ToExpression["ValueQ[" &lt;&gt; st &lt;&gt; "]"]] (* "abc2" *) </code></pre> https://mathematica.stackexchange.com/q/15195 18 Checking if a point is in a convex 3D polyhedron s0rce https://mathematica.stackexchange.com/users/65 2012-11-25T22:45:52Z 2017-10-05T03:36:13Z <p>Extending from these questions <a href="https://mathematica.stackexchange.com/questions/10333/how-to-check-if-a-3d-point-is-in-a-planar-polygon">How to check if a 3D point is in a planar polygon?</a> and <a href="https://mathematica.stackexchange.com/questions/9405/how-to-check-if-a-2d-point-is-in-a-polygon">How to check if a 2D point is in a polygon?</a>.</p> <p>I'm trying to do this to render specific shapes made up of spheres.</p> <p>For a sphere it is easy:</p> <pre><code>(* generate a grid of points*) d = 20; points = Table[{x, y, z}, {x, -d, d}, {y, -d, d}, {z, -d, d}]~Flatten~2; (*check if they are inside a spherical shell*) points2 = Select[points, 8.5 &lt; Norm[#] &lt; 10 &amp;]; (*render the spheres that are inside the spherical shell*) Graphics3D[{Sphere[#, 0.75]} &amp; /@ points2, Boxed -&gt; False] </code></pre> <p><img src="https://i.stack.imgur.com/vsbtV.png" alt="Mathematica graphics"></p> <p>However, I want to try and do the same rendering for other shapes, for example a pentagonal dipyramid</p> <pre><code>Graphics3D[{Lighter@Lighter@Blue, Opacity[.8], EdgeForm[Thickness[ 0.005]], PolyhedronData["PentagonalDipyramid", "Faces"]}, Boxed -&gt; False] </code></pre> <p><img src="https://i.stack.imgur.com/K69AH.png" alt="Mathematica graphics"></p> <p>However, I'm not sure how to check if the points on the grid are with in the polyhedron.</p> <p>I can access the faces... <code>PolyhedronData["PentagonalDipyramid", "Faces"]</code></p> https://mathematica.stackexchange.com/q/160801 17 How to check if a vector is an eigenvector of a matrix using mathematica? Conor Cosnett https://mathematica.stackexchange.com/users/36681 2017-11-27T19:16:35Z 2018-10-16T07:23:42Z <p>Here is a vector </p> <p>$$\begin{pmatrix}i\\7i\\-2\end{pmatrix}$$</p> <p>Here is a matrix</p> <p>$$\begin{pmatrix}2&amp; i&amp;0\\-i&amp;1&amp;1\\0 &amp;1&amp;0\end{pmatrix}$$</p> <p>Is there a simple way to determine whether the vector is an eigenvector of this matrix?</p> <p>Here is some code for your convenience.</p> <pre><code>h = {{2, I, 0 }, {-I, 1, 1}, {0, 1, 0}}; y = {I, 7 I, -2}; </code></pre> https://mathematica.stackexchange.com/q/108948 17 Is it OK that SameQ considers expressions identical, although they have different structure? Vladimir Reshetnikov https://mathematica.stackexchange.com/users/7288 2016-03-03T04:35:00Z 2017-08-24T11:43:42Z <pre><code>$Version (* 10.2.0 for Microsoft Windows (64-bit) (July 7, 2015) *) Information[SameQ] (* lhs===rhs yields True if the expression lhs is identical to rhs, and yields False otherwise. &gt;&gt; *) a = SparseArray[Automatic, {1}, 0, {1, {{0, 0}, {}}, {}}] &amp;; b = With[{s = SparseArray[Automatic, {1}, 0, {1, {{0, 0}, {}}, {}}]}, s &amp;]; SameQ[a, b] (* True *) Depth /@ {a, b} (* {6, 3} *) LeafCount /@ {a, b} (* {14, 2} *) Position[a, _] (* {{0}, {1, 0}, {1, 1}, {1, 2, 0}, {1, 2, 1}, {1, 2}, {1, 3}, {1, 4, 0}, {1, 4, 1}, {1, 4, 2, 0}, {1, 4, 2, 1, 0}, {1, 4, 2, 1, 1}, {1, 4, 2, 1, 2}, {1, 4, 2, 1}, {1, 4, 2, 2, 0}, {1, 4, 2, 2}, {1, 4, 2}, {1, 4, 3, 0}, {1, 4, 3}, {1, 4}, {1}, {}} *) Position[b, _] (* {{0}, {1}, {}} *) </code></pre> <p>Is it expected behavior? Are the expressions stored in variables <code>a</code> and <code>b</code> identical?</p> <hr> <p><em>Update:</em> I found function <code>SystemPrivate`VerbatimSameQ</code> that appears to check if two expressions are really completely identical.</p> https://mathematica.stackexchange.com/q/61619 17 A bug in the overloading of MemberQ and FreeQ for Association? Mr.Wizard https://mathematica.stackexchange.com/users/121 2014-10-08T18:23:02Z 2015-06-01T22:20:18Z <p><strong>Bug introduced in 10.0.0 and fixed in 10.1.0.</strong></p> <hr> <p>While answering <a href="https://mathematica.stackexchange.com/q/61574/121">Pattern match for nested Association</a> I came across behavior of <a href="http://reference.wolfram.com/mathematica/ref/MemberQ.html" rel="nofollow noreferrer"><code>MemberQ</code></a> and <a href="http://reference.wolfram.com/mathematica/ref/FreeQ.html" rel="nofollow noreferrer"><code>FreeQ</code></a> in 10.0.1 that I believe is incorrect. The option <code>Heads -&gt; False</code> does not work properly:</p> <pre><code>expr1 = foo[bar, baz]; expr2 = &lt;|1 -&gt; a, 2 -&gt; b|&gt;; MemberQ[expr1, _?Print, Heads -&gt; False]; </code></pre> <blockquote> <p>bar</p> <p>baz</p> </blockquote> <pre><code>MemberQ[expr2, _?Print, Heads -&gt; False]; </code></pre> <blockquote> <p>Association</p> <p>a</p> <p>b</p> </blockquote> <pre><code>FreeQ[expr1, _?Print, Heads -&gt; False]; </code></pre> <blockquote> <p>bar</p> <p>baz</p> <p>foo[bar,baz]</p> </blockquote> <pre><code>FreeQ[expr2, _?Print, Heads -&gt; False]; </code></pre> <blockquote> <p>Association</p> <p>a</p> <p>b</p> <p>&lt;|1->a,2->b|></p> </blockquote> <p>Observe that in both cases <code>Association</code> is checked against the pattern even though <code>foo</code> is not.</p> <hr> <p>Sjoerd wrote:</p> <blockquote> <p>I believe the error is not the printing of <code>Association</code> but that it prints anything at all. <code>Association</code> is atomic and it should have no (visible) members. Compare with <code>expr3=Graph[{1-&gt;2}]</code>.</p> </blockquote> <p>The 10.0.1 documentation says:</p> <blockquote> <p><code>MemberQ</code> works on associations, testing values but not keys.</p> <p><code>FreeQ</code> works on associations, testing values but not keys.</p> </blockquote> <p>Therefore this overloading is documented and I believe options should work propertly.</p> https://mathematica.stackexchange.com/q/48349 16 How do I get Mathematica to return a function call unevaluated? Charles https://mathematica.stackexchange.com/users/779 2014-05-22T13:26:57Z 2016-07-11T08:48:52Z <p>How do I get <em>Mathematica</em> to return a function call (conditionally) unevaluated? I expect this may use the slightly-mysterious <code>Hold</code> function.</p> <p>As a toy example, suppose I want to define <code>AlgebraicQ</code> such that <code>AlgebraicQ[x]</code> returns <code>True</code> or <code>False</code> when <code>Element[x, Algebraics]</code> evaluates to <code>True</code> or <code>False</code>, but otherwise to returns <code>AlgebraicQ[x]</code>, just like the other predicate functions do. (I can't just ask if <code>Element[x, Algebraics] == True</code>, because this is itself unevaluated.)</p> <p>Edit: The first thing that came to mind didn't work, as you can see: <img src="https://i.stack.imgur.com/j5VO2.png" alt="With the definition AlgebraicQ(a_) := Element(a, Algebraics), the function AlgebraicQ[Pi+E] returns Element(E+Pi, Algebraics) instead of the desired AlgebraicQ(Pi+E). Parens used in place of brackets because of platform limitations."></p> <p>I had tried this before posting, but on a recommendation I tried again with a fresh kernel (pictured above) with the same results. I also tried</p> <pre><code>AlgebraicQ[a_] := True /; Element[x, Algebraics] AlgebraicQ[a_] := False /; ! Element[x, Algebraics] </code></pre> <p>based on an earlier suggestion but this seems not to work at all.</p> <hr> <h1>Final working solution</h1> <p>based on Szabolcs' answer:</p> <pre><code>AlgebraicQ[a_] := With[{result = Element[a, Algebraics]}, result /; MatchQ[result, True | False]] </code></pre> <p>which tests as expected:</p> <blockquote> <p>AlgebraicQ /@ {7, Pi, Pi + E}</p> <p>Out= {True, False, AlgebraicQ[E + Pi]}</p> </blockquote> https://mathematica.stackexchange.com/q/169193 14 Checking if a word exists Sumit https://mathematica.stackexchange.com/users/8070 2018-03-17T17:46:11Z 2018-03-22T15:47:05Z <p>Probably the simplest way to check a word if it exists is to use <code>DictionaryWordQ[]</code></p> <pre><code>DictionaryWordQ["color"] </code></pre> <blockquote> <p>True</p> </blockquote> <p>But it does not recognise British spelling:</p> <pre><code>DictionaryWordQ["colour"] </code></pre> <blockquote> <p>False</p> </blockquote> <p>So my guess is that <code>DictionaryWordQ[]</code> checks words from <code>WordList[]</code>:</p> <pre><code>Select[WordList[], StringLength[#] &gt; 3 &amp;&amp; StringTake[#, 4] == "colo" &amp;] </code></pre> <blockquote> <p>{cologne,colon,colonel,colonial,colonialism,colonialist,colonic,colonist,colonization,colonize,colonized,colonizer,colonnade,colonnaded,colony,colophon,color,coloration,coloratura,colored,colorfast,colorful,colorimetric,coloring,colorist,colorize,colorless,colorlessness,colors,colossal,colossus,colostomy,colostrum}</p> </blockquote> <p>No <code>colour</code>. However,</p> <pre><code>WordData["colour"] </code></pre> <blockquote> <p>{{colour,Noun,Color},{colour,Noun,Gloss},{colour,Noun,VisualProperty},{colour,Noun,Timber},{colour,Noun,Interest},{colour,Noun,Variety},{colour,Noun,Race},{colour,Noun,Stuff},{colour,Adjective},{colour,Verb,Discolor},{colour,Verb,Alter},{colour,Verb,TouchOn},{colour,Verb,Rationalize},{colour,Verb,Beautify},{colour,Verb,Influence}}</p> </blockquote> <p>So the word <code>colour</code> is known to <em>Mathematica</em>, but <code>DictionaryWordQ[]</code> does not see it!</p> <p>Interestingly,</p> <pre><code>DictionaryWordQ["qt"] </code></pre> <blockquote> <p>True</p> </blockquote> <pre><code>Select[WordList[], StringLength[#] &gt; 1 &amp;&amp; StringTake[#, 2] == "qt" &amp;] </code></pre> <blockquote> <p>{}</p> </blockquote> <p>which gives an impression that <code>DictionaryWordQ[]</code> is looking at a bigger set of words compared to <code>WordData[]</code> (or it is a bug!).</p> <p><strong>Question:</strong></p> <p>What is the best way to verify if a word exists?</p> <p>(Version used: 11.1, 10.4 in Linux)</p> https://mathematica.stackexchange.com/q/88582 14 IsomorphicGraphQ fails to recognize that two empty graphs are identical Szabolcs https://mathematica.stackexchange.com/users/12 2015-07-19T10:36:34Z 2016-09-28T22:23:52Z <p><strong>Bug introduced in 9.0.1 or earlier and fixed in 11.0.1</strong></p> <hr> <p>Consider the following two graphs:</p> <pre><code>g1 = Graph[{1, 2, 3}, {}]; g2 = SimpleGraph@Graph[{1, 2, 3}, {1 &lt;-&gt; 1}]; </code></pre> <p>They are clearly the same thing because <code>SimpleGraph</code> will remove the self loop (only edge) from <code>g2</code>.</p> <p>Yet <code>IsomorphicGraphQ</code> disagrees:</p> <pre><code>IsomorphicGraphQ[g1, g2] (* False *) </code></pre> <p>I believe this is a bug and it exists in all of 9.0.1 through to 10.4.0.</p> <p>Is there a workaround?</p> https://mathematica.stackexchange.com/q/193691 14 Why is IsomorphicGraphQ so very slow (in this instance) Sander https://mathematica.stackexchange.com/users/14625 2019-03-21T05:14:24Z 2019-04-25T16:18:44Z <p><strong>Bug introduced in 10.0 or earlier and persisting through 12.0.0</strong></p> <hr> <p>Can someone explain why the following code does not complete within a reasonable time?</p> <pre><code> l = {{208, 108}, {208, 112}, {208, 116}, {208, 120}, {208, 124}, {208, 128}, {208, 132}, {208, 136}, {208, 140}, {208, 144}, {208, 148}, {208, 152}, {212, 211}, {212, 208}, {212, 209}, {212, 210}, {209, 109}, {209, 113}, {209, 117}, {209, 121}, {209, 125}, {209, 129}, {209, 133}, {209, 137}, {209, 141}, {209, 145}, {209, 149}, {209, 153}, {210, 110}, {210, 114}, {210, 118}, {210, 122}, {210, 126}, {210, 130}, {210, 134}, {210, 138}, {210, 142}, {210, 146}, {210, 150}, {210, 154}, {211, 111}, {211, 115}, {211, 119}, {211, 123}, {211, 127}, {211, 131}, {211, 135}, {211, 139}, {211, 143}, {211, 147}, {211, 151}, {211, 155}}; g1 = Graph[DirectedEdge[{10, 53, #1, 91}, {10, 53, #2, 91}] &amp; @@@ l]; g2 = Graph[DirectedEdge[{10, 53, #1, 84}, {10, 53, #2, 84}] &amp; @@@ l]; Print["edges: ", EdgeCount[g1], " vertices: ", VertexCount[g1]]; IsomorphicGraphQ[g1, g2] (* edges: 52 vertices: 53 *) (*$Aborted *) </code></pre> <p>I would expect an execution time of milliseconds, while in reality it does not complete within minutes. </p> <p>The above code works well for smaller examples with similar structure and <code>IsomorphicGraph</code> works with much larger graphs, as shown below (from the help file):</p> <pre><code>In:= {g = GridGraph[{10, 10, 10}],h = Graph[RandomSample[VertexList[g], VertexCount[g]], EdgeList[g]]}; In:= IsomorphicGraphQ[g, h] // Timing Out= {0.002527, True} In:= {VertexCount[g], EdgeCount[g]} Out= {1000, 2700} </code></pre> https://mathematica.stackexchange.com/q/60081 13 Better way to test that a matrix is upper-triangular? xyz https://mathematica.stackexchange.com/users/9627 2014-09-19T02:44:49Z 2014-09-19T07:25:46Z <p>Today, I got to thinking about how to test that a matrix was upper-triangular. So I had a try at it.</p> <h3>Algorithm</h3> <p>$\quad \quad a_{ij}=0$ Or $i \leq j$ $\Rightarrow$ $True$</p> <p>My solution is</p> <pre><code>upperTriangularMatrixQ[mat_?MatrixQ] /; Equal @@ Dimensions@mat := And @@ Flatten @ MapIndexed[#1 == 0 || LessEqual @@ #2 &amp;, mat, {2}] </code></pre> <h3>Performance testing</h3> <p><strong>Sample data</strong></p> <pre><code>testMat1 = UpperTriangularize@RandomInteger[{1, 100}, {1000, 1000}]; testMat2 = RandomInteger[{1, 100}, {1000, 1000}]; </code></pre> <p><strong>Test</strong></p> <pre><code>upperTriangularMatrixQ@testMat1 // Timing </code></pre> <blockquote> <pre><code>{3.151, True} </code></pre> </blockquote> <pre><code>upperTriangularMatrixQ@testMat2 // Timing </code></pre> <blockquote> <pre><code>{3.978, False} </code></pre> </blockquote> <p><strong>My question</strong></p> <p>Can you come up with a better performing algorithm to solve this problem?</p> https://mathematica.stackexchange.com/q/78155 13 Unexpected slowness checking for empty list? ciao https://mathematica.stackexchange.com/users/11467 2015-03-25T01:04:29Z 2015-04-23T21:28:25Z <p>I <em>thought</em> I had a firm understanding of the differences between <code>Equal</code> and <code>SameQ</code>, but this has me puzzled:</p> <pre><code>test = RandomInteger[100000000, 10000000]; {} == test // AbsoluteTiming {} === test // AbsoluteTiming </code></pre> <p><code>==</code> takes a chunk of time, <code>===</code> is "instant". I can't fathom why the first is not short-circuited pronto. Explanations?</p> https://mathematica.stackexchange.com/q/31498 13 How can one write a robust ListableQ function? Mr.Wizard https://mathematica.stackexchange.com/users/121 2013-09-01T13:31:20Z 2013-09-01T13:49:56Z <p>I would like to have a test that determines if a particular function is Listable. In the case of Symbols this is merely a matter of checking <code>Attributes</code>. <code>Function</code> definitions with the <code>Listable</code> attribute are a bit more involved but quite easy.</p> <p><strong>However I specifically want to test for <em>inherent listability</em> in as many cases as possible.</strong></p> <p>For example consider the function from <strong>Case #4</strong> in <a href="https://mathematica.stackexchange.com/questions/7924/alternatives-to-procedural-loops-and-iterating-over-lists-in-mathematica">Alternatives to procedural loops and iterating over lists in Mathematica</a>:</p> <pre><code>(3 - #)/(7 * #) &amp; </code></pre> <p>This function is <em>inherently listable</em>:</p> <pre><code>fn = (3 - #)/(7*#) &amp;; Map[fn, {1, 2, 3}] fn @ {1, 2, 3} </code></pre> <blockquote> <pre><code>{2/7, 1/14, 0} {2/7, 1/14, 0} </code></pre> </blockquote> <ul> <li><p>One must consider Functions with multiple arguments, both the <code>Slot</code> and named parameter type.</p></li> <li><p>Ideally the test would handle pattern-based (DownValues) functions to the extent that is possible.</p></li> </ul> https://mathematica.stackexchange.com/q/111438 12 Why is an empty list not tested by VectorQ? gwr https://mathematica.stackexchange.com/users/764 2016-03-30T11:31:23Z 2016-03-30T18:10:40Z <p>I am a bit amazed by this behavior of <a href="http://reference.wolfram.com/language/ref/VectorQ.html?q=VectorQ" rel="nofollow"><code>VectorQ[ expr, test ]</code></a>:</p> <pre><code>VectorQ[ {}, NumberQ ] </code></pre> <blockquote> <p>True</p> </blockquote> <p>It seems that the test itself is never applied and thus does not matter, so <code>NumericQ</code> or <code>OddQ</code> for example will also return <code>True</code>.</p> <p>I find this behavior quite unfortunate, given that the documentation states:</p> <blockquote> <p>VectorQ[ expr, NumberQ] tests whether expr is a vector of numbers.</p> </blockquote> <p>One might also note that <code>NumberQ[ Nothing ]</code> or <code>NumericQ[{}]</code> return <code>False</code> and that <code>NumberQ /@ {}</code> returns <code>{}</code>.</p> <p>Is this a <em>bug</em> or a <em>feature</em> (true to mathematics)?</p> https://mathematica.stackexchange.com/q/3541 12 Why does Assuming[x > 0, TrueQ[x > 0]] return False? max https://mathematica.stackexchange.com/users/796 2012-03-26T22:42:11Z 2012-03-26T23:28:36Z <pre><code>Assuming[x&gt;0,TrueQ[x&gt;0]] </code></pre> <p>should, as I understand it, test <code>TrueQ[x&gt;0]</code> <strong>after</strong> assuming <code>x&gt;0</code>. Could someone explain the output <code>False</code> to me, please?</p> https://mathematica.stackexchange.com/q/98674 12 ValueQ returns false positive for one argument type only ibeatty https://mathematica.stackexchange.com/users/23076 2015-11-05T03:05:45Z 2015-11-05T07:53:55Z <p>I'm seeing some perplexing behavior from <code>ValueQ</code> in 10.3. Consider:</p> <pre><code>f[r_List, n_Integer] := r^n; ValueQ[f[{}, 1]] (* ==&gt; True *) ValueQ[f[{}, 0.3]] (* ==&gt; False *) ValueQ[f[{}, \[Pi]]] (* ==&gt; False *) ValueQ[f[{a, b, c}, 3/2]] (* ==&gt; True *) (* THIS IS UNEXPECTED *) f[{1,2,3}, 3/2] (* ==&gt; f[{1,2,3}, 3/2] *) </code></pre> <p>What I expect is that any <code>ValueQ</code> call that has an argument list that matches the types in the function definition's pattern -- and therefore could be transformed by the rule associated with that definition -- will return <code>True</code>. Any call with an argument list having different types will return <code>False</code>. As I understand it, what <code>ValueQ</code> does is test whether a rule exists that would transform its argument.</p> <p>And that's what happens, EXCEPT for the final case, in which a <code>{List,Rational}</code> slips through when only a <code>{List,Integer}</code> should. <code>ValueQ</code> returns <code>True</code>. And yet, if I actually evaluate that function with those arguments, no transformation occurs, because (of course) no appropriate rule exists.</p> <p>It seems that <code>ValueQ</code> is simply failing. Is this a bug, or do I fail to understand some subtlety here?</p> https://mathematica.stackexchange.com/q/183750 11 About shorthand notation for conditional pattern (Paul Wellin "Essentials of Programming in Mathematica") tchappy ha https://mathematica.stackexchange.com/users/45848 2018-10-13T09:45:16Z 2018-10-18T09:53:45Z <p>I am reading Paul Wellin's "Essentials of Programming in Mathematica".</p> <p>Mr. Wellin wrote the following in his book:</p> <blockquote> <p>There is a convenient shorthand notation for conditional patterns that is commonly used. The condition <em><code>expr_ /; test</code></em> can be shortened to <em><code>expr_?test</code></em>.</p> </blockquote> <p>But,</p> <pre><code>MatchQ[Range, {s__/;NumberQ[s]}] </code></pre> <p>returns an error and <code>False</code>. And, </p> <pre><code>MatchQ[Range, {s__?NumberQ}] </code></pre> <p>returns <code>True</code>.</p> <p>Why?</p> https://mathematica.stackexchange.com/q/8796 11 Evaluating an If condition to yield True/False groovybaby https://mathematica.stackexchange.com/users/1852 2012-07-27T23:36:05Z 2018-10-02T11:16:13Z <p>I would like to decide whether an option passed to my custom function has the value <code>Automatic</code> or something else. This is my attempt:</p> <pre><code>f[x_, OptionsPattern[{DataRange -&gt; Automatic}]]:= Module[{opt = OptionValue[DataRange]},{x, If[opt == Automatic, True, opt]}]; </code></pre> <p>However,</p> <pre><code>f[x, DataRange -&gt; 20] </code></pre> <p>produces</p> <pre><code>{x, If[20 == Automatic, True, opt\$540]} </code></pre> <p>rather than the expected</p> <pre><code>{x, 20} </code></pre> <p>What do I need to change?</p> https://mathematica.stackexchange.com/q/157783 10 Test if a list is a constant integer multiple of another list Lokdal https://mathematica.stackexchange.com/users/23117 2017-10-13T19:38:47Z 2017-11-10T06:57:47Z <p>I have a list of lists, and I want to eliminate all the lists that are constant integer multiples of another list. My initial approach was to divide the lists using nested tables.</p> <pre><code>ex1 = {{1, 1, 1}, {1, 1, 2}, {2, 2, 2}, {2, 2, 4}, {3, 3, 5}}; Table[Table[ If[Subtract @@ MinMax[ex1[[j]]/ex1[[i]]] == 0, ex1[[j]] = ex1[[i]]], {j, i + 1, Length[ex1]}], {i, Length[ex1]}]; Union[ex1] {{1, 1, 1}, {1, 1, 2}, {3, 3, 5}} </code></pre> <p>However, some of my lists have zeroes, which breaks my code.</p> <pre><code>ex2 = {{1, 1, 0}, {1, 1, 2}, {2, 2, 0}, {2, 2, 4}, {3, 3, 5}}; Table[Table[ If[Subtract @@ MinMax[ex2[[j]]/ex2[[i]]] == 0, ex2[[j]] = ex2[[i]]], {j, i + 1, Length[ex2]}], {i, Length[ex2]}]; Union[ex2] "CHAOS ENSUES" </code></pre> <p>I have an alternative approach which is stupid and ugly.</p> <pre><code>ex3 = {{1, 1, 0}, {1, 1, 2}, {2, 2, 0}, {2, 2, 4}, {3, 3, 5}}; Table[ Table[ Table[ If[ex3[[i]]*k == ex3[[j]], ex3[[j]] = ex3[[i]]], {k, j}], {j, i + 1, Length[ex3]}], {i, Length[ex3]}]; Union[ex3] {{1, 1, 0}, {1, 1, 2}, {3, 3, 5}} </code></pre> <p>I'm certain there's a better way, but I can't come up with it. I was frustrated by the ugliness of with my first attempt, but at least it was somewhat clever. Can you suggest something nicer?</p>