# Functions defined by an algorithm

## Introduction

### Goals

1. to define functions from algorithms, consistent with functions already implemented in Casyopee,

2. to display graphs, tables and curves of these functions in the same screens as the other functions, and allow certain calculations.

3. to propose a simple programming structure for a first initiation.

### Situations

1. in a geometric context: trajectories

2. in analysis : approximation of anti-derivative, Euler method for differential equations

3. ... imagine .
.

### Choices

1. Two special variables x and y are pre-declared.
2. The function is defined
* As a piecewise linear function f such that f (x) = y for each pair of values (x, y) obtained in the iteration.
* Or as a function f with values in IR▓ f (t) = [X (t); Y (t)], such that the function X (resp. Y) is piecewise linear and, for each integer t takes the value of x (resp y) obtained at step t of the iteration.
3. The other variables are identified by an alphabetical character not already used for another objet.
4. The program is made of two blocks
* A declaration  and initialization block.
* An iteration block under one of four forms.
5. Inside the body of the loop, assigning a formula to a declared variable is only permitted.
6. The condition for exiting (or continuating) the loop is either variable > formula or variable e" formula or variable <formula or variable d" formula,
7. The are entered like in other Casyopee modules. They may include one or more program variables and other symbolic objects defined in Casyopee (functions, parameters).
8. There is no input / output instruction. The observable result of programming is the creation of a function.
9. There is no alternative structure (if .. then .. else...).

### Interface

The functions by algorithm are created via the algebra tab menu. The creation window opens The central part has two resizable panels. On the left is the program area. One operates in this area by way of the first 5 menu entries.
On top of the right panel there is a test area where the successive values of the variables involved in the iteration will be displayed. Below, there are several choices:
(1) the choice of the type of function

* Choosing left corresponds to a piecewise linear function f such that f (x) = y for each pair of values (x, y) obtained in the iteration. With this choice, the iteration will be stopped as soon as two consecutive values of x will be in decreasing order.

* Choosing the right corresponds to a function f with values in IR▓ f (t) = [X (t); Y (t)], such that the function X (resp. Y) is piecewise linear and, for each integer t takes the value of x (resp y) obtained at step t of the iteration. With this choice, the iteration will be continued without considering the order of the successive values of x.
The difference between the two options is illustrated by the last example in the UserManual.

(2) a field to enter a free comment to identify the function.

(3) the maximum number of iterations. If the program does not end with a number of iterations less than the value chosen, it will be stopped when this number is reached.

At the bottom of the window, there is the creation bar common to all input windows: choice of an identifier of the function, validation or exit. Validation is only possible after a test without syntax  or execution error.
If you choose to exit, no function is created, but the program is not lost: it will be available again at the next activation of the window. If you choose OK, the function appears in the list of functions of the algebra strand, with the comment to identify it. Then with the context menu (right click) you can either delete it or change it.
The function button also appears in the curves menu of the geometry tab and among the buttons of the graphical window. See their uses in the UserManual.menu entries

#### Declarevariable

This entry creates a variable and afffecter it an initial value.
In the bar that opens there is a list to choose an identifier in the set of available characters and a field to enter a formula.
The syntax of the formula will be checked during a test.
After validation, a line is created: identifier É! formula. Note: when a line of the declaration part is selected, pressing the Enter key has the same effect.

#### Assign variablein the loop

This entry will affect a value to a declared variable  in the body of the iteration (loop), In the bar that opens there is a list to choose an identifier in the set of declared variables and a field to enter a formula.
The syntax of the formula will be checked during a test.
After validation, a line is created: identifier É! formula
Note: when a line of the loop body is selected, pressing the Enter key has the same effect.
##### Modify
This entry allows you to edit a line already entered.
Two cases:
(1) it is one of the two lines delimiting the loop body.
Initially the structure is Loop ... EndLoop. With this structure, the program will stop only when the maximum number of iterations has been reached.
In the bar that opens, three other structures can be selected using the list on the left. 1. If the WHILE structure is selected, two lists (one for the variable, the other for signs  >, e", <or d") and a formula input field are provided for a continuation condition.
2. If the FOR structure is selected to a list for the loop variable and a formula input field are provided. The entry field is the upper value taken by the variable.
3. If theRepeat until structure is chosen  two lists (one for the variable, the other for one of the signs>, e", <or d") and a formula input field are provided to a condition of stop.
Note: in the WHILE   (resp. Repeat until) structure,Casyopee will check that the variable in the continuation (resp stop) condition evolves in the loop. In the structure FOR it will be verified that there is no assignment to the loop variable.

(2) for other lines, an input bar lets you change the variable and / or formula.

Note: double click on a line has the same effect
##### Comment
This entry inserts a line of text that will not be evaluated.

#### Delete line

Removes the selected line. It is not possible to delete the declaration lines of x and y, or those delimiting the loop body.

#### Test

The first click will display a check list of the program variables.
We can thus choose the variables whose values we want to examine during the iteration.
A second click will display a table of he sequence of values of the selected variables.
All syntax and execution errors are detected.
If there is no error, it is possible to create the function by the OK button.

#### Parameters

The formulae used in the program may involve algebraic objects Casyopee, and therefore the parameters.
During the test, the current values of the parameters are used. It is therefore useful to set and / or edit the associated data (min, max, not, etc.
This menu item will display a parameters management window.

#### Reset all

Resets the program to the initial state.