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

Documentation  / Mathematica / Built-in Functions  / Advanced Documentation / Linear Algebra / Sparse Arrays /

 

Element-wise Operations

If you want to work on the elements of a sparse matrix, you can do this easily with Mathematica. First, a matrix of floating point numbers is created.

In[1]:=

Out[2]//MatrixForm=

In general arithmetic operations applied to a matrix work on each element. This adds 5 to the matrix.

In[3]:=

Out[3]//MatrixForm=

Here, every element of the matrix is squared.

In[4]:=

Out[4]//MatrixForm=

If one matrix is divided by another, the division is done element by element. If the dimensions of the two matrices do not agree, then an error ensues.

In[5]:=

Out[5]//MatrixForm=

To apply the Sin function to every element, you apply Sin to the entire matrix.

In[6]:=

Out[6]//MatrixForm=

If both of the arguments of an operation are matrices, the operation is carried out on corresponding elements. This result is the same as multiplying the matrix by two.

In[7]:=

Out[7]//MatrixForm=

If the one argument is a matrix and the other is a vector, the operation is carried out between rows of the matrix and elements of the vector. This is an efficient way of generating a diagonal sparse matrix.

In[8]:=

Out[10]//MatrixForm=

In[11]:=

Out[11]=

In[12]:=

Out[12]//MatrixForm=

These operations are all fast because they only need to operate on the elements that are actually stored (including the default element).

Note that multiplying two matrices using the operator Times is equivalent to multiplying the corresponding elements. On the other hand matrix multiplication can be done with the function Dot, which is described in the section Matrix Multiplication. An example of element-wise multiplication is shown in the following example.

In[8]:=

Out[9]//MatrixForm=

Listability

If you want to apply your own function to each element in a matrix, you may give your function the attribute Listable. This function squares each element and divides the result by 3.

In[1]:=

In[3]:=

In[4]:=

Out[4]//MatrixForm=

When a listable operation is applied to a sparse array, it only operates on the elements that are actually stored (including the default element), so the value of fun[0] is only computed once.

More information on Mathematica programming can be found in The Mathematica Book.

Map

Instead of using listability you can use Map to apply a function to every element in a matrix.

In[1]:=

Map will apply the function fun to every element in the matrix.

In[2]:=

Out[2]=

You can see the matrix that the sparse array represents. Note that because the function f did not have any definition, it is just kept wrapped around every element.

In[3]:=

Out[3]//MatrixForm=

Here, a function that squares its argument and divides the result by 5 is applied to every element in the matrix.

In[4]:=

Out[4]=

In[5]:=

Out[5]//MatrixForm=

When Map applies a function to a sparse array, it only operates on the elements that are actually stored (including the default element).

More information on Mathematica programming can be found in The Mathematica Book.



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


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