# Using Spot Containers

guide_containers

Spot provides a set of "wrappers" that can be used to integrate external opertors with Spot utilities.

## Function containers

Suppose that we have an existing function that implements a discretized Heaviside step function. This function returns a vector with the cumulative sum of the input vector (i.e., Matlab's cumsum function.) In the "adjoint" mode, our function returns the cumulative-sum vector, but in reverse order. Here is one simple way to implement such a function:

```type heaviside.m
```
```function y = heaviside(x,mode)
%heaviside  The discrete Heaviside transform.
if mode == 1
y = cumsum(x);
else
y = flipud(cumsum(flipud(x)));
end
end
```

In order to make this function available as a Spot operator, we wrap it using opFunction:

```n = 5; m = 5;
A = opFunction(n,m,@heaviside);
```

Note that we provide the number of rows and columns for this particular instance, because all Spot operators must have their dimensions defined. Now A can be used like any other Spot operator. For example:

```B = A(1:2:end,:);         % B is a new operator with even rows of A
double(B)                 % print the elements of B
```
```Error using double.heaviside
Too many input arguments.
Error in opFunction>@(x,mode)funhandle(x,mode) (line 61)
fun = @(x,mode) funhandle(x,mode);
Error in opFunction/multiply (line 84)
y = op.funHandle(x,mode);
Error in opSpot/applyMultiply (line 96)
y(:,i) = op.multiply(x(:,i), mode);
Error in opFoG/multiply (line 124)
z = applyMultiply(op.children{2},y,mode);
Error in opSpot/applyMultiply (line 96)
y(:,i) = op.multiply(x(:,i), mode);
Error in opSubsRef/multiply (line 120)
y = applyMultiply(op.opIntrnl,x,mode);
Error in opSpot/applyMultiply (line 96)
y(:,i) = op.multiply(x(:,i), mode);
Error in opCTranspose/multiply (line 87)
y = applyMultiply(A,x,2);
Error in opSpot/applyMultiply (line 96)
y(:,i) = op.multiply(x(:,i), mode);
Error in * (line 73)
y = A.applyMultiply(B,1);
Error in opSpot/double (line 13)
M = (A'*speye(size(A,1)))';
Error in guide_containers (line 29)
double(B)                 % print the elements of B```

## Matrix containers

Spot's default behavior when multiplying operators with matrices is to apply the operator to each column of the matrix. For example, this next command scales the columns of the Heaviside operator:

```C = A * diag(1:m)
```

But if we instead want to form a new operator formed from the products of A and the diagonal, we first need to wrap the diagonal matrix:

```C = A * opMatrix(diag(1:m))
```

Though in this special case, we might have as well used the Spot operator opDiag instead of the more general (and in this case, cumbersome) opMatrix|(diag(1:m))|.

## Class containers

Finally, the opClass container can be used to encapsulate objects defined by external toolboxes.