Team LiB   Previous Section   Next Section

3.2 Working Example

Because of the nature of the material in this chapter, we haven't been able to apply one common example across all the recipes. We have a linear-structure example that we use in our recipes on lists, stacks, and queues. We extend that example to cover arrays. However, when it comes to matrices, we decided to use a simple, textbook-style example for clarity.

3.2.1 Linear Structures

You are building a quality-control system for a biotech company. One of the production lines produces a special liquid for laboratory research. A specific characteristic you need to measure is the liquid's purity, which is indicated by a purity index. The normal level of purity is represented by a purity index of 100. Everything over 100 is assumed to be abnormal and requires some action to bring the product back into spec.

The liquid comes off the production line in containers. The table that stores quality control data contains the ID of each container along with the measured purity of the liquid within the container. The following CREATE TABLE statement shows the structure of this table. The ContainerId column contains the container ID numbers, and the Purity column contains the purity index values.

CREATE TABLE ProductionLine (
   ContainerId INTEGER,
   Purity INTEGER
)

Currently, the table contains purity information on 12 different containers:

ContainerId Purity      
----------- ----------- 
1           100
2           100
3           101
4           102
5           103
6           100
7           103
8           108
9           109
10          100
11          100
12          100

Your job, our job actually, is to develop some tools to help in evaluating the quality of the product and the functioning of the production machines.

3.2.2 Arrays

To demonstrate the use of arrays, we'll extend our production-line example. If you need to control several production lines, you can represent their purity data as an array. For example:

CREATE TABLE ProductionFacility(
   Line INTEGER,
   ContainerId INTEGER,
   Purity INTEGER
)

At first glance, this table might not look like an array, but it is. Each row represents one purity-level reading, which is stored in the Purity column. The Line and ContainerId columns are the indices to the array. Given a line number and a container ID number, you can easily retrieve the associated purity value.

Our facility has four production lines, and our ProductionFacility table is currently populated with the following data:

Line        ContainerId Purity      
----------- ----------- ----------- 
0           1           100
0           2           100
0           3           100
1           1           102
1           2           103
1           3           100
2           1           103
2           2           108
2           3           109
3           1           100
3           2           100
3           3           100

The sequence of rows in this output is not important. And, while we've chosen to identify production lines by number, the production lines could easily be identified by a name or by some other identifier. However, the ContainerId values for each line must not only be numeric; they must be sequential for us to make use of the linear structure and related algorithms that we demonstrate in this chapter's recipes.

3.2.3 Matrices

When you write a program to work with matrices, it's usually best to store your matrices in a table just as you would store arrays. Use one row for each matrix element, and include the element's coordinates as part of that row. For demonstration purposes, we are going to use the following Matrices table:

CREATE TABLE Matrices (
   Matrix VARCHAR(20),
   X INTEGER,
   Y INTEGER,
   Value INTEGER
)

Each row in the Matrices table represents one matrix element. The columns are used as follows:

Name

Associates a row with a specific matrix. Our table can hold multiple matrices, and each is given a unique name.

X

Holds the X coordinate of the element in question.

Y

Holds the Y coordinate of the element in question.

Value

Holds the value of the element in question.

We fill our sample table with the following:

  • Two 2 x 2 matrices, named A and B

  • One vector, named S

  • One 3 x 3 matrix, named D

Following is the output from a SELECT query against the Matrices table, showing the raw data that we are going to operate on in our recipes:

SELECT * FROM Matrices:

Name                 X           Y           Value       
-------------------- ----------- ----------- ----------- 
A                    1           1           6
A                    1           2           3
A                    2           1           4
A                    2           2           7
B                    1           1           6
B                    1           2           3
B                    2           1           5
B                    2           2           2
S                    1           1           5
S                    2           1           6
D                    1           1           3
D                    1           2           4
D                    1           3           5
D                    2           1           5
D                    2           2           6
D                    2           3           7
D                    3           1           8
D                    3           2           9
D                    3           3           0
    Team LiB   Previous Section   Next Section