title: Why does scan upcast?
body: <p>This code to calculate the trace of a matrix (based on an example in the Theano "loop" tutorial) works fine:</p>
  
  <pre><code>import numpy as np
  import theano as th
  import theano.tensor as T
  floatX = 'float32'
  X = T.matrix()
  results = th.scan(lambda i,j,t_f : T.cast(X[i,j] + t_f, floatX),
                        sequences=[T.arange(X.shape[0]), T.arange(X.shape[1])],
                        outputs_info=np.asarray(0., dtype=floatX))[0]
  result = results[-1]
  compute_trace = th.function([X], result)
  x = np.eye(5, dtype=floatX)
  x[0] = np.arange(5, dtype=floatX)
  print compute_trace(x)
  </code></pre>
  
  <p>But if I remove the cast operation from the lambda function like this:</p>
  
  <pre><code>lambda i,j,t_f : X[i,j] + t_f
  </code></pre>
  
  <p>The following error message is produced: </p>
  
  <p>ValueError: When compiling the inner function of scan the following error has been encountered: The initial state (<code>outputs_info</code> in scan nomenclature) of variable IncSubtensor{Set;:int64:}.0 (argument number 2) has dtype float32, while the result of the inner function (<code>fn</code>) has dtype float64. This can happen if the inner function of scan results in an upcast or downcast.</p>
  
  <p>Why so? <code>X</code> and <code>outputs_info</code> are explicitly set to float32. How does the result of adding them get to be float64?</p>