Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.

Testing Matrices

Mathematica provides a number of functions for testing sparse matrices and extracting size information.

The predicate MatrixQ can be used to test sparse matrices. It can also be used to test dense matrices as shown previously.

In[1]:= 

Out[2]//MatrixForm=

This tests that sp is a sparse matrix.

In[3]:= 

Out[3]=

MatrixQ takes an optional second argument that specifies a test to apply to every element. In this example every element is tested to see if it is an integer.

In[4]:= 

Out[4]=

In the example below, every element must be an integer greater than 1. Because some of the elements are 0, the result is False.

In[5]:= 

Out[5]=

In addition to MatrixQ, there are predicates VectorQ and ArrayQ, which are useful for testing vectors and tensors.

In[6]:= 

Out[6]=

In[7]:= 

Out[7]=

ArrayQ can also take a rank argument to test the depth of the array. In this example, the sparse argument is not a rank-4 tensor and the result is False.

In[8]:= 

Out[8]=

ArrayQ also takes a third argument that tests each element. In this example the result is True because all the elements are NumberQ.

In[9]:= 

Out[9]=

The command Dimension is useful for extracting size information.

In[10]:= 

Out[11]//MatrixForm=

In[12]:= 

Out[12]=

Dimensions returns a list of length 2 when the input is a matrix, stating that two indices are used to reference any element in the matrix. Another way to test the number of indices required to reference elements is with ArrayDepth; this is equivalent to Length[Dimensions[sp]].

In[13]:= 

Out[13]=

To compare if the elements of two matrices are equal it is possible to use Equal, typically entered using a Equal short-hand notation. For example, comparing a matrix with itself returns True.

In[14]:= 

Out[15]=

Equal uses the value of numbers so it can be used to compare integer and real values.

In[16]:= 

Out[16]=

It should be noted that Equal works on any Mathematica expression. If you want to compare two matrices for equality using properties of the matrix as a whole, it may be better to compare matrix norms. These are discussed in a later section.


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2009 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: