Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [expression-test]

Questions about Mathematica's predicate functions.

79
votes
11answers
17k views

How to check if a 2D point is in a polygon?

Background: I use code from An Efficient Test For A Point To Be In A Convex Polygon Wolfram Demonstration to check if a point ( mouse pointer ) is in a ( convex ) polygon. Clearly this code fails for ...
27
votes
1answer
3k views

_?NumericQ equivalent for lists

A few months ago I got a simple answer (see: 1 ) to the question of how to force numeric evaluation during use of functions such as NMinimize[] - namely the use of <...
26
votes
4answers
562 views

Is there a GraphicsPrimitiveQ (or a complete list of Heads of graphics primitives)?

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 ...
25
votes
1answer
346 views

Does Mathematica store hidden information for matrix objects?

Bug introduced in 10.0 and fixed in 10.2.0 I found a case where calling one built-in function (HermitianMatrixQ) for a given matrix changes the behavior of another ...
23
votes
1answer
293 views

Which second arguments is VectorQ/MatrixQ/ArrayQ optimized for?

VectorQ and similar functions (MatrixQ, ArrayQ) treat certain second arguments specially. ...
22
votes
6answers
9k views

How to check if an expression is a real-valued number

What is a simple, fast way to test whether an expression is a real-valued number? I ask since there is no RealQ function. If we call this test ...
22
votes
2answers
1k views

Why no RationalQ or RealQ?

Interesting pattern came up as I go through the homework replies of my students. Why is there no RationalQ or RealQ? We have <...
22
votes
2answers
715 views

Implementing a safe ValueQ that does not evaluate its argument

Mathematica has a built-in function ValueQ. Quoting the docs, ValueQ[expr] gives True if ...
21
votes
9answers
3k views

How do you check if there are any equal arguments(even sublist) in a list?

I would like to set up a function which has to return True if at least two arguments of a given List are equal. So if I give {1,4,6,2} to the function it has to ...
21
votes
4answers
1k views

Why does Mathematica claim there is no even prime?

I wonder if this is a bug, or if I'm misunderstanding something: Exists[n, EvenQ[n] && PrimeQ[n]] // Resolve (* ==> False *) So if I interpret this ...
20
votes
2answers
3k views

Determine whether some expression contains a given symbol

Given a symbol t and an expression expr, how can I determine whether or not the symbol t ...
18
votes
6answers
3k views

Fastest square number test

What is the fastest possible square number test in Mathematica, both for machine size and big integers? I presume starting in version 8 the fastest will be a dedicated C LibraryLink function.
18
votes
3answers
368 views

How can I test properties of a symbol from the string name without the symbol completely evaluating

Suppose I have a few symbols, one of which has a value: {abc1, abc2 = 5, abc3}; I can use Names to get the list of names, as ...
18
votes
3answers
3k views

Checking if a point is in a convex 3D polyhedron

Extending from these questions How to check if a 3D point is in a planar polygon? and How to check if a 2D point is in a polygon?. I'm trying to do this to render specific shapes made up of spheres. ...
17
votes
7answers
1k views

How to check if a vector is an eigenvector of a matrix using mathematica?

Here is a vector $$\begin{pmatrix}i\\7i\\-2\end{pmatrix}$$ Here is a matrix $$\begin{pmatrix}2& i&0\\-i&1&1\\0 &1&0\end{pmatrix}$$ Is there a simple way to determine ...
17
votes
1answer
222 views

A bug in the overloading of MemberQ and FreeQ for Association?

Bug introduced in 10.0.0 and fixed in 10.1.0. While answering Pattern match for nested Association I came across behavior of MemberQ and ...
16
votes
2answers
1k views

How do I get Mathematica to return a function call unevaluated?

How do I get Mathematica to return a function call (conditionally) unevaluated? I expect this may use the slightly-mysterious Hold function. As a toy example, ...
14
votes
2answers
956 views

Checking if a word exists

Probably the simplest way to check a word if it exists is to use DictionaryWordQ[] DictionaryWordQ["color"] True But it ...
14
votes
1answer
225 views

IsomorphicGraphQ fails to recognize that two empty graphs are identical

Bug introduced in 9.0.1 or earlier and fixed in 11.0.1 Consider the following two graphs: ...
14
votes
1answer
142 views

Why is IsomorphicGraphQ so very slow (in this instance)

Bug introduced in 10.0 or earlier and persisting through 12.0.0 Can someone explain why the following code does not complete within a reasonable time? ...
13
votes
2answers
659 views

Better way to test that a matrix is upper-triangular?

Today, I got to thinking about how to test that a matrix was upper-triangular. So I had a try at it. Algorithm $\quad \quad a_{ij}=0 $ Or $i \leq j$ $\Rightarrow$ $True$ My solution is ...
13
votes
1answer
883 views

Unexpected slowness checking for empty list?

I thought I had a firm understanding of the differences between Equal and SameQ, but this has me puzzled: ...
13
votes
2answers
229 views

How can one write a robust ListableQ function?

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 Attributes. ...
12
votes
1answer
442 views

Why is an empty list not tested by VectorQ?

I am a bit amazed by this behavior of VectorQ[ expr, test ]: VectorQ[ {}, NumberQ ] True It seems that the test itself is ...
12
votes
1answer
437 views

Why does Assuming[x > 0, TrueQ[x > 0]] return False?

Assuming[x>0,TrueQ[x>0]] should, as I understand it, test TrueQ[x>0] after assuming ...
12
votes
1answer
220 views

ValueQ returns false positive for one argument type only

I'm seeing some perplexing behavior from ValueQ in 10.3. Consider: ...
11
votes
2answers
349 views

About shorthand notation for conditional pattern (Paul Wellin “Essentials of Programming in Mathematica”)

I am reading Paul Wellin's "Essentials of Programming in Mathematica". Mr. Wellin wrote the following in his book: There is a convenient shorthand notation for conditional patterns that is ...
11
votes
2answers
2k views

Evaluating an If condition to yield True/False

I would like to decide whether an option passed to my custom function has the value Automatic or something else. This is my attempt: ...
10
votes
5answers
745 views

Test if a list is a constant integer multiple of another list

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. ...
10
votes
3answers
1k views
10
votes
1answer
590 views

Difficulty understanding difference between == and ===

After doing this: ...
9
votes
5answers
14k views

How to detect if a sequence of integers is consecutive

I'm not a math-guy really (a programmer actually) and this is my first question. And excuse me if I can not explain my problem good. The problem is I have a sequence of integers and I want to detect ...
9
votes
2answers
626 views

How does Mathematica decide that Log[2,8] is integer?

At school we learned that $\log_b(x)=\log(x)/\log(b)$, which is implemented in Mathematica as Log[b, x], but the results are different. ...
9
votes
2answers
271 views

Test for non-numeric variables

I have a table, testdata, with numeric and non-numeric data. testData = {{1, 2, 3, no, 5}, {5, 6, 7, 8, 9.1}, {1, y, y, n, n}}; I can locate the positions of ...
9
votes
1answer
321 views

UnitaryMatrixQ function not returning true for a unitary matrix

So I know this matrix is unitary. It's a well proven fact in quantum mechanics and you can even check for yourself on pen and paper, Heres a quick proof: Assume $H$ is hermitian (i.e. $H^\dagger=H$) ...
9
votes
2answers
582 views

TrueQ returning False on true statement

Why does TrueQ return False on the following? TrueQ[Log[2]/Log[8] == 1/3] (* False *) ...
9
votes
1answer
187 views

1 is not the SameQ as Null, but why might 1 be Equal to Null?

The command SameQ[1, Null] returns False which is what I would expect, but the command ...
9
votes
1answer
115 views

Ordering real numeric quantities

Suppose I want to have a function (call it NLess) that given two real numeric quantities, a and b, it returns True if a is smaller than b, ignoring precision. Here are some test cases: ...
9
votes
0answers
190 views

Bug in PositiveDefiniteMatrixQ?

Fixed in 10.1.0. Exists in at least 9.0.1 -- 10.0.2. This seems like a bug in PositiveDefiniteMatrixQ to me: ...
8
votes
4answers
708 views

How to check is there any negative term in a large list?

I want to check if a data set of size $10^{10}$ contains any non-positive elements. Positive[Name of dataset] returns a list of ...
8
votes
2answers
1k views

A hack for aggressive type-checking?

I tend to like programming in a style where I can be very explicit about the types of expressions. While Mathematica isn't and shouldn't be a strongly-typed language like Haskell, I think it would ...
8
votes
1answer
172 views

Can anyone guess what Internal`RadicalMemberQ does?

There is an undocumented function Internal`RadicalMemberQ which takes two arguments. But whatever input I try for the two arguments, it seems to crash the kernel (...
8
votes
1answer
287 views

Is there a defined priority for pattern matching?

If you define a function g, like so: g[x_?EvenQ] := "even" g[x_?NumberQ] := "number" why will Mathematica always return ...
8
votes
2answers
451 views

Using OddQ and EvenQ in piecewise functions: Buggy?

I was trying to evaluate a sum over a piecewise function, not unlike this example. However, my piecewise function needed to be defined differently for even and odd ...
7
votes
1answer
105 views

Why does `SameQ` act inconsistently with `Image`s wrapped in `{}` versus not?

Take an Image that is displayed in a GraphicsBox, e.g. the result of ...
7
votes
1answer
142 views

Mystery with ValueQ

Consider the following ClearAll[jd]; ValueQ[jd] We expect False and get it. Now consider ...
7
votes
1answer
146 views

SameQ for unsorted lists

I have to compare two or more lists and check if they are the same, but their elements can be in different position. For instance ...
6
votes
5answers
839 views

Checking if all the variables are present in the defined function

I want to check if a user input the function with all the specified variables or not. For that I choose the replace variables with some values and check for if the result is a number or not via a ...
6
votes
2answers
3k views

Difference between != and =!=

I (think I) understand the difference between == (semantic) and === (syntactic). So when comparing != and =!= I was expecting a similar difference. So can anyone explain why the third line in this ...