# Introduction to SWP: Step 3 — Points and sets

## Contents |

## Concept of points and sets

SWP language is specially designed to work with sets of points. One may use scripts to create points and sets and save them to variables like numbers and apply operators to them. So point and set are data types, two of the most important in SWP.

## Notion of field

SWP point is collection of **fields**, the text names which real numbers was assigned to.

Firstly, SWP is designed to operate sets of point in space of any dimension \(\mathbb R^n\). So the number of Cartesian coordinates is not known beforehand. Although on line, plane and space coordinated could be called \(x\), \(y\) and \(z\), there is no uniform name for coordinates of 4 and more dimensioned space. Alongside naming coordinates 1, 2, 3, etc. would be not very comfortable.

Secondly, SWP is a tool to operate weighted points, i.e. points are not just defined by spatial coordinates but may contain another characteristics which number and sense varies from task to task (e.g. weight, type, brightness and others).

So SWP user have to set manually the list of fields included in the point, i.e. think of its names (strings) and assign values (real numbers). For further calculation user have to inform SWP commands which fields actually contains source data.

### Points creation, simplest case

Creating of a point could be done using a command like this:

p := point ("x" = 1.0, "y" = 1.5, "w" = 10);

Here `p`

is a variable which point will be assigned to, and `point ("x" = 1.0, "y" = 1.5, "w" = 10)`

is expression of point creation. In the round branches after function point name required list of fields and its values is set. Fields can have any string names while values should be a real number. Field names and values are separated by equality sign. Fields are separated with commas, a point may contain as many fields as necessary.

If a field name is not followed by the equality sign and a value its value is set to zero.

### Printing points

Like numeric variables points could be converted to strings using "->str" construction after the point. Resulting string contains the list of converted point's fields and values. Like the real number converting point converting may be supplied number of decimal digits in square branches.

To out not all but chosen fields one can list required fields in the square branches separating by commas. To combine the precisions and list of required fields setting in the square branches more complex construction should be used:

["apply" = (list of field), "p" = precision]

Here "apply" and "p" are arguments of conversion to string responded to the list of outputted fields and number of digits printed after the decimal point correspondingly. The value of the argument are typed after "=" sign, the arguments are separated by ",". Arguments could listed in any order, so constructions

["apply" = ("x", "y"), "p" = 2]

are

["p" = 3, "apply" = ("x", "y")]

have the same effect.

For example:

p := point ("x" = 1.0, "y" = 1.5, "w" = 10, t); print "Full information: " + p->str + "\n\n"; print "Precision 2: " + p->[2]str + "\n\n"; print "Just 'w' and 'x' fields: " + p->["w", "x"]str + "\n\n"; print "Precision 2, just 'w' and 'x' fields: " + p->["p"=2, "apply" = ("w", "x")]str + "\n\n";

выведет

Full information: "t" = +0.000000000000000e+00, "w" = +1.000000000000000e+01, "x" = +1.000000000000000e+00, "y" = +1.500000000000000e+00 Precision 2: "t" = 0.00, "w" = 10.00, "x" = 1.00, "y" = 1.50 Just 'w' and 'x' fields: "w" = +1.000000000000000e+01, "x" = +1.000000000000000e+00 Precision 2, just 'w' and 'x' fields: "w" = 10.00, "x" = 1.00

Please pay attention on round branches around the list of printed fields. If just one field is required they are not necessary, however if two or more fields are to be set absence of branches would case SWP thinking that just the first one is a value of "apply" argument while the second and following ones are separate arguments of the conversion to string (of course, most likely unknown one, so the error will occur).

SWP keeps and prints fields in alphabetic order to optimize speed of execution.

## Sets

**Set** is a collection of points containing *the same* list of fields.

For example, if one creates set each point of which contain exactly two fields named `x`

and `y`

, this set could be treated as a collection of points in a plane (literary *set of point*).

Same way set of points with two fields named `x`

and `f`

and treat it as table of function, i.e. argument `x`

is matched with function value `f`

. Of course nothing can prevent from working with a set of two fields `x`

and `y`

like with tabulated function, in most cases it is the user who can choice field names.

So sets are basic working SWP tools. Possibility of choosing field names let not only opportunity to supply each point with required number of characteristics but wide freedom of semantic interpretation of the fields.

Sets of points could be created using standard generators of distributions of point in space, loaded from a text file, created by another program or returned by an object catalogue DB query.

Also the sets are returned as results of calculating correlation function, distribution function and other calculations, almost everything calculated in SWP is returned as set.

Sets may be plotted. It is obviously a similar process for sets of points in space and tabulated functions, one just have to tell SWP names of fields containing values of abscissa and ordinate.

### How the sets are kept

SWP saves all sets in disk files. If a running script contains a command to create a set of a command returning a set then SWP creates files containing these sets.

It is done to let the results of calculation be preserved after end of script execution. Let an example script start with generating set and calculating a correlation function for 3 hours and end with handling this function. If this script contains an error just after correlation function calculation without set saving its after fix restarting will cause loosing 3 hours of time!

All SWP set-creating internal function assigns a unique file name to each created set. Identical sets (generated in the same way from the same source data) will have the same file name. If function saw that such file already exists set regeneration or recalculation would not be done. This allows to same time both during re-run the same or fixed script as well as in a case when script containing commands to create the same set second time.

*Because of this SWP feature it is highly recommended to put each independent script to a separate disk directory (folder). After finding serious errors in the script it might be useful to delete all created files and re-run script.*

### Manual set creation

Sometimes it could be useful to create a set by hand directly from a script, for example to same results of calculation for a model grid in a form suitable for drawing a plot.

Like any other data sets could be assigned to variables. The first step of the manual set creation is to call a command like

s := set ("pnt" = point ("x", "f"), "name" = "set1");

Here "`s`

" is the variable to keep created set in. Argument "`pnt`

" specify a basic set point: in this case just the list of its fields but not their values are significant, so they are omitted. Argument "`name`

" specify name of a file to save set to.

To start point creation one have to run command

s -> begin_put;

This command tells system to begin saving set to file (*opens file for writing*). This plain way of set creation doesn't check if the file already exists. If it so the file will be overwritten and its content lost.

Adding a point to set is provided by two commands:

s ~= point ("x" = 1.5, "f" = 2.0); s -> put;

This script fragment adding to the set a point with values of fields `x`

and `f`

equals to \(1.5\) и \(2.0\) correspondingly.

For example, script

#!/usr/bin/swp s := set ("pnt" = point ("x", "y"), "name" = "set1"); s -> begin_put; s ~= point ("x" = 1.5, "y" = 2.0); s -> put; s ~= point ("x" = 0.5, "y" = -2.1); s -> put; s ~= point ("x" = -1.5, "y" = .3); s -> put; s ~= point ("x" = 1.0, "y" = 12); s -> put;

creates a set of four points.

### Printing set to the screen

To out all set's points to the screen typing "`print s->str;`

" would not be enough. Although this command will print some information on set it wouldn't contain values of set's all points fields. Correct method would be

forall p in s do print p->str + "\n";

The command "`forall p in s->list do`

" tells that variable "p" will go over all points of the set "s". The next line is for standard printing values of the point "p" fields to screen:

"x" = +1.500000000000000e+00, "y" = +2.000000000000000e+00 "x" = +5.000000000000000e-01, "y" = -2.100000000000000e+00 "x" = -1.500000000000000e+00, "y" = +3.000000000000000e-01 "x" = +1.000000000000000e+00, "y" = +1.200000000000000e+01

Of course, the second line could be supplied with argument Разумеется, в третьей строке можно указывать аргументы вывода значений точки на экран как обычно.