x = T.iscalar('x')
result = T.arange(x)
do_something = theano.function(inputs=[x], outputs=result)
do_something(5)

"""
Explanation: First Something Easy
End of explanation
"""


X = T.matrix("X")
results, _ = theano.scan(lambda v: v*2, sequences=X)
my_iterator = theano.function(inputs=[X], outputs=[results])
my_iterator([[1, 2, 3],[4, 5, 6],[7, 8, 9]])

"""
Explanation: A Simple Loop
End of explanation
"""


k = theano.shared(0)
n = T.iscalar("n")

_, updates = theano.scan(lambda : ({k : (k + 1)}), n_steps=n)
acc = theano.function([n],[], updates=updates, allow_input_downcast=True)

k.get_value()

acc(5)
k.get_value()

"""
Explanation: Accumulator with shared variable
Scan returns the updates, which are used by the function to update the shared variable.
End of explanation
"""


k = theano.shared(1)
n = T.iscalar("n")
    
#def geo_func(prior_result):
#    current_result = prior_result + (1.0 / k)
#    updates = {k: k*2}
#    return current_result, updates

output = T.as_tensor_variable(np.asarray(0, np.float64))
results, updates = theano.scan(lambda prior : (prior + 1.0/k, {k : k*2}), outputs_info=output, n_steps=n)
#results, updates = theano.scan(geo_func, outputs_info=output, n_steps=n)
#result = results[-1]
geo = theano.function(inputs=[n], outputs=[results], updates=updates)
geo(2)

"""
Explanation: Geometric Series with Shared Variable and Recursion
End of explanation
"""


