> For the complete documentation index, see [llms.txt](https://jona-projects.gitbook.io/kiwicalc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jona-projects.gitbook.io/kiwicalc/functioncollection.md).

# FunctionCollection

### class `FunctionCollection`

The `FunctionCollection` class is used in order to represent a collection of functions, namely, a collection of `Function` objects.

#### Properties

* `functions` - returns a list of the `Function` objects in the collection.
* `num_of_functions` - returns the number of the `Function` objects in the collection.

#### `__init__()`

You can create a new instance `FunctionCollection` by entering `Function` objects or strings that represent the functions. Here is the signature of the method:

```
                        def __init__(self, *functions, gen_copies=False):
                    
```

In addition, if `gen_copies` is set to `True`, the collection will take the copies of the items instead of the originals. Here are some examples:

```
                        functions = FunctionCollection(Function("f(x) = x^2"), Function("g(x) = sin(x)"), Function("h(x) = 2x"))
print(functions)

                    
```

```
                        functions = FunctionCollection("f(x) = x^2", "g(x) = sin(x)", "h(x) = 2x")
                    
```

```
                        functions = FunctionCollection(Function("f(x) =x^2"), "g(x) = sin(x)", Function("h(x) = 2x"))
                    
```

The output will be the same in all of these ways:

```
                        1. f(x)=x**2
2. g(x)=sin(x)
3. h(x)=2x
                    
```

#### `add_function()`

You can add new functions to the collection after initialization via the `add_function` method. You can either enter a `Function` object or a string that represent a function.For instance:

```
                        functions.add_function(Function("f(x) = 2x"))
```

```
                        functions.add_function("f(x) = 2x")
                    
```

#### `extend()`

You can add a collection of functions via the `extend()` method. The method takes a collection of either `Function` objects or strings. Here is the signature of the method:

```
                        def extend(self, functions: Iterable[Union[Function, str]]):
                    
```

For example:

```
                        functions.extend(["f(x,y) = x+y", "g(x,y) = cos(x)+cos(y)", "h(x,y) = sin(x)*sin(y)"])
                    
```

#### `clear()`

Pretty straightforward, you can use the `clear()` method to clear the collection of functions. After that, the collection will considered empty.

#### `is_empty()`

The function will return `true` if the collection is empty, otherwise, `False`. For example:

```
                        functions = FunctionCollection()
print(functions.is_empty())
functions.add_function("f(x) = ln(x)")
print(functions.is_empty())
                        
```

The output is:

```
         True
False
                    
```

```
     functions = FunctionCollection(Function("f(x) =x^2"), "g(x) = sin(x)", Function("h(x) = 2x"))
functions.clear()
print(functions.is_empty())
                    
```

The output is

```
                        True
                    
```

#### `random_function()`

Fetch a random `Function` object from the collection. For example:

```
                        functions = FunctionCollection(Function("f(x) =x^2"), "g(x) = sin(x)", Function("h(x) = 2x"))
print(functions.random_function())
                    
```

And the output is:

```
                        h(x)=2x
                    
```

#### `random_value()`

Fetch a random value from a random function in the collection.

Parameters:

* `a` - lower bound of the range of numbers to pick the parameters from.
* `b` - higher bound of the range of numbers
* `mode` - if mode is `"int"`, integer parameters will be randomly picked via `random.randint(a, b)`, if mode is `"float"`, parameters of type `float` will be randomly chosen via the `random.uniform(a, b)` method. The default is `"int"`.

For example:

```
                        functions = FunctionCollection(Function("f(x) =x^2"), "g(x) = sin(x)", Function("h(x) = 2x"))
print(functions.random_value(1, 10))
                    
```

```
                        functions = FunctionCollection("f(x,y) = x + y", "g(x,y) = x - y", "h(x,y) = sin(x) * cos(y)")
print(functions.random_value(1, 10))
                    
```

#### `plot()`

You can plot all of the functions on a shared axis system via the `plot()` method. Here is the signature of the method:

```
                    def plot(self, start: float = -10, stop: float = 10,
        step: float = 0.01, ymin: float = -10, ymax: float = 10, text=None, show_axis=True, show=True):
                
```

For example:

```
                        functions = FunctionCollection("f(x,y) = x + y", "g(x,y) = x - y", "h(x,y) = sin(x) * cos(y)")
functions.plot()
                    
```

#### `scatter()`

You can scatter all of the functions on a shared axis system via the `plot()` method. Here is the signature of the method:

```
                        def scatter(self, start: float = -10, stop: float = 10,
        step: float = 0.01, ymin: float = -10, ymax: float = 10, text=None, show_axis=True, show=True):
                    
```

For instance:

```
                        functions = FunctionCollection("f(x,y) = x + y", "g(x,y) = x - y", "h(x,y) = sin(x) * cos(y)")
functions.scatter()
                    
```

#### `__len__()`

You can also fetch the number of functions in the collection via the `len()` method. For instance:

```
                        functions = FunctionCollection("f(x) = 2x", "g(x) = x^2", "h(x) = sin(x)")
print(len(functions))
                    
```

Output:

```
                        3
       
```
