# Difference between revisions of "Data structures"

m (Created page with ' The power of NSM originates for a large part from the extensive use of multiple dimensions: The grouping of values into data structures makes it easy to handle large amounts of …') |
(conform NSM help) |
||

Line 1: | Line 1: | ||

− | |||

The power of NSM originates for a large part from the extensive use of multiple dimensions: The grouping of values into data structures makes it easy to handle large amounts of data. It also simplifies data administration. | The power of NSM originates for a large part from the extensive use of multiple dimensions: The grouping of values into data structures makes it easy to handle large amounts of data. It also simplifies data administration. | ||

+ | The multiple dimensions is also its weakest part. Manipulation involves large data structures, and more memory. | ||

+ | == Example 1: matrices and structures == | ||

+ | Consider a variable ''WeightOfJohn'' as a single value, for example the weight of an individual named John. | ||

− | + | WeightOfJohn = 78.5; < kg > | |

+ | Here the variable ''WeightOfJohn'' consists of one value. Compare this with the variable ''Weights'' where we put in all the weights of the family of John Doe. | ||

+ | Weights = 78.5, 59.3, 20, 5; <kg> | ||

− | + | Where the first weight is John's weight, the second weight is the weight of his wife Jane , the third is the weight of his son Steven and the last is from his baby Alice. So we can put a variable ''Name'' next to weight with the names of the family members: | |

− | + | Name = "John", "Jane", "Steven", "Alice"; | |

− | WeightOfJohn | + | As you can see the variables can consist of numeric values or of text values. In [[NSM]] you cannot mix these values within a single variable. Both variables have one dimension consisting of four elements. The variable ''WeightOfJohn'' as defined earlier consist of one element and in this case has also one dimension. Should we regroup the variable ''Name'' as |

− | + | <pre> | |

+ | John Jane | ||

− | Weights | + | Steven Alice |

+ | </pre> | ||

+ | with the parents in the first row and the children in the second row (and males in first column and females in second column) than the variable names has two dimension each of length two. In NSM we call this variable a [[matrix]] of size 2 x 2. The variable ''Weights'' with four values is than a matrix of size 4 and ''WeightOfJohn'' a matrix of size one. Scalars (single values without a dimension) do not exist in NSM, except for empty variables with no values at all => verwijzing <=). The regrouping in NSM is done by the [[Redim]]() function. In the example above we could regroup the variable Weights also to a 2 x 2 matrix. So | ||

+ | <pre> | ||

+ | Weights = Redim(Weights; 2,2) | ||

− | + | 78.5 59.3 | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | 78.5 59.3 | + | |

− | + | ||

− | + | ||

+ | 20 5 | ||

+ | </pre> | ||

After one year the family has another daughter, Jenny. In the current matrix, there is no space for Jenny. All rows of a matrix have the same length, the length of that specific dimension. So do all columns. If the family is allowed to have more than one male and one female child, we need another data structure for this family: | After one year the family has another daughter, Jenny. In the current matrix, there is no space for Jenny. All rows of a matrix have the same length, the length of that specific dimension. So do all columns. If the family is allowed to have more than one male and one female child, we need another data structure for this family: | ||

− | NameStruct = {NamesMale; NamesFemales}; | + | NameStruct = {NamesMale; NamesFemales}; |

With | With | ||

+ | <pre> | ||

+ | NameMales = "John"," Steven" | ||

− | + | NameFemales = "Jane", "Alice", "Jenny" | |

+ | </pre> | ||

+ | Now we call ''NameMales'' is a matrix of size 2 and ''NameFemales'' a matrix of size 3. The variable ''NameStruct'' is a structure consisting of two matrices, in this case two one-dimensional matrices. Note that is this structure, there is no information anymore on who are the parents and who are the children. If this information was more important than about the sexes, the structure would be | ||

− | + | NameStruct = {NamesParents; NamesChildren}; | |

− | + | ||

− | + | ||

− | + | ||

− | NameStruct = {NamesParents; NamesChildren}; | + | |

With | With | ||

+ | <pre> | ||

+ | NamesParents = "John"," Jane" | ||

− | + | NamesChildren = "Steven", "Alice", "Jenny" | |

− | + | </pre> | |

− | NamesChildren = "Steven", "Alice", "Jenny" | + | |

− | + | ||

We could also make a structure of four matrices: the names of the male adults, the female adults, the male children and the female children. | We could also make a structure of four matrices: the names of the male adults, the female adults, the male children and the female children. | ||

− | + | == Example 2: more than 2 dimensions == | |

− | Example 2: more than 2 dimensions | + | |

The head of a school wants to know how her students are doing. She asks each teacher for the average results of their class: | The head of a school wants to know how her students are doing. She asks each teacher for the average results of their class: | ||

− | AverageResults = 7.0, 6.5, 8.1, 6.6, 7.2, 7.7 | + | AverageResults = 7.0, 6.5, 8.1, 6.6, 7.2, 7.7 |

− | The variable AverageResults consists of a one-dimensional matrix with the first element representing the average results of the first class, second of second class | + | The variable ''AverageResults'' consists of a one-dimensional matrix with the first element representing the average results of the first class, second of second class etc. |

On second thought, these average values are a rather coarse measure and she decides she needs the class average results for each examination period. The variable she is now interested in will be a two-dimensional matrix. The first dimension represents the classes and has length = six. The second dimension represents the examination periods, and its length will equal the number of examination periods over the year. | On second thought, these average values are a rather coarse measure and she decides she needs the class average results for each examination period. The variable she is now interested in will be a two-dimensional matrix. The first dimension represents the classes and has length = six. The second dimension represents the examination periods, and its length will equal the number of examination periods over the year. | ||

− | + | <math> | |

− | + | \begin{pmatrix} | |

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> | ||

+ | ( ''de a's worden dan vervangen door echte getallen'' ) | ||

If the head of the school wants to differentiate between the results of boys and girl students, she could add another dimension. This would represent sex and have length = two. The result is a 6 (classes) x 4 (examination periods) x2 (sexes) matrix | If the head of the school wants to differentiate between the results of boys and girl students, she could add another dimension. This would represent sex and have length = two. The result is a 6 (classes) x 4 (examination periods) x2 (sexes) matrix | ||

+ | <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

− | + | </math> | |

− | + | ||

− | + | ||

An inspector supervises five schools. He gets the results of the heads of all the schools. They al send a 6x4x2 matrix, which the inspector can combine into a 6 (classes) x4 (examination periods) x 2 (sexes) x 5 (schools) matrix: | An inspector supervises five schools. He gets the results of the heads of all the schools. They al send a 6x4x2 matrix, which the inspector can combine into a 6 (classes) x4 (examination periods) x 2 (sexes) x 5 (schools) matrix: | ||

+ | <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> | ||

+ | <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> | ||

+ | <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> | ||

+ | <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> | ||

+ | <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> <math> | ||

+ | \begin{pmatrix} | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a \\ | ||

+ | a & a & a & a & a & a | ||

+ | \end{pmatrix} | ||

+ | </math> | ||

+ | == see also == | ||

+ | * [[ Pivotal dimensions]] | ||

− | + | [[category:NSM Programming]] | |

− | + | ||

− | + | ||

− | [[category:NSM | + |

## Latest revision as of 20:20, 17 June 2010

The power of NSM originates for a large part from the extensive use of multiple dimensions: The grouping of values into data structures makes it easy to handle large amounts of data. It also simplifies data administration. The multiple dimensions is also its weakest part. Manipulation involves large data structures, and more memory.

## Example 1: matrices and structures

Consider a variable *WeightOfJohn* as a single value, for example the weight of an individual named John.

WeightOfJohn = 78.5; < kg >

Here the variable *WeightOfJohn* consists of one value. Compare this with the variable *Weights* where we put in all the weights of the family of John Doe.

Weights = 78.5, 59.3, 20, 5; <kg>

Where the first weight is John's weight, the second weight is the weight of his wife Jane , the third is the weight of his son Steven and the last is from his baby Alice. So we can put a variable *Name* next to weight with the names of the family members:

Name = "John", "Jane", "Steven", "Alice";

As you can see the variables can consist of numeric values or of text values. In NSM you cannot mix these values within a single variable. Both variables have one dimension consisting of four elements. The variable *WeightOfJohn* as defined earlier consist of one element and in this case has also one dimension. Should we regroup the variable *Name* as

John Jane Steven Alice

with the parents in the first row and the children in the second row (and males in first column and females in second column) than the variable names has two dimension each of length two. In NSM we call this variable a matrix of size 2 x 2. The variable *Weights* with four values is than a matrix of size 4 and *WeightOfJohn* a matrix of size one. Scalars (single values without a dimension) do not exist in NSM, except for empty variables with no values at all => verwijzing <=). The regrouping in NSM is done by the Redim() function. In the example above we could regroup the variable Weights also to a 2 x 2 matrix. So

Weights = Redim(Weights; 2,2) 78.5 59.3 20 5

After one year the family has another daughter, Jenny. In the current matrix, there is no space for Jenny. All rows of a matrix have the same length, the length of that specific dimension. So do all columns. If the family is allowed to have more than one male and one female child, we need another data structure for this family:

NameStruct = {NamesMale; NamesFemales};

With

NameMales = "John"," Steven" NameFemales = "Jane", "Alice", "Jenny"

Now we call *NameMales* is a matrix of size 2 and *NameFemales* a matrix of size 3. The variable *NameStruct* is a structure consisting of two matrices, in this case two one-dimensional matrices. Note that is this structure, there is no information anymore on who are the parents and who are the children. If this information was more important than about the sexes, the structure would be

NameStruct = {NamesParents; NamesChildren};

With

NamesParents = "John"," Jane" NamesChildren = "Steven", "Alice", "Jenny"

We could also make a structure of four matrices: the names of the male adults, the female adults, the male children and the female children.

## Example 2: more than 2 dimensions

The head of a school wants to know how her students are doing. She asks each teacher for the average results of their class:

AverageResults = 7.0, 6.5, 8.1, 6.6, 7.2, 7.7

The variable *AverageResults* consists of a one-dimensional matrix with the first element representing the average results of the first class, second of second class etc.

On second thought, these average values are a rather coarse measure and she decides she needs the class average results for each examination period. The variable she is now interested in will be a two-dimensional matrix. The first dimension represents the classes and has length = six. The second dimension represents the examination periods, and its length will equal the number of examination periods over the year.

( *de a's worden dan vervangen door echte getallen* )

If the head of the school wants to differentiate between the results of boys and girl students, she could add another dimension. This would represent sex and have length = two. The result is a 6 (classes) x 4 (examination periods) x2 (sexes) matrix

An inspector supervises five schools. He gets the results of the heads of all the schools. They al send a 6x4x2 matrix, which the inspector can combine into a 6 (classes) x4 (examination periods) x 2 (sexes) x 5 (schools) matrix: