Welcome to Sparse Math

This software is intended to act as a Matlab-like environment with an emphasis on sparse matrix computation. This file acts as a quick tutorial for using Sparse Math.

Basic Syntax

The basic structure of spmath commands is much like that of Matlab. Expressions that are entered on the spmath command line are then executed and the result is printed out. For example:

spmath:1> 3 + 3
6
spmath:2> sqrt(49)
7

You can easily define your own variables by using the '=' operator, as in Matlab:

spmath:3> a=3
3
spmath:4> twenty_seven=27
27

Sparse Math supports variables in the form of numbers, matrices, strings and user-defined functions (discussed later). To see a list of variables, matrices, and functions, type "who" or "whos":

spmath:6> magic=[8 1 6;3 5 7;4 9 2]
8       1       6
3       5       7
4       9       2

spmath:7> greeting="Welcome"
Welcome
spmath:8> function a=a() a = 1 end

spmath:11> who
Current Variables:

slvr_krylov
magic
slvr_prec_type
pi
slvr_solver_name
slvr_prec_name
slvr_prec_args
true
a
slvr_maxmult
e
greeting
false
twenty_seven
slvr_tols

You can prevent spmath from printing the result by ending the command with a semicolon:

spmath:12> gr=12*12;
spmath:13> gr
144

Sparse Math also supports loop constructs and conditional constructs:

spmath:14> for(i=1:4) i end
1
2
3
4

spmath:15> if(3 > 4) "bad" else "good" end
good

Sparse Math will also let you define your own functions for use later:

spmath:16> function a=plus_one(b) a = b + 1; end

spmath:17> plus_one(1)
2

Sparse Math will read in sparse matrices from Harwell-Boeing files and store them in variables:

spmath:18> fidap=hbin("fidap005.rua")
(1,1)   1.03704e+06
(1,2)   259259
(1,10)  -1.18519e+06
(1,11)  -296297
(1,19)  148148
 (...)
(27,9)  148148
(27,17) -296297
(27,18) -1.18519e+06
(27,26) 259259
(27,27) 1.03704e+06

You can also take sparse matrix variables and write them to Harwell-Boeing files:

spmath:19> a=[1 0 0 1];
spmath:20> a=a'*a
1       0       0       1
0       0       0       0
0       0       0       0
1       0       0       1

spmath:21> a=sparse(a)
(1,1)   1
(1,4)   1
(4,1)   1
(4,4)   1

spmath:22> hbout(a, "fourcorn.rua")
1

Sparse Math contains many built-in iterative solvers and preconditionners for the solving of sparse linear systems. The "solve" command gives you access to these:

spmath:30> a=fdmat(15,15,1,[0 0 0 0 0 0]);
spmath:31> rhs=a*ones(nrow(a),1);
spmath:32> guess=zeros(nrow(a),1);
spmath:33> solve(a,rhs,guess);
 0  0.
 1  7.9571557
 2  0.23333922
 3  0.00814962946
 4  9.58796081E-05
 Iterative sovler has satisfied convergence test.
 5  8.2538827E-07
 # retrun code = 0    convergence rate =  1.74602489
 # the actual residual norm is  8.25388286E-07
 # the error norm is  1.46773467E-06

For more examples of Sparse Math's functionality, see the tests and demos included in SPMATH's "scripts" subdirectpry. You can load a demo file at any time by using the load command:

spmath:37> load("demo1.sp")

For help at any time, type "help."


This page was last modified August 14th, 2001 by Ben Langmead