<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def forest(S=3, r1=4, r2=2, p=0.1, is_sparse=False):
 """Generate a MDP example based on a simple forest management scenario. This function is used to generate a transition probability (``A`` × ``S`` × ``S``) array ``P`` and a reward (``S`` × ``A``) matrix ``R`` that model the following problem. A forest is managed by two actions: 'Wait' and 'Cut'. An action is decided each year with first the objective to maintain an old forest for wildlife and second to make money selling cut wood. Each year there is a probability ``p`` that a fire burns the forest. Here is how the problem is modelled. Let {0, 1 . . . ``S``-1 } be the states of the forest, with ``S``-1 being the oldest. Let 'Wait' be action 0 and 'Cut' be action 1. After a fire, the forest is in the youngest state, that is state 0. The transition matrix ``P`` of the problem can then be defined as follows:: P[0,:,:] = | . . 0 . | | . . . | | . . 1-p | | . . . | P[1,:,:] = | . . . | | . . . | | . . . | The reward matrix R is defined as follows:: | 0 | | . | R[:,0] = | . | | . | | 0 | | r1 | | 0 | | 1 | R[:,1] = | . | | . | | 1 | | r2 | Parameters --------- S : int, optional The number of states, which should be an integer greater than 1. Default: 3. r1 : float, optional The reward when the forest is in its oldest state and action 'Wait' is performed. Default: 4. r2 : float, optional The reward when the forest is in its oldest state and action 'Cut' is performed. Default: 2. p : float, optional The probability of wild fire occurence, in the range ]0, 1[. Default: 0.1. is_sparse : bool, optional If True, then the probability transition matrices will be returned in sparse format, otherwise they will be in dense format. Default: False. Returns ------- out : tuple ``out[0]`` contains the transition probability matrix P and ``out[1]`` contains the reward matrix R. If ``is_sparse=False`` then P is a numpy array with a shape of ``(A, S, S)`` and R is a numpy array with a shape of ``(S, A)``. If ``is_sparse=True`` then P is a tuple of length ``A`` where each ``P[a]`` is a scipy sparse CSR format matrix of shape ``(S, S)``; R remains the same as in the case of ``is_sparse=False``. Examples -------- array([[[ 0.1, 0.9, 0. ], [ 0.1, 0. , 0.9], [ 0.1, 0. , 0.9]], <BLANKLINE> [[ 1. , 0. , 0. ], [ 1. , 0. , 0. ], [ 1. , 0. , 0. ]]]) array([[ 0., 0.], [ 0., 1.], [ 4., 2.]]) 2 with 6 stored elements in Compressed Sparse Row format> with 3 stored elements in Compressed Sparse Row format> array([[ 0., 0.], [ 0., 1.], [ 4., 2.]]) True True """

    assert S > 1, "The number of states S must be greater than 1."
    assert (r1 > 0) and (r2 > 0), "The rewards must be non-negative."
    assert 0 <= p <= 1, "The probability p must be in [0; 1]."
    # Definition of Transition matrix
    if is_sparse:
        P = []
        rows = list(range(S)) * 2
        cols = [0] * S + list(range(1, S)) + [S - 1]
        vals = [p] * S + [1-p] * S
        P.append(_sp.coo_matrix((vals, (rows, cols)), shape=(S, S)).tocsr())
        rows = list(range(S))
        cols = [0] * S
        vals = [1] * S
        P.append(_sp.coo_matrix((vals, (rows, cols)), shape=(S, S)).tocsr())
    else:
        P = _np.zeros((2, S, S))
        P[0, :, :] = (1 - p) * _np.diag(_np.ones(S - 1), 1)
        P[0, :, 0] = p
        P[0, S - 1, S - 1] = (1 - p)
        P[1, :, :] = _np.zeros((S, S))
        P[1, :, 0] = 1
    # Definition of Reward matrix
    R = _np.zeros((S, 2))
    R[S - 1, 0] = r1
    R[:, 1] = _np.ones(S)
    R[0, 1] = 0
    R[S - 1, 1] = r2
    return(P, R)